> From the [Strife developer docs](https://strife.app/docs/tutorials/getting-started/nodejs). Every page is listed in [llms.txt](https://strife.app/docs/llms.txt).

# Node.js

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

### 1. Install Strife SDK

```bash
npm i @strifeapp/strife ravendb
```

### 2. Create and initialize a Strife store

> [!NOTE]
> The environment settings were provided during onboarding.

```js
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](https://ravendb.net/docs/article-page/7.0/csharp/client-api/faq/what-is-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](https://strife.app/docs/reference/fields.md) that you define,  and is automatically queryable using a [Document session](https://ravendb.net/docs/article-page/7.0/nodejs/client-api/session/what-is-a-session-and-how-does-it-work).

> [!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:

```js
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:

```js
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.

```js
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) => {…});
  }
}
```
