@vulkano/core 1.13.0 → 1.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/database/scaffold.js +118 -0
- package/package.json +1 -1
package/database/scaffold.js
CHANGED
|
@@ -56,6 +56,7 @@ module.exports = {
|
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
58
|
* Method to create a new record
|
|
59
|
+
*
|
|
59
60
|
* @param {Promise} data
|
|
60
61
|
*/
|
|
61
62
|
create(data) {
|
|
@@ -72,6 +73,7 @@ module.exports = {
|
|
|
72
73
|
|
|
73
74
|
/**
|
|
74
75
|
* Method to update a record
|
|
76
|
+
*
|
|
75
77
|
* @param {ObjectID} id
|
|
76
78
|
* @param {Object} data
|
|
77
79
|
* @returns {Promise}
|
|
@@ -102,6 +104,7 @@ module.exports = {
|
|
|
102
104
|
|
|
103
105
|
/**
|
|
104
106
|
* Method to delete a record
|
|
107
|
+
*
|
|
105
108
|
* @param {ObjectID} id
|
|
106
109
|
* @returns {Promise}
|
|
107
110
|
*/
|
|
@@ -113,6 +116,121 @@ module.exports = {
|
|
|
113
116
|
// Soft delete
|
|
114
117
|
return this.update(id, { active: false });
|
|
115
118
|
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Method to add a subdocument
|
|
123
|
+
*
|
|
124
|
+
* @param {String} key
|
|
125
|
+
* @param {ObjectID} parent
|
|
126
|
+
* @param {Object} data
|
|
127
|
+
* @returns {Promise}
|
|
128
|
+
*/
|
|
129
|
+
createSubdoc(key, parent, data) {
|
|
130
|
+
|
|
131
|
+
return this
|
|
132
|
+
.findOne({ _id: parent })
|
|
133
|
+
.then( (r) => {
|
|
134
|
+
|
|
135
|
+
if (!r) {
|
|
136
|
+
return VSError.reject('Invalid ID. Record not found.', 404);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
r[key].push(data);
|
|
140
|
+
|
|
141
|
+
r.markModified(key);
|
|
142
|
+
|
|
143
|
+
return r.save()
|
|
144
|
+
.then( () => {
|
|
145
|
+
return r[key][r[key].length - 1];
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
},
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Method to update a subdocument
|
|
154
|
+
*
|
|
155
|
+
* @param {String} key
|
|
156
|
+
* @param {ObjectID} parent
|
|
157
|
+
* @param {ObjectID} subdoc
|
|
158
|
+
* @param {Object} data
|
|
159
|
+
* @returns {Promise}
|
|
160
|
+
*/
|
|
161
|
+
updateSubdoc(key, parent, subdoc, data) {
|
|
162
|
+
|
|
163
|
+
return this
|
|
164
|
+
.findOne({ _id: parent })
|
|
165
|
+
.then( (r) => {
|
|
166
|
+
|
|
167
|
+
if (!r) {
|
|
168
|
+
return VSError.reject('Invalid ID. Record not found.', 404);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const current = r[key] ? r[key].id(subdoc) : null;
|
|
172
|
+
|
|
173
|
+
if (!current) {
|
|
174
|
+
return VSError.reject('Invalid ID. Item not found.', 404);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
r[key].id(subdoc).set({ ...data, _id: subdoc });
|
|
178
|
+
|
|
179
|
+
r.markModified(key);
|
|
180
|
+
|
|
181
|
+
return r.save()
|
|
182
|
+
.then( () => {
|
|
183
|
+
return r[key].id(subdoc);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
},
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Method to remove a subdocument
|
|
192
|
+
*
|
|
193
|
+
* @param {String} key
|
|
194
|
+
* @param {ObjectID} parent
|
|
195
|
+
* @param {ObjectID} subdoc
|
|
196
|
+
* @returns {Promise}
|
|
197
|
+
*/
|
|
198
|
+
removeSubdoc(key, parent, subdoc) {
|
|
199
|
+
|
|
200
|
+
return this
|
|
201
|
+
.findOne({ _id: parent })
|
|
202
|
+
.then( (r) => {
|
|
203
|
+
|
|
204
|
+
if (!r) {
|
|
205
|
+
return VSError.reject('Invalid ID. Record not found.', 404);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const current = r[key] ? r[key].id(subdoc) : null;
|
|
209
|
+
|
|
210
|
+
if (!current) {
|
|
211
|
+
return VSError.reject('Invalid ID. Item not found.', 404);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
r[key].id(subdoc).deleteOne();
|
|
215
|
+
|
|
216
|
+
r.markModified(key);
|
|
217
|
+
|
|
218
|
+
return r.save();
|
|
219
|
+
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
},
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* ALIAS: removeSubdoc
|
|
226
|
+
*
|
|
227
|
+
* @param {String} key
|
|
228
|
+
* @param {ObjectID} parent
|
|
229
|
+
* @param {ObjectID} subdoc
|
|
230
|
+
* @returns {Promise}
|
|
231
|
+
*/
|
|
232
|
+
deleteSubdoc(key, parent, subdoc) {
|
|
233
|
+
return this.removeSubdoc(key, parent, subdoc);
|
|
116
234
|
}
|
|
117
235
|
|
|
118
236
|
};
|