drizzle-orm-pg
Advanced tools
Comparing version 0.15.0 to 0.15.1-4a5b2e3
@@ -161,5 +161,6 @@ "use strict"; | ||
const tableName = table[table_1.PgTable.Symbol.Name]; | ||
const tableSchema = table[table_1.PgTable.Symbol.Schema]; | ||
const origTableName = table[table_1.PgTable.Symbol.OriginalName]; | ||
const alias = tableName === origTableName ? undefined : tableAlias; | ||
joinsArray.push((0, sql_1.sql) `${sql_1.sql.raw(joinMeta.joinType)} join ${new sql_1.Name(origTableName)} ${alias && new sql_1.Name(alias)} on ${joinMeta.on}`); | ||
joinsArray.push((0, sql_1.sql) `${sql_1.sql.raw(joinMeta.joinType)} join ${tableSchema ? new sql_1.Name(tableSchema) : sql_1.sql.raw('')}${sql_1.sql.raw(tableSchema ? '.' : '')}${new sql_1.Name(origTableName)} ${alias && new sql_1.Name(alias)} on ${joinMeta.on}`); | ||
if (index < joinKeys.length - 1) { | ||
@@ -166,0 +167,0 @@ joinsArray.push((0, sql_1.sql) ` `); |
{ | ||
"name": "drizzle-orm-pg", | ||
"version": "0.15.0", | ||
"version": "0.15.1-4a5b2e3", | ||
"description": "Drizzle ORM package for PostgreSQL database", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -281,2 +281,37 @@ <div align='center'> | ||
## Table schemas | ||
Drizzle won't append any schema before table definition by default. So if your tables are in `public` schema drizzle generate -> `select * from "users"` | ||
But if you will specify any custom schema you want, then drizzle will generate -> `select * from "custom_schema"."users"` | ||
> **Warning** | ||
> If you will have tables with same names in different schemas then drizzle will respond with `never[]` error in result types and error from database | ||
> | ||
> In this case you may use [alias syntax](https://github.com/drizzle-team/drizzle-orm/tree/main/drizzle-orm-pg#join-aliases-and-self-joins) | ||
#### Usage example | ||
```typescript | ||
// Table in default schema | ||
const publicUsersTable = pgTable('users', { | ||
id: serial('id').primaryKey(), | ||
name: text('name').notNull(), | ||
verified: boolean('verified').notNull().default(false), | ||
jsonb: jsonb<string[]>('jsonb'), | ||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(), | ||
}); | ||
// Table in custom schema | ||
const mySchema = pgSchema('mySchema'); | ||
const usersTable = mySchema('users', { | ||
id: serial('id').primaryKey(), | ||
name: text('name').notNull(), | ||
verified: boolean('verified').notNull().default(false), | ||
jsonb: jsonb<string[]>('jsonb'), | ||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(), | ||
}); | ||
``` | ||
## Select, Insert, Update, Delete | ||
@@ -283,0 +318,0 @@ |
@@ -36,2 +36,14 @@ import { GetColumnData } from 'drizzle-orm'; | ||
}; | ||
declare const isPgSchemaSym: unique symbol; | ||
export interface PgSchema { | ||
schemaName: string; | ||
} | ||
export declare function isPgSchema(obj: unknown): obj is PgSchema; | ||
export declare function pgSchema<T extends string = string>(schemaName: T): (<TTableName extends string, TColumnsMap extends Record<string, AnyPgColumnBuilder<{}>>>(name: TTableName, columns: TColumnsMap, extraConfig?: ((self: Simplify<{ [Key in keyof TColumnsMap]: import("./columns/common").BuildColumn<TTableName, TColumnsMap[Key]>; }, {}>) => PgTableExtraConfig) | undefined) => PgTableWithColumns<{ | ||
name: TTableName; | ||
columns: Simplify<{ [Key in keyof TColumnsMap]: import("./columns/common").BuildColumn<TTableName, TColumnsMap[Key]>; }, {}>; | ||
}>) & { | ||
[isPgSchemaSym]: boolean; | ||
schemaName: T; | ||
}; | ||
export declare function pgTable<TTableName extends string, TColumnsMap extends Record<string, AnyPgColumnBuilder>>(name: TTableName, columns: TColumnsMap, extraConfig?: (self: BuildColumns<TTableName, TColumnsMap>) => PgTableExtraConfig): PgTableWithColumns<{ | ||
@@ -41,1 +53,2 @@ name: TTableName; | ||
}>; | ||
export {}; |
19
table.js
"use strict"; | ||
var _a, _b, _c, _d; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.pgTable = exports.PgTable = exports.Checks = exports.ExtraConfig = exports.ForeignKeys = exports.Indexes = void 0; | ||
exports.pgTable = exports.pgSchema = exports.isPgSchema = exports.PgTable = exports.Checks = exports.ExtraConfig = exports.ForeignKeys = exports.Indexes = void 0; | ||
const table_1 = require("drizzle-orm/table"); | ||
@@ -36,4 +36,14 @@ /** @internal */ | ||
}); | ||
function pgTable(name, columns, extraConfig) { | ||
const rawTable = new PgTable(name); | ||
const isPgSchemaSym = Symbol('isPgSchema'); | ||
function isPgSchema(obj) { | ||
return !!obj && typeof obj === 'function' && isPgSchemaSym in obj; | ||
} | ||
exports.isPgSchema = isPgSchema; | ||
function pgSchema(schemaName) { | ||
const columnFactory = (name, columns, extraConfig) => pgTableWithSchema(name, columns, schemaName, extraConfig); | ||
return Object.assign(columnFactory, { [isPgSchemaSym]: true, schemaName }); | ||
} | ||
exports.pgSchema = pgSchema; | ||
function pgTableWithSchema(name, columns, schema, extraConfig) { | ||
const rawTable = new PgTable(name, schema); | ||
const builtColumns = Object.fromEntries(Object.entries(columns).map(([name, colBuilder]) => { | ||
@@ -54,3 +64,6 @@ const column = colBuilder.build(rawTable); | ||
} | ||
function pgTable(name, columns, extraConfig) { | ||
return pgTableWithSchema(name, columns, undefined, extraConfig); | ||
} | ||
exports.pgTable = pgTable; | ||
//# sourceMappingURL=table.js.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
228851
3426
695