query-interface.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
  19. var __export = (target, all) => {
  20. __markAsModule(target);
  21. for (var name in all)
  22. __defProp(target, name, { get: all[name], enumerable: true });
  23. };
  24. __export(exports, {
  25. OracleQueryInterface: () => OracleQueryInterface
  26. });
  27. const { QueryInterface } = require("../abstract/query-interface");
  28. const QueryTypes = require("../../query-types");
  29. const _ = require("lodash");
  30. class OracleQueryInterface extends QueryInterface {
  31. async upsert(tableName, insertValues, updateValues, where, options) {
  32. options = __spreadValues({}, options);
  33. const model = options.model;
  34. const primaryKeys = Object.values(model.primaryKeys).map((item) => item.field);
  35. const uniqueKeys = Object.values(model.uniqueKeys).filter((c) => c.fields.length > 0).map((c) => c.fields);
  36. const indexKeys = Object.values(model._indexes).filter((c) => c.unique && c.fields.length > 0).map((c) => c.fields);
  37. options.type = QueryTypes.UPSERT;
  38. options.updateOnDuplicate = Object.keys(updateValues);
  39. options.upsertKeys = [];
  40. for (const field of options.updateOnDuplicate) {
  41. const uniqueKey = uniqueKeys.find((fields) => fields.includes(field));
  42. if (uniqueKey) {
  43. options.upsertKeys = uniqueKey;
  44. break;
  45. }
  46. const indexKey = indexKeys.find((fields) => fields.includes(field));
  47. if (indexKey) {
  48. options.upsertKeys = indexKey;
  49. break;
  50. }
  51. }
  52. if (options.upsertKeys.length === 0 || _.intersection(options.updateOnDuplicate, primaryKeys).length) {
  53. options.upsertKeys = primaryKeys;
  54. }
  55. options.upsertKeys = _.uniq(options.upsertKeys);
  56. let whereHasNull = false;
  57. primaryKeys.forEach((element) => {
  58. if (where[element] === null) {
  59. whereHasNull = true;
  60. }
  61. });
  62. if (whereHasNull === true) {
  63. where = options.upsertKeys.reduce((result, attribute) => {
  64. result[attribute] = insertValues[attribute];
  65. return result;
  66. }, {});
  67. }
  68. const sql = this.queryGenerator.upsertQuery(tableName, insertValues, updateValues, where, model, options);
  69. if (sql.bind) {
  70. options.bind = void 0;
  71. }
  72. return await this.sequelize.query(sql, options);
  73. }
  74. }
  75. //# sourceMappingURL=query-interface.js.map