@orion-js/mongodb 3.11.8 → 4.0.0-alpha.2

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.
Files changed (172) hide show
  1. package/README.md +15 -0
  2. package/dist/index.cjs +81969 -0
  3. package/dist/index.d.ts +582 -0
  4. package/dist/index.js +81943 -0
  5. package/package.json +29 -27
  6. package/LICENSE +0 -21
  7. package/lib/connect/connections.d.ts +0 -13
  8. package/lib/connect/connections.js +0 -4
  9. package/lib/connect/getDBName.d.ts +0 -1
  10. package/lib/connect/getDBName.js +0 -23
  11. package/lib/connect/getMongoConnection.d.ts +0 -8
  12. package/lib/connect/getMongoConnection.js +0 -47
  13. package/lib/connect/getMongoConnection.test.d.ts +0 -1
  14. package/lib/connect/getMongoConnection.test.js +0 -43
  15. package/lib/connect/getMongoURLFromEnv.d.ts +0 -1
  16. package/lib/connect/getMongoURLFromEnv.js +0 -21
  17. package/lib/connect/index.d.ts +0 -4
  18. package/lib/connect/index.js +0 -8
  19. package/lib/createCollection/createIndexPromisesTest.d.ts +0 -1
  20. package/lib/createCollection/createIndexPromisesTest.js +0 -12
  21. package/lib/createCollection/createIndexes.d.ts +0 -3
  22. package/lib/createCollection/createIndexes.js +0 -67
  23. package/lib/createCollection/createIndexes.test.d.ts +0 -1
  24. package/lib/createCollection/createIndexes.test.js +0 -91
  25. package/lib/createCollection/createIndexesPromise.test.d.ts +0 -1
  26. package/lib/createCollection/createIndexesPromise.test.js +0 -32
  27. package/lib/createCollection/generateId.d.ts +0 -3
  28. package/lib/createCollection/generateId.js +0 -25
  29. package/lib/createCollection/generateId.test.d.ts +0 -1
  30. package/lib/createCollection/generateId.test.js +0 -108
  31. package/lib/createCollection/getMethods/aggregate.test.d.ts +0 -1
  32. package/lib/createCollection/getMethods/aggregate.test.js +0 -20
  33. package/lib/createCollection/getMethods/cleanModifier.d.ts +0 -4
  34. package/lib/createCollection/getMethods/cleanModifier.js +0 -75
  35. package/lib/createCollection/getMethods/cleanModifier.test.d.ts +0 -1
  36. package/lib/createCollection/getMethods/cleanModifier.test.js +0 -186
  37. package/lib/createCollection/getMethods/countDocuments.d.ts +0 -2
  38. package/lib/createCollection/getMethods/countDocuments.js +0 -16
  39. package/lib/createCollection/getMethods/countDocuments.test.d.ts +0 -1
  40. package/lib/createCollection/getMethods/countDocuments.test.js +0 -21
  41. package/lib/createCollection/getMethods/dataLoader/dataLoad/getDataLoader.d.ts +0 -9
  42. package/lib/createCollection/getMethods/dataLoader/dataLoad/getDataLoader.js +0 -25
  43. package/lib/createCollection/getMethods/dataLoader/dataLoad/getDataLoader.test.d.ts +0 -1
  44. package/lib/createCollection/getMethods/dataLoader/dataLoad/getDataLoader.test.js +0 -37
  45. package/lib/createCollection/getMethods/dataLoader/dataLoad/index.d.ts +0 -9
  46. package/lib/createCollection/getMethods/dataLoader/dataLoad/index.js +0 -21
  47. package/lib/createCollection/getMethods/dataLoader/index.d.ts +0 -5
  48. package/lib/createCollection/getMethods/dataLoader/index.js +0 -14
  49. package/lib/createCollection/getMethods/dataLoader/loadById.d.ts +0 -2
  50. package/lib/createCollection/getMethods/dataLoader/loadById.js +0 -13
  51. package/lib/createCollection/getMethods/dataLoader/loadById.test.d.ts +0 -1
  52. package/lib/createCollection/getMethods/dataLoader/loadById.test.js +0 -23
  53. package/lib/createCollection/getMethods/dataLoader/loadData.d.ts +0 -2
  54. package/lib/createCollection/getMethods/dataLoader/loadData.js +0 -49
  55. package/lib/createCollection/getMethods/dataLoader/loadMany.d.ts +0 -2
  56. package/lib/createCollection/getMethods/dataLoader/loadMany.js +0 -10
  57. package/lib/createCollection/getMethods/dataLoader/loadMany.test.d.ts +0 -1
  58. package/lib/createCollection/getMethods/dataLoader/loadMany.test.js +0 -85
  59. package/lib/createCollection/getMethods/dataLoader/loadOne.d.ts +0 -2
  60. package/lib/createCollection/getMethods/dataLoader/loadOne.js +0 -10
  61. package/lib/createCollection/getMethods/dataLoader/loadOne.test.d.ts +0 -1
  62. package/lib/createCollection/getMethods/dataLoader/loadOne.test.js +0 -76
  63. package/lib/createCollection/getMethods/deleteMany.d.ts +0 -2
  64. package/lib/createCollection/getMethods/deleteMany.js +0 -16
  65. package/lib/createCollection/getMethods/deleteOne.d.ts +0 -2
  66. package/lib/createCollection/getMethods/deleteOne.js +0 -16
  67. package/lib/createCollection/getMethods/estimatedDocumentCount.d.ts +0 -3
  68. package/lib/createCollection/getMethods/estimatedDocumentCount.js +0 -10
  69. package/lib/createCollection/getMethods/estimatedDocumentCount.test.d.ts +0 -1
  70. package/lib/createCollection/getMethods/estimatedDocumentCount.test.js +0 -15
  71. package/lib/createCollection/getMethods/find.d.ts +0 -2
  72. package/lib/createCollection/getMethods/find.js +0 -21
  73. package/lib/createCollection/getMethods/findOne.d.ts +0 -2
  74. package/lib/createCollection/getMethods/findOne.js +0 -18
  75. package/lib/createCollection/getMethods/findOneAndUpdate.d.ts +0 -3
  76. package/lib/createCollection/getMethods/findOneAndUpdate.js +0 -29
  77. package/lib/createCollection/getMethods/findOneAndUpdate.test.d.ts +0 -1
  78. package/lib/createCollection/getMethods/findOneAndUpdate.test.js +0 -15
  79. package/lib/createCollection/getMethods/getSelector.d.ts +0 -3
  80. package/lib/createCollection/getMethods/getSelector.js +0 -21
  81. package/lib/createCollection/getMethods/getSelector.test.d.ts +0 -1
  82. package/lib/createCollection/getMethods/getSelector.test.js +0 -46
  83. package/lib/createCollection/getMethods/index.d.ts +0 -16
  84. package/lib/createCollection/getMethods/index.js +0 -36
  85. package/lib/createCollection/getMethods/insertAndFind.d.ts +0 -3
  86. package/lib/createCollection/getMethods/insertAndFind.js +0 -31
  87. package/lib/createCollection/getMethods/insertAndFind.test.d.ts +0 -1
  88. package/lib/createCollection/getMethods/insertAndFind.test.js +0 -50
  89. package/lib/createCollection/getMethods/insertMany.d.ts +0 -3
  90. package/lib/createCollection/getMethods/insertMany.js +0 -36
  91. package/lib/createCollection/getMethods/insertMany.test.d.ts +0 -1
  92. package/lib/createCollection/getMethods/insertMany.test.js +0 -66
  93. package/lib/createCollection/getMethods/insertOne.d.ts +0 -3
  94. package/lib/createCollection/getMethods/insertOne.js +0 -31
  95. package/lib/createCollection/getMethods/insertOne.test.d.ts +0 -1
  96. package/lib/createCollection/getMethods/insertOne.test.js +0 -104
  97. package/lib/createCollection/getMethods/update.test.d.ts +0 -1
  98. package/lib/createCollection/getMethods/update.test.js +0 -287
  99. package/lib/createCollection/getMethods/updateAndFind.d.ts +0 -3
  100. package/lib/createCollection/getMethods/updateAndFind.js +0 -18
  101. package/lib/createCollection/getMethods/updateAndFind.test.d.ts +0 -1
  102. package/lib/createCollection/getMethods/updateAndFind.test.js +0 -38
  103. package/lib/createCollection/getMethods/updateItem.d.ts +0 -2
  104. package/lib/createCollection/getMethods/updateItem.js +0 -19
  105. package/lib/createCollection/getMethods/updateItem.test.d.ts +0 -1
  106. package/lib/createCollection/getMethods/updateItem.test.js +0 -38
  107. package/lib/createCollection/getMethods/updateMany.d.ts +0 -3
  108. package/lib/createCollection/getMethods/updateMany.js +0 -30
  109. package/lib/createCollection/getMethods/updateOne.d.ts +0 -3
  110. package/lib/createCollection/getMethods/updateOne.js +0 -30
  111. package/lib/createCollection/getMethods/upsert.d.ts +0 -3
  112. package/lib/createCollection/getMethods/upsert.js +0 -31
  113. package/lib/createCollection/getMethods/upsert.test.d.ts +0 -1
  114. package/lib/createCollection/getMethods/upsert.test.js +0 -92
  115. package/lib/createCollection/getMethods/validateModifier/index.d.ts +0 -1
  116. package/lib/createCollection/getMethods/validateModifier/index.js +0 -18
  117. package/lib/createCollection/getMethods/validateModifier/index.test.d.ts +0 -1
  118. package/lib/createCollection/getMethods/validateModifier/index.test.js +0 -74
  119. package/lib/createCollection/getMethods/validateModifier/validateInc.d.ts +0 -7
  120. package/lib/createCollection/getMethods/validateModifier/validateInc.js +0 -20
  121. package/lib/createCollection/getMethods/validateModifier/validateInc.test.d.ts +0 -1
  122. package/lib/createCollection/getMethods/validateModifier/validateInc.test.js +0 -12
  123. package/lib/createCollection/getMethods/validateModifier/validateOperator.d.ts +0 -5
  124. package/lib/createCollection/getMethods/validateModifier/validateOperator.js +0 -34
  125. package/lib/createCollection/getMethods/validateModifier/validatePush.d.ts +0 -8
  126. package/lib/createCollection/getMethods/validateModifier/validatePush.js +0 -27
  127. package/lib/createCollection/getMethods/validateModifier/validatePush.test.d.ts +0 -1
  128. package/lib/createCollection/getMethods/validateModifier/validatePush.test.js +0 -90
  129. package/lib/createCollection/getMethods/validateModifier/validateSet.d.ts +0 -7
  130. package/lib/createCollection/getMethods/validateModifier/validateSet.js +0 -19
  131. package/lib/createCollection/getMethods/validateModifier/validateSet.test.d.ts +0 -1
  132. package/lib/createCollection/getMethods/validateModifier/validateSet.test.js +0 -16
  133. package/lib/createCollection/getMethods/validateModifier/validateUnset.d.ts +0 -7
  134. package/lib/createCollection/getMethods/validateModifier/validateUnset.js +0 -19
  135. package/lib/createCollection/getMethods/validateModifier/validateUnset.test.d.ts +0 -1
  136. package/lib/createCollection/getMethods/validateModifier/validateUnset.test.js +0 -71
  137. package/lib/createCollection/getMethods/validateModifier/validateUpsert.d.ts +0 -1
  138. package/lib/createCollection/getMethods/validateModifier/validateUpsert.js +0 -43
  139. package/lib/createCollection/getMethods/validateModifier/validateUpsert.test.d.ts +0 -1
  140. package/lib/createCollection/getMethods/validateModifier/validateUpsert.test.js +0 -50
  141. package/lib/createCollection/getMethods/wrapErrors.d.ts +0 -1
  142. package/lib/createCollection/getMethods/wrapErrors.js +0 -29
  143. package/lib/createCollection/getSchemaAndModel.d.ts +0 -10
  144. package/lib/createCollection/getSchemaAndModel.js +0 -41
  145. package/lib/createCollection/handleError.d.ts +0 -1
  146. package/lib/createCollection/handleError.js +0 -25
  147. package/lib/createCollection/index.d.ts +0 -4
  148. package/lib/createCollection/index.js +0 -77
  149. package/lib/createCollection/initItem.d.ts +0 -2
  150. package/lib/createCollection/initItem.js +0 -13
  151. package/lib/createCollection/typedModel.test.d.ts +0 -1
  152. package/lib/createCollection/typedModel.test.js +0 -159
  153. package/lib/helpers/fromDot.d.ts +0 -1
  154. package/lib/helpers/fromDot.js +0 -12
  155. package/lib/helpers/fromDot.test.d.ts +0 -1
  156. package/lib/helpers/fromDot.test.js +0 -47
  157. package/lib/helpers/toDot.d.ts +0 -1
  158. package/lib/helpers/toDot.js +0 -11
  159. package/lib/helpers/toDot.test.d.ts +0 -1
  160. package/lib/helpers/toDot.test.js +0 -23
  161. package/lib/index.d.ts +0 -5
  162. package/lib/index.js +0 -35
  163. package/lib/service/index.d.ts +0 -3
  164. package/lib/service/index.js +0 -32
  165. package/lib/service/index.test.d.ts +0 -1
  166. package/lib/service/index.test.js +0 -73
  167. package/lib/tests/setup.d.ts +0 -1
  168. package/lib/tests/setup.js +0 -20
  169. package/lib/types/index.d.ts +0 -178
  170. package/lib/types/index.js +0 -2
  171. package/lib/types/types.test.d.ts +0 -1
  172. package/lib/types/types.test.js +0 -59
@@ -1,30 +0,0 @@
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 getSelector_1 = __importDefault(require("./getSelector"));
7
- const cleanModifier_1 = __importDefault(require("./cleanModifier"));
8
- const validateModifier_1 = __importDefault(require("./validateModifier"));
9
- const wrapErrors_1 = require("./wrapErrors");
10
- exports.default = (collection) => {
11
- const updateOne = async function (selectorArg, modifierArg, options = {}) {
12
- await collection.connectionPromise;
13
- let modifier = modifierArg;
14
- const selector = (0, getSelector_1.default)(arguments);
15
- if (!modifier) {
16
- throw new Error('Modifier is required when making an update');
17
- }
18
- if (collection.schema) {
19
- const schema = collection.getSchema();
20
- modifier = options.clean !== false ? await (0, cleanModifier_1.default)(schema, modifier) : modifier;
21
- if (options.validate !== false)
22
- await (0, validateModifier_1.default)(schema, modifier);
23
- }
24
- const result = await (0, wrapErrors_1.wrapErrors)(() => {
25
- return collection.rawCollection.updateOne(selector, modifier, options.mongoOptions);
26
- });
27
- return result;
28
- };
29
- return updateOne;
30
- };
@@ -1,3 +0,0 @@
1
- import { Collection, ModelClassBase, Upsert } from '../../types';
2
- declare const _default: <DocumentType extends ModelClassBase>(collection: Partial<Collection<DocumentType>>) => Upsert<DocumentType>;
3
- export default _default;
@@ -1,31 +0,0 @@
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 getSelector_1 = __importDefault(require("./getSelector"));
7
- const validateUpsert_1 = __importDefault(require("./validateModifier/validateUpsert"));
8
- const cleanModifier_1 = __importDefault(require("./cleanModifier"));
9
- const wrapErrors_1 = require("./wrapErrors");
10
- exports.default = (collection) => {
11
- const upsert = async function (selectorArg, modifierArg, options = {}) {
12
- await collection.connectionPromise;
13
- let modifier = modifierArg;
14
- let selector = (0, getSelector_1.default)(arguments);
15
- modifier.$setOnInsert = { ...modifier.$setOnInsert, _id: collection.generateId() };
16
- if (collection.schema) {
17
- const schema = collection.getSchema();
18
- if (options.clean !== false) {
19
- selector = (await (0, cleanModifier_1.default)(schema, { $set: selector })).$set;
20
- modifier = await (0, cleanModifier_1.default)(schema, modifier, { isUpsert: true });
21
- }
22
- if (options.validate !== false)
23
- await (0, validateUpsert_1.default)(schema, selector, modifier);
24
- }
25
- const result = await (0, wrapErrors_1.wrapErrors)(() => {
26
- return collection.rawCollection.updateOne(selector, modifier, { ...options.mongoOptions, upsert: true });
27
- });
28
- return result;
29
- };
30
- return upsert;
31
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1,92 +0,0 @@
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
- });
@@ -1 +0,0 @@
1
- export default function validateModifier(schema: any, modifier: any): Promise<void>;
@@ -1,18 +0,0 @@
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;
@@ -1,74 +0,0 @@
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
- });
@@ -1,7 +0,0 @@
1
- /**
2
- * Validates $inc
3
- */
4
- export default function ({ schema, operationDoc }: {
5
- schema: any;
6
- operationDoc: any;
7
- }): Promise<void>;
@@ -1,20 +0,0 @@
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;
@@ -1,12 +0,0 @@
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
- });
@@ -1,5 +0,0 @@
1
- export default function ({ schema, operationDoc, operation }: {
2
- schema: any;
3
- operationDoc: any;
4
- operation: any;
5
- }): Promise<void>;
@@ -1,34 +0,0 @@
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;
@@ -1,8 +0,0 @@
1
- /**
2
- * Validates $push and $addToSet
3
- */
4
- export default function ({ schema, operationDoc, operation }: {
5
- schema: any;
6
- operationDoc: any;
7
- operation: any;
8
- }): Promise<void>;
@@ -1,27 +0,0 @@
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;
@@ -1,90 +0,0 @@
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
- });
@@ -1,7 +0,0 @@
1
- /**
2
- * Validates $set
3
- */
4
- export default function ({ schema, operationDoc }: {
5
- schema: any;
6
- operationDoc: any;
7
- }): Promise<void>;
@@ -1,19 +0,0 @@
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;
@@ -1,16 +0,0 @@
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
- });
@@ -1,7 +0,0 @@
1
- /**
2
- * Validates $unset
3
- */
4
- export default function ({ schema, operationDoc }: {
5
- schema: any;
6
- operationDoc: any;
7
- }): Promise<void>;
@@ -1,19 +0,0 @@
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;
@@ -1,71 +0,0 @@
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
- });
@@ -1 +0,0 @@
1
- export default function (schema: any, selector: any, modifier: any): Promise<void>;