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