Data Overview
Introduction
Section titled “Introduction”Xeno.JS provides the application-facing infrastructure you need to access relational data and execute database operations without making the database provider the center of your application architecture.
The workflow is:
Choose a database ↓Configure it with XenoAppBuilder and a provider plugin ↓Build the application ↓Resolve the database services ↓Use a provider-specific Data Source ↓Use Unit of Work when operations must run in a transactionXeno.JS currently supports:
- PostgreSQL through the Node PostgreSQL integration.
- SQLite through the libSQL client.
For provider-specific configuration and usage, see:
Before you start
Section titled “Before you start”You need:
- a Xeno.JS application;
- the database provider dependencies required by your selected database;
- a database connection URL or provider-specific connection options;
- an
XenoAppBuilderinstance.
The database is configured during application bootstrap. Application code should consume the registered database services rather than creating database clients directly.
Configure the database
Section titled “Configure the database”Use XenoAppBuilder.addDb() with a plugin from the selected database integration.
For PostgreSQL, pass withPostgresql() and configure its connection options:
import { XenoAppBuilder } from '@xeno-js/core'import { withPostgresql } from '@xeno-js/postgresql'
const app = new XenoAppBuilder()
app.addDb(withPostgresql((options, config) => { options.connectionString = config.get('DATABASE_URL') ?? 'postgres://user:password@localhost:5432/myapp'}))
await app.build()For SQLite/libSQL, pass withSqlite() and configure its URL:
import { XenoAppBuilder } from '@xeno-js/core'import { withSqlite } from '@xeno-js/sqlite'
const app = new XenoAppBuilder()
app.addDb(withSqlite((options) => { options.url = 'file:./data/app.db'}))
await app.build()The option names are provider-specific:
| Provider | Plugin | Connection option |
|---|---|---|
| PostgreSQL | withPostgresql |
connectionString |
| SQLite/libSQL | withSqlite |
url |
Register the database in XenoAppBuilder
Section titled “Register the database in XenoAppBuilder”Database registration belongs in the application bootstrap.
A typical application setup looks like:
import { XenoAppBuilder } from '@xeno-js/core'import { withPostgresql } from '@xeno-js/postgresql'
const app = new XenoAppBuilder()
app .addDb(withPostgresql((options, config) => { options.connectionString = config.get('DATABASE_URL') ?? 'postgres://user:password@localhost:5432/myapp' }))
const application = await app.build()addDb() invokes the plugin to queue the provider-specific database module. build() initializes the configured modules and returns the runtime application, not the service container.
After the build completes, the database services are available through dependency injection.
Resolve the database
Section titled “Resolve the database”The database context is registered under the DB_CONTEXT token.
DB_CONTEXT is scoped, so resolve it from an active service scope. Use runInScope() on the runtime application:
await application.runInScope(async (services) => { const db = services.DB_CONTEXT // Use the scoped database context here.})The exact type of DB_CONTEXT depends on the application’s Xeno registry.
For most application code, however, you should not pass the database context throughout the application manually. Instead, create a provider-specific Data Source and inject it where data access is required.
Create a Data Source
Section titled “Create a Data Source”PostgreSQL
Section titled “PostgreSQL”import type { IReadDataSource } from '@xeno-js/core'
export class UserDataSource implements IReadDataSource { async findUserById(id: string) { return this._db.query.users.findFirst({ where: (users, { eq }) => eq(users.id, id), }) }}SQLite
Section titled “SQLite”import type { IReadDataSource } from '@xeno-js/core'import type { DbContext } from '@xeno-js/sqlite'import type { DbSchema } from './schema'
export class UserDataSource implements IReadDataSource { constructor(private readonly _db: DbContext<DbSchema>) async findUserById(id: string) { return this._db.query.users.findFirst({ where: (users, { eq }) => eq(users.id, id), }) }}The provider-specific pages contain the complete configuration and Data Source examples:
Register your Data Source
Section titled “Register your Data Source”Your application Data Source is a normal dependency-injection service. Declare its token and type in the application’s Xeno registry before registering it.
For example:
import { XenoAppBuilder } from '@xeno-js/core'import { withPostgresql } from '@xeno-js/postgresql'import type { DbContext, DbTransaction } from '@xeno-js/postgresql'import type { XenoRegistry } from '@xeno-js/shared'import type { DbSchema } from './schema'
interface AppRegistry extends XenoRegistry<DbContext<DbSchema>, DbTransaction> { USER_DATA_SOURCE: UserDataSource}
const app = new XenoAppBuilder<AppRegistry>()
app.addDb(withPostgresql<AppRegistry>((options, config) => { options.connectionString = config.get('DATABASE_URL') ?? 'postgres://user:password@localhost:5432/myapp'}))
app.addServices((services) => { services.addScoped('USER_DATA_SOURCE', (scope) => { return new UserDataSource(scope.resolve('DB_CONTEXT')) })})
const application = await app.build()Because this Data Source is scoped, resolve it from scoped services within runInScope() or inject it into another service with a compatible lifetime.
For more information about registration and lifetimes, see:
Use Unit of Work for transactions
Section titled “Use Unit of Work for transactions”When an operation requires multiple database changes to succeed as one unit, use the registered Unit of Work.
UNIT_OF_WORK is scoped. Resolve it from the active scope and perform the work within that scope:
await application.runInScope(async (services) => { const unitOfWork = services.UNIT_OF_WORK const userDataSource = services.USER_DATA_SOURCE
await unitOfWork.runInTransaction(async () => { await userDataSource.createUser(user) await userDataSource.createProfile(profile) })})The transaction API is callback-based.
You do not manually call:
begin()commit()rollback()Instead, the work performed inside runInTransaction() is executed as one transaction.
For the complete transaction workflow, see Unit of Work.
Typical application flow
Section titled “Typical application flow”A typical application that uses Xeno.JS Data infrastructure can be organized like this:
HTTP / transport ↓Command or Query ↓Handler ↓Application service / Repository ↓Data Source ↓DatabaseFor transactional operations:
Command Handler ↓Unit of Work ↓Data Source(s) ↓Database transactionThe database provider remains infrastructure. Your application services, handlers, and repositories consume the data access services through explicit dependencies.
Complete example
Section titled “Complete example”The following example shows the basic composition of a PostgreSQL application.
import { XenoAppBuilder } from '@xeno-js/core'import { withPostgresql } from '@xeno-js/postgresql'import type { IReadDataSource } from '@xeno-js/core'import type { DbContext, DbTransaction } from '@xeno-js/postgresql'import type { XenoRegistry } from '@xeno-js/shared'import type { DbSchema } from './schema'
class UserDataSource implements IReadDataSource { constructor(private readonly _db: DbContext<DbSchema>) async findUserById(id: string) { return this._db.query.users.findFirst({ where: (users, { eq }) => eq(users.id, id), }) }}
interface AppRegistry extends XenoRegistry<DbContext<DbSchema>, DbTransaction> { USER_DATA_SOURCE: UserDataSource}
const app = new XenoAppBuilder<AppRegistry>()
app.addDb(withPostgresql<AppRegistry>((options, config) => { options.connectionString = config.get('DATABASE_URL') ?? 'postgres://user:password@localhost:5432/myapp'}))
app.addServices((services) => { services.addScoped('USER_DATA_SOURCE', (scope) => { return new UserDataSource(scope.resolve('DB_CONTEXT')) })})
const application = await app.build()
await application.runInScope(async (services) => { const dataSource = services.USER_DATA_SOURCE const user = await dataSource.findUserById('user-123')})This example demonstrates the complete Data workflow:
- configure the database;
- register the database with
addDb(); - register an application Data Source;
- build the application;
- resolve the scoped Data Source;
- perform data access within its scope.
The provider-specific pages show the complete implementation for each database.
Choose the right Data page
Section titled “Choose the right Data page”I need PostgreSQL
Section titled “I need PostgreSQL”Go to Node PostgreSQL.
Use this page when you need to:
- configure a PostgreSQL connection;
- register PostgreSQL in
XenoAppBuilder; - create a PostgreSQL Data Source;
- access the PostgreSQL database from application infrastructure.
I need SQLite
Section titled “I need SQLite”Go to SQLite.
Use this page when you need to:
- configure SQLite;
- register SQLite in
XenoAppBuilder; - create a SQLite Data Source;
- access the SQLite database from application infrastructure.
I need a transaction
Section titled “I need a transaction”Go to Unit of Work.
Use this page when multiple database operations must execute as one transactional operation.
Troubleshooting
Section titled “Troubleshooting”DB_CONTEXT cannot be resolved
Section titled “DB_CONTEXT cannot be resolved”Make sure the provider plugin has been registered:
app.addDb(withPostgresql((options) => { options.connectionString = 'postgres://user:password@localhost:5432/myapp'}))Then build the application and resolve the scoped service inside runInScope():
const application = await app.build()
await application.runInScope(async (services) => { const db = services.DB_CONTEXT})The application fails while bootstrapping the database
Section titled “The application fails while bootstrapping the database”Check:
- the provider-specific connection option (
connectionStringfor PostgreSQL orurlfor SQLite/libSQL); - the selected provider;
- the provider dependency installation;
- whether the configured database is reachable.
The error may be wrapped by XenoAppBuilder as a bootstrap failure for the database module.
The wrong database provider is being used
Section titled “The wrong database provider is being used”Choose the provider by passing its plugin to addDb().
PostgreSQL:
app.addDb(withPostgresql((options) => { options.connectionString = 'postgres://user:password@localhost:5432/myapp'}))SQLite/libSQL:
app.addDb(withSqlite((options) => { options.url = 'file:./data/app.db'}))Unit of Work is not available
Section titled “Unit of Work is not available”Make sure a database plugin has been configured and the application has been built. UNIT_OF_WORK is scoped, so resolve it inside an active scope:
const application = await app.build()
await application.runInScope(async (services) => { const unitOfWork = services.UNIT_OF_WORK})Data checklist
Section titled “Data checklist”Before implementing database access, verify:
- I selected PostgreSQL or SQLite.
- The required provider dependency is installed.
- I configured the selected provider’s connection option (
connectionStringorurl). - I registered the provider plugin with
XenoAppBuilder.addDb(). - I called
build()before resolving database services. - I use
DB_CONTEXTthrough dependency injection. - My application Data Source extends the correct provider-specific base class.
- I register my Data Source with the appropriate lifetime.
- I use
UNIT_OF_WORKfor transactional operations. - I keep database infrastructure outside the domain layer.
Related docs
Section titled “Related docs”Database providers
Section titled “Database providers”Dependency Injection
Section titled “Dependency Injection”Application
Section titled “Application”Support Us
Section titled “Support Us”Xeno.JS is an MIT-licensed open source project. It can grow thanks to the support of these awesome people. If you’d like to join them, please read more at support section
