@orion-js/mongodb 3.2.14 → 3.2.21

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.
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const getSelector_1 = __importDefault(require("./getSelector"));
7
7
  exports.default = (collection) => {
8
8
  const func = async function (selectorArg, options) {
9
+ await collection.connectionPromise;
9
10
  const selector = (0, getSelector_1.default)(arguments);
10
11
  const result = await collection.rawCollection.countDocuments(selector, options);
11
12
  return result;
@@ -8,6 +8,7 @@ const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
8
8
  const dataLoad_1 = __importDefault(require("./dataLoad"));
9
9
  function default_1(collection) {
10
10
  const loadData = async (options) => {
11
+ await collection.connectionPromise;
11
12
  const result = await (0, dataLoad_1.default)({
12
13
  loaderKey: {
13
14
  key: options.key,
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const getSelector_1 = __importDefault(require("./getSelector"));
7
7
  exports.default = (collection) => {
8
8
  const func = async function (selectorArg, options) {
9
+ await collection.connectionPromise;
9
10
  const selector = (0, getSelector_1.default)(arguments);
10
11
  const result = await collection.rawCollection.deleteMany(selector, options);
11
12
  return result;
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const getSelector_1 = __importDefault(require("./getSelector"));
7
7
  exports.default = (collection) => {
8
8
  const func = async function (selectorArg, options) {
9
+ await collection.connectionPromise;
9
10
  const selector = (0, getSelector_1.default)(arguments);
10
11
  const result = await collection.rawCollection.deleteOne(selector, options);
11
12
  return result;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = (collection) => {
4
4
  const func = async function (options) {
5
+ await collection.connectionPromise;
5
6
  const result = await collection.rawCollection.estimatedDocumentCount(options);
6
7
  return result;
7
8
  };
@@ -10,6 +10,7 @@ exports.default = (collection) => {
10
10
  const cursor = collection.rawCollection.find(selector, options);
11
11
  cursor._oldToArray = cursor.toArray;
12
12
  cursor.toArray = async () => {
13
+ await collection.connectionPromise;
13
14
  const items = await cursor._oldToArray();
14
15
  return items.map(item => collection.initItem(item));
15
16
  };
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const getSelector_1 = __importDefault(require("./getSelector"));
7
7
  exports.default = (collection) => {
8
8
  const findOne = async function (selectorArg, options) {
9
+ await collection.connectionPromise;
9
10
  const selector = (0, getSelector_1.default)(arguments);
10
11
  const item = await collection.rawCollection.findOne(selector, options);
11
12
  if (!item)
@@ -8,12 +8,13 @@ const validateModifier_1 = __importDefault(require("./validateModifier"));
8
8
  const cleanModifier_1 = __importDefault(require("./cleanModifier"));
9
9
  exports.default = (collection) => {
10
10
  const findOneAndUpdate = async function (selectorArg, modifierArg, options = {}) {
11
+ await collection.connectionPromise;
11
12
  let modifier = modifierArg;
12
13
  const selector = (0, getSelector_1.default)(arguments);
13
14
  if (!modifier) {
14
15
  throw new Error('Modifier is required when making an update');
15
16
  }
16
- if (collection.model) {
17
+ if (collection.schema) {
17
18
  const schema = collection.getSchema();
18
19
  modifier = options.clean !== false ? await (0, cleanModifier_1.default)(schema, modifier) : modifier;
19
20
  if (options.validate !== false)
@@ -10,6 +10,7 @@ const schema_1 = require("@orion-js/schema");
10
10
  const wrapErrors_1 = require("./wrapErrors");
11
11
  exports.default = (collection) => {
12
12
  const insertMany = async (docs, options = {}) => {
13
+ await collection.connectionPromise;
13
14
  for (let index = 0; index < docs.length; index++) {
14
15
  let doc = (0, lodash_1.cloneDeep)(docs[index]);
15
16
  if (!doc || !(0, isPlainObject_1.default)(doc)) {
@@ -18,7 +19,7 @@ exports.default = (collection) => {
18
19
  if (!doc._id) {
19
20
  doc._id = collection.generateId();
20
21
  }
21
- if (collection.model) {
22
+ if (collection.schema) {
22
23
  const schema = collection.getSchema();
23
24
  doc = await (0, schema_1.clean)(schema, (0, fromDot_1.default)(doc));
24
25
  await (0, schema_1.validate)(schema, doc);
@@ -9,6 +9,7 @@ const schema_1 = require("@orion-js/schema");
9
9
  const wrapErrors_1 = require("./wrapErrors");
10
10
  exports.default = (collection) => {
11
11
  const insertOne = async (insertDoc, options) => {
12
+ await collection.connectionPromise;
12
13
  let doc = insertDoc;
13
14
  if (!doc || !(0, isPlainObject_1.default)(doc)) {
14
15
  throw new Error('Insert must receive a document');
@@ -16,7 +17,7 @@ exports.default = (collection) => {
16
17
  if (!doc._id) {
17
18
  doc._id = collection.generateId();
18
19
  }
19
- if (collection.model) {
20
+ if (collection.schema) {
20
21
  const schema = collection.getSchema();
21
22
  doc = await (0, schema_1.clean)(schema, (0, fromDot_1.default)(doc));
22
23
  await (0, schema_1.validate)(schema, doc);
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const wrapErrors_1 = require("./wrapErrors");
4
4
  exports.default = (collection) => {
5
5
  const updateAndFind = async function (selector, modifier, options = {}) {
6
+ await collection.connectionPromise;
6
7
  return await (0, wrapErrors_1.wrapErrors)(async () => {
7
8
  return await collection.findOneAndUpdate(selector, modifier, {
8
9
  ...options,
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const wrapErrors_1 = require("./wrapErrors");
4
4
  exports.default = (collection) => {
5
5
  const updateItem = async function (item, modifier) {
6
+ await collection.connectionPromise;
6
7
  const updated = await (0, wrapErrors_1.wrapErrors)(async () => {
7
8
  return await collection.updateAndFind({ _id: item._id }, modifier);
8
9
  });
@@ -9,12 +9,13 @@ const validateModifier_1 = __importDefault(require("./validateModifier"));
9
9
  const wrapErrors_1 = require("./wrapErrors");
10
10
  exports.default = (collection) => {
11
11
  const updateMany = async function (selectorArg, modifierArg, options = {}) {
12
+ await collection.connectionPromise;
12
13
  let modifier = modifierArg;
13
14
  const selector = (0, getSelector_1.default)(arguments);
14
15
  if (!modifier) {
15
16
  throw new Error('Modifier is required when making an update');
16
17
  }
17
- if (collection.model) {
18
+ if (collection.schema) {
18
19
  const schema = collection.getSchema();
19
20
  modifier = options.clean !== false ? await (0, cleanModifier_1.default)(schema, modifier) : modifier;
20
21
  if (options.validate !== false)
@@ -9,12 +9,13 @@ const validateModifier_1 = __importDefault(require("./validateModifier"));
9
9
  const wrapErrors_1 = require("./wrapErrors");
10
10
  exports.default = (collection) => {
11
11
  const updateOne = async function (selectorArg, modifierArg, options = {}) {
12
+ await collection.connectionPromise;
12
13
  let modifier = modifierArg;
13
14
  const selector = (0, getSelector_1.default)(arguments);
14
15
  if (!modifier) {
15
16
  throw new Error('Modifier is required when making an update');
16
17
  }
17
- if (collection.model) {
18
+ if (collection.schema) {
18
19
  const schema = collection.getSchema();
19
20
  modifier = options.clean !== false ? await (0, cleanModifier_1.default)(schema, modifier) : modifier;
20
21
  if (options.validate !== false)
@@ -9,10 +9,11 @@ const cleanModifier_1 = __importDefault(require("./cleanModifier"));
9
9
  const wrapErrors_1 = require("./wrapErrors");
10
10
  exports.default = (collection) => {
11
11
  const upsert = async function (selectorArg, modifierArg, options = {}) {
12
+ await collection.connectionPromise;
12
13
  let modifier = modifierArg;
13
14
  let selector = (0, getSelector_1.default)(arguments);
14
15
  modifier.$setOnInsert = { ...modifier.$setOnInsert, _id: collection.generateId() };
15
- if (collection.model) {
16
+ if (collection.schema) {
16
17
  const schema = collection.getSchema();
17
18
  if (options.clean !== false) {
18
19
  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 model = options.model && options.model.getModel ? options.model.getModel() : options.model;
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
  });
@@ -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.14",
3
+ "version": "3.2.21",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "files": [
@@ -17,13 +17,13 @@
17
17
  "upgrade-interactive": "yarn upgrade-interactive"
18
18
  },
19
19
  "dependencies": {
20
- "@orion-js/env": "^3.2.0",
21
- "@orion-js/helpers": "^3.2.0",
22
- "@orion-js/models": "^3.2.0",
23
- "@orion-js/resolvers": "^3.2.0",
24
- "@orion-js/schema": "^3.2.0",
25
- "@orion-js/services": "^3.2.10",
26
- "@orion-js/typed-model": "^3.2.10",
20
+ "@orion-js/env": "^3.2.21",
21
+ "@orion-js/helpers": "^3.2.21",
22
+ "@orion-js/models": "^3.2.21",
23
+ "@orion-js/resolvers": "^3.2.21",
24
+ "@orion-js/schema": "^3.2.21",
25
+ "@orion-js/services": "^3.2.21",
26
+ "@orion-js/typed-model": "^3.2.21",
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": "87d4cca0154b4beb6e6fa97f298b1440e0c3c231"
43
+ "gitHead": "160e776b7a8448c8e4f67cae79849d6520eaf0c1"
44
44
  }