query-interface.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __defProps = Object.defineProperties;
  4. var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
  5. var __getOwnPropSymbols = Object.getOwnPropertySymbols;
  6. var __hasOwnProp = Object.prototype.hasOwnProperty;
  7. var __propIsEnum = Object.prototype.propertyIsEnumerable;
  8. var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  9. var __spreadValues = (a, b) => {
  10. for (var prop in b || (b = {}))
  11. if (__hasOwnProp.call(b, prop))
  12. __defNormalProp(a, prop, b[prop]);
  13. if (__getOwnPropSymbols)
  14. for (var prop of __getOwnPropSymbols(b)) {
  15. if (__propIsEnum.call(b, prop))
  16. __defNormalProp(a, prop, b[prop]);
  17. }
  18. return a;
  19. };
  20. var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
  21. const _ = require("lodash");
  22. const Utils = require("../../utils");
  23. const DataTypes = require("../../data-types");
  24. const Transaction = require("../../transaction");
  25. const QueryTypes = require("../../query-types");
  26. class QueryInterface {
  27. constructor(sequelize, queryGenerator) {
  28. this.sequelize = sequelize;
  29. this.queryGenerator = queryGenerator;
  30. }
  31. async createDatabase(database, options) {
  32. options = options || {};
  33. const sql = this.queryGenerator.createDatabaseQuery(database, options);
  34. return await this.sequelize.query(sql, options);
  35. }
  36. async dropDatabase(database, options) {
  37. options = options || {};
  38. const sql = this.queryGenerator.dropDatabaseQuery(database);
  39. return await this.sequelize.query(sql, options);
  40. }
  41. async createSchema(schema, options) {
  42. options = options || {};
  43. const sql = this.queryGenerator.createSchema(schema);
  44. return await this.sequelize.query(sql, options);
  45. }
  46. async dropSchema(schema, options) {
  47. options = options || {};
  48. const sql = this.queryGenerator.dropSchema(schema);
  49. return await this.sequelize.query(sql, options);
  50. }
  51. async dropAllSchemas(options) {
  52. options = options || {};
  53. if (!this.queryGenerator._dialect.supports.schemas) {
  54. return this.sequelize.drop(options);
  55. }
  56. const schemas = await this.showAllSchemas(options);
  57. return Promise.all(schemas.map((schemaName) => this.dropSchema(schemaName, options)));
  58. }
  59. async showAllSchemas(options) {
  60. options = __spreadProps(__spreadValues({}, options), {
  61. raw: true,
  62. type: this.sequelize.QueryTypes.SELECT
  63. });
  64. const showSchemasSql = this.queryGenerator.showSchemasQuery(options);
  65. const schemaNames = await this.sequelize.query(showSchemasSql, options);
  66. return _.flatten(schemaNames.map((value) => value.schema_name ? value.schema_name : value));
  67. }
  68. async databaseVersion(options) {
  69. return await this.sequelize.query(this.queryGenerator.versionQuery(), __spreadProps(__spreadValues({}, options), { type: QueryTypes.VERSION }));
  70. }
  71. async createTable(tableName, attributes, options, model) {
  72. let sql = "";
  73. options = __spreadValues({}, options);
  74. if (options && options.uniqueKeys) {
  75. _.forOwn(options.uniqueKeys, (uniqueKey) => {
  76. if (uniqueKey.customIndex === void 0) {
  77. uniqueKey.customIndex = true;
  78. }
  79. });
  80. }
  81. if (model) {
  82. options.uniqueKeys = options.uniqueKeys || model.uniqueKeys;
  83. }
  84. attributes = _.mapValues(attributes, (attribute) => this.sequelize.normalizeAttribute(attribute));
  85. await this.ensureEnums(tableName, attributes, options, model);
  86. if (!tableName.schema && (options.schema || !!model && model._schema)) {
  87. tableName = this.queryGenerator.addSchema({
  88. tableName,
  89. _schema: !!model && model._schema || options.schema
  90. });
  91. }
  92. attributes = this.queryGenerator.attributesToSQL(attributes, {
  93. table: tableName,
  94. context: "createTable",
  95. withoutForeignKeyConstraints: options.withoutForeignKeyConstraints
  96. });
  97. sql = this.queryGenerator.createTableQuery(tableName, attributes, options);
  98. return await this.sequelize.query(sql, options);
  99. }
  100. async tableExists(tableName, options) {
  101. const sql = this.queryGenerator.tableExistsQuery(tableName);
  102. const out = await this.sequelize.query(sql, __spreadProps(__spreadValues({}, options), {
  103. type: QueryTypes.SHOWTABLES
  104. }));
  105. return out.length === 1;
  106. }
  107. async dropTable(tableName, options) {
  108. options = __spreadValues({}, options);
  109. options.cascade = options.cascade || options.force || false;
  110. const sql = this.queryGenerator.dropTableQuery(tableName, options);
  111. await this.sequelize.query(sql, options);
  112. }
  113. async _dropAllTables(tableNames, skip, options) {
  114. for (const tableName of tableNames) {
  115. if (!skip.includes(tableName.tableName || tableName)) {
  116. await this.dropTable(tableName, __spreadProps(__spreadValues({}, options), { cascade: true }));
  117. }
  118. }
  119. }
  120. async dropAllTables(options) {
  121. options = options || {};
  122. const skip = options.skip || [];
  123. const tableNames = await this.showAllTables(options);
  124. const foreignKeys = await this.getForeignKeysForTables(tableNames, options);
  125. for (const tableName of tableNames) {
  126. let normalizedTableName = tableName;
  127. if (_.isObject(tableName)) {
  128. normalizedTableName = `${tableName.schema}.${tableName.tableName}`;
  129. }
  130. for (const foreignKey of foreignKeys[normalizedTableName]) {
  131. await this.sequelize.query(this.queryGenerator.dropForeignKeyQuery(tableName, foreignKey));
  132. }
  133. }
  134. await this._dropAllTables(tableNames, skip, options);
  135. }
  136. async renameTable(before, after, options) {
  137. options = options || {};
  138. const sql = this.queryGenerator.renameTableQuery(before, after);
  139. return await this.sequelize.query(sql, options);
  140. }
  141. async showAllTables(options) {
  142. options = __spreadProps(__spreadValues({}, options), {
  143. raw: true,
  144. type: QueryTypes.SHOWTABLES
  145. });
  146. const showTablesSql = this.queryGenerator.showTablesQuery(this.sequelize.config.database);
  147. const tableNames = await this.sequelize.query(showTablesSql, options);
  148. return _.flatten(tableNames);
  149. }
  150. async describeTable(tableName, options) {
  151. let schema = null;
  152. let schemaDelimiter = null;
  153. if (typeof options === "string") {
  154. schema = options;
  155. } else if (typeof options === "object" && options !== null) {
  156. schema = options.schema || null;
  157. schemaDelimiter = options.schemaDelimiter || null;
  158. }
  159. if (typeof tableName === "object" && tableName !== null) {
  160. schema = tableName.schema;
  161. tableName = tableName.tableName;
  162. }
  163. const sql = this.queryGenerator.describeTableQuery(tableName, schema, schemaDelimiter);
  164. options = __spreadProps(__spreadValues({}, options), { type: QueryTypes.DESCRIBE });
  165. try {
  166. const data = await this.sequelize.query(sql, options);
  167. if (_.isEmpty(data)) {
  168. throw new Error(`No description found for "${tableName}" table. Check the table name and schema; remember, they _are_ case sensitive.`);
  169. }
  170. return data;
  171. } catch (e) {
  172. if (e.original && e.original.code === "ER_NO_SUCH_TABLE") {
  173. throw new Error(`No description found for "${tableName}" table. Check the table name and schema; remember, they _are_ case sensitive.`);
  174. }
  175. throw e;
  176. }
  177. }
  178. async addColumn(table, key, attribute, options) {
  179. if (!table || !key || !attribute) {
  180. throw new Error("addColumn takes at least 3 arguments (table, attribute name, attribute definition)");
  181. }
  182. options = options || {};
  183. attribute = this.sequelize.normalizeAttribute(attribute);
  184. return await this.sequelize.query(this.queryGenerator.addColumnQuery(table, key, attribute), options);
  185. }
  186. async removeColumn(tableName, attributeName, options) {
  187. return this.sequelize.query(this.queryGenerator.removeColumnQuery(tableName, attributeName), options);
  188. }
  189. normalizeAttribute(dataTypeOrOptions) {
  190. let attribute;
  191. if (Object.values(DataTypes).includes(dataTypeOrOptions)) {
  192. attribute = { type: dataTypeOrOptions, allowNull: true };
  193. } else {
  194. attribute = dataTypeOrOptions;
  195. }
  196. return this.sequelize.normalizeAttribute(attribute);
  197. }
  198. quoteIdentifier(identifier2, force) {
  199. return this.queryGenerator.quoteIdentifier(identifier2, force);
  200. }
  201. quoteIdentifiers(identifiers) {
  202. return this.queryGenerator.quoteIdentifiers(identifiers);
  203. }
  204. async changeColumn(tableName, attributeName, dataTypeOrOptions, options) {
  205. options = options || {};
  206. const query = this.queryGenerator.attributesToSQL({
  207. [attributeName]: this.normalizeAttribute(dataTypeOrOptions)
  208. }, {
  209. context: "changeColumn",
  210. table: tableName
  211. });
  212. const sql = this.queryGenerator.changeColumnQuery(tableName, query);
  213. return this.sequelize.query(sql, options);
  214. }
  215. async assertTableHasColumn(tableName, columnName, options) {
  216. const description = await this.describeTable(tableName, options);
  217. if (description[columnName]) {
  218. return description;
  219. }
  220. throw new Error(`Table ${tableName} doesn't have the column ${columnName}`);
  221. }
  222. async renameColumn(tableName, attrNameBefore, attrNameAfter, options) {
  223. options = options || {};
  224. const data = (await this.assertTableHasColumn(tableName, attrNameBefore, options))[attrNameBefore];
  225. const _options = {};
  226. _options[attrNameAfter] = {
  227. attribute: attrNameAfter,
  228. type: data.type,
  229. allowNull: data.allowNull,
  230. defaultValue: data.defaultValue
  231. };
  232. if (data.defaultValue === null && !data.allowNull) {
  233. delete _options[attrNameAfter].defaultValue;
  234. }
  235. const sql = this.queryGenerator.renameColumnQuery(tableName, attrNameBefore, this.queryGenerator.attributesToSQL(_options));
  236. return await this.sequelize.query(sql, options);
  237. }
  238. async addIndex(tableName, attributes, options, rawTablename) {
  239. if (!Array.isArray(attributes)) {
  240. rawTablename = options;
  241. options = attributes;
  242. attributes = options.fields;
  243. }
  244. if (!rawTablename) {
  245. rawTablename = tableName;
  246. }
  247. options = Utils.cloneDeep(options);
  248. options.fields = attributes;
  249. const sql = this.queryGenerator.addIndexQuery(tableName, options, rawTablename);
  250. return await this.sequelize.query(sql, __spreadProps(__spreadValues({}, options), { supportsSearchPath: false }));
  251. }
  252. async showIndex(tableName, options) {
  253. const sql = this.queryGenerator.showIndexesQuery(tableName, options);
  254. return await this.sequelize.query(sql, __spreadProps(__spreadValues({}, options), { type: QueryTypes.SHOWINDEXES }));
  255. }
  256. async getForeignKeysForTables(tableNames, options) {
  257. if (tableNames.length === 0) {
  258. return {};
  259. }
  260. options = __spreadProps(__spreadValues({}, options), { type: QueryTypes.FOREIGNKEYS });
  261. const results = await Promise.all(tableNames.map((tableName) => this.sequelize.query(this.queryGenerator.getForeignKeysQuery(tableName, this.sequelize.config.database), options)));
  262. const result = {};
  263. tableNames.forEach((tableName, i) => {
  264. if (_.isObject(tableName)) {
  265. tableName = `${tableName.schema}.${tableName.tableName}`;
  266. }
  267. result[tableName] = Array.isArray(results[i]) ? results[i].map((r) => r.constraint_name) : [results[i] && results[i].constraint_name];
  268. result[tableName] = result[tableName].filter(_.identity);
  269. });
  270. return result;
  271. }
  272. async getForeignKeyReferencesForTable(tableName, options) {
  273. const queryOptions = __spreadProps(__spreadValues({}, options), {
  274. type: QueryTypes.FOREIGNKEYS
  275. });
  276. const query = this.queryGenerator.getForeignKeysQuery(tableName, this.sequelize.config.database);
  277. return this.sequelize.query(query, queryOptions);
  278. }
  279. async removeIndex(tableName, indexNameOrAttributes, options) {
  280. options = options || {};
  281. const sql = this.queryGenerator.removeIndexQuery(tableName, indexNameOrAttributes, options);
  282. return await this.sequelize.query(sql, options);
  283. }
  284. async addConstraint(tableName, options) {
  285. if (!options.fields) {
  286. throw new Error("Fields must be specified through options.fields");
  287. }
  288. if (!options.type) {
  289. throw new Error("Constraint type must be specified through options.type");
  290. }
  291. options = Utils.cloneDeep(options);
  292. const sql = this.queryGenerator.addConstraintQuery(tableName, options);
  293. return await this.sequelize.query(sql, options);
  294. }
  295. async showConstraint(tableName, constraintName, options) {
  296. const sql = this.queryGenerator.showConstraintsQuery(tableName, constraintName);
  297. return await this.sequelize.query(sql, __spreadProps(__spreadValues({}, options), { type: QueryTypes.SHOWCONSTRAINTS }));
  298. }
  299. async removeConstraint(tableName, constraintName, options) {
  300. return this.sequelize.query(this.queryGenerator.removeConstraintQuery(tableName, constraintName), options);
  301. }
  302. async insert(instance, tableName, values, options) {
  303. options = Utils.cloneDeep(options);
  304. options.hasTrigger = instance && instance.constructor.options.hasTrigger;
  305. const sql = this.queryGenerator.insertQuery(tableName, values, instance && instance.constructor.rawAttributes, options);
  306. options.type = QueryTypes.INSERT;
  307. options.instance = instance;
  308. const results = await this.sequelize.query(sql, options);
  309. if (instance)
  310. results[0].isNewRecord = false;
  311. return results;
  312. }
  313. async upsert(tableName, insertValues, updateValues, where, options) {
  314. options = __spreadValues({}, options);
  315. const model = options.model;
  316. options.type = QueryTypes.UPSERT;
  317. options.updateOnDuplicate = Object.keys(updateValues);
  318. options.upsertKeys = options.conflictFields || [];
  319. if (options.upsertKeys.length === 0) {
  320. const primaryKeys = Object.values(model.primaryKeys).map((item) => item.field);
  321. const uniqueKeys = Object.values(model.uniqueKeys).filter((c) => c.fields.length > 0).map((c) => c.fields);
  322. const indexKeys = Object.values(model._indexes).filter((c) => c.unique && c.fields.length > 0).map((c) => c.fields);
  323. for (const field of options.updateOnDuplicate) {
  324. const uniqueKey = uniqueKeys.find((fields) => fields.includes(field));
  325. if (uniqueKey) {
  326. options.upsertKeys = uniqueKey;
  327. break;
  328. }
  329. const indexKey = indexKeys.find((fields) => fields.includes(field));
  330. if (indexKey) {
  331. options.upsertKeys = indexKey;
  332. break;
  333. }
  334. }
  335. if (options.upsertKeys.length === 0 || _.intersection(options.updateOnDuplicate, primaryKeys).length) {
  336. options.upsertKeys = primaryKeys;
  337. }
  338. options.upsertKeys = _.uniq(options.upsertKeys);
  339. }
  340. const sql = this.queryGenerator.insertQuery(tableName, insertValues, model.rawAttributes, options);
  341. return await this.sequelize.query(sql, options);
  342. }
  343. async bulkInsert(tableName, records, options, attributes) {
  344. options = __spreadValues({}, options);
  345. options.type = QueryTypes.INSERT;
  346. const results = await this.sequelize.query(this.queryGenerator.bulkInsertQuery(tableName, records, options, attributes), options);
  347. return results[0];
  348. }
  349. async update(instance, tableName, values, identifier2, options) {
  350. options = __spreadValues({}, options);
  351. options.hasTrigger = instance && instance.constructor.options.hasTrigger;
  352. const sql = this.queryGenerator.updateQuery(tableName, values, identifier2, options, instance.constructor.rawAttributes);
  353. options.type = QueryTypes.UPDATE;
  354. options.instance = instance;
  355. return await this.sequelize.query(sql, options);
  356. }
  357. async bulkUpdate(tableName, values, identifier2, options, attributes) {
  358. options = Utils.cloneDeep(options);
  359. if (typeof identifier2 === "object")
  360. identifier2 = Utils.cloneDeep(identifier2);
  361. const sql = this.queryGenerator.updateQuery(tableName, values, identifier2, options, attributes);
  362. const table = _.isObject(tableName) ? tableName : { tableName };
  363. const model = options.model ? options.model : _.find(this.sequelize.modelManager.models, { tableName: table.tableName });
  364. options.type = QueryTypes.BULKUPDATE;
  365. options.model = model;
  366. return await this.sequelize.query(sql, options);
  367. }
  368. async delete(instance, tableName, identifier2, options) {
  369. const cascades = [];
  370. const sql = this.queryGenerator.deleteQuery(tableName, identifier2, {}, instance.constructor);
  371. options = __spreadValues({}, options);
  372. if (!!instance.constructor && !!instance.constructor.associations) {
  373. const keys = Object.keys(instance.constructor.associations);
  374. const length = keys.length;
  375. let association;
  376. for (let i = 0; i < length; i++) {
  377. association = instance.constructor.associations[keys[i]];
  378. if (association.options && association.options.onDelete && association.options.onDelete.toLowerCase() === "cascade" && association.options.useHooks === true) {
  379. cascades.push(association.accessors.get);
  380. }
  381. }
  382. }
  383. for (const cascade of cascades) {
  384. let instances = await instance[cascade](options);
  385. if (!instances)
  386. continue;
  387. if (!Array.isArray(instances))
  388. instances = [instances];
  389. for (const _instance of instances)
  390. await _instance.destroy(options);
  391. }
  392. options.instance = instance;
  393. return await this.sequelize.query(sql, options);
  394. }
  395. async bulkDelete(tableName, where, options, model) {
  396. options = Utils.cloneDeep(options);
  397. options = _.defaults(options, { limit: null });
  398. if (options.truncate === true) {
  399. return this.sequelize.query(this.queryGenerator.truncateTableQuery(tableName, options), options);
  400. }
  401. if (typeof identifier === "object")
  402. where = Utils.cloneDeep(where);
  403. return await this.sequelize.query(this.queryGenerator.deleteQuery(tableName, where, options, model), options);
  404. }
  405. async select(model, tableName, optionsArg) {
  406. const options = __spreadProps(__spreadValues({}, optionsArg), { type: QueryTypes.SELECT, model });
  407. return await this.sequelize.query(this.queryGenerator.selectQuery(tableName, options, model), options);
  408. }
  409. async increment(model, tableName, where, incrementAmountsByField, extraAttributesToBeUpdated, options) {
  410. options = Utils.cloneDeep(options);
  411. const sql = this.queryGenerator.arithmeticQuery("+", tableName, where, incrementAmountsByField, extraAttributesToBeUpdated, options);
  412. options.type = QueryTypes.UPDATE;
  413. options.model = model;
  414. return await this.sequelize.query(sql, options);
  415. }
  416. async decrement(model, tableName, where, incrementAmountsByField, extraAttributesToBeUpdated, options) {
  417. options = Utils.cloneDeep(options);
  418. const sql = this.queryGenerator.arithmeticQuery("-", tableName, where, incrementAmountsByField, extraAttributesToBeUpdated, options);
  419. options.type = QueryTypes.UPDATE;
  420. options.model = model;
  421. return await this.sequelize.query(sql, options);
  422. }
  423. async rawSelect(tableName, options, attributeSelector, Model) {
  424. options = Utils.cloneDeep(options);
  425. options = _.defaults(options, {
  426. raw: true,
  427. plain: true,
  428. type: QueryTypes.SELECT
  429. });
  430. const sql = this.queryGenerator.selectQuery(tableName, options, Model);
  431. if (attributeSelector === void 0) {
  432. throw new Error("Please pass an attribute selector!");
  433. }
  434. const data = await this.sequelize.query(sql, options);
  435. if (!options.plain) {
  436. return data;
  437. }
  438. const result = data ? data[attributeSelector] : null;
  439. if (!options || !options.dataType) {
  440. return result;
  441. }
  442. const dataType = options.dataType;
  443. if (dataType instanceof DataTypes.DECIMAL || dataType instanceof DataTypes.FLOAT) {
  444. if (result !== null) {
  445. return parseFloat(result);
  446. }
  447. }
  448. if (dataType instanceof DataTypes.INTEGER || dataType instanceof DataTypes.BIGINT) {
  449. if (result !== null) {
  450. return parseInt(result, 10);
  451. }
  452. }
  453. if (dataType instanceof DataTypes.DATE) {
  454. if (result !== null && !(result instanceof Date)) {
  455. return new Date(result);
  456. }
  457. }
  458. return result;
  459. }
  460. async createTrigger(tableName, triggerName, timingType, fireOnArray, functionName, functionParams, optionsArray, options) {
  461. const sql = this.queryGenerator.createTrigger(tableName, triggerName, timingType, fireOnArray, functionName, functionParams, optionsArray);
  462. options = options || {};
  463. if (sql) {
  464. return await this.sequelize.query(sql, options);
  465. }
  466. }
  467. async dropTrigger(tableName, triggerName, options) {
  468. const sql = this.queryGenerator.dropTrigger(tableName, triggerName);
  469. options = options || {};
  470. if (sql) {
  471. return await this.sequelize.query(sql, options);
  472. }
  473. }
  474. async renameTrigger(tableName, oldTriggerName, newTriggerName, options) {
  475. const sql = this.queryGenerator.renameTrigger(tableName, oldTriggerName, newTriggerName);
  476. options = options || {};
  477. if (sql) {
  478. return await this.sequelize.query(sql, options);
  479. }
  480. }
  481. async createFunction(functionName, params, returnType, language, body, optionsArray, options) {
  482. const sql = this.queryGenerator.createFunction(functionName, params, returnType, language, body, optionsArray, options);
  483. options = options || {};
  484. if (sql) {
  485. return await this.sequelize.query(sql, options);
  486. }
  487. }
  488. async dropFunction(functionName, params, options) {
  489. const sql = this.queryGenerator.dropFunction(functionName, params);
  490. options = options || {};
  491. if (sql) {
  492. return await this.sequelize.query(sql, options);
  493. }
  494. }
  495. async renameFunction(oldFunctionName, params, newFunctionName, options) {
  496. const sql = this.queryGenerator.renameFunction(oldFunctionName, params, newFunctionName);
  497. options = options || {};
  498. if (sql) {
  499. return await this.sequelize.query(sql, options);
  500. }
  501. }
  502. ensureEnums() {
  503. }
  504. async setIsolationLevel(transaction, value, options) {
  505. if (!transaction || !(transaction instanceof Transaction)) {
  506. throw new Error("Unable to set isolation level for a transaction without transaction object!");
  507. }
  508. if (transaction.parent || !value) {
  509. return;
  510. }
  511. options = __spreadProps(__spreadValues({}, options), { transaction: transaction.parent || transaction });
  512. const sql = this.queryGenerator.setIsolationLevelQuery(value, {
  513. parent: transaction.parent
  514. });
  515. if (!sql)
  516. return;
  517. return await this.sequelize.query(sql, options);
  518. }
  519. async startTransaction(transaction, options) {
  520. if (!transaction || !(transaction instanceof Transaction)) {
  521. throw new Error("Unable to start a transaction without transaction object!");
  522. }
  523. options = __spreadProps(__spreadValues({}, options), { transaction: transaction.parent || transaction });
  524. options.transaction.name = transaction.parent ? transaction.name : void 0;
  525. const sql = this.queryGenerator.startTransactionQuery(transaction);
  526. return await this.sequelize.query(sql, options);
  527. }
  528. async deferConstraints(transaction, options) {
  529. options = __spreadProps(__spreadValues({}, options), { transaction: transaction.parent || transaction });
  530. const sql = this.queryGenerator.deferConstraintsQuery(options);
  531. if (sql) {
  532. return await this.sequelize.query(sql, options);
  533. }
  534. }
  535. async commitTransaction(transaction, options) {
  536. if (!transaction || !(transaction instanceof Transaction)) {
  537. throw new Error("Unable to commit a transaction without transaction object!");
  538. }
  539. if (transaction.parent) {
  540. return;
  541. }
  542. options = __spreadProps(__spreadValues({}, options), {
  543. transaction: transaction.parent || transaction,
  544. supportsSearchPath: false,
  545. completesTransaction: true
  546. });
  547. const sql = this.queryGenerator.commitTransactionQuery(transaction);
  548. const promise = this.sequelize.query(sql, options);
  549. transaction.finished = "commit";
  550. return await promise;
  551. }
  552. async rollbackTransaction(transaction, options) {
  553. if (!transaction || !(transaction instanceof Transaction)) {
  554. throw new Error("Unable to rollback a transaction without transaction object!");
  555. }
  556. options = __spreadProps(__spreadValues({}, options), {
  557. transaction: transaction.parent || transaction,
  558. supportsSearchPath: false,
  559. completesTransaction: true
  560. });
  561. options.transaction.name = transaction.parent ? transaction.name : void 0;
  562. const sql = this.queryGenerator.rollbackTransactionQuery(transaction);
  563. const promise = this.sequelize.query(sql, options);
  564. transaction.finished = "rollback";
  565. return await promise;
  566. }
  567. }
  568. exports.QueryInterface = QueryInterface;
  569. //# sourceMappingURL=query-interface.js.map