drizzle-orm-pg
Advanced tools
Comparing version 0.15.0 to 0.15.1-19c5c02
@@ -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-19c5c02", | ||
"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,10 @@ import { GetColumnData } from 'drizzle-orm'; | ||
}; | ||
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]>; }, {}>; | ||
}>) & PgSchema; | ||
export declare function pgTable<TTableName extends string, TColumnsMap extends Record<string, AnyPgColumnBuilder>>(name: TTableName, columns: TColumnsMap, extraConfig?: (self: BuildColumns<TTableName, TColumnsMap>) => PgTableExtraConfig): PgTableWithColumns<{ | ||
@@ -38,0 +46,0 @@ name: TTableName; |
23
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,18 @@ /** @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 schemaValue = { | ||
schemaName, | ||
[isPgSchemaSym]: true, | ||
}; | ||
const columnFactory = (name, columns, extraConfig) => pgTableWithSchema(name, columns, schemaName, extraConfig); | ||
return Object.assign(columnFactory, schemaValue); | ||
} | ||
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 +68,6 @@ const column = colBuilder.build(rawTable); | ||
} | ||
function pgTable(name, columns, extraConfig) { | ||
return pgTableWithSchema(name, columns, undefined, extraConfig); | ||
} | ||
exports.pgTable = pgTable; | ||
//# sourceMappingURL=table.js.map |
@@ -13,2 +13,4 @@ import { SelectFields, SelectFieldsOrdered } from './operations'; | ||
checks: Check[]; | ||
name: string; | ||
schema: string | undefined; | ||
}; | ||
@@ -15,0 +17,0 @@ export declare function getTableColumns<TTable extends AnyPgTable>(table: TTable): import("./columns").AnyPgColumn<{ |
@@ -16,4 +16,6 @@ "use strict"; | ||
const foreignKeys = getTableForeignKeys(table); | ||
const name = table[drizzle_orm_1.Table.Symbol.Name]; | ||
const schema = table[drizzle_orm_1.Table.Symbol.Schema]; | ||
const extraConfig = table[table_1.PgTable.Symbol.ExtraConfig]; | ||
if (typeof extraConfig === 'undefined') | ||
if (typeof extraConfig === 'undefined') { | ||
return { | ||
@@ -24,3 +26,6 @@ columns, | ||
checks, | ||
name, | ||
schema | ||
}; | ||
} | ||
const builtConfig = extraConfig(table[table_1.PgTable.Symbol.Columns]); | ||
@@ -43,2 +48,4 @@ Object.entries(builtConfig).forEach(([_, builder]) => { | ||
checks, | ||
name, | ||
schema | ||
}; | ||
@@ -45,0 +52,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
229268
3434
695