query-interface.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropSymbols = Object.getOwnPropertySymbols;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __propIsEnum = Object.prototype.propertyIsEnumerable;
  6. var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  7. var __spreadValues = (a, b) => {
  8. for (var prop in b || (b = {}))
  9. if (__hasOwnProp.call(b, prop))
  10. __defNormalProp(a, prop, b[prop]);
  11. if (__getOwnPropSymbols)
  12. for (var prop of __getOwnPropSymbols(b)) {
  13. if (__propIsEnum.call(b, prop))
  14. __defNormalProp(a, prop, b[prop]);
  15. }
  16. return a;
  17. };
  18. const _ = require("lodash");
  19. const Utils = require("../../utils");
  20. const QueryTypes = require("../../query-types");
  21. const Op = require("../../operators");
  22. const { QueryInterface } = require("../abstract/query-interface");
  23. class MSSqlQueryInterface extends QueryInterface {
  24. async removeColumn(tableName, attributeName, options) {
  25. options = __spreadValues({ raw: true }, options || {});
  26. const findConstraintSql = this.queryGenerator.getDefaultConstraintQuery(tableName, attributeName);
  27. const [results0] = await this.sequelize.query(findConstraintSql, options);
  28. if (results0.length) {
  29. const dropConstraintSql = this.queryGenerator.dropConstraintQuery(tableName, results0[0].name);
  30. await this.sequelize.query(dropConstraintSql, options);
  31. }
  32. const findForeignKeySql = this.queryGenerator.getForeignKeyQuery(tableName, attributeName);
  33. const [results] = await this.sequelize.query(findForeignKeySql, options);
  34. if (results.length) {
  35. const dropForeignKeySql = this.queryGenerator.dropForeignKeyQuery(tableName, results[0].constraint_name);
  36. await this.sequelize.query(dropForeignKeySql, options);
  37. }
  38. const primaryKeyConstraintSql = this.queryGenerator.getPrimaryKeyConstraintQuery(tableName, attributeName);
  39. const [result] = await this.sequelize.query(primaryKeyConstraintSql, options);
  40. if (result.length) {
  41. const dropConstraintSql = this.queryGenerator.dropConstraintQuery(tableName, result[0].constraintName);
  42. await this.sequelize.query(dropConstraintSql, options);
  43. }
  44. const removeSql = this.queryGenerator.removeColumnQuery(tableName, attributeName);
  45. return this.sequelize.query(removeSql, options);
  46. }
  47. async upsert(tableName, insertValues, updateValues, where, options) {
  48. const model = options.model;
  49. const wheres = [];
  50. options = __spreadValues({}, options);
  51. if (!Utils.isWhereEmpty(where)) {
  52. wheres.push(where);
  53. }
  54. let indexes = Object.values(model.uniqueKeys).map((item) => item.fields);
  55. indexes = indexes.concat(Object.values(model._indexes).filter((item) => item.unique).map((item) => item.fields));
  56. const attributes = Object.keys(insertValues);
  57. for (const index of indexes) {
  58. if (_.intersection(attributes, index).length === index.length) {
  59. where = {};
  60. for (const field of index) {
  61. where[field] = insertValues[field];
  62. }
  63. wheres.push(where);
  64. }
  65. }
  66. where = { [Op.or]: wheres };
  67. options.type = QueryTypes.UPSERT;
  68. options.raw = true;
  69. const sql = this.queryGenerator.upsertQuery(tableName, insertValues, updateValues, where, model, options);
  70. return await this.sequelize.query(sql, options);
  71. }
  72. }
  73. exports.MSSqlQueryInterface = MSSqlQueryInterface;
  74. //# sourceMappingURL=query-interface.js.map