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
DataStoreimplementations ActiveQL provides you want to you use or - provide your own
DataStoreimplementation. - Provide configuration to the actual
DataStoreimplementation
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
| Config | Type | Default | Description |
|---|---|---|---|
indexAttributes | string[] | - | Create indexes for these attributes to improve filter query performance |
maxPageSize | number | - | Maximum page size for pagination |
createEntityViews | boolean | false | Generate SQL views for each entity |
dropAllViews | boolean | false | Drop all views before creating entity views (prevents orphaned views) |
supressIndexWarning | boolean | false | Suppress 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 fieldcreated_at- Creation timestampupdated_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:
Intfields become INTEGERFloatfields become REALBooleanfields become BOOLEANDatefields become DATEDateTimefields 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
| Config | Type | Description |
|---|---|---|
user | string | user account of the Postgres database |
database | string | database name |
password | string|(() => string|Promise<string>) | password to connect to Postgres database |
port | number | port to connect to Postgres database |
host | string | host on which the Postgres database runs |
ssl | boolean|ConnectionOptions | whether to use ssl when connecting |
indexAttributes | string[] | 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']})
};