1 |
|
2 |
|
3 | 'use strict';
|
4 |
|
5 | const _ = require('lodash');
|
6 | const Promise = require('bluebird');
|
7 | const validLocks = ['forShare', 'forUpdate'];
|
8 |
|
9 | function supportsReturning(client = {}) {
|
10 | if (!client.config || !client.config.client) return false;
|
11 | return ['postgresql', 'postgres', 'pg', 'oracle', 'mssql'].includes(client.config.client);
|
12 | }
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | const Sync = function(syncing, options) {
|
20 | options = options || {};
|
21 | this.query = syncing.query();
|
22 | this.syncing = syncing.resetQuery();
|
23 | this.options = options;
|
24 | if (options.debug) this.query.debug();
|
25 | if (options.transacting) {
|
26 | this.query.transacting(options.transacting);
|
27 | if (validLocks.indexOf(options.lock) > -1) this.query[options.lock]();
|
28 | }
|
29 | if (options.withSchema) this.query.withSchema(options.withSchema);
|
30 | };
|
31 |
|
32 | _.extend(Sync.prototype, {
|
33 |
|
34 |
|
35 | prefixFields: function(fields) {
|
36 | const tableName = this.syncing.tableName;
|
37 | const prefixed = {};
|
38 | for (const key in fields) {
|
39 | prefixed[tableName + '.' + key] = fields[key];
|
40 | }
|
41 | return prefixed;
|
42 | },
|
43 |
|
44 |
|
45 | first: Promise.method(function(attributes) {
|
46 | const model = this.syncing;
|
47 | const query = this.query;
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | const whereAttributes = _.omitBy(attributes, (attribute, name) => {
|
55 | return _.isPlainObject(attribute) || name === model.idAttribute;
|
56 | });
|
57 | const formattedAttributes = model.format(whereAttributes);
|
58 |
|
59 | if (model.idAttribute in attributes) {
|
60 | formattedAttributes[model.idAttribute] = attributes[model.idAttribute];
|
61 | }
|
62 |
|
63 | if (!_.isEmpty(formattedAttributes)) query.where(this.prefixFields(formattedAttributes));
|
64 | query.limit(1);
|
65 |
|
66 | return this.select();
|
67 | }),
|
68 |
|
69 |
|
70 |
|
71 | count: Promise.method(function(column) {
|
72 | const knex = this.query,
|
73 | options = this.options,
|
74 | relatedData = this.syncing.relatedData,
|
75 | fks = {};
|
76 |
|
77 | return Promise.bind(this)
|
78 | .then(function() {
|
79 |
|
80 |
|
81 | if (relatedData)
|
82 | return Promise.try(function() {
|
83 | if (relatedData.isThrough()) {
|
84 | fks[relatedData.key('foreignKey')] = relatedData.parentFk;
|
85 | const through = new relatedData.throughTarget(fks);
|
86 | relatedData.pivotColumns = through.parse(relatedData.pivotColumns);
|
87 | } else if (relatedData.type === 'hasMany') {
|
88 | const fk = relatedData.key('foreignKey');
|
89 | knex.where(fk, relatedData.parentFk);
|
90 | }
|
91 | });
|
92 | })
|
93 | .then(function() {
|
94 | options.query = knex;
|
95 |
|
96 | |
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | return this.syncing.triggerThen('counting', this.syncing, options);
|
109 | })
|
110 | .then(function() {
|
111 | return knex.count((column || '*') + ' as count');
|
112 | })
|
113 | .then(function(rows) {
|
114 | return rows[0].count;
|
115 | });
|
116 | }),
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | select: Promise.method(function() {
|
124 | const knex = this.query;
|
125 | const options = this.options;
|
126 | const relatedData = this.syncing.relatedData;
|
127 | const fks = {};
|
128 | let columns = null;
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | const queryContainsColumns = _(knex._statements)
|
134 | .filter({grouping: 'columns'})
|
135 | .some('value.length');
|
136 |
|
137 | return Promise.bind(this)
|
138 | .then(function() {
|
139 |
|
140 |
|
141 | options.query = knex;
|
142 |
|
143 |
|
144 |
|
145 | if (relatedData)
|
146 | return Promise.try(function() {
|
147 | if (relatedData.isThrough()) {
|
148 | fks[relatedData.key('foreignKey')] = relatedData.parentFk;
|
149 | const through = new relatedData.throughTarget(fks);
|
150 |
|
151 | return through.triggerThen('fetching', through, relatedData.pivotColumns, options).then(function() {
|
152 | relatedData.pivotColumns = through.parse(relatedData.pivotColumns);
|
153 | });
|
154 | }
|
155 | });
|
156 | })
|
157 | .tap(() => {
|
158 |
|
159 | if (relatedData) {
|
160 | relatedData.selectConstraints(knex, options);
|
161 | } else {
|
162 |
|
163 | if (options._beforeFn) options._beforeFn.call(knex, knex);
|
164 |
|
165 | if (options.columns) {
|
166 |
|
167 | columns = Array.isArray(options.columns) ? options.columns : [options.columns];
|
168 | } else if (!queryContainsColumns) {
|
169 |
|
170 |
|
171 | columns = [_.result(this.syncing, 'tableName') + '.*'];
|
172 | }
|
173 | }
|
174 |
|
175 |
|
176 |
|
177 | options.query = knex;
|
178 |
|
179 | |
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 | return this.syncing.triggerThen('fetching', this.syncing, columns, options);
|
203 | })
|
204 | .then(() => knex.select(columns));
|
205 | }),
|
206 |
|
207 |
|
208 | insert: Promise.method(function() {
|
209 | const syncing = this.syncing;
|
210 | return this.query.insert(
|
211 | syncing.format(_.extend(Object.create(null), syncing.attributes)),
|
212 | supportsReturning(this.query.client) ? '*' : null
|
213 | );
|
214 | }),
|
215 |
|
216 |
|
217 | update: Promise.method(function(attrs) {
|
218 | const syncing = this.syncing,
|
219 | query = this.query;
|
220 | if (syncing.id != null) query.where(syncing.format({[syncing.idAttribute]: syncing.id}));
|
221 | if (_.filter(query._statements, {grouping: 'where'}).length === 0) {
|
222 | throw new Error('A model cannot be updated without a "where" clause or an idAttribute.');
|
223 | }
|
224 | var updating = syncing.format(_.extend(Object.create(null), attrs));
|
225 | if (syncing.id === updating[syncing.idAttribute]) {
|
226 | delete updating[syncing.idAttribute];
|
227 | }
|
228 | if (supportsReturning(query.client)) query.returning('*');
|
229 | return query.update(updating);
|
230 | }),
|
231 |
|
232 |
|
233 | del: Promise.method(function() {
|
234 | const query = this.query,
|
235 | syncing = this.syncing;
|
236 | if (syncing.id != null) query.where(syncing.format({[syncing.idAttribute]: syncing.id}));
|
237 | if (_.filter(query._statements, {grouping: 'where'}).length === 0) {
|
238 | throw new Error('A model cannot be destroyed without a "where" clause or an idAttribute.');
|
239 | }
|
240 | return this.query.del();
|
241 | })
|
242 | });
|
243 |
|
244 | module.exports = Sync;
|