UNPKG

7.11 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.filterAttributes(attributes, relationships);
11 this.filterRelationshipsSorted(attributes, relationships);
12 return this;
13}
14
15QueryObject.prototype.filterAttributes = function(attributes, relationships) {
16 var newAttributes = {};
17 for(var attribute in attributes) {
18 if(!relationships.hasOwnProperty(attribute)) {
19 newAttributes[attribute] = attributes[attribute];
20 }
21 }
22 this.attributes = Object.assign({}, this.attributes, newAttributes);
23};
24
25QueryObject.prototype.filterRelationshipsSorted = function(attributes, relationships) {
26 var relationshipsSorted = {};
27 for(var attribute in attributes) {
28 if(relationships.hasOwnProperty(attribute)) {
29 switch(relationships[attribute].type) {
30 case Relationship.Types.belongsToOne: {
31 relationshipsSorted.belongsToOne = {};
32 relationshipsSorted.belongsToOne[attribute] = attributes[attribute];
33 break;
34 }
35 case Relationship.Types.belongsToMany: {
36 relationshipsSorted.belongsToMany = {};
37 relationshipsSorted.belongsToMany[attribute] = attributes[attribute];
38 break;
39 }
40 case Relationship.Types.hasOne: {
41 relationshipsSorted.hasOne = {};
42 relationshipsSorted.hasOne[attribute] = attributes[attribute];
43 break;
44 }
45 case Relationship.Types.hasMany: {
46 relationshipsSorted.hasMany = {};
47 relationshipsSorted.hasMany[attribute] = attributes[attribute];
48 break;
49 }
50 }
51 }
52 }
53 this.relationshipsSorted = Object.assign({}, this.relationshipsSorted, relationshipsSorted);
54};
55
56//
57// Relationships Create
58//
59
60QueryObject.prototype.createBelongsToOne = function() {
61 var attributes = [];
62 var relationships = [];
63 var promises = [];
64 for(var attribute in this.relationshipsSorted.belongsToOne) {
65 if(this.relationships.hasOwnProperty(attribute)) {
66 var relationship = this.relationships[attribute];
67 var promise = relationship.createBelongsToOne(
68 this.relationshipsSorted.belongsToOne[attribute],
69 this.options
70 );
71 attributes.push(attribute);
72 relationships.push(relationship);
73 promises.push(promise);
74 }
75 }
76 return Promise.all(promises).then((models) => {
77 var newAttributes = {};
78 models.map((model, index) => {
79 this.relationshipsCreated[attributes[index]] = model;
80 newAttributes[attributes[index]] = model;
81 });
82 this.filterAttributes(newAttributes, this.relationships);
83 this.filterRelationshipsSorted(newAttributes, this.relationships);
84 });
85};
86
87QueryObject.prototype.createBelongsToMany = function() {
88 var attributes = [];
89 var relationships = [];
90 var promises = [];
91 for(var attribute in this.relationshipsSorted.belongsToMany) {
92 if(this.relationships.hasOwnProperty(attribute)) {
93 var relationship = this.relationships[attribute];
94 var promise = relationship.createBelongsToMany(
95 this.relationshipsSorted.belongsToMany[attribute],
96 this.options
97 );
98 attributes.push(attribute);
99 relationships.push(relationship);
100 promises.push(promise);
101 }
102 }
103 return Promise.all(promises).then((modelsMany) => {
104 var newAttributes = {};
105 modelsMany.map((models, index) => {
106 this.relationshipsCreated[attributes[index]] = models;
107 newAttributes[attributes[index]] = models;
108 });
109 this.filterAttributes(newAttributes, this.relationships);
110 this.filterRelationshipsSorted(newAttributes, this.relationships);
111 });
112};
113
114QueryObject.prototype.createHasOne = function(parentModel) {
115 var attributes = [];
116 var relationships = [];
117 var promises = [];
118 for(var attribute in this.relationshipsSorted.hasOne) {
119 if(this.relationships.hasOwnProperty(attribute)) {
120 var relationship = this.relationships[attribute];
121 var promise = relationship.createHasOne(
122 this.relationshipsSorted.hasOne[attribute],
123 parentModel,
124 this.options
125 );
126 attributes.push(attribute);
127 relationships.push(relationship);
128 promises.push(promise);
129 }
130 }
131 return Promise.all(promises).then((models) => {
132 models.map((model, index) => {
133 this.relationshipsCreated[attributes[index]] = model;
134 });
135 });
136};
137
138QueryObject.prototype.createHasMany = function(parentModel) {
139 var attributes = [];
140 var relationships = [];
141 var promises = [];
142 for(var attribute in this.relationshipsSorted.hasMany) {
143 if(this.relationships.hasOwnProperty(attribute)) {
144 var relationship = this.relationships[attribute];
145 var promise = relationship.createHasMany(
146 this.relationshipsSorted.hasMany[attribute],
147 parentModel,
148 this.options
149 );
150 attributes.push(attribute);
151 relationships.push(relationship);
152 promises.push(promise);
153 }
154 }
155 return Promise.all(promises).then((modelsMany) => {
156 modelsMany.map((models, index) => {
157 this.relationshipsCreated[attributes[index]] = models;
158 });
159 });
160};
161
162//
163// Relationships Attach
164//
165
166QueryObject.prototype.attachBelongsToOne = function() {
167 for(var attribute in this.relationshipsSorted.belongsToOne) {
168 if(this.relationships.hasOwnProperty(attribute)) {
169 this.attributes = this.relationships[attribute].attachAsAttribute(
170 this.attributes,
171 this.relationshipsSorted.belongsToOne[attribute]
172 );
173 }
174 }
175};
176
177QueryObject.prototype.attachBelongsToMany = function(childModel, options) {
178 var promises = [];
179 for(var attribute in this.relationshipsSorted.belongsToMany) {
180 if(this.relationships.hasOwnProperty(attribute)) {
181 var promise = this.relationships[attribute].attach(
182 childModel,
183 this.relationshipsSorted.belongsToMany[attribute],
184 options
185 );
186 promises.push(promise);
187 }
188 }
189 return Promise.all(promises);
190};
191
192//
193// Concat
194//
195QueryObject.prototype.concatCreatedRelationships = function(model) {
196 return Object.assign({}, model, this.relationshipsCreated);
197};
198
199//
200// Getters
201//
202
203QueryObject.prototype.getTable = function() {
204 return this.table;
205};
206
207QueryObject.prototype.getRelationships = function() {
208 return this.relationships;
209};
210
211QueryObject.prototype.getAttributes = function() {
212 return this.attributes;
213};
214
215QueryObject.prototype.getJoin = function() {
216 return Object.assign({},
217 this.relationshipsSorted.belongsToOne,
218 this.relationshipsSorted.belongsToMany,
219 this.relationshipsSorted.hasOne,
220 this.relationshipsSorted.hasMany);
221};
222
223QueryObject.prototype.getOptions = function() {
224 return this.options;
225};
226
227module.exports = QueryObject;
\No newline at end of file