query-interface.js
4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"use strict";
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
const _ = require("lodash");
const Utils = require("../../utils");
const Op = require("../../operators");
const { QueryInterface } = require("../abstract/query-interface");
const QueryTypes = require("../../query-types");
class Db2QueryInterface extends QueryInterface {
async getForeignKeyReferencesForTable(tableName, options) {
const queryOptions = __spreadProps(__spreadValues({}, options), {
type: QueryTypes.FOREIGNKEYS
});
const query = this.queryGenerator.getForeignKeysQuery(tableName, this.sequelize.config.username.toUpperCase());
return this.sequelize.query(query, queryOptions);
}
async upsert(tableName, insertValues, updateValues, where, options) {
options = __spreadValues({}, options);
const model = options.model;
const wheres = [];
const attributes = Object.keys(insertValues);
let indexes = [];
let indexFields;
options = _.clone(options);
if (!Utils.isWhereEmpty(where)) {
wheres.push(where);
}
indexes = _.map(model.uniqueKeys, (value) => {
return value.fields;
});
model._indexes.forEach((value) => {
if (value.unique) {
indexFields = value.fields.map((field) => {
if (_.isPlainObject(field)) {
return field.attribute;
}
return field;
});
indexes.push(indexFields);
}
});
for (const index of indexes) {
if (_.intersection(attributes, index).length === index.length) {
where = {};
for (const field of index) {
where[field] = insertValues[field];
}
wheres.push(where);
}
}
where = { [Op.or]: wheres };
options.type = QueryTypes.UPSERT;
options.raw = true;
const sql = this.queryGenerator.upsertQuery(tableName, insertValues, updateValues, where, model, options);
const result = await this.sequelize.query(sql, options);
return [result, void 0];
}
async createTable(tableName, attributes, options, model) {
let sql = "";
options = __spreadValues({}, options);
if (options && options.uniqueKeys) {
_.forOwn(options.uniqueKeys, (uniqueKey) => {
if (uniqueKey.customIndex === void 0) {
uniqueKey.customIndex = true;
}
});
}
if (model) {
options.uniqueKeys = options.uniqueKeys || model.uniqueKeys;
}
attributes = _.mapValues(attributes, (attribute) => this.sequelize.normalizeAttribute(attribute));
if (options.indexes) {
options.indexes.forEach((fields) => {
const fieldArr = fields.fields;
if (fieldArr.length === 1) {
fieldArr.forEach((field) => {
for (const property in attributes) {
if (field === attributes[property].field) {
attributes[property].unique = true;
}
}
});
}
});
}
if (options.alter) {
if (options.indexes) {
options.indexes.forEach((fields) => {
const fieldArr = fields.fields;
if (fieldArr.length === 1) {
fieldArr.forEach((field) => {
for (const property in attributes) {
if (field === attributes[property].field && attributes[property].unique) {
attributes[property].unique = false;
}
}
});
}
});
}
}
if (!tableName.schema && (options.schema || !!model && model._schema)) {
tableName = this.queryGenerator.addSchema({
tableName,
_schema: !!model && model._schema || options.schema
});
}
attributes = this.queryGenerator.attributesToSQL(attributes, { table: tableName, context: "createTable" });
sql = this.queryGenerator.createTableQuery(tableName, attributes, options);
return await this.sequelize.query(sql, options);
}
}
exports.Db2QueryInterface = Db2QueryInterface;
//# sourceMappingURL=query-interface.js.map