API Overview
Core (@jsorm/core)
Section titled “Core (@jsorm/core)”defineModel()
Section titled “defineModel()”defineModel<TName, TFields, TRelations, TDatabase>( name: TName, schema: { db: TDatabase; fields: TFields; relations?: TRelations }): ModelDefinitionDefines a model with typed metadata. The first parameter is the table name in the database (explicit, not transformed). The TypeScript type of the result comes from the declared variable. db references a database from config.
defineConfig()
Section titled “defineConfig()”defineConfig({ models: "src/models", databases: { main: { access: "server", provider: "postgres-node", options: { naming: { fields: "snake_case" } } }, },});Defines the project configuration.
env(name: string): stringHelper to mark values as env vars in config.
options: { database: env("DATABASE_URL_SQLITE") }Field types: string(length?), uuid(), boolean(), integer(), decimal(precision?, scale?), text(), dateTime(), json(), enum(values).
Modifiers: .primary(), .autoCreate(), .autoUpdate(), .default(value), .unique(), .nullable(), .hidden().
Relations: belongsTo(target, options?), hasOne(target, options?), hasMany(target, options?), manyToMany(target, options?).
Client (@jsorm/client)
Section titled “Client (@jsorm/client)”createJsorm()
Section titled “createJsorm()”const jsorm = createJsorm<DB>(config, models, { main: (ctx) => createSqliteNodeProvider(ctx.models, ctx.options),});Creates the typed instance with Proxy for table access.
Table access
Section titled “Table access”// Main database — direct table accessjsorm.users.get()jsorm.users.create({ name: "Ana" })
// Non-main database — namespaced accessjsorm.cache.users.get()jsorm.analytics.sessions.get()Instance methods
Section titled “Instance methods”| Method | Description |
|---|---|
jsorm.raw({ sql, params?, db? }) |
Raw SQL |
jsorm.close(db?) |
Close connection |
jsorm.syncSchema(db?) |
Create missing tables |
jsorm.getMissingTables(db?) |
List missing tables |
jsorm.providers |
ProviderManager for registering providers |
TableMethods
Section titled “TableMethods”All methods are async and return { data: T }.
| Method | Return |
|---|---|
.first({ select?, where? }) |
Promise<ProviderResult<T | null>> |
.get({ select?, where?, pagination? }) |
Promise<ProviderResult<T[]> & { meta?: ... }> |
.cursor(opts) |
Promise<CursorResult<T>> |
.count({ where? }) |
Promise<ProviderResult<number>> |
.exists({ where? }) |
Promise<ProviderResult<boolean>> |
.create(data) |
Promise<ProviderResult<T>> |
.createMany(data[]) |
Promise<ProviderResult<T[]>> |
.update({ where, data }) |
Promise<ProviderResult<number>> |
.updateMany(data[]) |
Promise<ProviderResult<number>> |
.delete({ where? }) |
Promise<ProviderResult<number>> |
.deleteMany({ where? }) |
Promise<ProviderResult<number>> |
Many-to-Many Relations
Section titled “Many-to-Many Relations”| Method | Description |
|---|---|
.attach(parentId, relation, data) |
|
.detach(parentId, relation, data?) |
|
.toggle(parentId, relation, data) |
