query-interface.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 sequelizeErrors = require("../../errors");
  22. const { QueryInterface } = require("../abstract/query-interface");
  23. const QueryTypes = require("../../query-types");
  24. class MySQLQueryInterface extends QueryInterface {
  25. async removeColumn(tableName, columnName, options) {
  26. options = options || {};
  27. const [results] = await this.sequelize.query(this.queryGenerator.getForeignKeyQuery(tableName.tableName ? tableName : {
  28. tableName,
  29. schema: this.sequelize.config.database
  30. }, columnName), __spreadValues({ raw: true }, options));
  31. if (results.length && results[0].constraint_name !== "PRIMARY") {
  32. await Promise.all(results.map((constraint) => this.sequelize.query(this.queryGenerator.dropForeignKeyQuery(tableName, constraint.constraint_name), __spreadValues({ raw: true }, options))));
  33. }
  34. return await this.sequelize.query(this.queryGenerator.removeColumnQuery(tableName, columnName), __spreadValues({ raw: true }, options));
  35. }
  36. async upsert(tableName, insertValues, updateValues, where, options) {
  37. options = __spreadValues({}, options);
  38. options.type = QueryTypes.UPSERT;
  39. options.updateOnDuplicate = Object.keys(updateValues);
  40. options.upsertKeys = Object.values(options.model.primaryKeys).map((item) => item.field);
  41. const model = options.model;
  42. const sql = this.queryGenerator.insertQuery(tableName, insertValues, model.rawAttributes, options);
  43. return await this.sequelize.query(sql, options);
  44. }
  45. async removeConstraint(tableName, constraintName, options) {
  46. const sql = this.queryGenerator.showConstraintsQuery(tableName.tableName ? tableName : {
  47. tableName,
  48. schema: this.sequelize.config.database
  49. }, constraintName);
  50. const constraints = await this.sequelize.query(sql, __spreadProps(__spreadValues({}, options), {
  51. type: this.sequelize.QueryTypes.SHOWCONSTRAINTS
  52. }));
  53. const constraint = constraints[0];
  54. let query;
  55. if (!constraint || !constraint.constraintType) {
  56. throw new sequelizeErrors.UnknownConstraintError({
  57. message: `Constraint ${constraintName} on table ${tableName} does not exist`,
  58. constraint: constraintName,
  59. table: tableName
  60. });
  61. }
  62. if (constraint.constraintType === "FOREIGN KEY") {
  63. query = this.queryGenerator.dropForeignKeyQuery(tableName, constraintName);
  64. } else {
  65. query = this.queryGenerator.removeIndexQuery(constraint.tableName, constraint.constraintName);
  66. }
  67. return await this.sequelize.query(query, options);
  68. }
  69. }
  70. exports.MySQLQueryInterface = MySQLQueryInterface;
  71. //# sourceMappingURL=query-interface.js.map