Skip to content

API reference

db(options) creates a Layeron module that declares an application database.

Terminal window
import { db } from "@layeron/modules"

Create a Layeron db module.

Terminal window
db(options: DbModuleOptions): DbModule

Options accepted by db(options).

FieldTypeDescription
namestringStable logical database name inside the Layeron app.
namespacestring | undefinedPlatform namespace for the instance. Defaults to default.
capacityDbCapacityOptions | undefinedLogical database capacity mode. Omit this field to use fixed capacity with one shard.
shardingDbShardingOptions | undefinedD1 Plus sharding policy.
realtimeDbRealtimeOptions | undefinedRealtime database event streaming settings.
schemaRecord<Layeron.Modules.Db.DbTableDefinition> | undefinedDeclarative schema used to generate SQL migrations and typed Table API metadata.
databaseNamestring | undefinedCloudflare D1 database name.
bindingstring | undefinedCustom binding name for advanced runtime access.
sharedDbSharedResourceOptions | undefinedReference an existing Cloudflare D1 database in the same Cloudflare account. The owning project controls schema and migrations.
placementLayeron.Common.Placement.LayeronPlacement | undefinedPlacement overrides for this database product instance.
observabilityLayeron.Observability.ObservabilityConfig | undefinedObservability settings for the database module.
sqlstring | string[] | DbMigrationInput[] | undefinedOne or more SQL migrations. Omit when schema declares the database shape.

Create a query object for a SQL statement.

Create a query object for a SQL statement.

TypeSpec operation: databaseSql.

Terminal window
database.sql(statement: string | DbSqlRequest, params?: unknown[]): DbQuery

SQL request object accepted by database.sql().

FieldTypeDescription
statementstringSQL statement.
paramsunknown[] | undefinedSQL query parameters.
shardDbShardHint | undefinedD1 Plus shard routing hint.

Query object returned by database.sql().

FieldTypeDescription
statementstringSQL statement.
paramsunknown[]SQL query parameters.
shardDbShardHint | undefinedD1 Plus shard routing hint.

Result returned by database SQL helpers.

FieldTypeDescription
resultsunknown[]Rows returned by the SQL statement.
rowsunknown[]Alias of results for row-oriented helpers.
metaDbSqlResultMetaExecution metadata such as changes, duration, shard, and batch count.

D1 Plus shard routing hint.

FieldTypeDescription
tablestringTable name.
keyunknownShard key value.

Named SQL migration.

FieldTypeDescription
namestring | undefinedMigration version. Layeron generates a name when omitted.
sqlstringSQL statements for the migration.
destructiveboolean | undefinedMarks the migration as destructive metadata. Apply still requires migration safety approval.

Logical database capacity settings.

FieldTypeDescription
mode”fixed” | “manual” | “auto”Capacity mode. fixed uses one shard, manual uses the requested target shard count, and auto declares a future automatic expansion policy while starting from the requested target shard count or one shard.
targetShardCountint32 | undefinedTarget shard count. Valid values are powers of two from 1 to 64.
maxShardCountint32 | undefinedMaximum shard count for future expansion. Valid values are powers of two from 1 to 64.

D1 Plus sharding policy.

FieldTypeDescription
tablesDbShardingTableOptions[]Tables that can be routed by D1 Plus.

D1 Plus table sharding settings.

FieldTypeDescription
namestringTable name.
keyColumnstringColumn used as the shard key.

Realtime database event streaming settings.

FieldTypeDescription
tablesRecord<Layeron.Modules.Db.DbRealtimeTableOptions | boolean>Tables that emit realtime database events.

Realtime settings for one database table.

FieldTypeDescription
events”insert” | “update” | “delete”[] | undefinedEvents to emit. Defaults to insert, update, and delete.
keystring | undefinedPrimary key column included in event identity. Defaults to the schema primary key.
filtersstring[] | undefinedColumns clients may use as equality filters in websocket subscriptions.
requiredFiltersstring[] | undefinedFilter columns that every private subscription grant must include.
access”private” | “public” | undefinedSubscription access mode. Private tables require a short-lived grant. Public tables allow direct websocket subscriptions.

This generated reference covers these TypeSpec symbols.

KindSymbol
modelDbModuleOptions
modelDbCapacityOptions
modelDbShardingOptions
modelDbShardingTableOptions
modelDbRealtimeOptions
modelDbRealtimeTableOptions
modelDbTableDefinition
modelDbColumnDefinition
modelDbIndexDefinition
modelDbSharedResourceOptions
modelDbMigrationInput
modelDbModule
modelDbRealtimeApi
modelDbRealtimeAuthorizeInput
modelDbRealtimeGrant
modelDbSqlRequest
modelDbShardHint
modelDbSqlResult
modelDbSqlResultMeta
modelDbQuery
modelDbBatchResult
modelDbBatchOptions
operationdb
operationdatabaseSql
operationdatabaseStatement
operationdatabaseExec
operationdatabaseBatch
operationdatabaseTransaction
operationdbQueryAwait
operationdbQueryAll
operationdbQueryRun
operationdbQueryFirst
operationdbQueryOne
operationdbQueryMaybeOne
operationdbQueryAs
operationdbQueryJson
HelperDescription
await database.sql(...)Executes in automatic query mode.
.all()Executes and returns all rows.
.run()Executes a statement intended for writes.
.as(key)Returns rows wrapped in a named JSON object.
.json(key?, init?)Returns a JSON Response.