@type-map-for/postgres
v0.1.1
Published
Compile-time TypeScript → PostgreSQL field mapping
Readme
@type-map-for/postgres
Generate PostgreSQL table schemas from TypeScript interfaces at compile time.
Installation
npm install @type-map-for/postgresUsage
import type { PostgresFieldMap } from '@type-map-for/postgres';
interface User {
id: number;
name: string;
email?: string;
tags: string[];
metadata: { source: string };
}
type UserSchema = PostgresFieldMap<User>;
// Result: Array<{
// { column: 'id'; pgType: 'integer'; nullable: false }
// { column: 'name'; pgType: 'text'; nullable: false }
// { column: 'email'; pgType: 'text'; nullable: true }
// { column: 'tags'; pgType: 'text[]'; nullable: false }
// { column: 'metadata'; pgType: 'jsonb'; nullable: false }
// }>
// Use with schema validation
const userSchema: UserSchema = [
{ column: 'id', pgType: 'integer', nullable: false },
{ column: 'name', pgType: 'text', nullable: false },
{ column: 'email', pgType: 'text', nullable: true },
{ column: 'tags', pgType: 'text[]', nullable: false },
{ column: 'metadata', pgType: 'jsonb', nullable: false }
] as const satisfies UserSchema;Type Mapping
| TypeScript | PostgreSQL |
|------------|------------|
| string | text |
| string[] | text[] |
| number | integer |
| number[] | integer[] |
| boolean | boolean |
| boolean[] | boolean[] |
| object | jsonb |
| object[] | jsonb[] |
| T \| undefined | nullable: true |
Types
PostgresField<T, K>- Schema field for propertyKof typeTPostgresFieldMap<T>- Complete schema array for interfaceT
Contributing
Issues and pull requests welcome at github.com/nathancanine/type-map-for.
License
MIT
