Registry / database / postgres-schema-builder

postgres-schema-builder

JSON →
library1.3.0jsnpmunverified

A TypeScript library for building PostgreSQL schemas and writing typesafe queries. Version 1.3.0, last updated in 2020 (no recent releases). It uses a declarative schema definition with TableSchema, automatic TypeScript type inference from column types, and a Table API for CRUD operations. Key differentiator: strong TypeScript type safety for schema and queries, including compile-time checks for nullable columns and foreign keys. Not actively maintained; no query builder for joins or aggregations. Alternative: kysely or drizzle-orm.

npm install postgres-schema-builder
INSTALL
IMPORT
SIG · POSTGRES-SCHEMA-BU
P
postgres-schema-builder
databasejavascriptv1.3.0
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.

TableSchema
✓ import { TableSchema } from 'postgres-schema-builder'
✗ const TableSchema = require('postgres-schema-builder').TableSchema
CommonJS require works but ESM is preferred; the package ships both CJS and ESM.
ColumnType
✓ import { ColumnType } from 'postgres-schema-builder'
Use for defining column types like ColumnType.Integer, ColumnType.Varchar.
Table
✓ import { Table } from 'postgres-schema-builder'
✗ import { Table } from 'postgres-schema-builder/src'
Do not import from subpaths; the package has no deep imports.
SQL
✓ import { SQL } from 'postgres-schema-builder'
Used for raw SQL fragments if needed.
NativeFunction
✓ import { NativeFunction } from 'postgres-schema-builder'
For default values like NativeFunction.Now.
JSONType
✓ import { JSONType } from 'postgres-schema-builder'
For defining JSON columns with a TypeScript interface.
ForeignKeyUpdateDeleteRule
✓ import { ForeignKeyUpdateDeleteRule } from 'postgres-schema-builder'
Use for foreign key constraints.

Defines a PostgreSQL schema using TableSchema, creates table objects, and demonstrates typesafe insert/select queries.

import { TableSchema, ColumnType, NativeFunction, JSONType, ForeignKeyUpdateDeleteRule, TableRecord, Table, SQL } from 'postgres-schema-builder' // Define schema export namespace DatabaseV1 { const baseSchema = { date_added: { type: ColumnType.TimestampTZ, nullable: false, defaultValue: { func: NativeFunction.Now } }, date_removed: { type: ColumnType.TimestampTZ, nullable: true }, } export const users = TableSchema({ ...baseSchema, user_id: { type: ColumnType.Integer, primaryKey: true, unique: true }, name: { type: ColumnType.Varchar, nullable: false }, settings: { type: JSONType<{ key: string }>(), nullable: false }, }) } // Create interfaces and table objects export const Tables = DatabaseV1 export interface IUserDBResult extends TableRecord<typeof Tables.users> { } export const UsersTable = Table(Tables, 'users') // Typesafe queries const createSQL = UsersTable.create() // returns string const dropSQL = UsersTable.drop() const insertSQL = UsersTable.insert(['name'])(['Fresh Herrmann']) const selectSQL = UsersTable.select('*', ['user_id'])([42]) console.log(createSQL, dropSQL, insertSQL, selectSQL)
Debug
Known issues
gotchaThe package does not support JOIN queries out of the box; you must use raw SQL or another query builder.
fix
For joins, construct raw SQL using the SQL helper or consider alternatives like kysely.
affects: >=1.0.0
deprecatedThe package uses deprecated TableSchema syntax without explicit versioning; schema versions are defined by namespaces.
fix
No direct replacement; use namespaces to organize schema versions.
affects: >=1.0.0
gotchaBoolean columns are not supported; use ColumnType.Boolean but it may not be correctly typed.
fix
Check types manually; consider using ColumnType.Integer (0/1) as a workaround.
affects: >=1.0.0
gotchaThe package is not actively maintained; last release 2020, no recent bug fixes.
fix
Evaluate if you need ongoing support; consider migrating to maintained alternatives.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module 'postgres-schema-builder'
Missing installation or import path incorrect.
fix
Run 'npm install postgres-schema-builder' and ensure import is 'postgres-schema-builder', not a subpath.
Property 'insert' does not exist on type 'Table<...>'
Incorrect TypeScript version or missing type declarations.
fix
Use TypeScript >=3.5. Ensure tsconfig.json includes 'esModuleInterop'. The package ships .d.ts files.
Type 'null' is not assignable to type 'string | number'
Trying to insert null into a non-nullable column.
fix
Check your schema: set nullable: true for columns that allow null values.
Cannot use namespace 'DatabaseV1' as a type
TableRecord expects a type, not a namespace.
fix
Use 'typeof Tables.users' instead of 'DatabaseV1.users' for the interface.
Upgrade
Version history
1.3.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
postgres-schema-builder — npm install postgres-schema-builder · libregistry