drizzle-orm-pg
Advanced tools
Comparing version 0.12.0-beta.19 to 0.12.0-beta.20
@@ -46,6 +46,17 @@ import { Connector, Dialect, Driver, Logger, MigrationMeta, Session } from 'drizzle-orm'; | ||
buildDeleteQuery<TTable extends AnyPgTable>({ table, where, returning, }: PgDeleteConfig<TTable>): AnyPgSQL<GetTableName<TTable>>; | ||
buildUpdateSet<TTableName extends TableName>(table: AnyPgTable, set: PgUpdateSet<AnyPgTable>): AnyPgSQL<TTableName>; | ||
buildUpdateSet(table: AnyPgTable, set: PgUpdateSet<AnyPgTable>): AnyPgSQL; | ||
orderSelectedFields(fields: PgSelectFields<TableName>, resultTableName: string): PgSelectFieldsOrdered; | ||
buildUpdateQuery<TTable extends AnyPgTable>({ table, set, where, returning, }: PgUpdateConfig<TTable>): AnySQL; | ||
private prepareTableFieldsForQuery; | ||
/** | ||
* Builds selection SQL with provided fields/expressions | ||
* | ||
* Examples: | ||
* | ||
* `select <selection> from` | ||
* | ||
* `insert ... returning <selection>` | ||
* | ||
* If `isSingleTable` is true, then columns won't be prefixed with table name | ||
*/ | ||
private buildSelection; | ||
buildSelectQuery({ fields, where, table, joins, orderBy, limit, offset, }: PgSelectConfig): AnyPgSQL; | ||
@@ -52,0 +63,0 @@ buildInsertQuery({ table, values, onConflict, returning }: AnyPgInsertConfig): AnyPgSQL; |
@@ -146,3 +146,3 @@ "use strict"; | ||
const returningSql = returning | ||
? drizzle_orm_1.sql.fromList([(0, drizzle_orm_1.sql) ` returning `, ...this.prepareTableFieldsForQuery(returning, { isSingleTable: true })]) | ||
? (0, drizzle_orm_1.sql) ` returning ${this.buildSelection(returning, { isSingleTable: true })}` | ||
: undefined; | ||
@@ -158,3 +158,4 @@ const whereSql = where ? (0, drizzle_orm_1.sql) ` where ${where}` : undefined; | ||
const col = table[utils_1.tableColumns][colName]; | ||
const res = (0, drizzle_orm_1.sql) `${new sql_1.Name(col.name)} = ${value}`; | ||
const mappedValue = (value instanceof sql_1.SQL || value === null) ? value : (0, drizzle_orm_1.param)(value, col); | ||
const res = (0, drizzle_orm_1.sql) `${new sql_1.Name(col.name)} = ${mappedValue}`; | ||
if (i < setSize - 1) { | ||
@@ -177,3 +178,3 @@ return [res, drizzle_orm_1.sql.raw(', ')]; | ||
const returningSql = returning | ||
? (0, drizzle_orm_1.sql) ` returning ${drizzle_orm_1.sql.fromList(this.prepareTableFieldsForQuery(returning, { isSingleTable: true }))}` | ||
? (0, drizzle_orm_1.sql) ` returning ${this.buildSelection(returning, { isSingleTable: true })}` | ||
: undefined; | ||
@@ -183,5 +184,16 @@ const whereSql = where ? (0, drizzle_orm_1.sql) ` where ${where}` : undefined; | ||
} | ||
prepareTableFieldsForQuery(columns, { isSingleTable = false } = {}) { | ||
/** | ||
* Builds selection SQL with provided fields/expressions | ||
* | ||
* Examples: | ||
* | ||
* `select <selection> from` | ||
* | ||
* `insert ... returning <selection>` | ||
* | ||
* If `isSingleTable` is true, then columns won't be prefixed with table name | ||
*/ | ||
buildSelection(columns, { isSingleTable = false } = {}) { | ||
const columnsLen = columns.length; | ||
return columns | ||
const chunks = columns | ||
.map(({ column }, i) => { | ||
@@ -216,6 +228,7 @@ const chunk = []; | ||
.flat(1); | ||
return drizzle_orm_1.sql.fromList(chunks); | ||
} | ||
buildSelectQuery({ fields, where, table, joins, orderBy, limit, offset, }) { | ||
const joinKeys = Object.keys(joins); | ||
const fieldsSql = drizzle_orm_1.sql.fromList(this.prepareTableFieldsForQuery(fields, { isSingleTable: joinKeys.length === 0 })); | ||
const selection = this.buildSelection(fields, { isSingleTable: joinKeys.length === 0 }); | ||
const joinsArray = []; | ||
@@ -245,10 +258,10 @@ joinKeys.forEach((tableAlias, index) => { | ||
const offsetSql = offset ? (0, drizzle_orm_1.sql) ` offset ${offset}` : undefined; | ||
return (0, drizzle_orm_1.sql) `select ${fieldsSql} from ${table}${joinsSql}${whereSql}${orderBySql}${limitSql}${offsetSql}`; | ||
return (0, drizzle_orm_1.sql) `select ${selection} from ${table}${joinsSql}${whereSql}${orderBySql}${limitSql}${offsetSql}`; | ||
} | ||
buildInsertQuery({ table, values, onConflict, returning }) { | ||
const joinedValues = []; | ||
const valuesSqlList = []; | ||
const columns = table[utils_1.tableColumns]; | ||
const columnKeys = Object.keys(columns); | ||
const insertOrder = Object.values(columns).map((column) => new sql_1.Name(column.name)); | ||
values.forEach((value) => { | ||
values.forEach((value, valueIndex) => { | ||
const valueList = []; | ||
@@ -261,12 +274,18 @@ columnKeys.forEach((colKey) => { | ||
} | ||
else if (colValue instanceof sql_1.SQL || colValue === null) { | ||
valueList.push(colValue); | ||
} | ||
else { | ||
valueList.push(column.mapToDriverValue(colValue)); | ||
valueList.push((0, drizzle_orm_1.param)(colValue, column)); | ||
} | ||
}); | ||
joinedValues.push(valueList); | ||
valuesSqlList.push(valueList); | ||
if (valueIndex < values.length - 1) { | ||
valuesSqlList.push((0, drizzle_orm_1.sql) `, `); | ||
} | ||
}); | ||
const valuesSql = drizzle_orm_1.sql.fromList(valuesSqlList); | ||
const returningSql = returning | ||
? (0, drizzle_orm_1.sql) ` returning ${drizzle_orm_1.sql.fromList(this.prepareTableFieldsForQuery(returning, { isSingleTable: true }))}` | ||
? (0, drizzle_orm_1.sql) ` returning ${this.buildSelection(returning, { isSingleTable: true })}` | ||
: undefined; | ||
const valuesSql = joinedValues.length === 1 ? joinedValues[0] : joinedValues; | ||
const onConflictSql = onConflict ? (0, drizzle_orm_1.sql) ` on conflict ${onConflict}` : undefined; | ||
@@ -273,0 +292,0 @@ return (0, drizzle_orm_1.sql) `insert into ${table} ${insertOrder} values ${valuesSql}${onConflictSql}${returningSql}`; |
{ | ||
"name": "drizzle-orm-pg", | ||
"version": "0.12.0-beta.19", | ||
"version": "0.12.0-beta.20", | ||
"description": "Drizzle ORM package for PostgreSQL database", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
204037
2831