Skip to content
Strife Docs

Tutorials Getting started

Node.js

Connect to the Strife API and interact with your data in a few steps:

1. Install Strife SDK

Terminal
npm i @strifeapp/strife ravendb

2. Create and initialize a Strife store

Note:

The environment settings were provided during onboarding.

JavaScript
import { DocumentStore } from 'ravendb';

const certificate = import.meta.env.STRIFE_CERTIFICATE;

let authOptions = {
  certificate: Buffer.from(certificate, 'base64'),
  type: 'pfx',
  password: import.meta.env.STRIFE_CERTIFICATE_PASSWORD,
};

const store = new DocumentStore(
  import.meta.env.STRIFE_DATABASE_URLS.split(','),
  import.meta.env.STRIFE_DATABASE,
  authOptions
);

export default store.initialize();

3. Define a Collection

A Collection is a group of records, called Documents, that all share a common schema. You can define as many Collections as your application needs. Each Document in a Collection is stored in the Database based on the Fields that you define, and is automatically queryable using a Document session.

Note:

A Collection in Strife should not confused with a Collection in RavenDB

JSON
{
    "collection": "Articles",
    "fields": [
        {
            "label": "Heading",
            "editor": {
                "name": "str-input",
                "propertyName": "heading",
            },
        }
    ]
}

4. Query your pages

Using async await syntax:

JavaScript
import store from '@/store/index';

const session = store.openSession();

let page = await session
  .query({ indexName: 'Content/ByUrl' })
 q  .whereEquals('url', '/about')
  .firstOrNull();

console.log(`Found page with ${page.heading}`);

Or promises:

JavaScript
session.query({ collection: "Articles" })
  .all()
  .then((articles) => {
    if(articles.length) {
      articles.map(article => console.log(`${article.displayName} was published: ${article.publishedAt}`));
    } else {
      console.log('No articles found');
    }
  });

5. Query your Media

Using promises:

JavaScript
await session.query({ collection: 'Files'})
  .whereEquals('mimeType', 'video/mp4')
  .all()
  .then(files => {
    if(files.length) {
      files.map(file => console.log(`${file.name} with size: ${file.size}`));
    } else {
      console.log('No files found');
    }
  });

Search your Content

Using promises:

JavaScript
await session.query({ indexName: "Content/FulltextSearch" })
    .search('fields', params.query)
    .orderByScore()
    .skip((params.page - 1) * PAGE_SIZE)
    .take(PAGE_SIZE)
    .all()
    .then(hits => {
        if(hits.length) {
          hits.map(hit => console.log(`${hit.displayName}`));
        } else {
          console.log('No hits');
        }
    });

6. Setup Real time preview

Use in web component by subscribing to the Strife studio's events and call the provided callback function.

JavaScript
import { subscribe } from '@strifeapp/strife';

export default class Sections extends LitElement {

  static properties = {
    sections: { type: Array },
  };

  firstUpdated() {
    // Subscribe to Live Preview events using the`subscribe` function
    this.unsubscribe = subscribe((data) => {
      this.sections = data.sections;
    });
  }

  disconnectedCallback() {
    super.disconnectedCallback();
    this.unsubscribe();
  }

  render() {
    return repeat(this.sections, (item) => item.id, (item, index) => {…});
  }
}