1 | 'use strict';
|
2 |
|
3 | var algorithms = {
|
4 |
|
5 | where: function(query, table, where, count) {
|
6 | count = (typeof count === 'number') ? count : 0;
|
7 | let operator = '=';
|
8 | for(let column in where) {
|
9 | if(!count) {
|
10 | query = query.where([table, column], operator, where[column]);
|
11 | } else {
|
12 | query = query.and([table, column], operator, where[column]);
|
13 | }
|
14 | count++;
|
15 | }
|
16 | return {
|
17 | query: query,
|
18 | count: count
|
19 | };
|
20 | }
|
21 |
|
22 | };
|
23 |
|
24 | var Query = {
|
25 |
|
26 | |
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | create: function(params) {
|
35 | return params.ORM.querier.table(params.table)
|
36 | .insert([params.data])
|
37 | .returning(params.returning)
|
38 | .exec();
|
39 | },
|
40 |
|
41 | |
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | update: function(params) {
|
51 | let query = params.ORM.querier.table(params.table);
|
52 | query = query.update(params.data);
|
53 | let whereCount = 0;
|
54 | let results = algorithms.where(query, params.table, params.where, whereCount);
|
55 | whereCount += results.count;
|
56 | query = results.query;
|
57 | query = query.returning(params.returning);
|
58 | return query.exec();
|
59 | },
|
60 |
|
61 | |
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | find: function(params) {
|
73 | return new Promise((resolve, reject) => {
|
74 |
|
75 |
|
76 | let query = params.ORM.querier.table(params.table)
|
77 | .distinct()
|
78 | .select([[params.table, '*']]);
|
79 |
|
80 |
|
81 | for(let table in params.join) {
|
82 | let relationship = params.relationships[table];
|
83 | let throughTable = relationship.getThroughTable();
|
84 | let parent = (relationship.type === 'BELONGS_TO_MANY')
|
85 | ? relationship.identifer.parent_0
|
86 | : relationship.identifer.parent;
|
87 | let child = (relationship.type === 'BELONGS_TO_MANY')
|
88 | ? relationship.identifer.child_0
|
89 | : relationship.identifer.child;
|
90 | query.innerJoin(throughTable).on([throughTable, parent], '=', [params.table, child]);
|
91 | }
|
92 |
|
93 | let whereCount = 0;
|
94 |
|
95 | let results = algorithms.where(query, params.table, params.where, whereCount);
|
96 | whereCount += results.count;
|
97 | query = results.query;
|
98 |
|
99 | for(let table in params.join) {
|
100 | let relationship = params.relationships[table];
|
101 | let throughTable = relationship.getThroughTable();
|
102 |
|
103 |
|
104 | if(relationship.type === 'BELONGS_TO_MANY') {
|
105 | let relationTable = relationship.modelFactoryName;
|
106 | let parent_0 = relationship.identifer.parent_0;
|
107 | let child_0 = relationship.identifer.child_0;
|
108 | let parent_1 = relationship.identifer.parent_1;
|
109 | let child_1 = relationship.identifer.child_1;
|
110 |
|
111 | let relation = params.ORM.querier.table(throughTable)
|
112 | .select([[throughTable, parent_0]])
|
113 | .innerJoin(relationTable)
|
114 | .on([throughTable, parent_1], '=', [relationTable, child_1]);
|
115 |
|
116 | let joinWhereCount = 0;
|
117 | params.join[table].map((table) => {
|
118 |
|
119 | let results = algorithms.where(relation, relationTable, table, joinWhereCount);
|
120 | joinWhereCount += results.count;
|
121 | relation = results.query;
|
122 | });
|
123 |
|
124 | if(!whereCount) {
|
125 | query = query.where([params.table, child_0]);
|
126 | } else {
|
127 | query = query.and([params.table, child_0]);
|
128 | }
|
129 | query = query.in(relation.sql());
|
130 | whereCount++;
|
131 | }
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | else {
|
137 |
|
138 | let results = algorithms.where(query, table, params.join[table], whereCount);
|
139 | whereCount += results.count;
|
140 | query = results.query;
|
141 | }
|
142 | }
|
143 |
|
144 |
|
145 | query.exec()
|
146 | .then((results) => resolve(results))
|
147 | .catch((error) => reject(error));
|
148 | });
|
149 | },
|
150 |
|
151 | |
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | delete: function(params) {
|
160 | let query = params.ORM.querier.table(params.table);
|
161 | query = query.delete();
|
162 | let whereCount = 0;
|
163 | let results = algorithms.where(query, params.table, params.where, whereCount);
|
164 | whereCount += results.count;
|
165 | query = results.query;
|
166 | query = query.returning(params.returning);
|
167 | return query.exec();
|
168 | }
|
169 |
|
170 | };
|
171 |
|
172 | module.exports = Query; |
\ | No newline at end of file |