starta.microservice 0.1.8804 → 0.1.8942

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,9 +1,18 @@
1
1
  import { Db } from 'mongodb';
2
2
  import RedisService from '../../../adapters/redisService';
3
+ import { AbstractCursor, Collection } from 'mongodb';
4
+ type CollectionMigration = {
5
+ collectionName: string;
6
+ getFindQuery: (c: Collection) => object;
7
+ action: (item: any) => Promise<void>;
8
+ batchSize?: number;
9
+ };
3
10
  declare abstract class BaseMigration {
4
11
  db: Db;
5
12
  redis: RedisService;
6
13
  constructor(db: Db, redis: RedisService);
7
14
  abstract up(): any;
15
+ protected performMigration(version: string, migrations: Array<CollectionMigration>): Promise<void>;
16
+ protected processCursor(collectionName: string, getItemsCursor: (c: Collection) => AbstractCursor, recordsUpdated: number, action: (item: any) => Promise<void>, version: string, batchSize?: number): Promise<number>;
8
17
  }
9
18
  export default BaseMigration;
@@ -1,10 +1,69 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ const executionContext_1 = __importDefault(require("../../../executionContext"));
3
7
  class BaseMigration {
4
8
  constructor(db, redis) {
5
9
  this.db = db;
6
10
  this.redis = redis;
7
11
  }
12
+ async performMigration(version, migrations) {
13
+ let totalRecordsUpdated = 0;
14
+ for (let index = 0; index < migrations.length; index++) {
15
+ const migration = migrations[index];
16
+ totalRecordsUpdated += await this.processCursor(migration.collectionName, (c) => c.find({
17
+ ...migration.getFindQuery(c),
18
+ [version]: {
19
+ $exists: false,
20
+ },
21
+ }), totalRecordsUpdated, async (techcard) => {
22
+ techcard.V198 = true;
23
+ techcard.numberToPrepare = 1;
24
+ }, version);
25
+ }
26
+ const unsetPromises = migrations.map((m) => this.db.collection(m.collectionName).updateMany({
27
+ [version]: 1,
28
+ }, {
29
+ $unset: {
30
+ [version]: 1,
31
+ },
32
+ }));
33
+ await Promise.all(unsetPromises);
34
+ }
35
+ async processCursor(collectionName, getItemsCursor, recordsUpdated, action, version, batchSize = 30) {
36
+ const cursor = getItemsCursor(this.db.collection(collectionName));
37
+ let promises = [];
38
+ const incrementRecords = () => recordsUpdated++;
39
+ while (await cursor.hasNext()) {
40
+ const cursorItem = await cursor.next();
41
+ await action(cursorItem);
42
+ promises.push((async () => {
43
+ await this.db
44
+ .collection(collectionName)
45
+ .replaceOne({ _id: cursorItem._id }, cursorItem);
46
+ const records = incrementRecords();
47
+ if (records % 2500 === 0) {
48
+ executionContext_1.default
49
+ .getLogger()
50
+ .generic.warn(version +
51
+ ': updated ' +
52
+ records +
53
+ ' records. Currently updating: ' +
54
+ collectionName);
55
+ }
56
+ })());
57
+ if (promises.length >= batchSize) {
58
+ await Promise.all(promises);
59
+ promises = [];
60
+ }
61
+ }
62
+ if (promises.length > 0) {
63
+ await Promise.all(promises);
64
+ }
65
+ return recordsUpdated;
66
+ }
8
67
  }
9
68
  exports.default = BaseMigration;
10
69
  //# sourceMappingURL=BaseMigration.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"BaseMigration.js","sourceRoot":"","sources":["../../../../src/adapters/database/DBScripts/BaseMigration.ts"],"names":[],"mappings":";;AAGA,MAAe,aAAa;IAK1B,YAAY,EAAM,EAAE,KAAmB;QACrC,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;CAGF;AAED,kBAAe,aAAa,CAAC"}
1
+ {"version":3,"file":"BaseMigration.js","sourceRoot":"","sources":["../../../../src/adapters/database/DBScripts/BaseMigration.ts"],"names":[],"mappings":";;;;;AAGA,iFAAyD;AASzD,MAAe,aAAa;IAK1B,YAAY,EAAM,EAAE,KAAmB;QACrC,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;I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IAAI,QAAQ,GAAG,EAAE,CAAC;QAClB,MAAM,gBAAgB,GAAG,GAAG,EAAE,CAAC,cAAc,EAAE,CAAC;QAEhD,OAAO,MAAM,MAAM,CAAC,OAAO,EAAE,EAAE;YAC7B,MAAM,UAAU,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;YACvC,MAAM,MAAM,CAAC,UAAU,CAAC,CAAC;YAEzB,QAAQ,CAAC,IAAI,CACX,CAAC,KAAK,IAAI,EAAE;gBACV,MAAM,IAAI,CAAC,EAAE;qBACV,UAAU,CAAC,cAAc,CAAC;qBAC1B,UAAU,CAAC,EAAE,GAAG,EAAE,UAAU,CAAC,GAAG,EAAE,EAAE,UAAU,CAAC,CAAC;gBAEnD,MAAM,OAAO,GAAG,gBAAgB,EAAE,CAAC;gBACnC,IAAI,OAAO,GAAG,IAAI,KAAK,CAAC,EAAE;oBACxB,0BAAgB;yBACb,SAAS,EAAE;yBACX,OAAO,CAAC,IAAI,CACX,OAAO;wBACL,YAAY;wBACZ,OAAO;wBACP,gCAAgC;wBAChC,cAAc,CACjB,CAAC;iBACL;YACH,CAAC,CAAC,EAAE,CACL,CAAC;YAEF,IAAI,QAAQ,CAAC,MAAM,IAAI,SAAS,EAAE;gBAChC,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;gBAC5B,QAAQ,GAAG,EAAE,CAAC;aACf;SACF;QAED,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE;YACvB,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;SAC7B;QAED,OAAO,cAAc,CAAC;IACxB,CAAC;CACF;AAED,kBAAe,aAAa,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starta.microservice",
3
- "version": "0.1.8804",
3
+ "version": "0.1.8942",
4
4
  "main": "./lib/index.js",
5
5
  "description": "Core library for Starta.one microservice",
6
6
  "author": "Collaboracia OÜ",