@orion-js/mongodb 3.2.48 → 3.2.55

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.
@@ -12,4 +12,5 @@ import insertMany from './insertMany';
12
12
  import updateItem from './updateItem';
13
13
  import countDocuments from './countDocuments';
14
14
  import estimatedDocumentCount from './estimatedDocumentCount';
15
- export { findOne, find, findOneAndUpdate, insertOne, insertMany, updateOne, updateMany, updateAndFind, updateItem, deleteOne, deleteMany, upsert, estimatedDocumentCount, countDocuments };
15
+ import insertAndFind from './insertAndFind';
16
+ export { findOne, find, findOneAndUpdate, insertOne, insertMany, insertAndFind, 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.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;
6
+ exports.countDocuments = exports.estimatedDocumentCount = exports.upsert = exports.deleteMany = exports.deleteOne = exports.updateItem = exports.updateAndFind = exports.updateMany = exports.updateOne = exports.insertAndFind = 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"));
@@ -32,3 +32,5 @@ const countDocuments_1 = __importDefault(require("./countDocuments"));
32
32
  exports.countDocuments = countDocuments_1.default;
33
33
  const estimatedDocumentCount_1 = __importDefault(require("./estimatedDocumentCount"));
34
34
  exports.estimatedDocumentCount = estimatedDocumentCount_1.default;
35
+ const insertAndFind_1 = __importDefault(require("./insertAndFind"));
36
+ exports.insertAndFind = insertAndFind_1.default;
@@ -0,0 +1,3 @@
1
+ import { Collection, InsertAndFind } from '../../types';
2
+ declare const _default: <DocumentType>(collection: Partial<Collection>) => InsertAndFind<DocumentType>;
3
+ export default _default;
@@ -0,0 +1,31 @@
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 isPlainObject_1 = __importDefault(require("lodash/isPlainObject"));
7
+ const fromDot_1 = __importDefault(require("../../helpers/fromDot"));
8
+ const schema_1 = require("@orion-js/schema");
9
+ const wrapErrors_1 = require("./wrapErrors");
10
+ exports.default = (collection) => {
11
+ const insertAndFind = async (insertDoc, options) => {
12
+ await collection.connectionPromise;
13
+ let doc = insertDoc;
14
+ if (!doc || !(0, isPlainObject_1.default)(doc)) {
15
+ throw new Error('Insert must receive a document');
16
+ }
17
+ if (!doc._id) {
18
+ doc._id = collection.generateId();
19
+ }
20
+ if (collection.schema) {
21
+ const schema = collection.getSchema();
22
+ doc = await (0, schema_1.clean)(schema, (0, fromDot_1.default)(doc));
23
+ await (0, schema_1.validate)(schema, doc);
24
+ }
25
+ await (0, wrapErrors_1.wrapErrors)(async () => {
26
+ await collection.rawCollection.insertOne(doc);
27
+ });
28
+ return doc;
29
+ };
30
+ return insertAndFind;
31
+ };
@@ -0,0 +1,50 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const insertOne_1 = __importDefault(require("./insertOne"));
16
+ const helpers_1 = require("@orion-js/helpers");
17
+ const __1 = __importDefault(require(".."));
18
+ const typed_model_1 = require("@orion-js/typed-model");
19
+ describe('insertAndFind', () => {
20
+ it('should return a function', async () => {
21
+ const Tests = (0, __1.default)({ name: (0, helpers_1.generateId)() });
22
+ const insertOne = (0, insertOne_1.default)(Tests);
23
+ expect(typeof insertOne).toBe('function');
24
+ });
25
+ it('inserts and finds a cleaned document', async () => {
26
+ let TestSchema1 = class TestSchema1 {
27
+ };
28
+ __decorate([
29
+ (0, typed_model_1.Prop)(),
30
+ __metadata("design:type", String)
31
+ ], TestSchema1.prototype, "_id", void 0);
32
+ __decorate([
33
+ (0, typed_model_1.Prop)({
34
+ clean: string => `cleaned ${string}`
35
+ }),
36
+ __metadata("design:type", String)
37
+ ], TestSchema1.prototype, "hello", void 0);
38
+ TestSchema1 = __decorate([
39
+ (0, typed_model_1.TypedSchema)()
40
+ ], TestSchema1);
41
+ const Tests = (0, __1.default)({ name: (0, helpers_1.generateId)(), schema: TestSchema1 });
42
+ const result = await Tests.insertAndFind({ hello: 'world' });
43
+ const count = await Tests.estimatedDocumentCount();
44
+ expect(count).toBe(1);
45
+ expect(result).toEqual({
46
+ _id: result._id,
47
+ hello: 'cleaned world'
48
+ });
49
+ });
50
+ });
@@ -42,6 +42,7 @@ const createCollection = (options) => {
42
42
  collection.findOneAndUpdate = (0, getMethods_1.findOneAndUpdate)(collection);
43
43
  collection.insertOne = (0, getMethods_1.insertOne)(collection);
44
44
  collection.insertMany = (0, getMethods_1.insertMany)(collection);
45
+ collection.insertAndFind = (0, getMethods_1.insertAndFind)(collection);
45
46
  collection.updateOne = (0, getMethods_1.updateOne)(collection);
46
47
  collection.updateMany = (0, getMethods_1.updateMany)(collection);
47
48
  collection.deleteMany = (0, getMethods_1.deleteMany)(collection);
@@ -81,6 +81,7 @@ export declare type UpdateItem<ModelClass> = (item: {
81
81
  } & ModelClass, modifier: ModelToUpdateFilter<ModelClass>) => Promise<void>;
82
82
  export declare type InsertOne<ModelClass> = (doc: ModelToDocumentTypeWithIdOptional<ModelClass>, options?: InsertOptions) => Promise<string>;
83
83
  export declare type InsertMany<ModelClass> = (doc: Array<ModelToDocumentTypeWithIdOptional<ModelClass>>, options?: InsertOptions) => Promise<Array<string>>;
84
+ export declare type InsertAndFind<ModelClass> = (doc: ModelToDocumentTypeWithIdOptional<ModelClass>, options?: InsertOptions) => Promise<ModelClass>;
84
85
  export declare type DeleteMany<ModelClass> = (selector: ModelToMongoSelector<ModelClass>, options?: MongoDB.DeleteOptions) => Promise<MongoDB.DeleteResult>;
85
86
  export declare type DeleteOne<ModelClass> = (selector: ModelToMongoSelector<ModelClass>, options?: MongoDB.DeleteOptions) => Promise<MongoDB.DeleteResult>;
86
87
  export declare type UpdateOne<ModelClass> = (selector: ModelToMongoSelector<ModelClass>, modifier: ModelToUpdateFilter<ModelClass>, options?: UpdateOptions) => Promise<MongoDB.UpdateResult>;
@@ -136,6 +137,7 @@ export interface Collection<ModelClass = any> {
136
137
  find: Find<ModelClass>;
137
138
  insertOne: InsertOne<ModelClass>;
138
139
  insertMany: InsertMany<ModelClass>;
140
+ insertAndFind: InsertAndFind<ModelClass>;
139
141
  deleteMany: DeleteMany<ModelClass>;
140
142
  deleteOne: DeleteOne<ModelClass>;
141
143
  updateOne: UpdateOne<ModelClass>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orion-js/mongodb",
3
- "version": "3.2.48",
3
+ "version": "3.2.55",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "files": [
@@ -20,11 +20,11 @@
20
20
  "@orion-js/env": "^3.2.40",
21
21
  "@orion-js/helpers": "^3.2.48",
22
22
  "@orion-js/logger": "^3.2.40",
23
- "@orion-js/models": "^3.2.48",
24
- "@orion-js/resolvers": "^3.2.48",
25
- "@orion-js/schema": "^3.2.40",
23
+ "@orion-js/models": "^3.2.53",
24
+ "@orion-js/resolvers": "^3.2.53",
25
+ "@orion-js/schema": "^3.2.53",
26
26
  "@orion-js/services": "^3.2.48",
27
- "@orion-js/typed-model": "^3.2.48",
27
+ "@orion-js/typed-model": "^3.2.53",
28
28
  "dataloader": "2.1.0",
29
29
  "dot-object": "2.1.4",
30
30
  "mongodb": "4.5.0"
@@ -41,5 +41,5 @@
41
41
  "publishConfig": {
42
42
  "access": "public"
43
43
  },
44
- "gitHead": "ff49b7ffd93c1a72e66c2ed93fdb056360e85e96"
44
+ "gitHead": "ce5164ec1e1f513042436572b0fbf0d4df0fd224"
45
45
  }