@crowdin/app-project-module 2.8.0 → 2.9.0

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.
@@ -8,7 +8,7 @@ export declare class DrizzleStorage implements Storage {
8
8
  protected readonly schema: typeof POSTGRESQL_SCHEMA;
9
9
  protected readonly type: DB_TYPE;
10
10
  protected readonly migrationPromise: Promise<void>;
11
- protected migrated: boolean;
11
+ private _delayedMigrationPromise;
12
12
  protected readonly config: Config | UnauthorizedConfig;
13
13
  constructor({ config, type, delayedMigration, }: {
14
14
  config: Config | UnauthorizedConfig;
@@ -16,6 +16,8 @@ export declare class DrizzleStorage implements Storage {
16
16
  delayedMigration?: boolean;
17
17
  });
18
18
  migrateDelayed(): Promise<void>;
19
+ private _cascadeDelete;
20
+ private _executeMigration;
19
21
  saveCrowdinCredentials(credentials: CrowdinCredentials): Promise<void>;
20
22
  updateCrowdinCredentials(credentials: CrowdinCredentials): Promise<void>;
21
23
  getCrowdinCredentials(id: string): Promise<CrowdinCredentials | undefined>;
@@ -31,7 +31,6 @@ class DrizzleStorage {
31
31
  // schema: typeof D1_SCHEMA;
32
32
  constructor({ config, type, delayedMigration, }) {
33
33
  var _a, _b, _c, _d, _e;
34
- this.migrated = false;
35
34
  let db;
36
35
  if (type === 'd1') {
37
36
  db = (0, d1_1.initD1)(Object.assign(Object.assign({}, config.d1Config), { additionalSchema: (_a = config.customTables) === null || _a === void 0 ? void 0 : _a.schema }));
@@ -47,14 +46,14 @@ class DrizzleStorage {
47
46
  directoryPath: config.migrateToPostgreFromSQLite ? config.dbFolder : undefined,
48
47
  additionalMigrationFolder: (_c = config.customTables) === null || _c === void 0 ? void 0 : _c.migrationFolder,
49
48
  });
50
- this.migrated = true;
49
+ this._delayedMigrationPromise = Promise.resolve();
51
50
  }
52
51
  else {
53
52
  db = (0, sqlite_1.initSqlite)(config.dbFolder, (_d = config.customTables) === null || _d === void 0 ? void 0 : _d.schema);
54
53
  this.migrationPromise = delayedMigration
55
54
  ? Promise.resolve()
56
55
  : (0, sqlite_1.migrateSqlite)(db, (_e = config.customTables) === null || _e === void 0 ? void 0 : _e.migrationFolder);
57
- this.migrated = true;
56
+ this._delayedMigrationPromise = Promise.resolve();
58
57
  }
59
58
  // using cast here to avoid ts errors, since all drivers should have the same methods, just different clients
60
59
  this.db = db;
@@ -64,26 +63,55 @@ class DrizzleStorage {
64
63
  }
65
64
  migrateDelayed() {
66
65
  return __awaiter(this, void 0, void 0, function* () {
67
- var _a, _b, _c;
68
- if (!this.migrated) {
69
- if (this.type === 'd1') {
70
- yield (0, d1_1.migrateD1)(this.db, (_a = this.config.customTables) === null || _a === void 0 ? void 0 : _a.d1Migrations);
71
- }
72
- else if (this.type === 'postgresql') {
73
- yield (0, postgresql_1.migratePostgreSQL)({
74
- db: this.db,
75
- name: this.config.name,
76
- directoryPath: this.config.migrateToPostgreFromSQLite ? this.config.dbFolder : undefined,
77
- additionalMigrationFolder: (_b = this.config.customTables) === null || _b === void 0 ? void 0 : _b.migrationFolder,
78
- });
79
- }
80
- else {
81
- yield (0, sqlite_1.migrateSqlite)(this.db, (_c = this.config.customTables) === null || _c === void 0 ? void 0 : _c.migrationFolder);
82
- }
83
- this.migrated = true;
66
+ if (!this._delayedMigrationPromise) {
67
+ this._delayedMigrationPromise = this._executeMigration();
68
+ }
69
+ return this._delayedMigrationPromise;
70
+ });
71
+ }
72
+ // Each backend needs a different atomic delete strategy:
73
+ // - better-sqlite3: synchronous transaction (async callbacks are not awaited by the driver)
74
+ // - D1: batch API (Workers runtime does not support BEGIN/COMMIT via the JS binding)
75
+ // - PostgreSQL: standard async transaction
76
+ _cascadeDelete(pairs) {
77
+ return __awaiter(this, void 0, void 0, function* () {
78
+ if (this.type === 'sqlite') {
79
+ this.db.transaction((tx) => {
80
+ for (const { table, condition } of pairs) {
81
+ tx.delete(table).where(condition).run();
82
+ }
83
+ });
84
+ }
85
+ else if (this.type === 'd1') {
86
+ const d1Db = this.db;
87
+ yield d1Db.batch(pairs.map(({ table, condition }) => d1Db.delete(table).where(condition)));
88
+ }
89
+ else {
90
+ yield this.db.transaction((tx) => __awaiter(this, void 0, void 0, function* () {
91
+ for (const { table, condition } of pairs) {
92
+ yield tx.delete(table).where(condition);
93
+ }
94
+ }));
84
95
  }
85
96
  });
86
97
  }
98
+ _executeMigration() {
99
+ var _a, _b, _c;
100
+ if (this.type === 'd1') {
101
+ return (0, d1_1.migrateD1)(this.db, (_a = this.config.customTables) === null || _a === void 0 ? void 0 : _a.d1Migrations);
102
+ }
103
+ else if (this.type === 'postgresql') {
104
+ return (0, postgresql_1.migratePostgreSQL)({
105
+ db: this.db,
106
+ name: this.config.name,
107
+ directoryPath: this.config.migrateToPostgreFromSQLite ? this.config.dbFolder : undefined,
108
+ additionalMigrationFolder: (_b = this.config.customTables) === null || _b === void 0 ? void 0 : _b.migrationFolder,
109
+ });
110
+ }
111
+ else {
112
+ return (0, sqlite_1.migrateSqlite)(this.db, (_c = this.config.customTables) === null || _c === void 0 ? void 0 : _c.migrationFolder);
113
+ }
114
+ }
87
115
  saveCrowdinCredentials(credentials) {
88
116
  return __awaiter(this, void 0, void 0, function* () {
89
117
  yield this.migrationPromise;
@@ -168,22 +196,23 @@ class DrizzleStorage {
168
196
  deleteCrowdinCredentials(id) {
169
197
  return __awaiter(this, void 0, void 0, function* () {
170
198
  yield this.migrationPromise;
171
- yield this.db.delete(this.schema.crowdinCredentials).where((0, drizzle_orm_1.eq)(this.schema.crowdinCredentials.id, id));
172
- yield this.db
173
- .delete(this.schema.integrationCredentials)
174
- .where((0, drizzle_orm_1.eq)(this.schema.integrationCredentials.crowdin_id, id));
175
- yield this.db.delete(this.schema.syncSettings).where((0, drizzle_orm_1.eq)(this.schema.syncSettings.crowdin_id, id));
176
- yield this.db.delete(this.schema.appMetadata).where((0, drizzle_orm_1.eq)(this.schema.appMetadata.crowdin_id, id));
177
- yield this.db.delete(this.schema.filesSnapshot).where((0, drizzle_orm_1.eq)(this.schema.filesSnapshot.crowdin_id, id));
178
- yield this.db.delete(this.schema.webhooks).where((0, drizzle_orm_1.eq)(this.schema.webhooks.crowdin_id, id));
179
- yield this.db.delete(this.schema.userErrors).where((0, drizzle_orm_1.eq)(this.schema.userErrors.crowdin_id, id));
180
- yield this.db.delete(this.schema.integrationSettings).where((0, drizzle_orm_1.eq)(this.schema.integrationSettings.crowdin_id, id));
181
- yield this.db.delete(this.schema.job).where((0, drizzle_orm_1.eq)(this.schema.job.crowdin_id, id));
182
- yield this.db
183
- .delete(this.schema.translationFileCache)
184
- .where((0, drizzle_orm_1.eq)(this.schema.translationFileCache.crowdin_id, id));
185
- yield this.db.delete(this.schema.unsyncedFiles).where((0, drizzle_orm_1.eq)(this.schema.unsyncedFiles.crowdin_id, id));
186
- yield this.db.delete(this.schema.syncedData).where((0, drizzle_orm_1.eq)(this.schema.syncedData.crowdin_id, id));
199
+ yield this._cascadeDelete([
200
+ { table: this.schema.crowdinCredentials, condition: (0, drizzle_orm_1.eq)(this.schema.crowdinCredentials.id, id) },
201
+ {
202
+ table: this.schema.integrationCredentials,
203
+ condition: (0, drizzle_orm_1.eq)(this.schema.integrationCredentials.crowdin_id, id),
204
+ },
205
+ { table: this.schema.syncSettings, condition: (0, drizzle_orm_1.eq)(this.schema.syncSettings.crowdin_id, id) },
206
+ { table: this.schema.appMetadata, condition: (0, drizzle_orm_1.eq)(this.schema.appMetadata.crowdin_id, id) },
207
+ { table: this.schema.filesSnapshot, condition: (0, drizzle_orm_1.eq)(this.schema.filesSnapshot.crowdin_id, id) },
208
+ { table: this.schema.webhooks, condition: (0, drizzle_orm_1.eq)(this.schema.webhooks.crowdin_id, id) },
209
+ { table: this.schema.userErrors, condition: (0, drizzle_orm_1.eq)(this.schema.userErrors.crowdin_id, id) },
210
+ { table: this.schema.integrationSettings, condition: (0, drizzle_orm_1.eq)(this.schema.integrationSettings.crowdin_id, id) },
211
+ { table: this.schema.job, condition: (0, drizzle_orm_1.eq)(this.schema.job.crowdin_id, id) },
212
+ { table: this.schema.translationFileCache, condition: (0, drizzle_orm_1.eq)(this.schema.translationFileCache.crowdin_id, id) },
213
+ { table: this.schema.unsyncedFiles, condition: (0, drizzle_orm_1.eq)(this.schema.unsyncedFiles.crowdin_id, id) },
214
+ { table: this.schema.syncedData, condition: (0, drizzle_orm_1.eq)(this.schema.syncedData.crowdin_id, id) },
215
+ ]);
187
216
  });
188
217
  }
189
218
  saveIntegrationCredentials(id, credentials, crowdinId) {
@@ -248,41 +277,50 @@ class DrizzleStorage {
248
277
  deleteIntegrationCredentials(id) {
249
278
  return __awaiter(this, void 0, void 0, function* () {
250
279
  yield this.migrationPromise;
251
- yield this.db.delete(this.schema.integrationCredentials).where((0, drizzle_orm_1.eq)(this.schema.integrationCredentials.id, id));
252
- yield this.db.delete(this.schema.syncSettings).where((0, drizzle_orm_1.eq)(this.schema.syncSettings.integration_id, id));
253
- yield this.db.delete(this.schema.filesSnapshot).where((0, drizzle_orm_1.eq)(this.schema.filesSnapshot.integration_id, id));
254
- yield this.db.delete(this.schema.webhooks).where((0, drizzle_orm_1.eq)(this.schema.webhooks.integration_id, id));
255
- yield this.db.delete(this.schema.userErrors).where((0, drizzle_orm_1.eq)(this.schema.userErrors.integration_id, id));
256
- yield this.db
257
- .delete(this.schema.integrationSettings)
258
- .where((0, drizzle_orm_1.eq)(this.schema.integrationSettings.integration_id, id));
259
- yield this.db.delete(this.schema.job).where((0, drizzle_orm_1.eq)(this.schema.job.integration_id, id));
260
- yield this.db
261
- .delete(this.schema.translationFileCache)
262
- .where((0, drizzle_orm_1.eq)(this.schema.translationFileCache.integration_id, id));
263
- yield this.db.delete(this.schema.unsyncedFiles).where((0, drizzle_orm_1.eq)(this.schema.unsyncedFiles.integration_id, id));
264
- yield this.db.delete(this.schema.syncedData).where((0, drizzle_orm_1.eq)(this.schema.syncedData.integration_id, id));
280
+ yield this._cascadeDelete([
281
+ { table: this.schema.integrationCredentials, condition: (0, drizzle_orm_1.eq)(this.schema.integrationCredentials.id, id) },
282
+ { table: this.schema.syncSettings, condition: (0, drizzle_orm_1.eq)(this.schema.syncSettings.integration_id, id) },
283
+ { table: this.schema.filesSnapshot, condition: (0, drizzle_orm_1.eq)(this.schema.filesSnapshot.integration_id, id) },
284
+ { table: this.schema.webhooks, condition: (0, drizzle_orm_1.eq)(this.schema.webhooks.integration_id, id) },
285
+ { table: this.schema.userErrors, condition: (0, drizzle_orm_1.eq)(this.schema.userErrors.integration_id, id) },
286
+ {
287
+ table: this.schema.integrationSettings,
288
+ condition: (0, drizzle_orm_1.eq)(this.schema.integrationSettings.integration_id, id),
289
+ },
290
+ { table: this.schema.job, condition: (0, drizzle_orm_1.eq)(this.schema.job.integration_id, id) },
291
+ {
292
+ table: this.schema.translationFileCache,
293
+ condition: (0, drizzle_orm_1.eq)(this.schema.translationFileCache.integration_id, id),
294
+ },
295
+ { table: this.schema.unsyncedFiles, condition: (0, drizzle_orm_1.eq)(this.schema.unsyncedFiles.integration_id, id) },
296
+ { table: this.schema.syncedData, condition: (0, drizzle_orm_1.eq)(this.schema.syncedData.integration_id, id) },
297
+ ]);
265
298
  });
266
299
  }
267
300
  deleteAllIntegrationCredentials(crowdinId) {
268
301
  return __awaiter(this, void 0, void 0, function* () {
269
302
  yield this.migrationPromise;
270
- yield this.db
271
- .delete(this.schema.integrationCredentials)
272
- .where((0, drizzle_orm_1.eq)(this.schema.integrationCredentials.crowdin_id, crowdinId));
273
- yield this.db.delete(this.schema.syncSettings).where((0, drizzle_orm_1.eq)(this.schema.syncSettings.crowdin_id, crowdinId));
274
- yield this.db.delete(this.schema.filesSnapshot).where((0, drizzle_orm_1.eq)(this.schema.filesSnapshot.crowdin_id, crowdinId));
275
- yield this.db.delete(this.schema.webhooks).where((0, drizzle_orm_1.eq)(this.schema.webhooks.crowdin_id, crowdinId));
276
- yield this.db.delete(this.schema.userErrors).where((0, drizzle_orm_1.eq)(this.schema.userErrors.crowdin_id, crowdinId));
277
- yield this.db
278
- .delete(this.schema.integrationSettings)
279
- .where((0, drizzle_orm_1.eq)(this.schema.integrationSettings.crowdin_id, crowdinId));
280
- yield this.db.delete(this.schema.job).where((0, drizzle_orm_1.eq)(this.schema.job.crowdin_id, crowdinId));
281
- yield this.db
282
- .delete(this.schema.translationFileCache)
283
- .where((0, drizzle_orm_1.eq)(this.schema.translationFileCache.crowdin_id, crowdinId));
284
- yield this.db.delete(this.schema.unsyncedFiles).where((0, drizzle_orm_1.eq)(this.schema.unsyncedFiles.crowdin_id, crowdinId));
285
- yield this.db.delete(this.schema.syncedData).where((0, drizzle_orm_1.eq)(this.schema.syncedData.crowdin_id, crowdinId));
303
+ yield this._cascadeDelete([
304
+ {
305
+ table: this.schema.integrationCredentials,
306
+ condition: (0, drizzle_orm_1.eq)(this.schema.integrationCredentials.crowdin_id, crowdinId),
307
+ },
308
+ { table: this.schema.syncSettings, condition: (0, drizzle_orm_1.eq)(this.schema.syncSettings.crowdin_id, crowdinId) },
309
+ { table: this.schema.filesSnapshot, condition: (0, drizzle_orm_1.eq)(this.schema.filesSnapshot.crowdin_id, crowdinId) },
310
+ { table: this.schema.webhooks, condition: (0, drizzle_orm_1.eq)(this.schema.webhooks.crowdin_id, crowdinId) },
311
+ { table: this.schema.userErrors, condition: (0, drizzle_orm_1.eq)(this.schema.userErrors.crowdin_id, crowdinId) },
312
+ {
313
+ table: this.schema.integrationSettings,
314
+ condition: (0, drizzle_orm_1.eq)(this.schema.integrationSettings.crowdin_id, crowdinId),
315
+ },
316
+ { table: this.schema.job, condition: (0, drizzle_orm_1.eq)(this.schema.job.crowdin_id, crowdinId) },
317
+ {
318
+ table: this.schema.translationFileCache,
319
+ condition: (0, drizzle_orm_1.eq)(this.schema.translationFileCache.crowdin_id, crowdinId),
320
+ },
321
+ { table: this.schema.unsyncedFiles, condition: (0, drizzle_orm_1.eq)(this.schema.unsyncedFiles.crowdin_id, crowdinId) },
322
+ { table: this.schema.syncedData, condition: (0, drizzle_orm_1.eq)(this.schema.syncedData.crowdin_id, crowdinId) },
323
+ ]);
286
324
  });
287
325
  }
288
326
  saveMetadata(id, metadata, crowdinId) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crowdin/app-project-module",
3
- "version": "2.8.0",
3
+ "version": "2.9.0",
4
4
  "description": "Module that generates for you all common endpoints for serving standalone Crowdin App",
5
5
  "main": "out/index.js",
6
6
  "types": "out/index.d.ts",