| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- "use strict";
- var __defProp = Object.defineProperty;
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
- var __getOwnPropNames = Object.getOwnPropertyNames;
- var __hasOwnProp = Object.prototype.hasOwnProperty;
- var __export = (target, all) => {
- for (var name in all)
- __defProp(target, name, { get: all[name], enumerable: true });
- };
- var __copyProps = (to, from, except, desc) => {
- if (from && typeof from === "object" || typeof from === "function") {
- for (let key of __getOwnPropNames(from))
- if (!__hasOwnProp.call(to, key) && key !== except)
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
- }
- return to;
- };
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
- var insert_exports = {};
- __export(insert_exports, {
- SQLiteInsertBase: () => SQLiteInsertBase,
- SQLiteInsertBuilder: () => SQLiteInsertBuilder
- });
- module.exports = __toCommonJS(insert_exports);
- var import_entity = require("../../entity.cjs");
- var import_query_promise = require("../../query-promise.cjs");
- var import_sql = require("../../sql/sql.cjs");
- var import_table = require("../table.cjs");
- var import_table2 = require("../../table.cjs");
- var import_utils = require("../../utils.cjs");
- var import_utils2 = require("../utils.cjs");
- var import_query_builder = require("./query-builder.cjs");
- class SQLiteInsertBuilder {
- constructor(table, session, dialect, withList) {
- this.table = table;
- this.session = session;
- this.dialect = dialect;
- this.withList = withList;
- }
- static [import_entity.entityKind] = "SQLiteInsertBuilder";
- values(values) {
- values = Array.isArray(values) ? values : [values];
- if (values.length === 0) {
- throw new Error("values() must be called with at least one value");
- }
- const mappedValues = values.map((entry) => {
- const result = {};
- const cols = this.table[import_table2.Table.Symbol.Columns];
- for (const colKey of Object.keys(entry)) {
- const colValue = entry[colKey];
- result[colKey] = (0, import_entity.is)(colValue, import_sql.SQL) ? colValue : new import_sql.Param(colValue, cols[colKey]);
- }
- return result;
- });
- return new SQLiteInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
- }
- select(selectQuery) {
- const select = typeof selectQuery === "function" ? selectQuery(new import_query_builder.QueryBuilder()) : selectQuery;
- if (!(0, import_entity.is)(select, import_sql.SQL) && !(0, import_utils.haveSameKeys)(this.table[import_table2.Columns], select._.selectedFields)) {
- throw new Error(
- "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
- );
- }
- return new SQLiteInsertBase(this.table, select, this.session, this.dialect, this.withList, true);
- }
- }
- class SQLiteInsertBase extends import_query_promise.QueryPromise {
- constructor(table, values, session, dialect, withList, select) {
- super();
- this.session = session;
- this.dialect = dialect;
- this.config = { table, values, withList, select };
- }
- static [import_entity.entityKind] = "SQLiteInsert";
- /** @internal */
- config;
- returning(fields = this.config.table[import_table.SQLiteTable.Symbol.Columns]) {
- this.config.returning = (0, import_utils.orderSelectedFields)(fields);
- return this;
- }
- /**
- * Adds an `on conflict do nothing` clause to the query.
- *
- * Calling this method simply avoids inserting a row as its alternative action.
- *
- * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
- *
- * @param config The `target` and `where` clauses.
- *
- * @example
- * ```ts
- * // Insert one row and cancel the insert if there's a conflict
- * await db.insert(cars)
- * .values({ id: 1, brand: 'BMW' })
- * .onConflictDoNothing();
- *
- * // Explicitly specify conflict target
- * await db.insert(cars)
- * .values({ id: 1, brand: 'BMW' })
- * .onConflictDoNothing({ target: cars.id });
- * ```
- */
- onConflictDoNothing(config = {}) {
- if (!this.config.onConflict) this.config.onConflict = [];
- if (config.target === void 0) {
- this.config.onConflict.push(import_sql.sql` on conflict do nothing`);
- } else {
- const targetSql = Array.isArray(config.target) ? import_sql.sql`${config.target}` : import_sql.sql`${[config.target]}`;
- const whereSql = config.where ? import_sql.sql` where ${config.where}` : import_sql.sql``;
- this.config.onConflict.push(import_sql.sql` on conflict ${targetSql} do nothing${whereSql}`);
- }
- return this;
- }
- /**
- * Adds an `on conflict do update` clause to the query.
- *
- * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
- *
- * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
- *
- * @param config The `target`, `set` and `where` clauses.
- *
- * @example
- * ```ts
- * // Update the row if there's a conflict
- * await db.insert(cars)
- * .values({ id: 1, brand: 'BMW' })
- * .onConflictDoUpdate({
- * target: cars.id,
- * set: { brand: 'Porsche' }
- * });
- *
- * // Upsert with 'where' clause
- * await db.insert(cars)
- * .values({ id: 1, brand: 'BMW' })
- * .onConflictDoUpdate({
- * target: cars.id,
- * set: { brand: 'newBMW' },
- * where: sql`${cars.createdAt} > '2023-01-01'::date`,
- * });
- * ```
- */
- onConflictDoUpdate(config) {
- if (config.where && (config.targetWhere || config.setWhere)) {
- throw new Error(
- 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
- );
- }
- if (!this.config.onConflict) this.config.onConflict = [];
- const whereSql = config.where ? import_sql.sql` where ${config.where}` : void 0;
- const targetWhereSql = config.targetWhere ? import_sql.sql` where ${config.targetWhere}` : void 0;
- const setWhereSql = config.setWhere ? import_sql.sql` where ${config.setWhere}` : void 0;
- const targetSql = Array.isArray(config.target) ? import_sql.sql`${config.target}` : import_sql.sql`${[config.target]}`;
- const setSql = this.dialect.buildUpdateSet(this.config.table, (0, import_utils.mapUpdateSet)(this.config.table, config.set));
- this.config.onConflict.push(
- import_sql.sql` on conflict ${targetSql}${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`
- );
- return this;
- }
- /** @internal */
- getSQL() {
- return this.dialect.buildInsertQuery(this.config);
- }
- toSQL() {
- const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
- return rest;
- }
- /** @internal */
- _prepare(isOneTimeQuery = true) {
- return this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](
- this.dialect.sqlToQuery(this.getSQL()),
- this.config.returning,
- this.config.returning ? "all" : "run",
- true,
- void 0,
- {
- type: "insert",
- tables: (0, import_utils2.extractUsedTable)(this.config.table)
- }
- );
- }
- prepare() {
- return this._prepare(false);
- }
- run = (placeholderValues) => {
- return this._prepare().run(placeholderValues);
- };
- all = (placeholderValues) => {
- return this._prepare().all(placeholderValues);
- };
- get = (placeholderValues) => {
- return this._prepare().get(placeholderValues);
- };
- values = (placeholderValues) => {
- return this._prepare().values(placeholderValues);
- };
- async execute() {
- return this.config.returning ? this.all() : this.run();
- }
- $dynamic() {
- return this;
- }
- }
- // Annotate the CommonJS export names for ESM import in node:
- 0 && (module.exports = {
- SQLiteInsertBase,
- SQLiteInsertBuilder
- });
- //# sourceMappingURL=insert.cjs.map
|