@goatlab/fluent-formio 0.2.4 → 0.6.3
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/README.md +31 -359
- package/dist/index.js +5 -0
- package/package.json +127 -59
- package/.DS_Store +0 -0
- package/.babelrc +0 -9
- package/.eslintignore +0 -2
- package/.eslintrc.js +0 -61
- package/LICENSE +0 -21
- package/Makefile +0 -2
- package/lib/@goatlab/fluent-formio.min.js +0 -2
- package/lib/@goatlab/fluent-formio.min.js.map +0 -1
- package/src/Errors/AuthenticationError.js +0 -6
- package/src/FluentConnector.js +0 -397
- package/src/FormioConnector.spec.js +0 -299
- package/src/Utilities.js +0 -280
- package/src/Wrapers/Connection.js +0 -69
- package/src/Wrapers/Event.js +0 -42
- package/src/index.js +0 -3
- package/test/config/setup.js +0 -72
- package/test/config/webpack.config.test.js +0 -19
- package/webpack.config.js +0 -88
package/src/FluentConnector.js
DELETED
|
@@ -1,397 +0,0 @@
|
|
|
1
|
-
import to from "await-to-js";
|
|
2
|
-
import moment from "moment";
|
|
3
|
-
import jwtDecode from "jwt-decode";
|
|
4
|
-
import Utilities from "./Utilities";
|
|
5
|
-
import axios from "axios";
|
|
6
|
-
import { Interface } from "@goatlab/goat-fluent";
|
|
7
|
-
import Connection from "./Wrapers/Connection";
|
|
8
|
-
import AuthenticationError from "./Errors/AuthenticationError";
|
|
9
|
-
|
|
10
|
-
export default Interface.compose({
|
|
11
|
-
methods: {
|
|
12
|
-
getToken() {
|
|
13
|
-
if (typeof localStorage === "undefined") return;
|
|
14
|
-
const token = localStorage.getItem("formioToken");
|
|
15
|
-
if (!token || this.getTokenType(token) === "x-token") return token;
|
|
16
|
-
|
|
17
|
-
const decodedToken = jwtDecode(token);
|
|
18
|
-
const expDate = moment.unix(decodedToken.exp);
|
|
19
|
-
if (moment().isSameOrAfter(expDate))
|
|
20
|
-
throw new AuthenticationError("Token has expired.");
|
|
21
|
-
|
|
22
|
-
return token;
|
|
23
|
-
},
|
|
24
|
-
baseUrl() {
|
|
25
|
-
const { baseUrl, name } = this.connector;
|
|
26
|
-
|
|
27
|
-
if (!baseUrl) {
|
|
28
|
-
throw new Error(
|
|
29
|
-
`You did not provide a baseUrl for the "${name}" connector`
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
return baseUrl.replace(/\/+$/, "");
|
|
33
|
-
},
|
|
34
|
-
async get() {
|
|
35
|
-
if (this.ownerId) {
|
|
36
|
-
this.andWhere("owner", "=", this.ownerId);
|
|
37
|
-
}
|
|
38
|
-
let error;
|
|
39
|
-
let result;
|
|
40
|
-
|
|
41
|
-
[error, result] = await to(this.httpGET());
|
|
42
|
-
|
|
43
|
-
if (error) {
|
|
44
|
-
console.log(error);
|
|
45
|
-
throw new Error("Error while getting submissions");
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
result = this.jsApplySelect(result && result.data);
|
|
49
|
-
result = this.jsApplyOrderBy(result);
|
|
50
|
-
|
|
51
|
-
return result;
|
|
52
|
-
},
|
|
53
|
-
async all() {
|
|
54
|
-
return this.get();
|
|
55
|
-
},
|
|
56
|
-
async numberOfRows() {
|
|
57
|
-
let url = this.getUrl();
|
|
58
|
-
let headers = this.getHeaders();
|
|
59
|
-
let filters = this.getFilters();
|
|
60
|
-
let limit = "?limit=1";
|
|
61
|
-
let skip = this.getSkip();
|
|
62
|
-
let order = this.getOrder();
|
|
63
|
-
let select = this.getSelect();
|
|
64
|
-
let spacer = "";
|
|
65
|
-
|
|
66
|
-
url = url + spacer + limit;
|
|
67
|
-
url = filters ? url + this.getSpacer(url) + filters : url;
|
|
68
|
-
url = skip ? url + this.getSpacer(url) + skip : url;
|
|
69
|
-
url = order ? url + this.getSpacer(url) + order : url;
|
|
70
|
-
url = select ? url + this.getSpacer(url) + select : url;
|
|
71
|
-
|
|
72
|
-
const isOnline = await Connection.isOnline();
|
|
73
|
-
|
|
74
|
-
if (!isOnline) {
|
|
75
|
-
throw new Error(`Cannot make get request to ${url}.You are not online`);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const response = await axios.get(url, { headers });
|
|
79
|
-
|
|
80
|
-
return response.headers["content-range"].split("/")[1];
|
|
81
|
-
},
|
|
82
|
-
async paginate(paginator) {
|
|
83
|
-
const numberOfRows = await this.numberOfRows();
|
|
84
|
-
|
|
85
|
-
this.offset((paginator.page - 1) * paginator.rowsPerPage).take(
|
|
86
|
-
paginator.rowsPerPage
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
const results = {};
|
|
90
|
-
results.data = await this.get();
|
|
91
|
-
results.paginator = {
|
|
92
|
-
page: paginator.page,
|
|
93
|
-
rowsPerPage: paginator.rowsPerPage,
|
|
94
|
-
rowsNumber: numberOfRows
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
console.log("paginator", results);
|
|
98
|
-
|
|
99
|
-
return results;
|
|
100
|
-
},
|
|
101
|
-
async insert(data, options) {
|
|
102
|
-
if (Array.isArray(data)) {
|
|
103
|
-
return this.ArrayInsert(data, options);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
let [error, result] = await to(this.httpPOST(data));
|
|
107
|
-
|
|
108
|
-
if (error) {
|
|
109
|
-
// console.log(error);
|
|
110
|
-
throw new Error("Cannot insert data");
|
|
111
|
-
}
|
|
112
|
-
return result.data;
|
|
113
|
-
},
|
|
114
|
-
async update(data) {
|
|
115
|
-
if (!data._id) {
|
|
116
|
-
throw new Error(
|
|
117
|
-
"Formio connector error. Cannot update a Model without _id key"
|
|
118
|
-
);
|
|
119
|
-
}
|
|
120
|
-
if (data._id.includes("_local")) {
|
|
121
|
-
throw new Error(
|
|
122
|
-
"Formio connector error. Cannot update a local document"
|
|
123
|
-
);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
let [error, result] = await to(this.httpPUT(data));
|
|
127
|
-
|
|
128
|
-
if (error) {
|
|
129
|
-
console.log(error);
|
|
130
|
-
throw new Error("Cannot insert data");
|
|
131
|
-
}
|
|
132
|
-
return result.data;
|
|
133
|
-
},
|
|
134
|
-
async clear({ sure } = {}) {
|
|
135
|
-
if (!sure || sure !== true) {
|
|
136
|
-
throw new Error(
|
|
137
|
-
'Clear() method will delete everything!, you must set the "sure" parameter "clear({sure:true})" to continue'
|
|
138
|
-
);
|
|
139
|
-
}
|
|
140
|
-
let promises = [];
|
|
141
|
-
|
|
142
|
-
let [error, data] = await to(this.select("_id").pluck("_id"));
|
|
143
|
-
|
|
144
|
-
if (error) {
|
|
145
|
-
console.log(error);
|
|
146
|
-
throw new Error("Cannot get remote Model");
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
data.forEach(_id => {
|
|
150
|
-
promises.push(this.httpDelete(_id));
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
return axios.all(promises);
|
|
154
|
-
},
|
|
155
|
-
async remove(_id) {
|
|
156
|
-
let [error, removed] = await to(this.httpDelete(_id));
|
|
157
|
-
|
|
158
|
-
if (error) {
|
|
159
|
-
console.log(error);
|
|
160
|
-
throw new Error(`FormioConnector: Could not delete ${_id}`);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
return removed;
|
|
164
|
-
},
|
|
165
|
-
async find(_id) {
|
|
166
|
-
if (typeof _id !== "string") {
|
|
167
|
-
throw new Error(
|
|
168
|
-
'Formio connector find() method only accepts strings "' +
|
|
169
|
-
typeof _id +
|
|
170
|
-
'" given "' +
|
|
171
|
-
_id +
|
|
172
|
-
'"'
|
|
173
|
-
);
|
|
174
|
-
}
|
|
175
|
-
let [error, data] = await to(this.where("_id", "=", _id).first());
|
|
176
|
-
|
|
177
|
-
if (error) {
|
|
178
|
-
console.log(error);
|
|
179
|
-
throw new Error("Find() could not get remote data");
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
return data;
|
|
183
|
-
},
|
|
184
|
-
getUrl() {
|
|
185
|
-
const baseUrl = this && this.baseUrl() ? this.baseUrl() : undefined;
|
|
186
|
-
let path = Utilities.get(() => this.remoteConnection.path, undefined);
|
|
187
|
-
const id = Utilities.get(() => this.remoteConnection.id, undefined);
|
|
188
|
-
const pullForm = Utilities.get(
|
|
189
|
-
() => this.remoteConnection.pullForm,
|
|
190
|
-
undefined
|
|
191
|
-
);
|
|
192
|
-
|
|
193
|
-
if (!pullForm && path) {
|
|
194
|
-
path = !id ? `${path}/submission` : `${path}/submission/${id}`;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
if (!baseUrl) {
|
|
198
|
-
throw new Error("Cannot get remote model. baseUrl not defined");
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
if (path) {
|
|
202
|
-
return `${baseUrl}/${path}`;
|
|
203
|
-
}
|
|
204
|
-
return baseUrl;
|
|
205
|
-
},
|
|
206
|
-
getHeaders() {
|
|
207
|
-
let headers = {};
|
|
208
|
-
let token = {};
|
|
209
|
-
if (typeof localStorage !== "undefined") {
|
|
210
|
-
token = this.getToken();
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
if (this.remoteConnection.token || this.remoteConnection.token === "") {
|
|
214
|
-
token = this.remoteConnection.token;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
if (!token) {
|
|
218
|
-
return headers;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
let type = this.getTokenType(token);
|
|
222
|
-
headers[type] = token;
|
|
223
|
-
return headers;
|
|
224
|
-
},
|
|
225
|
-
getSpacer(url) {
|
|
226
|
-
return url.substr(url.length - 1) === "&" ? "" : "&";
|
|
227
|
-
},
|
|
228
|
-
async httpGET() {
|
|
229
|
-
let url = this.getUrl();
|
|
230
|
-
let headers = this.getHeaders();
|
|
231
|
-
let filters = this.getFilters();
|
|
232
|
-
let limit = this.getLimit();
|
|
233
|
-
let skip = this.getSkip();
|
|
234
|
-
let order = this.getOrder();
|
|
235
|
-
let select = this.getSelect();
|
|
236
|
-
let spacer = "";
|
|
237
|
-
|
|
238
|
-
// Always limit the amount of requests
|
|
239
|
-
url = url + spacer + limit;
|
|
240
|
-
|
|
241
|
-
url = filters ? url + this.getSpacer(url) + filters : url;
|
|
242
|
-
|
|
243
|
-
url = skip ? url + this.getSpacer(url) + skip : url;
|
|
244
|
-
|
|
245
|
-
url = order ? url + this.getSpacer(url) + order : url;
|
|
246
|
-
|
|
247
|
-
url = select ? url + this.getSpacer(url) + select : url;
|
|
248
|
-
|
|
249
|
-
const isOnline = await Connection.isOnline();
|
|
250
|
-
|
|
251
|
-
if (!isOnline) {
|
|
252
|
-
throw new Error(`Cannot make get request to ${url}.You are not online`);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
return axios.get(url, { headers });
|
|
256
|
-
},
|
|
257
|
-
async httpPOST(data) {
|
|
258
|
-
let url = this.getUrl();
|
|
259
|
-
let headers = this.getHeaders();
|
|
260
|
-
const isOnline = await Connection.isOnline();
|
|
261
|
-
|
|
262
|
-
if (!isOnline) {
|
|
263
|
-
throw new Error(
|
|
264
|
-
`Cannot make request post to ${url}.You are not online`
|
|
265
|
-
);
|
|
266
|
-
}
|
|
267
|
-
return axios.post(url, data, { headers });
|
|
268
|
-
},
|
|
269
|
-
async httpPUT(data) {
|
|
270
|
-
const isOnline = await Connection.isOnline();
|
|
271
|
-
let url = `${this.getUrl()}/${data._id}`;
|
|
272
|
-
let headers = this.getHeaders();
|
|
273
|
-
|
|
274
|
-
if (!isOnline) {
|
|
275
|
-
throw new Error(
|
|
276
|
-
`Cannot make request post to ${url}.You are not online`
|
|
277
|
-
);
|
|
278
|
-
}
|
|
279
|
-
return axios.put(url, data, { headers });
|
|
280
|
-
},
|
|
281
|
-
httpDelete(_id) {
|
|
282
|
-
let headers = this.getHeaders();
|
|
283
|
-
let url = `${this.getUrl()}/${_id}`;
|
|
284
|
-
|
|
285
|
-
return axios.delete(url, { headers });
|
|
286
|
-
},
|
|
287
|
-
getTokenType(token) {
|
|
288
|
-
if (token.length > 32) {
|
|
289
|
-
return "x-jwt-token";
|
|
290
|
-
}
|
|
291
|
-
return "x-token";
|
|
292
|
-
},
|
|
293
|
-
getFilters() {
|
|
294
|
-
let filter = this.whereArray;
|
|
295
|
-
|
|
296
|
-
if (!filter || filter.length === 0) {
|
|
297
|
-
return undefined;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
let filterQuery = "";
|
|
301
|
-
|
|
302
|
-
filter.forEach(condition => {
|
|
303
|
-
let valueString = "";
|
|
304
|
-
let element = condition[0];
|
|
305
|
-
let operator = condition[1];
|
|
306
|
-
let value = condition[2];
|
|
307
|
-
|
|
308
|
-
switch (operator) {
|
|
309
|
-
case "=":
|
|
310
|
-
filterQuery = filterQuery + element + "=" + value + "&";
|
|
311
|
-
break;
|
|
312
|
-
case "!=":
|
|
313
|
-
filterQuery = filterQuery + element + "__ne=" + value + "&";
|
|
314
|
-
break;
|
|
315
|
-
case ">":
|
|
316
|
-
filterQuery = filterQuery + element + "__gt=" + value + "&";
|
|
317
|
-
break;
|
|
318
|
-
case ">=":
|
|
319
|
-
filterQuery = filterQuery + element + "__gte=" + value + "&";
|
|
320
|
-
break;
|
|
321
|
-
case "<":
|
|
322
|
-
filterQuery = filterQuery + element + "__lt=" + value + "&";
|
|
323
|
-
break;
|
|
324
|
-
case "<=":
|
|
325
|
-
filterQuery = filterQuery + element + "__lte=" + value + "&";
|
|
326
|
-
break;
|
|
327
|
-
case "in":
|
|
328
|
-
valueString = "";
|
|
329
|
-
value.forEach((val, index, array) => {
|
|
330
|
-
valueString =
|
|
331
|
-
index === array.length - 1
|
|
332
|
-
? valueString + val
|
|
333
|
-
: valueString + val + ",";
|
|
334
|
-
});
|
|
335
|
-
filterQuery = filterQuery + element + "__in=" + valueString + "&";
|
|
336
|
-
break;
|
|
337
|
-
case "nin":
|
|
338
|
-
valueString = "";
|
|
339
|
-
value.forEach((val, index, array) => {
|
|
340
|
-
valueString =
|
|
341
|
-
index === array.length - 1
|
|
342
|
-
? valueString + val
|
|
343
|
-
: valueString + val + ",";
|
|
344
|
-
});
|
|
345
|
-
filterQuery = filterQuery + element + "__nin=" + valueString + "&";
|
|
346
|
-
break;
|
|
347
|
-
case "exists":
|
|
348
|
-
filterQuery = filterQuery + element + "__exists=" + true + "&";
|
|
349
|
-
break;
|
|
350
|
-
case "!exists":
|
|
351
|
-
filterQuery = filterQuery + element + "__exists=" + false + "&";
|
|
352
|
-
break;
|
|
353
|
-
case "regex":
|
|
354
|
-
filterQuery = filterQuery + element + "__regex=" + value + "&";
|
|
355
|
-
break;
|
|
356
|
-
}
|
|
357
|
-
});
|
|
358
|
-
return filterQuery.substring(0, filterQuery.length - 1);
|
|
359
|
-
},
|
|
360
|
-
getLimit() {
|
|
361
|
-
let limit = "?limit=";
|
|
362
|
-
|
|
363
|
-
if (!this.limitNumber || this.limitNumber === 0) {
|
|
364
|
-
this.limitNumber = 50;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
return `${limit}${this.limitNumber}`;
|
|
368
|
-
},
|
|
369
|
-
getSkip() {
|
|
370
|
-
let skip = "skip=";
|
|
371
|
-
|
|
372
|
-
if (!this.offsetNumber) {
|
|
373
|
-
this.offsetNumber = 0;
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
return skip + this.offsetNumber;
|
|
377
|
-
},
|
|
378
|
-
getOrder() {
|
|
379
|
-
let order = "sort=";
|
|
380
|
-
const or = this.orderByArray[1] === "DESC" ? "-" : "";
|
|
381
|
-
return order + or + this.orderByArray[0];
|
|
382
|
-
},
|
|
383
|
-
getSelect() {
|
|
384
|
-
let select = this.selectArray;
|
|
385
|
-
|
|
386
|
-
select = select.map(e => {
|
|
387
|
-
return e.split(" as ")[0];
|
|
388
|
-
});
|
|
389
|
-
|
|
390
|
-
if (!select) {
|
|
391
|
-
return;
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
return "select=" + select.join(",");
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
});
|
|
@@ -1,299 +0,0 @@
|
|
|
1
|
-
/* global describe, it, before */
|
|
2
|
-
import "babel-polyfill";
|
|
3
|
-
import chai from "chai";
|
|
4
|
-
import to from "await-to-js";
|
|
5
|
-
import Model from "Fluent/Model";
|
|
6
|
-
import Fluent from "Fluent/Fluent";
|
|
7
|
-
import Collection from "Fluent/Collection";
|
|
8
|
-
import DB from "models/DB";
|
|
9
|
-
|
|
10
|
-
chai.expect();
|
|
11
|
-
const expect = chai.expect;
|
|
12
|
-
let testModel;
|
|
13
|
-
|
|
14
|
-
describe("Given a FLUENT Remote Instance", () => {
|
|
15
|
-
before(() => {
|
|
16
|
-
testModel = Fluent.extend(Model, {
|
|
17
|
-
properties: {
|
|
18
|
-
name: "myTestModel",
|
|
19
|
-
remoteConnection: {
|
|
20
|
-
baseUrl: "https://vnikkswzjatywzi.form.io/",
|
|
21
|
-
path: "mytestmodel",
|
|
22
|
-
token: undefined
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}).compose(Fluent.privatize)();
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it("name should be Private", () => {
|
|
29
|
-
expect(testModel.name).to.be.equal(undefined);
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it("name should be visible using a getter and composable overwriting properties", () => {
|
|
33
|
-
expect(testModel.getModelName()).to.be.equal("myTestModel");
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it("Should insert Data", async () => {
|
|
37
|
-
let inserted = await testModel.remote().insert({
|
|
38
|
-
name: "Ignacio",
|
|
39
|
-
age: 29
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
let inserted2 = await testModel.remote().insert({
|
|
43
|
-
name: "Andres",
|
|
44
|
-
age: 15
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
expect(inserted._id).to.be.a("string");
|
|
48
|
-
expect(inserted2._id).to.be.a("string");
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it("Should get remote data", async () => {
|
|
52
|
-
let [error, data] = await to(testModel.remote().all());
|
|
53
|
-
|
|
54
|
-
if (error) {
|
|
55
|
-
console.log(error);
|
|
56
|
-
throw new Error("Cannot get remote Model");
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
expect(data[0].data.name).to.be.equal("Andres");
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it("DB should get data for any local Model", async () => {
|
|
63
|
-
let [error, data] = await to(
|
|
64
|
-
DB.table({
|
|
65
|
-
remoteConnection: {
|
|
66
|
-
baseUrl: "https://vnikkswzjatywzi.form.io/",
|
|
67
|
-
path: "mytestmodel",
|
|
68
|
-
token: undefined
|
|
69
|
-
}
|
|
70
|
-
})
|
|
71
|
-
.remote()
|
|
72
|
-
.all()
|
|
73
|
-
);
|
|
74
|
-
|
|
75
|
-
if (error) {
|
|
76
|
-
console.log(error);
|
|
77
|
-
throw new Error("Cannot get remote Model");
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
expect(data[0].data.name).to.be.equal("Andres");
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
it("select() should filter and name specific columns", async () => {
|
|
84
|
-
let [error, data] = await to(
|
|
85
|
-
testModel
|
|
86
|
-
.remote()
|
|
87
|
-
.select("data.name as Name")
|
|
88
|
-
.get()
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
if (error) {
|
|
92
|
-
console.log(error);
|
|
93
|
-
throw new Error("Cannot get remote Model");
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
expect(data[0].Name).to.be.equal("Andres");
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
it("pluck() should return a single array", async () => {
|
|
100
|
-
let [error, data] = await to(testModel.remote().pluck("data.name"));
|
|
101
|
-
|
|
102
|
-
if (error) {
|
|
103
|
-
console.log(error);
|
|
104
|
-
throw new Error("Cannot get remote Model");
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
expect(data[0]).to.be.equal("Andres");
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
it("orderBy() should order results desc", async () => {
|
|
111
|
-
let [error, data] = await to(
|
|
112
|
-
testModel
|
|
113
|
-
.remote()
|
|
114
|
-
.select("data.name as Name")
|
|
115
|
-
.orderBy("Name", "desc")
|
|
116
|
-
.get()
|
|
117
|
-
);
|
|
118
|
-
|
|
119
|
-
if (error) {
|
|
120
|
-
console.log(error);
|
|
121
|
-
throw new Error("Cannot get remote Model");
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
expect(data[0].Name).to.be.equal("Ignacio");
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
it("orderBy() should order results asc", async () => {
|
|
128
|
-
let [error, data] = await to(
|
|
129
|
-
testModel
|
|
130
|
-
.remote()
|
|
131
|
-
.select("data.name as Name")
|
|
132
|
-
.orderBy("Name", "asc")
|
|
133
|
-
.get()
|
|
134
|
-
);
|
|
135
|
-
|
|
136
|
-
if (error) {
|
|
137
|
-
console.log(error);
|
|
138
|
-
throw new Error("Cannot get remote Model");
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
expect(data[0].Name).to.be.equal("Andres");
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
it("orderBy() should order by Dates with Select()", async () => {
|
|
145
|
-
let [error, data] = await to(
|
|
146
|
-
testModel
|
|
147
|
-
.remote()
|
|
148
|
-
.select("data.name as Name", "created as created")
|
|
149
|
-
.orderBy("created", "asc", "date")
|
|
150
|
-
.get()
|
|
151
|
-
);
|
|
152
|
-
|
|
153
|
-
if (error) {
|
|
154
|
-
console.log(error);
|
|
155
|
-
throw new Error("Cannot get remote Model");
|
|
156
|
-
}
|
|
157
|
-
expect(data[0].Name).to.be.equal("Ignacio");
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
it("orderBy() should order by Dates without Select()", async () => {
|
|
161
|
-
let [error, data] = await to(
|
|
162
|
-
testModel
|
|
163
|
-
.remote()
|
|
164
|
-
.orderBy("created", "asc")
|
|
165
|
-
.get()
|
|
166
|
-
);
|
|
167
|
-
|
|
168
|
-
if (error) {
|
|
169
|
-
console.log(error);
|
|
170
|
-
throw new Error("Cannot get remote Model");
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
expect(data[0].data.name).to.be.equal("Ignacio");
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
it("limit() should limit the amount of results", async () => {
|
|
177
|
-
let [error, data] = await to(
|
|
178
|
-
testModel
|
|
179
|
-
.remote()
|
|
180
|
-
.select("data.name as Name", "created as created")
|
|
181
|
-
.orderBy("created", "asc", "date")
|
|
182
|
-
.limit(1)
|
|
183
|
-
.get()
|
|
184
|
-
);
|
|
185
|
-
|
|
186
|
-
if (error) {
|
|
187
|
-
console.log(error);
|
|
188
|
-
throw new Error("Cannot get remote Model");
|
|
189
|
-
}
|
|
190
|
-
expect(data.length).to.be.equal(1);
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
it("offset() should start at the given position", async () => {
|
|
194
|
-
let [error, data] = await to(
|
|
195
|
-
testModel
|
|
196
|
-
.remote()
|
|
197
|
-
.select("data.name as Name", "created as created")
|
|
198
|
-
.orderBy("created", "desc", "date")
|
|
199
|
-
.limit(1)
|
|
200
|
-
.offset(1)
|
|
201
|
-
.get()
|
|
202
|
-
);
|
|
203
|
-
|
|
204
|
-
if (error) {
|
|
205
|
-
console.log(error);
|
|
206
|
-
throw new Error("Cannot get remote Model");
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
expect(data[0].Name).to.be.equal("Ignacio");
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
it("where() should filter the data", async () => {
|
|
213
|
-
let [error, data] = await to(
|
|
214
|
-
testModel
|
|
215
|
-
.remote()
|
|
216
|
-
.where("data.name", "=", "Andres")
|
|
217
|
-
.select("data.name as Name", "created as created")
|
|
218
|
-
.get()
|
|
219
|
-
);
|
|
220
|
-
|
|
221
|
-
if (error) {
|
|
222
|
-
console.log(error);
|
|
223
|
-
throw new Error("Cannot get remote Model");
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
expect(data[0].Name).to.be.equal("Andres");
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
it("first() should take the first result from data", async () => {
|
|
230
|
-
let [error, data] = await to(
|
|
231
|
-
testModel
|
|
232
|
-
.remote()
|
|
233
|
-
.where("data.name", "=", "Ignacio")
|
|
234
|
-
.select("data.name as Name", "created as created")
|
|
235
|
-
.first()
|
|
236
|
-
);
|
|
237
|
-
|
|
238
|
-
if (error) {
|
|
239
|
-
console.log(error);
|
|
240
|
-
throw new Error("Cannot get remote Model");
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
expect(data.Name).to.be.equal("Ignacio");
|
|
244
|
-
});
|
|
245
|
-
|
|
246
|
-
it("collect() should return the data as collection", async () => {
|
|
247
|
-
let [error, data] = await to(testModel.remote().collect());
|
|
248
|
-
|
|
249
|
-
if (error) {
|
|
250
|
-
console.log(error);
|
|
251
|
-
throw new Error("Cannot get remote Model");
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
describe("Collection methods:", () => {
|
|
255
|
-
it("avg() should calculate avg on an obj attribute", () => {
|
|
256
|
-
let avg = data.avg("data.age");
|
|
257
|
-
|
|
258
|
-
expect(avg).to.be.equal(22);
|
|
259
|
-
});
|
|
260
|
-
|
|
261
|
-
it("chunks() and collapse() an array", () => {
|
|
262
|
-
var chunk = data.chunks(3).get();
|
|
263
|
-
|
|
264
|
-
expect(chunk.length).to.be.equal(1);
|
|
265
|
-
|
|
266
|
-
let collapsed = Collection(chunk)
|
|
267
|
-
.collapse()
|
|
268
|
-
.get();
|
|
269
|
-
|
|
270
|
-
expect(collapsed.length).to.be.equal(2);
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
it("concat() should merge two arrays", () => {});
|
|
274
|
-
});
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
it("clear() should remove all records from the Model", async () => {
|
|
278
|
-
let [error, data] = await to(testModel.remote().clear({ sure: true }));
|
|
279
|
-
|
|
280
|
-
if (error) {
|
|
281
|
-
console.log(error);
|
|
282
|
-
throw new Error("Cannot get remote Model");
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
[error, data] = await to(
|
|
286
|
-
testModel
|
|
287
|
-
.remote()
|
|
288
|
-
.select("_id")
|
|
289
|
-
.get()
|
|
290
|
-
);
|
|
291
|
-
|
|
292
|
-
if (error) {
|
|
293
|
-
console.log(error);
|
|
294
|
-
throw new Error("Cannot get remote Model");
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
expect(data.length).to.be.equal(0);
|
|
298
|
-
});
|
|
299
|
-
});
|