@orion-js/mongodb 3.0.20 → 3.0.27

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.
@@ -4,8 +4,10 @@ import upsert from './upsert';
4
4
  import findOneAndUpdate from './findOneAndUpdate';
5
5
  import updateOne from './updateOne';
6
6
  import updateMany from './updateMany';
7
+ import updateAndFind from './updateAndFind';
7
8
  import deleteOne from './deleteOne';
8
9
  import deleteMany from './deleteMany';
9
10
  import insertOne from './insertOne';
10
11
  import insertMany from './insertMany';
11
- export { findOne, find, findOneAndUpdate, insertOne, insertMany, updateOne, updateMany, deleteOne, deleteMany, upsert };
12
+ import updateItem from './updateItem';
13
+ export { findOne, find, findOneAndUpdate, insertOne, insertMany, updateOne, updateMany, updateAndFind, updateItem, deleteOne, deleteMany, upsert };
@@ -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.updateMany = exports.updateOne = exports.insertMany = exports.insertOne = exports.findOneAndUpdate = exports.find = exports.findOne = void 0;
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;
7
7
  const find_1 = __importDefault(require("./find"));
8
8
  exports.find = find_1.default;
9
9
  const findOne_1 = __importDefault(require("./findOne"));
@@ -16,6 +16,8 @@ const updateOne_1 = __importDefault(require("./updateOne"));
16
16
  exports.updateOne = updateOne_1.default;
17
17
  const updateMany_1 = __importDefault(require("./updateMany"));
18
18
  exports.updateMany = updateMany_1.default;
19
+ const updateAndFind_1 = __importDefault(require("./updateAndFind"));
20
+ exports.updateAndFind = updateAndFind_1.default;
19
21
  const deleteOne_1 = __importDefault(require("./deleteOne"));
20
22
  exports.deleteOne = deleteOne_1.default;
21
23
  const deleteMany_1 = __importDefault(require("./deleteMany"));
@@ -24,3 +26,5 @@ const insertOne_1 = __importDefault(require("./insertOne"));
24
26
  exports.insertOne = insertOne_1.default;
25
27
  const insertMany_1 = __importDefault(require("./insertMany"));
26
28
  exports.insertMany = insertMany_1.default;
29
+ const updateItem_1 = __importDefault(require("./updateItem"));
30
+ exports.updateItem = updateItem_1.default;
@@ -0,0 +1,3 @@
1
+ import { Collection, UpdateAndFind } from '../../types';
2
+ declare const _default: <DocumentType>(collection: Partial<Collection>) => UpdateAndFind<DocumentType>;
3
+ export default _default;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = (collection) => {
4
+ const updateAndFind = async function (selector, modifier, options = {}) {
5
+ return collection.findOneAndUpdate(selector, modifier, {
6
+ ...options,
7
+ mongoOptions: {
8
+ ...options.mongoOptions,
9
+ returnDocument: 'after'
10
+ }
11
+ });
12
+ };
13
+ return updateAndFind;
14
+ };
@@ -0,0 +1,38 @@
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 helpers_1 = require("@orion-js/helpers");
16
+ const typed_model_1 = require("@orion-js/typed-model");
17
+ const __1 = __importDefault(require(".."));
18
+ it('update and finds the item and doesnt replaces the variable using updateAndFind', async () => {
19
+ let Item = class Item {
20
+ };
21
+ __decorate([
22
+ (0, typed_model_1.Prop)(),
23
+ __metadata("design:type", String)
24
+ ], Item.prototype, "_id", void 0);
25
+ __decorate([
26
+ (0, typed_model_1.Prop)(),
27
+ __metadata("design:type", String)
28
+ ], Item.prototype, "text", void 0);
29
+ Item = __decorate([
30
+ (0, typed_model_1.TypedModel)()
31
+ ], Item);
32
+ const Tests = (0, __1.default)({ name: (0, helpers_1.generateId)(), model: Item });
33
+ await Tests.insertOne({ text: 'hello' });
34
+ const oldItem = await Tests.findOne();
35
+ const newItem = await Tests.updateAndFind(oldItem._id, { $set: { text: 'world' } });
36
+ expect(oldItem.text).toBe('hello');
37
+ expect(newItem.text).toBe('world');
38
+ });
@@ -0,0 +1,3 @@
1
+ import { Collection, UpdateItem } from '../../types';
2
+ declare const _default: <DocumentType>(collection: Partial<Collection>) => UpdateItem<DocumentType>;
3
+ export default _default;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = (collection) => {
4
+ const updateItem = async function (item, modifier) {
5
+ const updated = await collection.updateAndFind({ _id: item._id }, modifier);
6
+ for (const key in item) {
7
+ delete item[key];
8
+ }
9
+ for (const key in updated) {
10
+ item[key] = updated[key];
11
+ }
12
+ };
13
+ return updateItem;
14
+ };
@@ -0,0 +1,38 @@
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 helpers_1 = require("@orion-js/helpers");
16
+ const typed_model_1 = require("@orion-js/typed-model");
17
+ const __1 = __importDefault(require(".."));
18
+ it('updates the item variable using updateItem', async () => {
19
+ let Item = class Item {
20
+ };
21
+ __decorate([
22
+ (0, typed_model_1.Prop)(),
23
+ __metadata("design:type", String)
24
+ ], Item.prototype, "_id", void 0);
25
+ __decorate([
26
+ (0, typed_model_1.Prop)(),
27
+ __metadata("design:type", String)
28
+ ], Item.prototype, "text", void 0);
29
+ Item = __decorate([
30
+ (0, typed_model_1.TypedModel)()
31
+ ], Item);
32
+ const Tests = (0, __1.default)({ name: (0, helpers_1.generateId)(), model: Item });
33
+ await Tests.insertOne({ text: 'hello' });
34
+ const item = await Tests.findOne();
35
+ expect(item.text).toBe('hello');
36
+ await Tests.updateItem(item, { $set: { text: 'world' } });
37
+ expect(item.text).toBe('world');
38
+ });
@@ -30,7 +30,7 @@ const createCollection = (options) => {
30
30
  rawCollection,
31
31
  generateId: (0, generateId_1.default)(options),
32
32
  getSchema: () => {
33
- const schema = (0, lodash_1.cloneDeep)(model.getSchema());
33
+ const schema = (0, lodash_1.cloneDeep)(model.getCleanSchema());
34
34
  if (!schema._id) {
35
35
  schema._id = {
36
36
  type: 'ID'
@@ -52,6 +52,9 @@ const createCollection = (options) => {
52
52
  collection.deleteMany = (0, getMethods_1.deleteMany)(collection);
53
53
  collection.deleteOne = (0, getMethods_1.deleteOne)(collection);
54
54
  collection.upsert = (0, getMethods_1.upsert)(collection);
55
+ // update and find
56
+ collection.updateAndFind = (0, getMethods_1.updateAndFind)(collection);
57
+ collection.updateItem = (0, getMethods_1.updateItem)(collection);
55
58
  // plain passed methods
56
59
  collection.aggregate = (pipeline, options) => collection.rawCollection.aggregate(pipeline, options);
57
60
  collection.watch = (pipeline, options) => collection.rawCollection.watch(pipeline, options);
@@ -68,6 +68,10 @@ export declare type InitItem<ModelClass> = (doc: MongoDB.Document) => ModelClass
68
68
  export declare type FindOne<ModelClass> = (selector?: ModelToMongoSelector<ModelClass>, options?: MongoDB.FindOptions) => Promise<ModelClass>;
69
69
  export declare type Find<ModelClass> = (selector?: ModelToMongoSelector<ModelClass>, options?: MongoDB.FindOptions) => FindCursor<ModelClass>;
70
70
  export declare type FindOneAndUpdate<ModelClass> = (selector: ModelToMongoSelector<ModelClass>, modifier: ModelToUpdateFilter<ModelClass>, options?: FindOneAndUpdateUpdateOptions) => Promise<ModelClass>;
71
+ export declare type UpdateAndFind<ModelClass> = (selector: ModelToMongoSelector<ModelClass>, modifier: ModelToUpdateFilter<ModelClass>, options?: FindOneAndUpdateUpdateOptions) => Promise<ModelClass>;
72
+ export declare type UpdateItem<ModelClass> = (item: {
73
+ _id: string;
74
+ } & ModelClass, modifier: ModelToUpdateFilter<ModelClass>) => Promise<void>;
71
75
  export declare type InsertOne<ModelClass> = (doc: ModelToDocumentTypeWithoutId<ModelClass>, options?: InsertOptions) => Promise<string>;
72
76
  export declare type InsertMany<ModelClass> = (doc: Array<ModelToDocumentTypeWithoutId<ModelClass>>, options?: InsertOptions) => Promise<Array<string>>;
73
77
  export declare type DeleteMany<ModelClass> = (selector: ModelToMongoSelector<ModelClass>, options?: MongoDB.DeleteOptions) => Promise<MongoDB.DeleteResult>;
@@ -104,6 +108,8 @@ export interface Collection<ModelClass = any> {
104
108
  updateMany: UpdateMany<ModelClass>;
105
109
  upsert: Upsert<ModelClass>;
106
110
  findOneAndUpdate: FindOneAndUpdate<ModelClass>;
111
+ updateAndFind: UpdateAndFind<ModelClass>;
112
+ updateItem: UpdateItem<ModelClass>;
107
113
  aggregate: <T = MongoDB.Document>(pipeline?: MongoDB.Document[], options?: MongoDB.AggregateOptions) => MongoDB.AggregationCursor<T>;
108
114
  watch: <T = MongoDB.Document>(pipeline?: MongoDB.Document[], options?: MongoDB.ChangeStreamOptions) => MongoDB.ChangeStream<T>;
109
115
  loadData: DataLoader.LoadData<ModelClass>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orion-js/mongodb",
3
- "version": "3.0.20",
3
+ "version": "3.0.27",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "files": [
@@ -18,10 +18,10 @@
18
18
  },
19
19
  "dependencies": {
20
20
  "@orion-js/helpers": "^3.0.17",
21
- "@orion-js/models": "^3.0.20",
22
- "@orion-js/resolvers": "^3.0.17",
21
+ "@orion-js/models": "^3.0.27",
22
+ "@orion-js/resolvers": "^3.0.24",
23
23
  "@orion-js/schema": "^3.0.17",
24
- "@orion-js/typed-model": "^3.0.20",
24
+ "@orion-js/typed-model": "^3.0.27",
25
25
  "dataloader": "2.0.0",
26
26
  "dot-object": "2.1.4",
27
27
  "mongodb": "4.1.4"
@@ -38,5 +38,5 @@
38
38
  "publishConfig": {
39
39
  "access": "public"
40
40
  },
41
- "gitHead": "ed37da2a1a61a9a4c0b9935d77e29d83a423e199"
41
+ "gitHead": "084f56bda6733955cdc640b3c8adf1731ec95af4"
42
42
  }