@orion-js/mongodb 3.12.0 → 3.13.1
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/LICENSE +21 -0
- package/lib/connect/connections.d.ts +13 -0
- package/lib/connect/connections.js +4 -0
- package/lib/connect/getDBName.d.ts +1 -0
- package/lib/connect/getDBName.js +23 -0
- package/lib/connect/getMongoConnection.d.ts +8 -0
- package/lib/connect/getMongoConnection.js +47 -0
- package/lib/connect/getMongoConnection.test.d.ts +1 -0
- package/lib/connect/getMongoConnection.test.js +43 -0
- package/lib/connect/getMongoURLFromEnv.d.ts +1 -0
- package/lib/connect/getMongoURLFromEnv.js +21 -0
- package/lib/connect/index.d.ts +4 -0
- package/lib/connect/index.js +8 -0
- package/lib/createCollection/createIndexPromisesTest.d.ts +1 -0
- package/lib/createCollection/createIndexPromisesTest.js +12 -0
- package/lib/createCollection/createIndexes.d.ts +3 -0
- package/lib/createCollection/createIndexes.js +67 -0
- package/lib/createCollection/createIndexes.test.d.ts +1 -0
- package/lib/createCollection/createIndexes.test.js +91 -0
- package/lib/createCollection/createIndexesPromise.test.d.ts +1 -0
- package/lib/createCollection/createIndexesPromise.test.js +32 -0
- package/lib/createCollection/generateId.d.ts +3 -0
- package/lib/createCollection/generateId.js +25 -0
- package/lib/createCollection/generateId.test.d.ts +1 -0
- package/lib/createCollection/generateId.test.js +108 -0
- package/lib/createCollection/getMethods/aggregate.test.d.ts +1 -0
- package/lib/createCollection/getMethods/aggregate.test.js +20 -0
- package/lib/createCollection/getMethods/cleanModifier.d.ts +4 -0
- package/lib/createCollection/getMethods/cleanModifier.js +75 -0
- package/lib/createCollection/getMethods/cleanModifier.test.d.ts +1 -0
- package/lib/createCollection/getMethods/cleanModifier.test.js +186 -0
- package/lib/createCollection/getMethods/countDocuments.d.ts +2 -0
- package/lib/createCollection/getMethods/countDocuments.js +16 -0
- package/lib/createCollection/getMethods/countDocuments.test.d.ts +1 -0
- package/lib/createCollection/getMethods/countDocuments.test.js +21 -0
- package/lib/createCollection/getMethods/dataLoader/dataLoad/getDataLoader.d.ts +9 -0
- package/lib/createCollection/getMethods/dataLoader/dataLoad/getDataLoader.js +25 -0
- package/lib/createCollection/getMethods/dataLoader/dataLoad/getDataLoader.test.d.ts +1 -0
- package/lib/createCollection/getMethods/dataLoader/dataLoad/getDataLoader.test.js +37 -0
- package/lib/createCollection/getMethods/dataLoader/dataLoad/index.d.ts +9 -0
- package/lib/createCollection/getMethods/dataLoader/dataLoad/index.js +21 -0
- package/lib/createCollection/getMethods/dataLoader/index.d.ts +5 -0
- package/lib/createCollection/getMethods/dataLoader/index.js +14 -0
- package/lib/createCollection/getMethods/dataLoader/loadById.d.ts +2 -0
- package/lib/createCollection/getMethods/dataLoader/loadById.js +13 -0
- package/lib/createCollection/getMethods/dataLoader/loadById.test.d.ts +1 -0
- package/lib/createCollection/getMethods/dataLoader/loadById.test.js +23 -0
- package/lib/createCollection/getMethods/dataLoader/loadData.d.ts +2 -0
- package/lib/createCollection/getMethods/dataLoader/loadData.js +49 -0
- package/lib/createCollection/getMethods/dataLoader/loadMany.d.ts +2 -0
- package/lib/createCollection/getMethods/dataLoader/loadMany.js +10 -0
- package/lib/createCollection/getMethods/dataLoader/loadMany.test.d.ts +1 -0
- package/lib/createCollection/getMethods/dataLoader/loadMany.test.js +85 -0
- package/lib/createCollection/getMethods/dataLoader/loadOne.d.ts +2 -0
- package/lib/createCollection/getMethods/dataLoader/loadOne.js +10 -0
- package/lib/createCollection/getMethods/dataLoader/loadOne.test.d.ts +1 -0
- package/lib/createCollection/getMethods/dataLoader/loadOne.test.js +76 -0
- package/lib/createCollection/getMethods/deleteMany.d.ts +2 -0
- package/lib/createCollection/getMethods/deleteMany.js +16 -0
- package/lib/createCollection/getMethods/deleteOne.d.ts +2 -0
- package/lib/createCollection/getMethods/deleteOne.js +16 -0
- package/lib/createCollection/getMethods/estimatedDocumentCount.d.ts +3 -0
- package/lib/createCollection/getMethods/estimatedDocumentCount.js +10 -0
- package/lib/createCollection/getMethods/estimatedDocumentCount.test.d.ts +1 -0
- package/lib/createCollection/getMethods/estimatedDocumentCount.test.js +15 -0
- package/lib/createCollection/getMethods/find.d.ts +2 -0
- package/lib/createCollection/getMethods/find.js +21 -0
- package/lib/createCollection/getMethods/findOne.d.ts +2 -0
- package/lib/createCollection/getMethods/findOne.js +18 -0
- package/lib/createCollection/getMethods/findOneAndUpdate.d.ts +3 -0
- package/lib/createCollection/getMethods/findOneAndUpdate.js +29 -0
- package/lib/createCollection/getMethods/findOneAndUpdate.test.d.ts +1 -0
- package/lib/createCollection/getMethods/findOneAndUpdate.test.js +15 -0
- package/lib/createCollection/getMethods/getSelector.d.ts +3 -0
- package/lib/createCollection/getMethods/getSelector.js +21 -0
- package/lib/createCollection/getMethods/getSelector.test.d.ts +1 -0
- package/lib/createCollection/getMethods/getSelector.test.js +46 -0
- package/lib/createCollection/getMethods/index.d.ts +16 -0
- package/lib/createCollection/getMethods/index.js +36 -0
- package/lib/createCollection/getMethods/insertAndFind.d.ts +3 -0
- package/lib/createCollection/getMethods/insertAndFind.js +31 -0
- package/lib/createCollection/getMethods/insertAndFind.test.d.ts +1 -0
- package/lib/createCollection/getMethods/insertAndFind.test.js +50 -0
- package/lib/createCollection/getMethods/insertMany.d.ts +3 -0
- package/lib/createCollection/getMethods/insertMany.js +36 -0
- package/lib/createCollection/getMethods/insertMany.test.d.ts +1 -0
- package/lib/createCollection/getMethods/insertMany.test.js +66 -0
- package/lib/createCollection/getMethods/insertOne.d.ts +3 -0
- package/lib/createCollection/getMethods/insertOne.js +31 -0
- package/lib/createCollection/getMethods/insertOne.test.d.ts +1 -0
- package/lib/createCollection/getMethods/insertOne.test.js +104 -0
- package/lib/createCollection/getMethods/update.test.d.ts +1 -0
- package/lib/createCollection/getMethods/update.test.js +287 -0
- package/lib/createCollection/getMethods/updateAndFind.d.ts +3 -0
- package/lib/createCollection/getMethods/updateAndFind.js +18 -0
- package/lib/createCollection/getMethods/updateAndFind.test.d.ts +1 -0
- package/lib/createCollection/getMethods/updateAndFind.test.js +38 -0
- package/lib/createCollection/getMethods/updateItem.d.ts +2 -0
- package/lib/createCollection/getMethods/updateItem.js +19 -0
- package/lib/createCollection/getMethods/updateItem.test.d.ts +1 -0
- package/lib/createCollection/getMethods/updateItem.test.js +38 -0
- package/lib/createCollection/getMethods/updateMany.d.ts +3 -0
- package/lib/createCollection/getMethods/updateMany.js +30 -0
- package/lib/createCollection/getMethods/updateOne.d.ts +3 -0
- package/lib/createCollection/getMethods/updateOne.js +30 -0
- package/lib/createCollection/getMethods/upsert.d.ts +3 -0
- package/lib/createCollection/getMethods/upsert.js +31 -0
- package/lib/createCollection/getMethods/upsert.test.d.ts +1 -0
- package/lib/createCollection/getMethods/upsert.test.js +92 -0
- package/lib/createCollection/getMethods/validateModifier/index.d.ts +1 -0
- package/lib/createCollection/getMethods/validateModifier/index.js +18 -0
- package/lib/createCollection/getMethods/validateModifier/index.test.d.ts +1 -0
- package/lib/createCollection/getMethods/validateModifier/index.test.js +74 -0
- package/lib/createCollection/getMethods/validateModifier/validateInc.d.ts +7 -0
- package/lib/createCollection/getMethods/validateModifier/validateInc.js +20 -0
- package/lib/createCollection/getMethods/validateModifier/validateInc.test.d.ts +1 -0
- package/lib/createCollection/getMethods/validateModifier/validateInc.test.js +12 -0
- package/lib/createCollection/getMethods/validateModifier/validateOperator.d.ts +5 -0
- package/lib/createCollection/getMethods/validateModifier/validateOperator.js +34 -0
- package/lib/createCollection/getMethods/validateModifier/validatePush.d.ts +8 -0
- package/lib/createCollection/getMethods/validateModifier/validatePush.js +27 -0
- package/lib/createCollection/getMethods/validateModifier/validatePush.test.d.ts +1 -0
- package/lib/createCollection/getMethods/validateModifier/validatePush.test.js +90 -0
- package/lib/createCollection/getMethods/validateModifier/validateSet.d.ts +7 -0
- package/lib/createCollection/getMethods/validateModifier/validateSet.js +19 -0
- package/lib/createCollection/getMethods/validateModifier/validateSet.test.d.ts +1 -0
- package/lib/createCollection/getMethods/validateModifier/validateSet.test.js +16 -0
- package/lib/createCollection/getMethods/validateModifier/validateUnset.d.ts +7 -0
- package/lib/createCollection/getMethods/validateModifier/validateUnset.js +19 -0
- package/lib/createCollection/getMethods/validateModifier/validateUnset.test.d.ts +1 -0
- package/lib/createCollection/getMethods/validateModifier/validateUnset.test.js +71 -0
- package/lib/createCollection/getMethods/validateModifier/validateUpsert.d.ts +1 -0
- package/lib/createCollection/getMethods/validateModifier/validateUpsert.js +43 -0
- package/lib/createCollection/getMethods/validateModifier/validateUpsert.test.d.ts +1 -0
- package/lib/createCollection/getMethods/validateModifier/validateUpsert.test.js +50 -0
- package/lib/createCollection/getMethods/wrapErrors.d.ts +1 -0
- package/lib/createCollection/getMethods/wrapErrors.js +29 -0
- package/lib/createCollection/getSchemaAndModel.d.ts +10 -0
- package/lib/createCollection/getSchemaAndModel.js +41 -0
- package/lib/createCollection/handleError.d.ts +1 -0
- package/lib/createCollection/handleError.js +25 -0
- package/lib/createCollection/index.d.ts +4 -0
- package/lib/createCollection/index.js +77 -0
- package/lib/createCollection/initItem.d.ts +2 -0
- package/lib/createCollection/initItem.js +13 -0
- package/lib/createCollection/typedModel.test.d.ts +1 -0
- package/lib/createCollection/typedModel.test.js +159 -0
- package/lib/helpers/fromDot.d.ts +1 -0
- package/lib/helpers/fromDot.js +12 -0
- package/lib/helpers/fromDot.test.d.ts +1 -0
- package/lib/helpers/fromDot.test.js +47 -0
- package/lib/helpers/toDot.d.ts +1 -0
- package/lib/helpers/toDot.js +11 -0
- package/lib/helpers/toDot.test.d.ts +1 -0
- package/lib/helpers/toDot.test.js +23 -0
- package/lib/index.d.ts +5 -0
- package/lib/index.js +35 -0
- package/lib/service/index.d.ts +3 -0
- package/lib/service/index.js +32 -0
- package/lib/service/index.test.d.ts +1 -0
- package/lib/service/index.test.js +73 -0
- package/lib/tests/setup.d.ts +1 -0
- package/lib/tests/setup.js +20 -0
- package/lib/types/index.d.ts +178 -0
- package/lib/types/index.js +2 -0
- package/lib/types/types.test.d.ts +1 -0
- package/lib/types/types.test.js +59 -0
- package/package.json +27 -29
- package/README.md +0 -15
- package/dist/index.cjs +0 -81990
- package/dist/index.d.ts +0 -204
- package/dist/index.js +0 -81964
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const helpers_1 = require("@orion-js/helpers");
|
|
7
|
+
const models_1 = require("@orion-js/models");
|
|
8
|
+
const __1 = __importDefault(require(".."));
|
|
9
|
+
it('updates a document if exists', async () => {
|
|
10
|
+
const Tests = (0, __1.default)({ name: (0, helpers_1.generateId)() });
|
|
11
|
+
const docId = await Tests.insertOne({ hello: 'world' });
|
|
12
|
+
const { modifiedCount, upsertedId } = await Tests.upsert({ hello: 'world' }, { $set: { hello: 'country' } });
|
|
13
|
+
expect(modifiedCount).toBe(1);
|
|
14
|
+
expect(upsertedId).toBeNull();
|
|
15
|
+
const final = await Tests.findOne(docId);
|
|
16
|
+
expect(final.hello).toBe('country');
|
|
17
|
+
});
|
|
18
|
+
it('inserts a document if it does not exists', async () => {
|
|
19
|
+
const Tests = (0, __1.default)({ name: (0, helpers_1.generateId)() });
|
|
20
|
+
const { modifiedCount, upsertedId } = await Tests.upsert({ hello: 'world' }, { $set: { hello: 'country' } });
|
|
21
|
+
expect(modifiedCount).toBe(0);
|
|
22
|
+
expect(typeof upsertedId).toBe('string');
|
|
23
|
+
const final = await Tests.findOne({});
|
|
24
|
+
expect(final.hello).toBe('country');
|
|
25
|
+
});
|
|
26
|
+
it('adds default value when creating docs', async () => {
|
|
27
|
+
const now = new Date();
|
|
28
|
+
let calls = 0;
|
|
29
|
+
const schema = {
|
|
30
|
+
firstName: {
|
|
31
|
+
type: String,
|
|
32
|
+
defaultValue: () => 'Nicolás'
|
|
33
|
+
},
|
|
34
|
+
lastName: {
|
|
35
|
+
type: String
|
|
36
|
+
},
|
|
37
|
+
createdAt: {
|
|
38
|
+
type: Date,
|
|
39
|
+
defaultValue: () => {
|
|
40
|
+
calls++;
|
|
41
|
+
return now;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
const model = (0, models_1.createModel)({ name: (0, helpers_1.generateId)(), schema });
|
|
46
|
+
const Tests = (0, __1.default)({
|
|
47
|
+
name: (0, helpers_1.generateId)(),
|
|
48
|
+
model
|
|
49
|
+
});
|
|
50
|
+
const { modifiedCount, upsertedId } = await Tests.upsert({ firstName: 'Bastian' }, { $set: { lastName: 'Ermann' } });
|
|
51
|
+
expect(modifiedCount).toBe(0);
|
|
52
|
+
expect(calls).toBe(1);
|
|
53
|
+
expect(typeof upsertedId).toBe('string');
|
|
54
|
+
const final = await Tests.findOne({});
|
|
55
|
+
expect(final).toEqual({
|
|
56
|
+
_id: upsertedId,
|
|
57
|
+
firstName: 'Nicolás',
|
|
58
|
+
lastName: 'Ermann',
|
|
59
|
+
createdAt: now
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
it('should upsert documents passing cleaning validation', async () => {
|
|
63
|
+
const person = {
|
|
64
|
+
name: { type: String },
|
|
65
|
+
state: { type: String, optional: true }
|
|
66
|
+
};
|
|
67
|
+
const schema = {
|
|
68
|
+
_id: { type: 'ID' },
|
|
69
|
+
name: { type: String },
|
|
70
|
+
label: { type: String },
|
|
71
|
+
wife: { type: person },
|
|
72
|
+
friends: { type: [person] }
|
|
73
|
+
};
|
|
74
|
+
const model = (0, models_1.createModel)({ name: (0, helpers_1.generateId)(), schema });
|
|
75
|
+
const Tests = (0, __1.default)({
|
|
76
|
+
name: (0, helpers_1.generateId)(),
|
|
77
|
+
model
|
|
78
|
+
});
|
|
79
|
+
const { upsertedId } = await Tests.upsert({ name: 'Nicolás', label: 1234 }, {
|
|
80
|
+
$set: { 'wife.state': 'Hungry', 'wife.name': 'Francisca' },
|
|
81
|
+
$push: { friends: { name: 'Joaquín' } }
|
|
82
|
+
});
|
|
83
|
+
await Tests.updateOne(upsertedId, { $set: { 'wife.state': 'Full' } });
|
|
84
|
+
const doc = await Tests.findOne(upsertedId);
|
|
85
|
+
expect(doc).toEqual({
|
|
86
|
+
_id: upsertedId,
|
|
87
|
+
name: 'Nicolás',
|
|
88
|
+
label: '1234',
|
|
89
|
+
wife: { state: 'Full', name: 'Francisca' },
|
|
90
|
+
friends: [{ name: 'Joaquín' }]
|
|
91
|
+
});
|
|
92
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function validateModifier(schema: any, modifier: any): Promise<void>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
// inspired by https://github.com/aldeed/simple-schema-js
|
|
7
|
+
const validateOperator_1 = __importDefault(require("./validateOperator"));
|
|
8
|
+
async function validateModifier(schema, modifier) {
|
|
9
|
+
for (const operation of Object.keys(modifier)) {
|
|
10
|
+
const operationDoc = modifier[operation];
|
|
11
|
+
// If non-operators are mixed in, throw error
|
|
12
|
+
if (operation.slice(0, 1) !== '$') {
|
|
13
|
+
throw new Error(`Expected '${operation}' to be a modifier operator like '$set'`);
|
|
14
|
+
}
|
|
15
|
+
await (0, validateOperator_1.default)({ schema, operationDoc, operation });
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.default = validateModifier;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const index_1 = __importDefault(require("./index"));
|
|
7
|
+
it('should pass validation when not all fields are present', async () => {
|
|
8
|
+
const wife = {
|
|
9
|
+
name: { type: String },
|
|
10
|
+
state: { type: String }
|
|
11
|
+
};
|
|
12
|
+
const schema = {
|
|
13
|
+
wife: { type: wife }
|
|
14
|
+
};
|
|
15
|
+
await (0, index_1.default)(schema, { $set: { 'wife.state': 'Full' } });
|
|
16
|
+
});
|
|
17
|
+
it('should throw an error when a not present field is passed', async () => {
|
|
18
|
+
const wife = {
|
|
19
|
+
name: { type: String },
|
|
20
|
+
state: { type: String }
|
|
21
|
+
};
|
|
22
|
+
const schema = {
|
|
23
|
+
wife: { type: wife }
|
|
24
|
+
};
|
|
25
|
+
expect.assertions(1);
|
|
26
|
+
try {
|
|
27
|
+
await (0, index_1.default)(schema, { $set: { 'mom.name': 'Paula' } });
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
expect(error.code).toBe('validationError');
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
it('validate arrays', async () => {
|
|
34
|
+
const friend = {
|
|
35
|
+
name: { type: String }
|
|
36
|
+
};
|
|
37
|
+
const schema = {
|
|
38
|
+
friends: { type: [friend] }
|
|
39
|
+
};
|
|
40
|
+
await (0, index_1.default)(schema, { $set: { 'friends.0.name': 'Roberto' } });
|
|
41
|
+
await (0, index_1.default)(schema, { $set: { friends: [{ name: 'Roberto' }] } });
|
|
42
|
+
expect.assertions(1);
|
|
43
|
+
try {
|
|
44
|
+
await (0, index_1.default)(schema, { $set: { friends: ['Roberto'] } });
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
expect(error.code).toBe('validationError');
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
it('validate $push operations', async () => {
|
|
51
|
+
const friend = {
|
|
52
|
+
name: { type: String }
|
|
53
|
+
};
|
|
54
|
+
const schema = {
|
|
55
|
+
friends: { type: [friend] }
|
|
56
|
+
};
|
|
57
|
+
await (0, index_1.default)(schema, { $push: { friends: { name: 'Roberto' } } });
|
|
58
|
+
await (0, index_1.default)(schema, {
|
|
59
|
+
$push: { friends: { $each: [{ name: 'Roberto' }, { name: 'Joaquín' }] } }
|
|
60
|
+
});
|
|
61
|
+
expect.assertions(1);
|
|
62
|
+
try {
|
|
63
|
+
await (0, index_1.default)(schema, { $set: { friends: ['Roberto'] } });
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
expect(error.code).toBe('validationError');
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
it('validate $unset operations', async () => {
|
|
70
|
+
const schema = {
|
|
71
|
+
name: { type: String, optional: true }
|
|
72
|
+
};
|
|
73
|
+
await (0, index_1.default)(schema, { $unset: { name: '' } });
|
|
74
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const schema_1 = require("@orion-js/schema");
|
|
4
|
+
/**
|
|
5
|
+
* Validates $inc
|
|
6
|
+
*/
|
|
7
|
+
async function default_1({ schema, operationDoc }) {
|
|
8
|
+
const errors = {};
|
|
9
|
+
for (const key of Object.keys(operationDoc)) {
|
|
10
|
+
const value = operationDoc[key];
|
|
11
|
+
const error = await (0, schema_1.validateKey)(schema, key, value);
|
|
12
|
+
if (error) {
|
|
13
|
+
errors[key] = error;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
if (Object.keys(errors).length) {
|
|
17
|
+
throw new schema_1.ValidationError(errors);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.default = default_1;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const index_1 = __importDefault(require("./index"));
|
|
7
|
+
it('should handle $inc correctly', async () => {
|
|
8
|
+
const schema = {
|
|
9
|
+
services: { type: 'blackbox' }
|
|
10
|
+
};
|
|
11
|
+
await (0, index_1.default)(schema, { $inc: { 'services.phoneVerification.tries': 1 } });
|
|
12
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const validatePush_1 = __importDefault(require("./validatePush"));
|
|
7
|
+
const validateUnset_1 = __importDefault(require("./validateUnset"));
|
|
8
|
+
const validateInc_1 = __importDefault(require("./validateInc"));
|
|
9
|
+
const validateSet_1 = __importDefault(require("./validateSet"));
|
|
10
|
+
const shouldCheck = function (key) {
|
|
11
|
+
if (key === '$pushAll')
|
|
12
|
+
throw new Error('$pushAll is not supported, use $push + $each');
|
|
13
|
+
return ['$pull', '$pullAll', '$pop', '$slice'].indexOf(key) === -1;
|
|
14
|
+
};
|
|
15
|
+
async function default_1({ schema, operationDoc, operation }) {
|
|
16
|
+
if (!shouldCheck(operation))
|
|
17
|
+
return;
|
|
18
|
+
if (operation === '$set') {
|
|
19
|
+
await (0, validateSet_1.default)({ schema, operationDoc });
|
|
20
|
+
}
|
|
21
|
+
else if (operation === '$unset') {
|
|
22
|
+
await (0, validateUnset_1.default)({ schema, operationDoc });
|
|
23
|
+
}
|
|
24
|
+
else if (operation === '$inc') {
|
|
25
|
+
await (0, validateInc_1.default)({ schema, operationDoc });
|
|
26
|
+
}
|
|
27
|
+
else if (operation === '$push' || operation === '$addToSet') {
|
|
28
|
+
await (0, validatePush_1.default)({ schema, operationDoc, operation });
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
throw new Error(operation + ' operation is not supported yet');
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.default = default_1;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const schema_1 = require("@orion-js/schema");
|
|
7
|
+
const fromDot_1 = __importDefault(require("../../../helpers/fromDot"));
|
|
8
|
+
/**
|
|
9
|
+
* Validates $push and $addToSet
|
|
10
|
+
*/
|
|
11
|
+
async function default_1({ schema, operationDoc, operation }) {
|
|
12
|
+
for (const key of Object.keys(operationDoc)) {
|
|
13
|
+
const value = operationDoc[key];
|
|
14
|
+
let toValidate = null;
|
|
15
|
+
if (operation === '$push' || operation === '$addToSet') {
|
|
16
|
+
if (typeof value === 'object' && '$each' in value) {
|
|
17
|
+
toValidate = value.$each;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
toValidate = [value];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const validationObject = (0, fromDot_1.default)({ [key]: toValidate });
|
|
24
|
+
await (0, schema_1.validate)(schema, validationObject, { omitRequired: true });
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.default = default_1;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const validateOperator_1 = __importDefault(require("./validateOperator"));
|
|
7
|
+
it('validate $push operations', async () => {
|
|
8
|
+
const friend = {
|
|
9
|
+
name: { type: String }
|
|
10
|
+
};
|
|
11
|
+
const person = {
|
|
12
|
+
friends: { type: [friend] }
|
|
13
|
+
};
|
|
14
|
+
const schema = {
|
|
15
|
+
person: { type: person }
|
|
16
|
+
};
|
|
17
|
+
const operation = '$push';
|
|
18
|
+
await (0, validateOperator_1.default)({
|
|
19
|
+
schema,
|
|
20
|
+
operationDoc: { 'person.friends': { name: 'Roberto' } },
|
|
21
|
+
operation
|
|
22
|
+
});
|
|
23
|
+
expect.assertions(1);
|
|
24
|
+
try {
|
|
25
|
+
await (0, validateOperator_1.default)({
|
|
26
|
+
schema,
|
|
27
|
+
operationDoc: { friends: 'Roberto' },
|
|
28
|
+
operation
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
expect(error.code).toBe('validationError');
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
it('validate $push operations with deep array', async () => {
|
|
36
|
+
const friend = {
|
|
37
|
+
name: { type: String }
|
|
38
|
+
};
|
|
39
|
+
const person = {
|
|
40
|
+
friends: { type: [friend] }
|
|
41
|
+
};
|
|
42
|
+
const schema = {
|
|
43
|
+
persons: { type: [person] }
|
|
44
|
+
};
|
|
45
|
+
const operation = '$push';
|
|
46
|
+
await (0, validateOperator_1.default)({
|
|
47
|
+
schema,
|
|
48
|
+
operationDoc: { 'persons.1.friends': { name: 'Roberto' } },
|
|
49
|
+
operation
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
it('validate $push with $each operations', async () => {
|
|
53
|
+
const friend = {
|
|
54
|
+
name: { type: String }
|
|
55
|
+
};
|
|
56
|
+
const schema = {
|
|
57
|
+
friends: { type: [friend] }
|
|
58
|
+
};
|
|
59
|
+
const operation = '$push';
|
|
60
|
+
await (0, validateOperator_1.default)({
|
|
61
|
+
schema,
|
|
62
|
+
operationDoc: { friends: { $each: [{ name: 'Roberto' }, { name: 'Joaquín' }] } },
|
|
63
|
+
operation
|
|
64
|
+
});
|
|
65
|
+
expect.assertions(1);
|
|
66
|
+
try {
|
|
67
|
+
await (0, validateOperator_1.default)({
|
|
68
|
+
schema,
|
|
69
|
+
operationDoc: { friends: { $each: [{ name: 'Joaquín' }, 'Roberto'] } },
|
|
70
|
+
operation
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
expect(error.code).toBe('validationError');
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
it('validate $addToSet', async () => {
|
|
78
|
+
const friend = {
|
|
79
|
+
name: { type: String }
|
|
80
|
+
};
|
|
81
|
+
const schema = {
|
|
82
|
+
friends: { type: [friend] }
|
|
83
|
+
};
|
|
84
|
+
const operation = '$addToSet';
|
|
85
|
+
await (0, validateOperator_1.default)({
|
|
86
|
+
schema,
|
|
87
|
+
operationDoc: { friends: { $each: [{ name: 'Roberto' }, { name: 'Joaquín' }] } },
|
|
88
|
+
operation
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const schema_1 = require("@orion-js/schema");
|
|
7
|
+
const mapKeys_1 = __importDefault(require("lodash/mapKeys"));
|
|
8
|
+
const fromDot_1 = __importDefault(require("../../../helpers/fromDot"));
|
|
9
|
+
const toDot_1 = __importDefault(require("../../../helpers/toDot"));
|
|
10
|
+
/**
|
|
11
|
+
* Validates $set
|
|
12
|
+
*/
|
|
13
|
+
async function default_1({ schema, operationDoc }) {
|
|
14
|
+
let cleaned = (0, toDot_1.default)(operationDoc);
|
|
15
|
+
cleaned = (0, mapKeys_1.default)(cleaned, (value, key) => key.replace('$.', '0.'));
|
|
16
|
+
cleaned = (0, fromDot_1.default)(cleaned);
|
|
17
|
+
await (0, schema_1.validate)(schema, cleaned, { omitRequired: true });
|
|
18
|
+
}
|
|
19
|
+
exports.default = default_1;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const index_1 = __importDefault(require("./index"));
|
|
7
|
+
it('should handle $ correctly', async () => {
|
|
8
|
+
const Email = {
|
|
9
|
+
address: { type: String },
|
|
10
|
+
verified: { type: Boolean }
|
|
11
|
+
};
|
|
12
|
+
const schema = {
|
|
13
|
+
emails: { type: [Email] }
|
|
14
|
+
};
|
|
15
|
+
await (0, index_1.default)(schema, { $set: { 'emails.$.verified': true } });
|
|
16
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const schema_1 = require("@orion-js/schema");
|
|
4
|
+
/**
|
|
5
|
+
* Validates $unset
|
|
6
|
+
*/
|
|
7
|
+
async function default_1({ schema, operationDoc }) {
|
|
8
|
+
const errors = {};
|
|
9
|
+
for (const key of Object.keys(operationDoc)) {
|
|
10
|
+
const error = await (0, schema_1.validateKey)(schema, key, null);
|
|
11
|
+
if (error) {
|
|
12
|
+
errors[key] = error;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
if (Object.keys(errors).length) {
|
|
16
|
+
throw new schema_1.ValidationError(errors);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.default = default_1;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const index_1 = __importDefault(require("./index"));
|
|
7
|
+
it('validate $unset operations', async () => {
|
|
8
|
+
const mom = {
|
|
9
|
+
name: { type: String, optional: true }
|
|
10
|
+
};
|
|
11
|
+
const schema = {
|
|
12
|
+
name: { type: String },
|
|
13
|
+
age: { type: String, optional: true },
|
|
14
|
+
mom: { type: mom }
|
|
15
|
+
};
|
|
16
|
+
await (0, index_1.default)(schema, {
|
|
17
|
+
$unset: {
|
|
18
|
+
age: ''
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
await (0, index_1.default)(schema, {
|
|
22
|
+
$unset: {
|
|
23
|
+
'mom.name': ''
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
expect.assertions(2);
|
|
27
|
+
try {
|
|
28
|
+
await (0, index_1.default)(schema, {
|
|
29
|
+
$unset: {
|
|
30
|
+
name: '',
|
|
31
|
+
age: ''
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
expect(error.code).toBe('validationError');
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
await (0, index_1.default)(schema, {
|
|
40
|
+
$unset: {
|
|
41
|
+
mom: ''
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
expect(error.code).toBe('validationError');
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
it('should allow an $unset operation on a children of a required blackbox', async () => {
|
|
50
|
+
const schema = {
|
|
51
|
+
data: {
|
|
52
|
+
type: 'blackbox'
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
await (0, index_1.default)(schema, {
|
|
56
|
+
$unset: {
|
|
57
|
+
'data.items': ''
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
expect.assertions(1);
|
|
61
|
+
try {
|
|
62
|
+
await (0, index_1.default)(schema, {
|
|
63
|
+
$unset: {
|
|
64
|
+
data: ''
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
expect(error.code).toBe('validationError');
|
|
70
|
+
}
|
|
71
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function (schema: any, selector: any, modifier: any): Promise<void>;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const index_1 = __importDefault(require("./index"));
|
|
7
|
+
const schema_1 = require("@orion-js/schema");
|
|
8
|
+
const omit_1 = __importDefault(require("lodash/omit"));
|
|
9
|
+
const fromDot_1 = __importDefault(require("../../../helpers/fromDot"));
|
|
10
|
+
const getPushSimulation = function ($push) {
|
|
11
|
+
if (!$push)
|
|
12
|
+
return {};
|
|
13
|
+
const simulation = {};
|
|
14
|
+
for (const key of Object.keys($push)) {
|
|
15
|
+
const value = $push[key];
|
|
16
|
+
if (typeof value === 'object' && '$each' in value) {
|
|
17
|
+
simulation[key] = value.$each;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
simulation[key] = [value];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return simulation;
|
|
24
|
+
};
|
|
25
|
+
const simulateNewDoc = function (selector, modifier) {
|
|
26
|
+
return (0, fromDot_1.default)({
|
|
27
|
+
...selector,
|
|
28
|
+
...modifier.$set,
|
|
29
|
+
...modifier.$inc,
|
|
30
|
+
...getPushSimulation(modifier.$push),
|
|
31
|
+
...getPushSimulation(modifier.$addToSet),
|
|
32
|
+
...modifier.$setOnInsert
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
const validateNewDoc = async function (schema, selector, modifier) {
|
|
36
|
+
const doc = simulateNewDoc(selector, modifier);
|
|
37
|
+
await (0, schema_1.validate)(schema, doc);
|
|
38
|
+
};
|
|
39
|
+
async function default_1(schema, selector, modifier) {
|
|
40
|
+
await validateNewDoc(schema, selector, modifier);
|
|
41
|
+
await (0, index_1.default)(schema, (0, omit_1.default)(modifier, '$setOnInsert'));
|
|
42
|
+
}
|
|
43
|
+
exports.default = default_1;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const validateUpsert_1 = __importDefault(require("./validateUpsert"));
|
|
7
|
+
it('pass validation when everything is fine', async () => {
|
|
8
|
+
const person = {
|
|
9
|
+
name: { type: String },
|
|
10
|
+
state: { type: String, optional: true }
|
|
11
|
+
};
|
|
12
|
+
const schema = {
|
|
13
|
+
name: { type: String },
|
|
14
|
+
age: { type: Number },
|
|
15
|
+
wife: { type: person },
|
|
16
|
+
friends: { type: [person] }
|
|
17
|
+
};
|
|
18
|
+
const selector = { name: 'Nicolás' };
|
|
19
|
+
const modifier = {
|
|
20
|
+
$inc: { age: 1 },
|
|
21
|
+
$set: { 'wife.state': 'Hungry', 'wife.name': 'Francisca' },
|
|
22
|
+
$push: { friends: { name: 'Joaquín' } }
|
|
23
|
+
};
|
|
24
|
+
await (0, validateUpsert_1.default)(schema, selector, modifier);
|
|
25
|
+
});
|
|
26
|
+
it('get correct validation errors', async () => {
|
|
27
|
+
const person = {
|
|
28
|
+
name: { type: String },
|
|
29
|
+
state: { type: String, optional: true }
|
|
30
|
+
};
|
|
31
|
+
const schema = {
|
|
32
|
+
name: { type: String },
|
|
33
|
+
age: { type: Number },
|
|
34
|
+
wife: { type: person },
|
|
35
|
+
friends: { type: [person] }
|
|
36
|
+
};
|
|
37
|
+
const selector = { name: 'Nicolás' };
|
|
38
|
+
const modifier = {
|
|
39
|
+
$inc: { age: 'not a number' },
|
|
40
|
+
$set: { 'wife.state': 1, 'wife.name': 'Francisca' },
|
|
41
|
+
$push: { friends: 'Joaquín' }
|
|
42
|
+
};
|
|
43
|
+
expect.assertions(1);
|
|
44
|
+
try {
|
|
45
|
+
await (0, validateUpsert_1.default)(schema, selector, modifier);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
expect(error.code).toBe('validationError');
|
|
49
|
+
}
|
|
50
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const wrapErrors: <TFunc extends () => Promise<any>>(operation: TFunc) => Promise<ReturnType<TFunc>>;
|