Registry /
database / postgres-schema-builder
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)
Errors
Common errors & fixes
Cannot find module 'postgres-schema-builder'
Missing installation or import path incorrect.
fixRun '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.
fixUse 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.
fixCheck 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.
fixUse 'typeof Tables.users' instead of 'DatabaseV1.users' for the interface.
Audit
Dependencies
No dependency data recorded yet.