@flink-app/flink 0.12.1-alpha.3 → 0.12.1-alpha.5

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.
@@ -171,6 +171,7 @@ export declare class FlinkApp<C extends FlinkContext> {
171
171
  private rawContentTypes?;
172
172
  private schedulingOptions?;
173
173
  private disableHttpServer;
174
+ private expressServer;
174
175
  private repos;
175
176
  /**
176
177
  * Internal cache used to track registered handlers and potentially any overlapping routes
@@ -181,6 +182,7 @@ export declare class FlinkApp<C extends FlinkContext> {
181
182
  constructor(opts: FlinkOptions);
182
183
  get ctx(): C;
183
184
  start(): Promise<this>;
185
+ stop(): Promise<void>;
184
186
  /**
185
187
  * Manually registers a handler.
186
188
  *
@@ -231,7 +231,7 @@ var FlinkApp = /** @class */ (function () {
231
231
  this.started = true;
232
232
  }
233
233
  else {
234
- (_d = this.expressApp) === null || _d === void 0 ? void 0 : _d.listen(this.port, function () {
234
+ this.expressServer = (_d = this.expressApp) === null || _d === void 0 ? void 0 : _d.listen(this.port, function () {
235
235
  FlinkLog_1.log.fontColorLog("magenta", "\u26A1\uFE0F HTTP server '".concat(_this.name, "' is running and waiting for connections on ").concat(_this.port));
236
236
  _this.started = true;
237
237
  });
@@ -241,6 +241,36 @@ var FlinkApp = /** @class */ (function () {
241
241
  });
242
242
  });
243
243
  };
244
+ FlinkApp.prototype.stop = function () {
245
+ return __awaiter(this, void 0, void 0, function () {
246
+ var _this = this;
247
+ return __generator(this, function (_a) {
248
+ switch (_a.label) {
249
+ case 0:
250
+ FlinkLog_1.log.info("🛑 Stopping Flink app...");
251
+ if (!this.scheduler) return [3 /*break*/, 2];
252
+ return [4 /*yield*/, this.scheduler.stop()];
253
+ case 1:
254
+ _a.sent();
255
+ _a.label = 2;
256
+ case 2:
257
+ if (this.expressServer) {
258
+ return [2 /*return*/, new Promise(function (resolve, reject) {
259
+ var int = setTimeout(function () {
260
+ reject("Failed to stop HTTP server in time");
261
+ }, 2000);
262
+ _this.expressServer.close(function () {
263
+ clearInterval(int);
264
+ FlinkLog_1.log.info("HTTP server stopped");
265
+ resolve();
266
+ });
267
+ })];
268
+ }
269
+ return [2 /*return*/];
270
+ }
271
+ });
272
+ });
273
+ };
244
274
  /**
245
275
  * Manually registers a handler.
246
276
  *
@@ -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 {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flink-app/flink",
3
- "version": "0.12.1-alpha.3",
3
+ "version": "0.12.1-alpha.5",
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": "51b6524e233acb2430953ffaed6382909f34db8e"
69
+ "gitHead": "1ff7b8cf9b30287d3be72af96c5767e90a562fbf"
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/FlinkApp.ts CHANGED
@@ -234,6 +234,7 @@ export class FlinkApp<C extends FlinkContext> {
234
234
  private rawContentTypes?: string[];
235
235
  private schedulingOptions?: FlinkOptions["scheduling"];
236
236
  private disableHttpServer = false;
237
+ private expressServer: any; // for simplicity, we don't want to import types from express/node here
237
238
 
238
239
  private repos: { [x: string]: FlinkRepo<C, any> } = {};
239
240
 
@@ -367,7 +368,7 @@ export class FlinkApp<C extends FlinkContext> {
367
368
  log.info("🚧 HTTP server is disabled, but flink app is running");
368
369
  this.started = true;
369
370
  } else {
370
- this.expressApp?.listen(this.port, () => {
371
+ this.expressServer = this.expressApp?.listen(this.port, () => {
371
372
  log.fontColorLog("magenta", `⚡️ HTTP server '${this.name}' is running and waiting for connections on ${this.port}`);
372
373
  this.started = true;
373
374
  });
@@ -376,6 +377,28 @@ export class FlinkApp<C extends FlinkContext> {
376
377
  return this;
377
378
  }
378
379
 
380
+ async stop() {
381
+ log.info("🛑 Stopping Flink app...");
382
+
383
+ if (this.scheduler) {
384
+ await this.scheduler.stop();
385
+ }
386
+
387
+ if (this.expressServer) {
388
+ return new Promise<void>((resolve, reject) => {
389
+ const int = setTimeout(() => {
390
+ reject("Failed to stop HTTP server in time");
391
+ }, 2000);
392
+
393
+ this.expressServer.close(() => {
394
+ clearInterval(int);
395
+ log.info("HTTP server stopped");
396
+ resolve();
397
+ });
398
+ });
399
+ }
400
+ }
401
+
379
402
  /**
380
403
  * Manually registers a handler.
381
404
  *
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,7 +51,7 @@ 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
57
  await this.collection.updateOne({ _id: oid }, { $set: model });
@@ -58,7 +64,7 @@ export abstract class FlinkRepo<C extends FlinkContext, Model extends Document>
58
64
  return null;
59
65
  }
60
66
 
61
- async updateMany<U = Partial<Model>>(query: any, model: U): Promise<number> {
67
+ async updateMany<U = PartialModel<Model>>(query: any, model: U): Promise<number> {
62
68
  const { modifiedCount } = await this.collection.updateMany(query, {
63
69
  $set: model as any,
64
70
  });