UNPKG

8.87 kBJavaScriptView Raw
1'use strict';
2var Relationship = require('./relationship');
3
4function QueryObject(table, attributes, relationships, options) {
5 this.table = table;
6 this.relationships = relationships;
7 this.relationshipsSorted = {};
8 this.relationshipsCreated = {};
9 this.options = options;
10 this.filter(attributes, relationships);
11 return this;
12}
13
14QueryObject.prototype.filter = function(attributes, relationships) {
15 var newAttributes = {};
16 var relationshipsSorted = {};
17 for(var attribute in attributes) {
18 if(relationships.hasOwnProperty(attribute)) {
19 switch(relationships[attribute].type) {
20 case Relationship.Types.belongsToOne: {
21 if(!relationshipsSorted.belongsToOne) relationshipsSorted.belongsToOne = {};
22 relationshipsSorted.belongsToOne[attribute] = attributes[attribute];
23 break;
24 }
25 case Relationship.Types.belongsToMany: {
26 if(!relationshipsSorted.belongsToMany) relationshipsSorted.belongsToMany = {};
27 relationshipsSorted.belongsToMany[attribute] = attributes[attribute];
28 break;
29 }
30 case Relationship.Types.hasOne: {
31 if(!relationshipsSorted.hasOne) relationshipsSorted.hasOne = {};
32 relationshipsSorted.hasOne[attribute] = attributes[attribute];
33 break;
34 }
35 case Relationship.Types.hasMany: {
36 if(!relationshipsSorted.hasMany) relationshipsSorted.hasMany = {};
37 relationshipsSorted.hasMany[attribute] = attributes[attribute];
38 break;
39 }
40 }
41 } else {
42 newAttributes[attribute] = attributes[attribute];
43 }
44 }
45 this.relationshipsSorted = Object.assign({}, this.relationshipsSorted, relationshipsSorted);
46 this.attributes = Object.assign({}, this.attributes, newAttributes);
47};
48
49//
50// Relationships Create
51//
52
53QueryObject.prototype.createBelongsToOne = function() {
54 var attributes = [];
55 var relationships = [];
56 var promises = [];
57 for(var attribute in this.relationshipsSorted.belongsToOne) {
58 if(this.relationships.hasOwnProperty(attribute)) {
59 var relationship = this.relationships[attribute];
60 var relationshipData = this.relationshipsSorted.belongsToOne[attribute];
61 //
62 // REFACTOR:
63 // Make sure we aren't trying to create injected models
64 if(!relationshipData[relationship.identifer.parent]) {
65 var promise = relationship.createBelongsToOne(relationshipData, this.options);
66 attributes.push(attribute);
67 relationships.push(relationship);
68 promises.push(promise);
69 }
70 // Attach
71 else {
72 // TODO: Rename to relationshipsAttached
73 this.relationshipsCreated[attribute] = relationshipData;
74 }
75 //
76 //
77 //
78 }
79 }
80 return Promise.all(promises).then((models) => {
81 var newAttributes = {};
82 models.map((model, index) => {
83 this.relationshipsCreated[attributes[index]] = model;
84 newAttributes[attributes[index]] = model;
85 });
86 this.filter(newAttributes, this.relationships);
87 });
88};
89
90QueryObject.prototype.createBelongsToMany = function() {
91 var attributes = [];
92 var relationships = [];
93 var promises = [];
94 for(var attribute in this.relationshipsSorted.belongsToMany) {
95 if(this.relationships.hasOwnProperty(attribute)) {
96 var relationship = this.relationships[attribute];
97 this.relationshipsSorted.belongsToMany[attribute].map((relationshipData) => {
98 //
99 // REFACTOR:
100 // Make sure we aren't trying to create injected models
101 let targetModelFactory = relationship.getTargetModel();
102 if(!relationshipData[targetModelFactory.identifer.parent]) {
103 var promise = relationship.createBelongsToOne(relationshipData, this.options);
104 attributes.push(attribute);
105 relationships.push(relationship);
106 promises.push(promise);
107 }
108 // Attach
109 else {
110 // TODO: Rename to relationshipsAttached
111 if(!this.relationshipsCreated[attribute]) {
112 this.relationshipsCreated[attribute] = [];
113 }
114 this.relationshipsCreated[attribute].push(relationshipData);
115 }
116 //
117 //
118 //
119 });
120 }
121 }
122 return Promise.all(promises).then((modelsMany) => {
123 var newAttributes = {};
124 modelsMany.map((models, index) => {
125 this.relationshipsCreated[attributes[index]] = models;
126 newAttributes[attributes[index]] = models;
127 });
128 this.filter(newAttributes, this.relationships);
129 });
130};
131
132QueryObject.prototype.createHasOne = function(parentModel) {
133 var attributes = [];
134 var relationships = [];
135 var promises = [];
136 for(var attribute in this.relationshipsSorted.hasOne) {
137 if(this.relationships.hasOwnProperty(attribute)) {
138 var relationship = this.relationships[attribute];
139 var relationshipData = this.relationshipsSorted.hasOne[attribute];
140 //
141 // REFACTOR:
142 // Make sure we aren't trying to create injected models
143 if(!relationshipData[relationship.identifer.parent]) {
144 var promise = relationship.createHasOne(relationshipData, parentModel, this.options);
145 attributes.push(attribute);
146 relationships.push(relationship);
147 promises.push(promise);
148 }
149 // Attach
150 else {
151 this.relationshipsCreated[attribute] = relationshipData;
152 }
153 //
154 //
155 //
156 }
157 }
158 return Promise.all(promises).then((models) => {
159 models.map((model, index) => {
160 this.relationshipsCreated[attributes[index]] = model;
161 });
162 });
163};
164
165QueryObject.prototype.createHasMany = function(parentModel) {
166 var attributes = [];
167 var relationships = [];
168 var promises = [];
169 for(var attribute in this.relationshipsSorted.hasMany) {
170 if(this.relationships.hasOwnProperty(attribute)) {
171 var relationship = this.relationships[attribute];
172 this.relationshipsSorted.hasMany[attribute].map((relationshipData) => {
173 //
174 // REFACTOR:
175 // Make sure we aren't trying to create injected models
176 if(!relationshipData[relationship.identifer.child]) {
177 var promise = relationship.createHasOne(relationshipData, parentModel, this.options);
178 attributes.push(attribute);
179 relationships.push(relationship);
180 promises.push(promise);
181 }
182 // Attach
183 else {
184 // TODO: Rename to relationshipsAttached
185 if(!this.relationshipsCreated[attribute]) {
186 this.relationshipsCreated[attribute] = [];
187 }
188 this.relationshipsCreated[attribute].push(relationshipData);
189 }
190 //
191 //
192 //
193 });
194 }
195 }
196 return Promise.all(promises).then((modelsMany) => {
197 modelsMany.map((models, index) => {
198 // TODO: Rename to relationshipsAttached
199 if(!this.relationshipsCreated[attributes[index]]) {
200 this.relationshipsCreated[attributes[index]] = [];
201 }
202 this.relationshipsCreated[attributes[index]].push(models);
203 });
204 });
205};
206
207//
208// Relationships Attach
209//
210
211QueryObject.prototype.attachBelongsToOne = function() {
212 for(var attribute in this.relationshipsSorted.belongsToOne) {
213 if(this.relationships.hasOwnProperty(attribute)) {
214 this.attributes = this.relationships[attribute].attachAsAttribute(
215 this.attributes,
216 this.relationshipsSorted.belongsToOne[attribute]
217 );
218 }
219 }
220};
221
222QueryObject.prototype.attachBelongsToMany = function(childModel, options) {
223 var promises = [];
224 for(var attribute in this.relationshipsSorted.belongsToMany) {
225 if(this.relationships.hasOwnProperty(attribute)) {
226 var promise = this.relationships[attribute].attach(
227 childModel,
228 this.relationshipsSorted.belongsToMany[attribute],
229 options
230 );
231 promises.push(promise);
232 }
233 }
234 return Promise.all(promises);
235};
236
237//
238// Concat
239//
240QueryObject.prototype.concatCreatedRelationships = function(model) {
241 return Object.assign({}, model, this.relationshipsCreated);
242};
243
244//
245// Getters
246//
247
248QueryObject.prototype.getTable = function() {
249 return this.table;
250};
251
252QueryObject.prototype.getRelationships = function() {
253 return this.relationships;
254};
255
256QueryObject.prototype.getAttributes = function() {
257 return this.attributes;
258};
259
260QueryObject.prototype.getJoin = function() {
261 return Object.assign({},
262 this.relationshipsSorted.belongsToOne,
263 this.relationshipsSorted.belongsToMany,
264 this.relationshipsSorted.hasOne,
265 this.relationshipsSorted.hasMany);
266};
267
268QueryObject.prototype.getOptions = function() {
269 return this.options;
270};
271
272module.exports = QueryObject;
\No newline at end of file