@orion-js/mongodb 3.2.4 → 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/countDocuments.d.ts +3 -0
- package/lib/createCollection/getMethods/countDocuments.js +14 -0
- package/lib/createCollection/getMethods/countDocuments.test.d.ts +1 -0
- package/lib/createCollection/getMethods/countDocuments.test.js +21 -0
- package/lib/createCollection/getMethods/estimatedDocumentCount.d.ts +3 -0
- package/lib/createCollection/getMethods/estimatedDocumentCount.js +9 -0
- package/lib/createCollection/getMethods/estimatedDocumentCount.test.d.ts +1 -0
- package/lib/createCollection/getMethods/estimatedDocumentCount.test.js +15 -0
- package/lib/createCollection/getMethods/findOneAndUpdate.js +1 -1
- package/lib/createCollection/getMethods/index.d.ts +3 -1
- package/lib/createCollection/getMethods/index.js +5 -1
- package/lib/createCollection/getMethods/insertMany.js +1 -1
- package/lib/createCollection/getMethods/insertMany.test.js +1 -1
- package/lib/createCollection/getMethods/insertOne.js +1 -1
- package/lib/createCollection/getMethods/insertOne.test.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 +7 -11
- package/lib/createCollection/typedModel.test.js +78 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -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 +68 -0
- package/lib/types/index.d.ts +39 -3
- package/package.json +7 -6
|
@@ -0,0 +1,14 @@
|
|
|
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
|
+
exports.default = (collection) => {
|
|
8
|
+
const func = async function (selectorArg, options) {
|
|
9
|
+
const selector = (0, getSelector_1.default)(arguments);
|
|
10
|
+
const result = await collection.rawCollection.countDocuments(selector, options);
|
|
11
|
+
return result;
|
|
12
|
+
};
|
|
13
|
+
return func;
|
|
14
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
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 __1 = __importDefault(require(".."));
|
|
8
|
+
describe('countDocuments operation', () => {
|
|
9
|
+
it('should count all documents', async () => {
|
|
10
|
+
const Tests = (0, __1.default)({ name: (0, helpers_1.generateId)() });
|
|
11
|
+
await Tests.insertMany([{ hello: 'world' }, { hello: 'world' }]);
|
|
12
|
+
const count = await Tests.countDocuments({});
|
|
13
|
+
expect(count).toBe(2);
|
|
14
|
+
});
|
|
15
|
+
it('should count filtering documents', async () => {
|
|
16
|
+
const Tests = (0, __1.default)({ name: (0, helpers_1.generateId)() });
|
|
17
|
+
await Tests.insertMany([{ name: '1' }, { name: '2' }]);
|
|
18
|
+
const count = await Tests.countDocuments({ name: '1' });
|
|
19
|
+
expect(count).toBe(1);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = (collection) => {
|
|
4
|
+
const func = async function (options) {
|
|
5
|
+
const result = await collection.rawCollection.estimatedDocumentCount(options);
|
|
6
|
+
return result;
|
|
7
|
+
};
|
|
8
|
+
return func;
|
|
9
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
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 __1 = __importDefault(require(".."));
|
|
8
|
+
describe('estimatedDocumentCount operation', () => {
|
|
9
|
+
it('should count all documents', async () => {
|
|
10
|
+
const Tests = (0, __1.default)({ name: (0, helpers_1.generateId)() });
|
|
11
|
+
await Tests.insertMany([{ hello: 'world' }, { hello: 'world' }]);
|
|
12
|
+
const count = await Tests.estimatedDocumentCount();
|
|
13
|
+
expect(count).toBe(2);
|
|
14
|
+
});
|
|
15
|
+
});
|
|
@@ -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)
|
|
@@ -10,4 +10,6 @@ import deleteMany from './deleteMany';
|
|
|
10
10
|
import insertOne from './insertOne';
|
|
11
11
|
import insertMany from './insertMany';
|
|
12
12
|
import updateItem from './updateItem';
|
|
13
|
-
|
|
13
|
+
import countDocuments from './countDocuments';
|
|
14
|
+
import estimatedDocumentCount from './estimatedDocumentCount';
|
|
15
|
+
export { findOne, find, findOneAndUpdate, insertOne, insertMany, updateOne, updateMany, updateAndFind, updateItem, deleteOne, deleteMany, upsert, estimatedDocumentCount, countDocuments };
|
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.upsert = exports.deleteMany = exports.deleteOne = exports.updateItem = exports.updateAndFind = exports.updateMany = exports.updateOne = exports.insertMany = exports.insertOne = exports.findOneAndUpdate = exports.find = exports.findOne = void 0;
|
|
6
|
+
exports.countDocuments = exports.estimatedDocumentCount = exports.upsert = exports.deleteMany = exports.deleteOne = exports.updateItem = exports.updateAndFind = exports.updateMany = exports.updateOne = exports.insertMany = exports.insertOne = exports.findOneAndUpdate = exports.find = exports.findOne = void 0;
|
|
7
7
|
const find_1 = __importDefault(require("./find"));
|
|
8
8
|
exports.find = find_1.default;
|
|
9
9
|
const findOne_1 = __importDefault(require("./findOne"));
|
|
@@ -28,3 +28,7 @@ const insertMany_1 = __importDefault(require("./insertMany"));
|
|
|
28
28
|
exports.insertMany = insertMany_1.default;
|
|
29
29
|
const updateItem_1 = __importDefault(require("./updateItem"));
|
|
30
30
|
exports.updateItem = updateItem_1.default;
|
|
31
|
+
const countDocuments_1 = __importDefault(require("./countDocuments"));
|
|
32
|
+
exports.countDocuments = countDocuments_1.default;
|
|
33
|
+
const estimatedDocumentCount_1 = __importDefault(require("./estimatedDocumentCount"));
|
|
34
|
+
exports.estimatedDocumentCount = estimatedDocumentCount_1.default;
|
|
@@ -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);
|
|
@@ -15,7 +15,7 @@ it('should return a function', async () => {
|
|
|
15
15
|
it('insert documents without errors', async () => {
|
|
16
16
|
const Tests = (0, __1.default)({ name: (0, helpers_1.generateId)() });
|
|
17
17
|
await Tests.insertMany([{ hello: 'world' }, { hello: 'world' }]);
|
|
18
|
-
const count = await Tests.
|
|
18
|
+
const count = await Tests.estimatedDocumentCount();
|
|
19
19
|
expect(count).toBe(2);
|
|
20
20
|
});
|
|
21
21
|
it('should throw an error when no document is passed', async () => {
|
|
@@ -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);
|
|
@@ -16,7 +16,7 @@ describe('InsertOne', () => {
|
|
|
16
16
|
it('insertOnes a document without errors', async () => {
|
|
17
17
|
const Tests = (0, __1.default)({ name: (0, helpers_1.generateId)() });
|
|
18
18
|
await Tests.insertOne({ hello: 'world' });
|
|
19
|
-
const count = await Tests.
|
|
19
|
+
const count = await Tests.estimatedDocumentCount();
|
|
20
20
|
expect(count).toBe(1);
|
|
21
21
|
});
|
|
22
22
|
it('should insertOne documents passing deep validation', async () => {
|
|
@@ -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);
|
|
@@ -54,6 +47,9 @@ const createCollection = (options) => {
|
|
|
54
47
|
collection.deleteMany = (0, getMethods_1.deleteMany)(collection);
|
|
55
48
|
collection.deleteOne = (0, getMethods_1.deleteOne)(collection);
|
|
56
49
|
collection.upsert = (0, getMethods_1.upsert)(collection);
|
|
50
|
+
// counts
|
|
51
|
+
collection.estimatedDocumentCount = (0, getMethods_1.estimatedDocumentCount)(collection);
|
|
52
|
+
collection.countDocuments = (0, getMethods_1.countDocuments)(collection);
|
|
57
53
|
// update and find
|
|
58
54
|
collection.updateAndFind = (0, getMethods_1.updateAndFind)(collection);
|
|
59
55
|
collection.updateItem = (0, getMethods_1.updateItem)(collection);
|
|
@@ -70,4 +70,82 @@ describe('Collections with typed model', () => {
|
|
|
70
70
|
const title = await person.title({ title: 'Mr.' });
|
|
71
71
|
expect(title).toBe('Mr. John Doe');
|
|
72
72
|
});
|
|
73
|
+
it('Should allow passing _id on insert', async () => {
|
|
74
|
+
let PersonOptionalId = class PersonOptionalId {
|
|
75
|
+
};
|
|
76
|
+
__decorate([
|
|
77
|
+
(0, typed_model_1.Prop)(),
|
|
78
|
+
__metadata("design:type", String)
|
|
79
|
+
], PersonOptionalId.prototype, "_id", void 0);
|
|
80
|
+
__decorate([
|
|
81
|
+
(0, typed_model_1.Prop)(),
|
|
82
|
+
__metadata("design:type", String)
|
|
83
|
+
], PersonOptionalId.prototype, "firstName", void 0);
|
|
84
|
+
__decorate([
|
|
85
|
+
(0, typed_model_1.Prop)({ optional: true }),
|
|
86
|
+
__metadata("design:type", String)
|
|
87
|
+
], PersonOptionalId.prototype, "lastName", void 0);
|
|
88
|
+
PersonOptionalId = __decorate([
|
|
89
|
+
(0, typed_model_1.TypedSchema)()
|
|
90
|
+
], PersonOptionalId);
|
|
91
|
+
const Persons = (0, _1.default)({
|
|
92
|
+
name: (0, helpers_1.generateId)(),
|
|
93
|
+
model: PersonOptionalId
|
|
94
|
+
});
|
|
95
|
+
await Persons.insertOne({
|
|
96
|
+
_id: '1',
|
|
97
|
+
firstName: 'John'
|
|
98
|
+
});
|
|
99
|
+
const person = await Persons.findOne('1');
|
|
100
|
+
expect(person.firstName).toBe('John');
|
|
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
|
+
});
|
|
73
151
|
});
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -32,3 +32,4 @@ exports.createCollection = createCollection_1.default;
|
|
|
32
32
|
Object.defineProperty(exports, "createIndexesPromises", { enumerable: true, get: function () { return createCollection_1.createIndexesPromises; } });
|
|
33
33
|
__exportStar(require("./connect"), exports);
|
|
34
34
|
__exportStar(require("./types"), exports);
|
|
35
|
+
__exportStar(require("./service"), exports);
|
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
exports.Repository = exports.MongoCollection = void 0;
|
|
7
|
+
const services_1 = require("@orion-js/services");
|
|
8
|
+
const createCollection_1 = __importDefault(require("../createCollection"));
|
|
9
|
+
function MongoCollection(options) {
|
|
10
|
+
return function (object, propertyName, index) {
|
|
11
|
+
services_1.Container.registerHandler({
|
|
12
|
+
object,
|
|
13
|
+
propertyName,
|
|
14
|
+
index,
|
|
15
|
+
value: containerInstance => {
|
|
16
|
+
if (!object.serviceType || object.serviceType !== 'repo') {
|
|
17
|
+
throw new Error('You must pass a class decorated with @Repository if you want to use @MongoCollection');
|
|
18
|
+
}
|
|
19
|
+
return (0, createCollection_1.default)(options);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
exports.MongoCollection = MongoCollection;
|
|
25
|
+
function Repository() {
|
|
26
|
+
return function (target) {
|
|
27
|
+
(0, services_1.Service)()(target);
|
|
28
|
+
target.prototype.service = target;
|
|
29
|
+
target.prototype.serviceType = 'repo';
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
exports.Repository = Repository;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const services_1 = require("@orion-js/services");
|
|
13
|
+
const typed_model_1 = require("@orion-js/typed-model");
|
|
14
|
+
const _1 = require(".");
|
|
15
|
+
describe('Collection as IOC', () => {
|
|
16
|
+
it('should create the collection and set the methods', async () => {
|
|
17
|
+
let User = class User {
|
|
18
|
+
};
|
|
19
|
+
__decorate([
|
|
20
|
+
(0, typed_model_1.Prop)(),
|
|
21
|
+
__metadata("design:type", String)
|
|
22
|
+
], User.prototype, "_id", void 0);
|
|
23
|
+
__decorate([
|
|
24
|
+
(0, typed_model_1.Prop)(),
|
|
25
|
+
__metadata("design:type", String)
|
|
26
|
+
], User.prototype, "name", void 0);
|
|
27
|
+
User = __decorate([
|
|
28
|
+
(0, typed_model_1.TypedSchema)()
|
|
29
|
+
], User);
|
|
30
|
+
let UserRepo = class UserRepo {
|
|
31
|
+
async createUser(user) {
|
|
32
|
+
return await this.users.insertOne(user);
|
|
33
|
+
}
|
|
34
|
+
async getUserByName(name) {
|
|
35
|
+
return await this.users.findOne({ name });
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
__decorate([
|
|
39
|
+
(0, _1.MongoCollection)({ name: 'users' }),
|
|
40
|
+
__metadata("design:type", Object)
|
|
41
|
+
], UserRepo.prototype, "users", void 0);
|
|
42
|
+
UserRepo = __decorate([
|
|
43
|
+
(0, _1.Repository)()
|
|
44
|
+
], UserRepo);
|
|
45
|
+
const instance = (0, services_1.getInstance)(UserRepo);
|
|
46
|
+
const userId = await instance.createUser({ name: 'Nico' });
|
|
47
|
+
const user = await instance.getUserByName('Nico');
|
|
48
|
+
expect(user._id).toBe(userId);
|
|
49
|
+
});
|
|
50
|
+
it('should throw an error when trying to set a collection param in a service that is not a repo', () => {
|
|
51
|
+
expect.assertions(1);
|
|
52
|
+
try {
|
|
53
|
+
let UserErrorRepo = class UserErrorRepo {
|
|
54
|
+
};
|
|
55
|
+
__decorate([
|
|
56
|
+
(0, _1.MongoCollection)({ name: 'users2' }),
|
|
57
|
+
__metadata("design:type", Object)
|
|
58
|
+
], UserErrorRepo.prototype, "users", void 0);
|
|
59
|
+
UserErrorRepo = __decorate([
|
|
60
|
+
(0, services_1.Service)()
|
|
61
|
+
], UserErrorRepo);
|
|
62
|
+
const instance = (0, services_1.getInstance)(UserErrorRepo);
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
expect(error.message).toBe('You must pass a class decorated with @Repository if you want to use @MongoCollection');
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
});
|
package/lib/types/index.d.ts
CHANGED
|
@@ -2,20 +2,27 @@ import * as MongoDB from 'mongodb';
|
|
|
2
2
|
import { Model } from '@orion-js/models';
|
|
3
3
|
import { Schema } from '@orion-js/schema';
|
|
4
4
|
import { OrionMongoClient } from '../connect/connections';
|
|
5
|
+
declare type RemoveFunctions<T> = OmitByValue<T, Function>;
|
|
5
6
|
export declare type DocumentWithId<T> = T & {
|
|
6
7
|
/**
|
|
7
8
|
* The ID of the document
|
|
8
9
|
*/
|
|
9
10
|
_id: string;
|
|
10
11
|
};
|
|
12
|
+
export declare type DocumentWithIdOptional<T> = Omit<T, '_id'> & {
|
|
13
|
+
/**
|
|
14
|
+
* The ID of the document
|
|
15
|
+
*/
|
|
16
|
+
_id?: string;
|
|
17
|
+
};
|
|
11
18
|
export declare type DocumentWithoutId<T> = Omit<T, '_id'>;
|
|
12
19
|
declare type OmitByValue<T, ValueType> = Pick<T, {
|
|
13
20
|
[Key in keyof T]-?: T[Key] extends ValueType ? never : Key;
|
|
14
21
|
}[keyof T]>;
|
|
15
|
-
declare type RemoveFunctions<T> = OmitByValue<T, Function>;
|
|
16
22
|
export declare type ModelToDocumentType<ModelClass> = RemoveFunctions<ModelClass>;
|
|
17
23
|
export declare type ModelToDocumentTypeWithId<ModelClass> = DocumentWithId<RemoveFunctions<ModelClass>>;
|
|
18
24
|
export declare type ModelToDocumentTypeWithoutId<ModelClass> = DocumentWithoutId<ModelToDocumentType<ModelClass>>;
|
|
25
|
+
export declare type ModelToDocumentTypeWithIdOptional<ModelClass> = DocumentWithIdOptional<ModelToDocumentType<ModelClass>>;
|
|
19
26
|
export declare type ModelToMongoSelector<ModelClass> = MongoSelector<ModelToDocumentType<ModelClass>>;
|
|
20
27
|
export declare type ModelToUpdateFilter<ModelClass> = MongoDB.UpdateFilter<ModelToDocumentTypeWithoutId<ModelClass>> | Partial<ModelToDocumentTypeWithoutId<ModelClass>>;
|
|
21
28
|
export interface CollectionIndex {
|
|
@@ -72,24 +79,51 @@ export declare type UpdateAndFind<ModelClass> = (selector: ModelToMongoSelector<
|
|
|
72
79
|
export declare type UpdateItem<ModelClass> = (item: {
|
|
73
80
|
_id: string;
|
|
74
81
|
} & ModelClass, modifier: ModelToUpdateFilter<ModelClass>) => Promise<void>;
|
|
75
|
-
export declare type InsertOne<ModelClass> = (doc:
|
|
76
|
-
export declare type InsertMany<ModelClass> = (doc: Array<
|
|
82
|
+
export declare type InsertOne<ModelClass> = (doc: ModelToDocumentTypeWithIdOptional<ModelClass>, options?: InsertOptions) => Promise<string>;
|
|
83
|
+
export declare type InsertMany<ModelClass> = (doc: Array<ModelToDocumentTypeWithIdOptional<ModelClass>>, options?: InsertOptions) => Promise<Array<string>>;
|
|
77
84
|
export declare type DeleteMany<ModelClass> = (selector: ModelToMongoSelector<ModelClass>, options?: MongoDB.DeleteOptions) => Promise<MongoDB.DeleteResult>;
|
|
78
85
|
export declare type DeleteOne<ModelClass> = (selector: ModelToMongoSelector<ModelClass>, options?: MongoDB.DeleteOptions) => Promise<MongoDB.DeleteResult>;
|
|
79
86
|
export declare type UpdateOne<ModelClass> = (selector: ModelToMongoSelector<ModelClass>, modifier: ModelToUpdateFilter<ModelClass>, options?: UpdateOptions) => Promise<MongoDB.UpdateResult>;
|
|
80
87
|
export declare type UpdateMany<ModelClass> = (selector: ModelToMongoSelector<ModelClass>, modifier: ModelToUpdateFilter<ModelClass>, options?: UpdateOptions) => Promise<MongoDB.UpdateResult | MongoDB.Document>;
|
|
81
88
|
export declare type Upsert<ModelClass> = (selector: ModelToMongoSelector<ModelClass>, modifier: ModelToUpdateFilter<ModelClass>, options?: UpdateOptions) => Promise<MongoDB.UpdateResult>;
|
|
82
89
|
export interface CreateCollectionOptions {
|
|
90
|
+
/**
|
|
91
|
+
* The name of the collection on the Mongo Database
|
|
92
|
+
*/
|
|
83
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
|
+
*/
|
|
84
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
|
+
*/
|
|
85
107
|
model?: any;
|
|
108
|
+
/**
|
|
109
|
+
* The indexes to use
|
|
110
|
+
*/
|
|
86
111
|
indexes?: Array<CollectionIndex>;
|
|
112
|
+
/**
|
|
113
|
+
* Select between random id generation o mongo (time based) id generation
|
|
114
|
+
*/
|
|
87
115
|
idGeneration?: 'mongo' | 'random';
|
|
88
116
|
}
|
|
117
|
+
export declare type EstimatedDocumentCount<ModelClass> = (options?: MongoDB.EstimatedDocumentCountOptions) => Promise<number>;
|
|
118
|
+
export declare type CountDocuments<ModelClass> = (selector: ModelToMongoSelector<ModelClass>, options?: MongoDB.CountDocumentsOptions) => Promise<number>;
|
|
89
119
|
export declare type CreateCollection = <ModelClass = any>(options: CreateCollectionOptions) => Collection<ModelClass>;
|
|
90
120
|
export interface Collection<ModelClass = any> {
|
|
91
121
|
name: string;
|
|
92
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
|
+
*/
|
|
93
127
|
model?: Model;
|
|
94
128
|
indexes: Array<CollectionIndex>;
|
|
95
129
|
generateId: () => string;
|
|
@@ -110,6 +144,8 @@ export interface Collection<ModelClass = any> {
|
|
|
110
144
|
findOneAndUpdate: FindOneAndUpdate<ModelClass>;
|
|
111
145
|
updateAndFind: UpdateAndFind<ModelClass>;
|
|
112
146
|
updateItem: UpdateItem<ModelClass>;
|
|
147
|
+
estimatedDocumentCount: EstimatedDocumentCount<ModelClass>;
|
|
148
|
+
countDocuments: CountDocuments<ModelClass>;
|
|
113
149
|
aggregate: <T = MongoDB.Document>(pipeline?: MongoDB.Document[], options?: MongoDB.AggregateOptions) => MongoDB.AggregationCursor<T>;
|
|
114
150
|
watch: <T = MongoDB.Document>(pipeline?: MongoDB.Document[], options?: MongoDB.ChangeStreamOptions) => MongoDB.ChangeStream<T>;
|
|
115
151
|
loadData: DataLoader.LoadData<ModelClass>;
|
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,10 +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.
|
|
25
|
-
"@orion-js/
|
|
22
|
+
"@orion-js/models": "^3.2.18",
|
|
23
|
+
"@orion-js/resolvers": "^3.2.18",
|
|
24
|
+
"@orion-js/schema": "^3.2.18",
|
|
25
|
+
"@orion-js/services": "^3.2.10",
|
|
26
|
+
"@orion-js/typed-model": "^3.2.18",
|
|
26
27
|
"dataloader": "2.1.0",
|
|
27
28
|
"dot-object": "2.1.4",
|
|
28
29
|
"mongodb": "4.5.0"
|
|
@@ -39,5 +40,5 @@
|
|
|
39
40
|
"publishConfig": {
|
|
40
41
|
"access": "public"
|
|
41
42
|
},
|
|
42
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "95db28fdf82bca1f23a07ea3a0d8f1218b7c1373"
|
|
43
44
|
}
|