Registry / database / linkgress-orm

linkgress-orm

JSON →
library0.4.27jsnpmunverified

A lightweight, type-safe ORM for PostgreSQL with LINQ-style queries and automatic type inference, version 0.4.27. Built exclusively for PostgreSQL (12+), it leverages advanced features like CTEs, LATERAL joins, and JSON aggregations for nested collection queries. Supports both pg and postgres npm clients. Released under MIT license with active development.

npm install linkgress-orm
INSTALL
IMPORT
SIG · LINKGRESS-ORM
L
linkgress-orm
databasejavascriptv0.4.27
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

DbContext
import { DbContext } from 'linkgress-orm'
const { DbContext } = require('linkgress-orm')
Package is ESM-only; CommonJS require is not supported.
DbEntity
import { DbEntity } from 'linkgress-orm'
import { DbEntity } from 'linkgress-orm/src/DbEntity'
All exports are at the package root; do not use deep imports as they are not part of the public API.
DbColumn
import { DbColumn } from 'linkgress-orm'
import DbColumn from 'linkgress-orm'
DbColumn is a named export, not a default export.
integer
import { integer } from 'linkgress-orm'
import { Integer } from 'linkgress-orm'
Type helpers are lowercase and exported as functions.
varchar
import { varchar } from 'linkgress-orm'

Define entities, configure DbContext with relationships, then execute a query with filtering and ordering.

import { DbContext, DbEntity, DbColumn, DbEntityTable, DbModelConfig, integer, varchar } from 'linkgress-orm'; import postgres from 'postgres'; const sql = postgres('postgres://user:pass@localhost:5432/mydb'); class User extends DbEntity { id!: DbColumn<number>; username!: DbColumn<string>; email!: DbColumn<string>; posts?: Post[]; } class Post extends DbEntity { id!: DbColumn<number>; title!: DbColumn<string>; userId!: DbColumn<number>; user?: User; } class AppDatabase extends DbContext { get users(): DbEntityTable<User> { return this.table(User); } get posts(): DbEntityTable<Post> { return this.table(Post); } protected override setupModel(model: DbModelConfig): void { model.entity(User, entity => { entity.toTable('users'); entity.property(e => e.id).hasType(integer('id').primaryKey().generatedAlwaysAsIdentity({ name: 'users_id_seq' })); entity.property(e => e.username).hasType(varchar('username', 100)).isRequired(); entity.property(e => e.email).hasType(varchar('email', 255)).isRequired(); entity.hasMany(e => e.posts, () => Post).withForeignKey(p => p.userId).withPrincipalKey(u => u.id); }); model.entity(Post, entity => { entity.toTable('posts'); entity.property(e => e.id).hasType(integer('id').primaryKey().generatedAlwaysAsIdentity({ name: 'posts_id_seq' })); entity.property(e => e.title).hasType(varchar('title', 200)).isRequired(); entity.property(e => e.userId).hasType(integer('user_id')); entity.belongsTo(e => e.user, () => User).withForeignKey(p => p.userId).withPrincipalKey(u => u.id); }); } } async function main() { const db = new AppDatabase(sql); const users = await db.users.select().where(u => u.username.like('john%')).orderBy(u => u.id.asc()).execute(); console.log(users); } main().catch(console.error);
Debug
Known issues
gotchaPackage requires both `pg` or `postgres` as peer dependency. Not installing one will cause runtime errors.
fix
Run `npm install linkgress-orm postgres` (or `pg`). Ensure version >=8.0 for `pg` or >=3.0 for `postgres`.
affects: <1.0
breakingDbContext constructor now expects a `postgres` Sql instance; earlier versions accepted a connection string. This change is in v0.4.0.
fix
Instead of `new AppDatabase('postgres://...')`, create a sql client first: `const sql = postgres('...'); new AppDatabase(sql);`
affects: >=0.4.0
deprecatedThe `magicSql` function is deprecated in favor of inline SQL via template literals.
fix
Use `sql` tagged template literals directly in queries instead of `magicSql`.
affects: <0.4.0
gotchaNavigational properties must be declared with `?` (optional) in entity classes, else TypeScript may fail to compile.
fix
Declare as `posts?: Post[]` and `user?: User` to allow undefined before loading.
affects: <1.0
breakingThe `onBeforeSave` hook now receives the entity instance, not a plain object. Signatures changed in v0.4.0.
fix
Update your hook: `onBeforeSave(entity: T) => { ... }` instead of `onBeforeSave(data: T) => { ... }`
affects: <0.4.0
gotchaDatabase column names are inferred from property names by default. Use `entity.property(e => e.id).hasType(integer('custom_id'))` to map to a different column name.
fix
Always explicitly set column names if the database schema differs from property names.
affects: <1.0
Errors
Common errors & fixes
TypeError: postgres is not a function
Importing `postgres` incorrectly: the package exports a default function, not a named export.
fix
Use `import postgres from 'postgres';` (default import) not `import { postgres } from 'postgres'`.
Cannot find module 'linkgress-orm' or its corresponding type declarations.
TypeScript is unable to resolve the package because `skipLibCheck` is false and type roots not configured.
fix
Ensure `skipLibCheck: true` in tsconfig.json, or add the package to `types`.
Property 'users' does not exist on type 'AppDatabase'.
The getter `users` is not defined or returns `void` instead of `DbEntityTable<User>`.
fix
Ensure the getter returns `this.table(User);` with proper type annotation.
Expected 1 argument, but got 0.
Calling `db.users.select()` without required arguments? Actually `select()` can be empty; this error may occur with `update()` or `delete()` without conditions.
fix
For `update()` and `delete()`, ensure you use `.where()` first or provide conditions.
Query failed: relation "users" does not exist
The database table name does not match the entity configuration, or migration hasn't been run.
fix
Use `entity.toTable('users')` to map to the correct table name, or run migrations to create the table.
Upgrade
Version history
0.4.27latest on npm
Audit
Dependencies
pgoptionalPostgreSQL client driver required for database connectivity
postgresoptionalAlternative PostgreSQL client driver
Agent activity
3 hits · last 30 days
node
2
Resources