@flink-app/flink 0.12.1-alpha.4 → 0.12.1-alpha.6

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.
@@ -1,5 +1,12 @@
1
1
  import { Collection, Db, Document, ObjectId } from "mongodb";
2
2
  import { FlinkContext } from "./FlinkContext";
3
+ /**
4
+ * Partial model to have intellisense for partial updates but
5
+ * also allow any other properties to be set such as nested objects etc.
6
+ */
7
+ type PartialModel<Model> = Partial<Model> & {
8
+ [x: string]: any;
9
+ };
3
10
  export declare abstract class FlinkRepo<C extends FlinkContext, Model extends Document> {
4
11
  private collectionName;
5
12
  private db;
@@ -14,9 +21,10 @@ export declare abstract class FlinkRepo<C extends FlinkContext, Model extends Do
14
21
  create<C = Omit<Model, "_id">>(model: C): Promise<C & {
15
22
  _id: string;
16
23
  }>;
17
- updateOne(id: string | ObjectId, model: Partial<Model>): Promise<Model | null>;
18
- updateMany<U = Partial<Model>>(query: any, model: U): Promise<number>;
24
+ updateOne(id: string | ObjectId, model: PartialModel<Model>): Promise<Model | null>;
25
+ updateMany<U = PartialModel<Model>>(query: any, model: U): Promise<number>;
19
26
  deleteById(id: string | ObjectId): Promise<number>;
20
27
  private buildId;
21
28
  private objectIdToString;
22
29
  }
30
+ export {};
@@ -46,6 +46,17 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
46
46
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
47
  }
48
48
  };
49
+ var __rest = (this && this.__rest) || function (s, e) {
50
+ var t = {};
51
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
52
+ t[p] = s[p];
53
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
54
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
55
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
56
+ t[p[i]] = s[p[i]];
57
+ }
58
+ return t;
59
+ };
49
60
  Object.defineProperty(exports, "__esModule", { value: true });
50
61
  exports.FlinkRepo = void 0;
51
62
  var mongodb_1 = require("mongodb");
@@ -129,12 +140,13 @@ var FlinkRepo = /** @class */ (function () {
129
140
  };
130
141
  FlinkRepo.prototype.updateOne = function (id, model) {
131
142
  return __awaiter(this, void 0, void 0, function () {
132
- var oid, res;
143
+ var oid, _id, modelWithoutId, res;
133
144
  return __generator(this, function (_a) {
134
145
  switch (_a.label) {
135
146
  case 0:
136
147
  oid = this.buildId(id);
137
- return [4 /*yield*/, this.collection.updateOne({ _id: oid }, { $set: model })];
148
+ _id = model._id, modelWithoutId = __rest(model, ["_id"]);
149
+ return [4 /*yield*/, this.collection.updateOne({ _id: oid }, { $set: modelWithoutId })];
138
150
  case 1:
139
151
  _a.sent();
140
152
  return [4 /*yield*/, this.collection.findOne({ _id: oid })];
@@ -150,14 +162,16 @@ var FlinkRepo = /** @class */ (function () {
150
162
  };
151
163
  FlinkRepo.prototype.updateMany = function (query, model) {
152
164
  return __awaiter(this, void 0, void 0, function () {
153
- var modifiedCount;
154
- return __generator(this, function (_a) {
155
- switch (_a.label) {
156
- case 0: return [4 /*yield*/, this.collection.updateMany(query, {
157
- $set: model,
158
- })];
165
+ var _a, _id, modelWithoutId, modifiedCount;
166
+ return __generator(this, function (_b) {
167
+ switch (_b.label) {
168
+ case 0:
169
+ _a = model, _id = _a._id, modelWithoutId = __rest(_a, ["_id"]);
170
+ return [4 /*yield*/, this.collection.updateMany(query, {
171
+ $set: modelWithoutId,
172
+ })];
159
173
  case 1:
160
- modifiedCount = (_a.sent()).modifiedCount;
174
+ modifiedCount = (_b.sent()).modifiedCount;
161
175
  return [2 /*return*/, modifiedCount];
162
176
  }
163
177
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flink-app/flink",
3
- "version": "0.12.1-alpha.4",
3
+ "version": "0.12.1-alpha.6",
4
4
  "description": "Typescript only framework for creating REST-like APIs on top of Express and mongodb",
5
5
  "types": "dist/src/index.d.ts",
6
6
  "main": "dist/src/index.js",
@@ -66,5 +66,5 @@
66
66
  "rimraf": "^3.0.2",
67
67
  "ts-node": "^9.1.1"
68
68
  },
69
- "gitHead": "8f06a4ab98e7179322d36546756d4305fd196f5a"
69
+ "gitHead": "ac1dabd317c41e0fee321e574f6aa97c5fed7830"
70
70
  }
@@ -4,6 +4,7 @@ import { FlinkRepo } from "../src/FlinkRepo";
4
4
  interface Model {
5
5
  _id: string;
6
6
  name: string;
7
+ nested?: { field: number };
7
8
  }
8
9
 
9
10
  class Repo extends FlinkRepo<any, Model> {}
@@ -61,9 +62,19 @@ describe("FlinkRepo", () => {
61
62
 
62
63
  const updatedDoc = await repo.updateOne(createdDoc._id + "", {
63
64
  name: "foo",
65
+ "nested.field": 1,
64
66
  });
65
67
 
66
68
  expect(updatedDoc).toBeDefined();
67
69
  expect(updatedDoc?.name).toBe("foo");
70
+ expect(updatedDoc?.nested?.field).toBe(1);
71
+ });
72
+
73
+ it("should update many documents", async () => {
74
+ await collection.insertMany([{ name: "foo" }, { name: "foo" }, { name: "foo" }]);
75
+
76
+ const updatedCount = await repo.updateMany({ name: "foo" }, { name: "bar", "nested.field": 1 });
77
+
78
+ expect(updatedCount).toBe(3);
68
79
  });
69
80
  });
package/src/FlinkRepo.ts CHANGED
@@ -1,6 +1,12 @@
1
1
  import { Collection, Db, Document, InsertOneResult, ObjectId } from "mongodb";
2
2
  import { FlinkContext } from "./FlinkContext";
3
3
 
4
+ /**
5
+ * Partial model to have intellisense for partial updates but
6
+ * also allow any other properties to be set such as nested objects etc.
7
+ */
8
+ type PartialModel<Model> = Partial<Model> & { [x: string]: any };
9
+
4
10
  export abstract class FlinkRepo<C extends FlinkContext, Model extends Document> {
5
11
  collection: Collection;
6
12
 
@@ -45,10 +51,12 @@ export abstract class FlinkRepo<C extends FlinkContext, Model extends Document>
45
51
  return { ...model, _id: result.insertedId.toString() };
46
52
  }
47
53
 
48
- async updateOne(id: string | ObjectId, model: Partial<Model>): Promise<Model | null> {
54
+ async updateOne(id: string | ObjectId, model: PartialModel<Model>): Promise<Model | null> {
49
55
  const oid = this.buildId(id);
50
56
 
51
- await this.collection.updateOne({ _id: oid }, { $set: model });
57
+ const { _id, ...modelWithoutId } = model;
58
+
59
+ await this.collection.updateOne({ _id: oid }, { $set: modelWithoutId });
52
60
 
53
61
  const res = await this.collection.findOne<Model>({ _id: oid });
54
62
 
@@ -58,9 +66,11 @@ export abstract class FlinkRepo<C extends FlinkContext, Model extends Document>
58
66
  return null;
59
67
  }
60
68
 
61
- async updateMany<U = Partial<Model>>(query: any, model: U): Promise<number> {
69
+ async updateMany<U = PartialModel<Model>>(query: any, model: U): Promise<number> {
70
+ const { _id, ...modelWithoutId } = model as any;
71
+
62
72
  const { modifiedCount } = await this.collection.updateMany(query, {
63
- $set: model as any,
73
+ $set: modelWithoutId as any,
64
74
  });
65
75
  return modifiedCount;
66
76
  }