ObjectStackObjectStack

Database Drivers

Configuration reference for supported database drivers

Database Drivers

ObjectStack supports multiple database backends through a unified driver interface. Configure your data source in objectstack.config.ts:

import { defineStack } from '@objectstack/spec';

export default defineStack({
  manifest: { /* ... */ },
  datasources: {
    default: {
      driver: 'postgres',
      url: process.env.DATABASE_URL,
    },
  },
});

Supported Drivers

DriverPackageUse Case
PostgreSQL@objectstack/driver-postgresProduction, enterprise workloads
MongoDB@objectstack/driver-mongoDocument-oriented, flexible schema
SQLite@objectstack/driver-sqliteLocal development, embedded apps
Memory@objectstack/driver-memoryTesting, prototyping

PostgreSQL

Configuration properties for the PostgreSQL driver (node-postgres / pg).

PropertyTypeRequiredDescription
urlstringoptionalConnection URI (e.g., postgres://user:pass@host:5432/db)
databasestringDatabase Name
hoststringoptionalHost address (default: localhost)
portnumberoptionalPort number (default: 5432)
usernamestringoptionalAuth User
passwordstringoptionalAuth Password
schemastringoptionalDefault Schema (default: public)
sslboolean | objectoptionalEnable SSL
applicationNamestringoptionalApplication Name
maxnumberoptionalMax Pool Size (default: 10)
minnumberoptionalMin Pool Size (default: 2)

Example:

datasources: {
  default: {
    driver: 'postgres',
    url: 'postgres://admin:secret@db.example.com:5432/myapp',
    schema: 'public',
    ssl: true,
    max: 20,
  },
}

MongoDB

Configuration properties for the MongoDB driver.

PropertyTypeRequiredDescription
urlstringoptionalConnection URI (e.g., mongodb://host:27017/db)
databasestringDatabase Name
hoststringoptionalHost address (default: localhost)
portnumberoptionalPort number (default: 27017)
usernamestringoptionalAuth User
passwordstringoptionalAuth Password
authSourcestringoptionalAuthentication Database (default: admin)
sslbooleanoptionalEnable SSL
replicaSetstringoptionalReplica Set Name
readPreferenceEnumoptionalRead Preference (primary, secondary, nearest)
maxPoolSizenumberoptionalMax Connection Pool Size (default: 10)
minPoolSizenumberoptionalMin Connection Pool Size (default: 0)

Example:

datasources: {
  default: {
    driver: 'mongo',
    url: 'mongodb+srv://admin:secret@cluster.mongodb.net/myapp',
    readPreference: 'nearest',
    maxPoolSize: 50,
  },
}

SQLite

SQLite is ideal for local-first development and embedded applications.

PropertyTypeRequiredDescription
filenamestringPath to the SQLite database file
modestringoptionalreadwrite (default), readonly, or memory

Example:

datasources: {
  default: {
    driver: 'sqlite',
    filename: './data/app.db',
  },
}

Memory Driver

The in-memory driver stores data in a JavaScript Map. Data is lost when the process exits.

datasources: {
  default: {
    driver: 'memory',
  },
}

Use the memory driver for unit tests. It requires no setup and runs instantly.

Multi-Datasource

ObjectStack supports multiple data sources. Objects can target specific datasources:

export default defineStack({
  datasources: {
    primary: { driver: 'postgres', url: process.env.PG_URL },
    analytics: { driver: 'postgres', url: process.env.ANALYTICS_URL },
    cache: { driver: 'mongo', url: process.env.MONGO_URL },
  },
});

Then in your object definition:

export const AuditLog = ObjectSchema.create({
  name: 'audit_log',
  datasource: 'analytics', // Routes to the analytics database
  fields: { /* ... */ },
});

On this page