MCPcopy
hub / github.com/sequelize/sequelize / upsert

Method upsert

lib/dialects/db2/query-interface.js:22–73  ·  view source on GitHub ↗
(tableName, insertValues, updateValues, where, options)

Source from the content-addressed store, hash-verified

20 }
21
22 async upsert(tableName, insertValues, updateValues, where, options) {
23 options = { ...options };
24
25 const model = options.model;
26 const wheres = [];
27 const attributes = Object.keys(insertValues);
28 let indexes = [];
29 let indexFields;
30
31 options = _.clone(options);
32
33 if (!Utils.isWhereEmpty(where)) {
34 wheres.push(where);
35 }
36
37 // Lets combine unique keys and indexes into one
38 indexes = _.map(model.uniqueKeys, value => {
39 return value.fields;
40 });
41
42 model._indexes.forEach(value => {
43 if (value.unique) {
44 // fields in the index may both the strings or objects with an attribute property - lets sanitize that
45 indexFields = value.fields.map(field => {
46 if (_.isPlainObject(field)) {
47 return field.attribute;
48 }
49 return field;
50 });
51 indexes.push(indexFields);
52 }
53 });
54
55 for (const index of indexes) {
56 if (_.intersection(attributes, index).length === index.length) {
57 where = {};
58 for (const field of index) {
59 where[field] = insertValues[field];
60 }
61 wheres.push(where);
62 }
63 }
64
65 where = { [Op.or]: wheres };
66
67 options.type = QueryTypes.UPSERT;
68 options.raw = true;
69
70 const sql = this.queryGenerator.upsertQuery(tableName, insertValues, updateValues, where, model, options);
71 const result = await this.sequelize.query(sql, options);
72 return [result, undefined];
73 }
74
75 async createTable(tableName, attributes, options, model) {
76 let sql = '';

Callers

nothing calls this directly

Calls 3

cloneMethod · 0.80
upsertQueryMethod · 0.45
queryMethod · 0.45

Tested by

no test coverage detected