Setup and start
Setup DataStore

Setup DataStore

DataStore Factory Method

You set up a DataStore by providing the RuntimeConfig with a factory method that returns a DataStore implementation. This is the place to

  • decide which of the DataStore implementations ActiveQL provides you want to you use or
  • provide your own DataStore implementation.
  • Provide configuration to the actual DataStore implementation

These are the DataStore implementations included in ActiveQL.

SQLite DataStore (default)

The SQLite DataStore is the default implementation that stores data in a SQLite database file. It's lightweight, requires no external database setup, and is suitable for development and small to medium production deployments.

import { SqliteDataStore } from 'activeql-foundation';
 
export const runtimeConfig: RuntimeConfig = {
  domainGraph: domainGraphBuilder.getDomainGraph(),
  dataStore: () => SqliteDataStore.create('data.db')
};

For an in-memory database (data is lost when the application stops):

export const runtimeConfig: RuntimeConfig = {
  domainGraph: domainGraphBuilder.getDomainGraph(),
  dataStore: () => SqliteDataStore.create(':memory:')
};

Config Options

ConfigTypeDefaultDescription
indexAttributesstring[]-Create indexes for these attributes to improve filter query performance
maxPageSizenumber-Maximum page size for pagination
createEntityViewsbooleanfalseGenerate SQL views for each entity
dropAllViewsbooleanfalseDrop all views before creating entity views (prevents orphaned views)
supressIndexWarningbooleanfalseSuppress warnings about missing indexes

Entity Views

When createEntityViews is enabled, the SqliteDataStore automatically creates SQL views for each entity. This allows you to query your data directly using SQL tools like the SQLite CLI, DB Browser for SQLite, or any SQL client.

export const runtimeConfig: RuntimeConfig = {
  domainGraph: domainGraphBuilder.getDomainGraph(),
  dataStore: () => SqliteDataStore.create('data.db', { createEntityViews: true })
};

For each entity, a view is created with the entity's collection name in snake_case. For example, an entity Product creates a view products with columns:

  • id - The entity ID
  • All entity attributes as individual columns (properly typed)
  • indication - The indication field
  • created_at - Creation timestamp
  • updated_at - Last update timestamp
  • __typename - The entity type name

This allows you to query data using standard SQL:

SELECT * FROM products WHERE price > 100;
SELECT * FROM users WHERE email LIKE '%@example.com';

The view columns are automatically typed:

  • Int fields become INTEGER
  • Float fields become REAL
  • Boolean fields become BOOLEAN
  • Date fields become DATE
  • DateTime fields become DATETIME
  • List fields are extracted as JSON arrays

If you rename or delete entities and want to clean up orphaned views, use the dropAllViews option:

dataStore: () => SqliteDataStore.create('data.db', {
  createEntityViews: true,
  dropAllViews: true
})

Postgres PgDataStore

The PgDataStore connects to a Postgres instance and is suitable for production.

const options = { host: 'localhost' };
 
export const runtimeConfig: RuntimeConfig = {
  domainGraph: domainGraphBuilder.getDomainGraph(),
  dataStore: () => PgDataStore.create( options )
};

The options object is of type ClientConfig.

Selection of config options

ConfigTypeDescription
userstringuser account of the Postgres database
databasestringdatabase name
passwordstring|(() => string|Promise<string>)password to connect to Postgres database
portnumberport to connect to Postgres database
hoststringhost on which the Postgres database runs
sslboolean|ConnectionOptionswhether to use ssl when connecting
indexAttributesstring[]creation of indexes for these attributes

For further config options see: https://node-postgres.com/apis/client (opens in a new tab)

Indexed Attributes

This option parameter is not from the PostgresClient but instructs the PgDataStore to add indexes for these attributes to the entities table at startup. This can dramatically improve performance when using filter queries with these attributes.

In this example we want to have an indexed query when using licence as filter.

export const runtimeConfig: RuntimeConfig = {
  domainGraph: domainGraphBuilder.getDomainGraph(),
  dataStore: () => PgDataStore.create({indexAttributes: ['licence']})
};