litedbmodel-gen
v0.1.1
Published
embedoc-based model code generator for litedbmodel — generates TypeScript column definitions from SQL DDL (PostgreSQL/MySQL/SQLite)
Maintainers
Readme
litedbmodel-gen
embedoc-based model code generator for litedbmodel. Parses SQL DDL (PostgreSQL / MySQL / SQLite) and generates TypeScript column definitions that stay in sync with your schema.
How It Works
litedbmodel-gen provides two embedoc plugins:
- Custom Datasource (
sql_schema) — reads and parses aschema.sqlfile into structured table definitions - Renderer (
litedbmodel_columns) — generates@column()decorator code from the datasource
Using embedoc's in-place marker system, only the column definitions inside markers are auto-updated. Hand-written code (relations, custom methods, exports) outside the markers is preserved.
@model('users')
class UserModel extends DBModel {
/*@embedoc:litedbmodel_columns table="users"*/
@column({ primaryKey: true }) id?: number;
@column() name?: string;
@column() email?: string | null;
@column.boolean() is_active?: boolean | null;
@column.datetime() created_at?: Date;
@column.datetime() updated_at?: Date;
/*@embedoc:end*/
// Hand-written — not touched by embedoc
@hasMany(() => [User.id, Post.user_id])
declare posts: Promise<Post[]>;
}
export const User = UserModel.asModel();
export type User = InstanceType<typeof User>;Quick Start
1. Initialize embedoc in your project
npx embedoc init2. Install litedbmodel-gen
npm install -D litedbmodel-gen3. Run the init command
npx litedbmodel-gen initThis automatically:
- Copies the Handlebars template to
.embedoc/templates/model.hbs - Registers the
sql_schemadatasource in.embedoc/datasources/index.ts - Registers the
litedbmodel_columnsrenderer in.embedoc/renderers/index.ts - Adds a
schemadatasource config toembedoc.config.yaml - Adds
./models/**/*.tsto the build targets
If your config file is not at the default embedoc.config.yaml:
npx litedbmodel-gen init path/to/embedoc.config.yaml4. Edit the config
Open embedoc.config.yaml and set the schema path and database dialect:
datasources:
schema:
type: sql_schema
path: "./db/schema.sql" # your DDL file
database: PostgreSQL # PostgreSQL | MySQL | SQLite
generators:
- output_path: "./models/{model_class}.ts"
template: model.hbs
overwrite: false5. Generate and build
# Create model files for each table
npx embedoc generate --datasource schema
# Fill in column definitions
npx embedoc build
# Or use watch mode for ongoing sync
npx embedoc watchWorkflow
schema.sql changes
|
v
embedoc build / watch
|
+-- existing models/*.ts
| column definitions inside markers are updated
| relations and exports are preserved
|
+-- new tables
embedoc generate --datasource schema
creates new model files from template
embedoc build fills in column definitionsSupported SQL Types
Common (all dialects)
| SQL Type | Decorator | TypeScript Type |
|----------|-----------|-----------------|
| INTEGER, INT, SMALLINT, SERIAL | @column() | number |
| BIGINT, BIGSERIAL | @column.bigint() | bigint |
| NUMERIC, DECIMAL, REAL, FLOAT, DOUBLE PRECISION | @column() | number |
| VARCHAR, TEXT, CHAR | @column() | string |
| BOOLEAN | @column.boolean() | boolean |
| TIMESTAMP, TIMESTAMPTZ, DATETIME | @column.datetime() | Date |
| DATE | @column.date() | Date |
| JSON, JSONB | @column.json<Record<string, unknown>>() | Record<string, unknown> |
| UUID | @column.uuid() | string |
PostgreSQL Arrays
| SQL Type | Decorator | TypeScript Type |
|----------|-----------|-----------------|
| TEXT[] | @column.stringArray() | string[] |
| INTEGER[] | @column.intArray() | number[] |
| NUMERIC[] | @column.numericArray() | (number \| null)[] |
| BOOLEAN[] | @column.booleanArray() | (boolean \| null)[] |
| TIMESTAMP[] | @column.datetimeArray() | (Date \| null)[] |
MySQL-specific
| SQL Type | Decorator | TypeScript Type |
|----------|-----------|-----------------|
| TINYINT(1) | @column.boolean() | boolean |
Primary Keys
Columns with PRIMARY KEY constraints use @column({ primaryKey: true }). For UUID primary keys: @column.uuid({ primaryKey: true }). Composite primary keys are supported.
Marker Syntax
Inside your model class:
/*@embedoc:litedbmodel_columns table="TABLE_NAME"*/
// auto-generated column definitions
/*@embedoc:end*/If your datasource is not named schema, specify it:
/*@embedoc:litedbmodel_columns table="users" datasource="my_schema"*/CLI
# Initialize litedbmodel-gen in an embedoc project
npx litedbmodel-gen init [config-path]API
The package also exports lower-level utilities:
import {
sqlSchema, // embedoc custom datasource
litedbmodelColumns, // embedoc renderer
parseSchema, // SQL DDL parser
generateColumnCode, // code generator
mapColumnType, // single column type mapper
} from 'litedbmodel-gen';parseSchema(sql, options?)
Parses SQL DDL and returns table definitions.
const tables = parseSchema(sql, { database: 'PostgreSQL' });
// Returns: TableDef[] = [{ name: string, columns: ColumnDef[] }]Requirements
- Node.js 18+
- embedoc >= 0.11.0
License
MIT
