@orion-js/mongodb 3.2.14 → 3.2.18
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/lib/createCollection/getMethods/findOneAndUpdate.js +1 -1
- package/lib/createCollection/getMethods/insertMany.js +1 -1
- package/lib/createCollection/getMethods/insertOne.js +1 -1
- package/lib/createCollection/getMethods/updateMany.js +1 -1
- package/lib/createCollection/getMethods/updateOne.js +1 -1
- package/lib/createCollection/getMethods/upsert.js +1 -1
- package/lib/createCollection/getSchemaAndModel.d.ts +10 -0
- package/lib/createCollection/getSchemaAndModel.js +41 -0
- package/lib/createCollection/index.js +4 -11
- package/lib/createCollection/typedModel.test.js +49 -0
- package/lib/types/index.d.ts +25 -0
- package/package.json +6 -6
|
@@ -13,7 +13,7 @@ exports.default = (collection) => {
|
|
|
13
13
|
if (!modifier) {
|
|
14
14
|
throw new Error('Modifier is required when making an update');
|
|
15
15
|
}
|
|
16
|
-
if (collection.
|
|
16
|
+
if (collection.schema) {
|
|
17
17
|
const schema = collection.getSchema();
|
|
18
18
|
modifier = options.clean !== false ? await (0, cleanModifier_1.default)(schema, modifier) : modifier;
|
|
19
19
|
if (options.validate !== false)
|
|
@@ -18,7 +18,7 @@ exports.default = (collection) => {
|
|
|
18
18
|
if (!doc._id) {
|
|
19
19
|
doc._id = collection.generateId();
|
|
20
20
|
}
|
|
21
|
-
if (collection.
|
|
21
|
+
if (collection.schema) {
|
|
22
22
|
const schema = collection.getSchema();
|
|
23
23
|
doc = await (0, schema_1.clean)(schema, (0, fromDot_1.default)(doc));
|
|
24
24
|
await (0, schema_1.validate)(schema, doc);
|
|
@@ -16,7 +16,7 @@ exports.default = (collection) => {
|
|
|
16
16
|
if (!doc._id) {
|
|
17
17
|
doc._id = collection.generateId();
|
|
18
18
|
}
|
|
19
|
-
if (collection.
|
|
19
|
+
if (collection.schema) {
|
|
20
20
|
const schema = collection.getSchema();
|
|
21
21
|
doc = await (0, schema_1.clean)(schema, (0, fromDot_1.default)(doc));
|
|
22
22
|
await (0, schema_1.validate)(schema, doc);
|
|
@@ -14,7 +14,7 @@ exports.default = (collection) => {
|
|
|
14
14
|
if (!modifier) {
|
|
15
15
|
throw new Error('Modifier is required when making an update');
|
|
16
16
|
}
|
|
17
|
-
if (collection.
|
|
17
|
+
if (collection.schema) {
|
|
18
18
|
const schema = collection.getSchema();
|
|
19
19
|
modifier = options.clean !== false ? await (0, cleanModifier_1.default)(schema, modifier) : modifier;
|
|
20
20
|
if (options.validate !== false)
|
|
@@ -14,7 +14,7 @@ exports.default = (collection) => {
|
|
|
14
14
|
if (!modifier) {
|
|
15
15
|
throw new Error('Modifier is required when making an update');
|
|
16
16
|
}
|
|
17
|
-
if (collection.
|
|
17
|
+
if (collection.schema) {
|
|
18
18
|
const schema = collection.getSchema();
|
|
19
19
|
modifier = options.clean !== false ? await (0, cleanModifier_1.default)(schema, modifier) : modifier;
|
|
20
20
|
if (options.validate !== false)
|
|
@@ -12,7 +12,7 @@ exports.default = (collection) => {
|
|
|
12
12
|
let modifier = modifierArg;
|
|
13
13
|
let selector = (0, getSelector_1.default)(arguments);
|
|
14
14
|
modifier.$setOnInsert = { ...modifier.$setOnInsert, _id: collection.generateId() };
|
|
15
|
-
if (collection.
|
|
15
|
+
if (collection.schema) {
|
|
16
16
|
const schema = collection.getSchema();
|
|
17
17
|
if (options.clean !== false) {
|
|
18
18
|
selector = (await (0, cleanModifier_1.default)(schema, { $set: selector })).$set;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Model } from '@orion-js/models';
|
|
2
|
+
import { Schema } from '@orion-js/schema';
|
|
3
|
+
import { CreateCollectionOptions } from '../types';
|
|
4
|
+
export declare function getModel(options: CreateCollectionOptions): Model;
|
|
5
|
+
export declare function prepareShema(schema: Schema): Schema;
|
|
6
|
+
export declare function getSchema(options: CreateCollectionOptions, optionsModel?: Model): Schema;
|
|
7
|
+
export declare function getSchemaAndModel(options: CreateCollectionOptions): {
|
|
8
|
+
schema: Schema;
|
|
9
|
+
model: Model;
|
|
10
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getSchemaAndModel = exports.getSchema = exports.prepareShema = exports.getModel = void 0;
|
|
4
|
+
const lodash_1 = require("lodash");
|
|
5
|
+
function getModel(options) {
|
|
6
|
+
if (!options.model)
|
|
7
|
+
return;
|
|
8
|
+
return options.model && options.model.getModel ? options.model.getModel() : options.model;
|
|
9
|
+
}
|
|
10
|
+
exports.getModel = getModel;
|
|
11
|
+
function prepareShema(schema) {
|
|
12
|
+
if (!schema._id) {
|
|
13
|
+
schema._id = {
|
|
14
|
+
type: String
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
return schema;
|
|
18
|
+
}
|
|
19
|
+
exports.prepareShema = prepareShema;
|
|
20
|
+
function getSchema(options, optionsModel) {
|
|
21
|
+
if (optionsModel) {
|
|
22
|
+
const schema = optionsModel ? (0, lodash_1.cloneDeep)(optionsModel.getCleanSchema()) : {};
|
|
23
|
+
return prepareShema(schema);
|
|
24
|
+
}
|
|
25
|
+
// schema is a typed model
|
|
26
|
+
if (options.schema && options.schema.getModel) {
|
|
27
|
+
const model = options.schema.getModel();
|
|
28
|
+
const schema = model ? (0, lodash_1.cloneDeep)(model.getCleanSchema()) : {};
|
|
29
|
+
return prepareShema(schema);
|
|
30
|
+
}
|
|
31
|
+
if (options.schema && (0, lodash_1.isPlainObject)(options.schema)) {
|
|
32
|
+
return prepareShema(options.schema);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.getSchema = getSchema;
|
|
36
|
+
function getSchemaAndModel(options) {
|
|
37
|
+
const model = getModel(options);
|
|
38
|
+
const schema = getSchema(options, model);
|
|
39
|
+
return { schema, model };
|
|
40
|
+
}
|
|
41
|
+
exports.getSchemaAndModel = getSchemaAndModel;
|
|
@@ -9,8 +9,8 @@ const getMethods_1 = require("./getMethods");
|
|
|
9
9
|
const dataLoader_1 = require("./getMethods/dataLoader");
|
|
10
10
|
const generateId_1 = __importDefault(require("./generateId"));
|
|
11
11
|
const createIndexes_1 = require("./createIndexes");
|
|
12
|
-
const lodash_1 = require("lodash");
|
|
13
12
|
const __1 = require("..");
|
|
13
|
+
const getSchemaAndModel_1 = require("./getSchemaAndModel");
|
|
14
14
|
exports.createIndexesPromises = [];
|
|
15
15
|
const createCollection = (options) => {
|
|
16
16
|
const connectionName = options.connectionName || 'main';
|
|
@@ -20,10 +20,11 @@ const createCollection = (options) => {
|
|
|
20
20
|
}
|
|
21
21
|
const db = orionConnection.db;
|
|
22
22
|
const rawCollection = db.collection(options.name);
|
|
23
|
-
const
|
|
23
|
+
const { schema, model } = (0, getSchemaAndModel_1.getSchemaAndModel)(options);
|
|
24
24
|
const collection = {
|
|
25
25
|
name: options.name,
|
|
26
26
|
connectionName,
|
|
27
|
+
schema,
|
|
27
28
|
model,
|
|
28
29
|
indexes: options.indexes || [],
|
|
29
30
|
db,
|
|
@@ -31,15 +32,7 @@ const createCollection = (options) => {
|
|
|
31
32
|
connectionPromise: orionConnection.connectionPromise,
|
|
32
33
|
rawCollection,
|
|
33
34
|
generateId: (0, generateId_1.default)(options),
|
|
34
|
-
getSchema: () =>
|
|
35
|
-
const schema = (0, lodash_1.cloneDeep)(model.getCleanSchema());
|
|
36
|
-
if (!schema._id) {
|
|
37
|
-
schema._id = {
|
|
38
|
-
type: 'ID'
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
return schema;
|
|
42
|
-
}
|
|
35
|
+
getSchema: () => schema
|
|
43
36
|
};
|
|
44
37
|
// helpers
|
|
45
38
|
collection.initItem = (0, initItem_1.default)(collection);
|
|
@@ -99,4 +99,53 @@ describe('Collections with typed model', () => {
|
|
|
99
99
|
const person = await Persons.findOne('1');
|
|
100
100
|
expect(person.firstName).toBe('John');
|
|
101
101
|
});
|
|
102
|
+
it('Should validate but not initialize documents when passed typed schema', async () => {
|
|
103
|
+
const resolve = async (person, { title }, viewer) => {
|
|
104
|
+
return `${title} ${person.firstName} ${person.lastName}`;
|
|
105
|
+
};
|
|
106
|
+
const titleResolver = (0, resolvers_1.modelResolver)({
|
|
107
|
+
returns: String,
|
|
108
|
+
resolve
|
|
109
|
+
});
|
|
110
|
+
let Person = class Person {
|
|
111
|
+
};
|
|
112
|
+
__decorate([
|
|
113
|
+
(0, typed_model_1.Prop)(),
|
|
114
|
+
__metadata("design:type", String)
|
|
115
|
+
], Person.prototype, "firstName", void 0);
|
|
116
|
+
__decorate([
|
|
117
|
+
(0, typed_model_1.Prop)({ max: 3 }),
|
|
118
|
+
__metadata("design:type", String)
|
|
119
|
+
], Person.prototype, "lastName", void 0);
|
|
120
|
+
__decorate([
|
|
121
|
+
(0, typed_model_1.ResolverProp)(titleResolver),
|
|
122
|
+
__metadata("design:type", Object)
|
|
123
|
+
], Person.prototype, "title", void 0);
|
|
124
|
+
Person = __decorate([
|
|
125
|
+
(0, typed_model_1.TypedSchema)()
|
|
126
|
+
], Person);
|
|
127
|
+
const Persons = (0, _1.default)({
|
|
128
|
+
name: (0, helpers_1.generateId)(),
|
|
129
|
+
schema: Person
|
|
130
|
+
});
|
|
131
|
+
expect.assertions(3);
|
|
132
|
+
try {
|
|
133
|
+
await Persons.insertOne({
|
|
134
|
+
_id: '1',
|
|
135
|
+
firstName: 'John',
|
|
136
|
+
lastName: 'ppoo'
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
expect(error.message).toBe('Validation Error: {lastName: stringTooLong}');
|
|
141
|
+
}
|
|
142
|
+
await Persons.insertOne({
|
|
143
|
+
_id: '1',
|
|
144
|
+
firstName: 'John',
|
|
145
|
+
lastName: 'Doe'
|
|
146
|
+
});
|
|
147
|
+
const person = await Persons.findOne('1');
|
|
148
|
+
expect(person.firstName).toBe('John');
|
|
149
|
+
expect(person.title).toBeUndefined();
|
|
150
|
+
});
|
|
102
151
|
});
|
package/lib/types/index.d.ts
CHANGED
|
@@ -87,10 +87,31 @@ export declare type UpdateOne<ModelClass> = (selector: ModelToMongoSelector<Mode
|
|
|
87
87
|
export declare type UpdateMany<ModelClass> = (selector: ModelToMongoSelector<ModelClass>, modifier: ModelToUpdateFilter<ModelClass>, options?: UpdateOptions) => Promise<MongoDB.UpdateResult | MongoDB.Document>;
|
|
88
88
|
export declare type Upsert<ModelClass> = (selector: ModelToMongoSelector<ModelClass>, modifier: ModelToUpdateFilter<ModelClass>, options?: UpdateOptions) => Promise<MongoDB.UpdateResult>;
|
|
89
89
|
export interface CreateCollectionOptions {
|
|
90
|
+
/**
|
|
91
|
+
* The name of the collection on the Mongo Database
|
|
92
|
+
*/
|
|
90
93
|
name: string;
|
|
94
|
+
/**
|
|
95
|
+
* The name of the connection to use. The Mongo URL of this connection will be search on env variables.
|
|
96
|
+
* If not found, the connection url will be `env.mongo_url`
|
|
97
|
+
* If defined, the connection url will be `env.mongo_url_${name}`
|
|
98
|
+
*/
|
|
91
99
|
connectionName?: string;
|
|
100
|
+
/**
|
|
101
|
+
* The schema used for cleaning and validation of the documents
|
|
102
|
+
*/
|
|
103
|
+
schema?: any;
|
|
104
|
+
/**
|
|
105
|
+
* @deprecated Use schema instead. If you use model, all items will be initialized with the model to add resolvers (which are also deprecated)
|
|
106
|
+
*/
|
|
92
107
|
model?: any;
|
|
108
|
+
/**
|
|
109
|
+
* The indexes to use
|
|
110
|
+
*/
|
|
93
111
|
indexes?: Array<CollectionIndex>;
|
|
112
|
+
/**
|
|
113
|
+
* Select between random id generation o mongo (time based) id generation
|
|
114
|
+
*/
|
|
94
115
|
idGeneration?: 'mongo' | 'random';
|
|
95
116
|
}
|
|
96
117
|
export declare type EstimatedDocumentCount<ModelClass> = (options?: MongoDB.EstimatedDocumentCountOptions) => Promise<number>;
|
|
@@ -99,6 +120,10 @@ export declare type CreateCollection = <ModelClass = any>(options: CreateCollect
|
|
|
99
120
|
export interface Collection<ModelClass = any> {
|
|
100
121
|
name: string;
|
|
101
122
|
connectionName?: string;
|
|
123
|
+
schema?: Schema;
|
|
124
|
+
/**
|
|
125
|
+
* @deprecated Use schema instead. If you use model, all items will be initialized with the model to add resolvers (which are also deprecated)
|
|
126
|
+
*/
|
|
102
127
|
model?: Model;
|
|
103
128
|
indexes: Array<CollectionIndex>;
|
|
104
129
|
generateId: () => string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orion-js/mongodb",
|
|
3
|
-
"version": "3.2.
|
|
3
|
+
"version": "3.2.18",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "lib/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -19,11 +19,11 @@
|
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@orion-js/env": "^3.2.0",
|
|
21
21
|
"@orion-js/helpers": "^3.2.0",
|
|
22
|
-
"@orion-js/models": "^3.2.
|
|
23
|
-
"@orion-js/resolvers": "^3.2.
|
|
24
|
-
"@orion-js/schema": "^3.2.
|
|
22
|
+
"@orion-js/models": "^3.2.18",
|
|
23
|
+
"@orion-js/resolvers": "^3.2.18",
|
|
24
|
+
"@orion-js/schema": "^3.2.18",
|
|
25
25
|
"@orion-js/services": "^3.2.10",
|
|
26
|
-
"@orion-js/typed-model": "^3.2.
|
|
26
|
+
"@orion-js/typed-model": "^3.2.18",
|
|
27
27
|
"dataloader": "2.1.0",
|
|
28
28
|
"dot-object": "2.1.4",
|
|
29
29
|
"mongodb": "4.5.0"
|
|
@@ -40,5 +40,5 @@
|
|
|
40
40
|
"publishConfig": {
|
|
41
41
|
"access": "public"
|
|
42
42
|
},
|
|
43
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "95db28fdf82bca1f23a07ea3a0d8f1218b7c1373"
|
|
44
44
|
}
|