@crowdin/app-project-module 0.20.1 → 0.20.3

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.
@@ -16,9 +16,10 @@ function handle(config, integration) {
16
16
  return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () {
17
17
  (0, util_1.log)('Recieved integration logout request', config.logger);
18
18
  if (integration.onLogout) {
19
- const credentials = yield (0, storage_1.getStorage)().getIntegrationCredentials(req.crowdinContext.clientId);
20
- yield integration.onLogout(req.crowdinContext.jwtPayload.context.project_id, req.crowdinApiClient, credentials === null || credentials === void 0 ? void 0 : credentials.credentials, credentials === null || credentials === void 0 ? void 0 : credentials.config);
19
+ (0, util_1.log)('Invoking onLogout hook', config.logger);
20
+ yield integration.onLogout(req.crowdinContext.jwtPayload.context.project_id, req.crowdinApiClient, req.integrationCredentials, req.integrationSettings);
21
21
  }
22
+ (0, util_1.log)(`Deleting integration credentials for ${req.crowdinContext.clientId} client`, config.logger);
22
23
  yield (0, storage_1.getStorage)().deleteIntegrationCredentials(req.crowdinContext.clientId);
23
24
  (0, connection_1.clearCache)(req.crowdinContext.crowdinId);
24
25
  res.status(204).end();
@@ -11,16 +11,35 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  const storage_1 = require("../storage");
13
13
  const util_1 = require("../util");
14
+ const connection_1 = require("../util/connection");
14
15
  function handle(config) {
15
16
  return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () {
16
17
  const event = req.body;
17
18
  (0, util_1.log)(`Recieved uninstall request ${JSON.stringify(event, null, 2)}`, config.logger);
19
+ const projectIntegration = config.projectIntegration;
18
20
  const organization = (event.domain || event.organizationId).toString();
19
- const allCredentials = yield (0, storage_1.getStorage)().getAllIntegrationCredentials(organization);
20
- yield (0, storage_1.getStorage)().deleteCrowdinCredentials(organization);
21
21
  if (config.onUninstall) {
22
+ let allCredentials = [];
23
+ if (projectIntegration) {
24
+ const loadedCredentials = yield (0, storage_1.getStorage)().getAllIntegrationCredentials(organization);
25
+ allCredentials = yield Promise.all(loadedCredentials.map((creds) => __awaiter(this, void 0, void 0, function* () {
26
+ let settings;
27
+ if (creds.config) {
28
+ settings = JSON.parse(creds.config);
29
+ }
30
+ const credentials = yield (0, connection_1.prepareIntegrationCredentials)(config, projectIntegration, creds);
31
+ return { settings, credentials };
32
+ })));
33
+ }
34
+ (0, util_1.log)('Invoking onUninstall hook', config.logger);
22
35
  yield config.onUninstall(organization, allCredentials);
23
36
  }
37
+ if (projectIntegration) {
38
+ (0, util_1.log)(`Deleting all integration credentials for ${organization} organization`, config.logger);
39
+ yield (0, storage_1.getStorage)().deleteAllIntegrationCredentials(organization);
40
+ }
41
+ (0, util_1.log)(`Deleting crowdin credentials for ${organization} organization`, config.logger);
42
+ yield (0, storage_1.getStorage)().deleteCrowdinCredentials(organization);
24
43
  (0, util_1.log)('App has been uninstalled', config.logger);
25
44
  res.status(204).end();
26
45
  }), config.onError);
package/out/index.js CHANGED
@@ -106,7 +106,7 @@ function addCrowdinEndpoints(app, config) {
106
106
  app.get('/api/subscription-info', json_response_1.default, (0, crowdin_client_1.default)(config), (0, subscription_info_1.default)(config));
107
107
  app.post('/api/settings', (0, crowdin_client_1.default)(config), (0, integration_credentials_1.default)(config, integrationLogic), (0, settings_save_1.default)(config));
108
108
  app.post('/api/login', (0, crowdin_client_1.default)(config, false, false), (0, integration_login_1.default)(config, integrationLogic));
109
- app.post('/api/logout', (0, crowdin_client_1.default)(config, false, false), (0, integration_logout_1.default)(config, integrationLogic));
109
+ app.post('/api/logout', (0, crowdin_client_1.default)(config, false, false), (0, integration_credentials_1.default)(config, integrationLogic), (0, integration_logout_1.default)(config, integrationLogic));
110
110
  app.get('/api/crowdin/files', json_response_1.default, (0, crowdin_client_1.default)(config), (0, integration_credentials_1.default)(config, integrationLogic), (0, crowdin_files_1.default)(config, integrationLogic));
111
111
  app.get('/api/crowdin/project', json_response_1.default, (0, crowdin_client_1.default)(config), (0, crowdin_project_1.default)(config));
112
112
  app.get('/api/crowdin/file-progress/:fileId', (0, crowdin_client_1.default)(config), (0, crowdin_file_progress_1.default)(config));
@@ -95,7 +95,10 @@ export interface Config extends ImagePath {
95
95
  /**
96
96
  * Uninstall hook for cleanup logic
97
97
  */
98
- onUninstall?: (organization: string, allCredentials: IntegrationCredentials[]) => Promise<void>;
98
+ onUninstall?: (organization: string, allCredentials: {
99
+ settings?: any;
100
+ credentials: any;
101
+ }[]) => Promise<void>;
99
102
  /**
100
103
  * Error interceptor (can be used to log error in centralized place)
101
104
  */
@@ -1,6 +1,6 @@
1
1
  import { Config, CrowdinCredentials, IntegrationCredentials, IntegrationSyncSettings } from '../models';
2
- export interface Storage<T> {
3
- connect(config: T): Promise<void>;
2
+ export interface Storage {
3
+ migrate(): Promise<void>;
4
4
  saveCrowdinCredentials(credentials: CrowdinCredentials): Promise<void>;
5
5
  updateCrowdinCredentials(credentials: CrowdinCredentials): Promise<void>;
6
6
  getCrowdinCredentials(id: string): Promise<CrowdinCredentials | undefined>;
@@ -12,6 +12,7 @@ export interface Storage<T> {
12
12
  getIntegrationCredentials(id: string): Promise<IntegrationCredentials | undefined>;
13
13
  getAllIntegrationCredentials(crowdinId: string): Promise<IntegrationCredentials[]>;
14
14
  deleteIntegrationCredentials(id: string): Promise<void>;
15
+ deleteAllIntegrationCredentials(crowdinId: string): Promise<void>;
15
16
  saveMetadata(id: string, metadata: any): Promise<void>;
16
17
  updateMetadata(id: string, metadata: any): Promise<void>;
17
18
  getMetadata(id: string): Promise<any | undefined>;
@@ -23,4 +24,4 @@ export interface Storage<T> {
23
24
  getSyncSettings(integrationId: string, crowdinId: string, type: string, provider: string): Promise<IntegrationSyncSettings | undefined>;
24
25
  }
25
26
  export declare function initialize(config: Config): Promise<void>;
26
- export declare function getStorage(): Storage<any>;
27
+ export declare function getStorage(): Storage;
@@ -19,26 +19,20 @@ function initialize(config) {
19
19
  return __awaiter(this, void 0, void 0, function* () {
20
20
  if (config.dbFolder) {
21
21
  (0, util_1.log)('Using SQLite database', config.logger);
22
- const sqlite = new sqlite_1.SQLiteStorage();
23
- storage = sqlite;
24
- yield sqlite.connect({ dbFolder: config.dbFolder });
25
- return;
22
+ storage = new sqlite_1.SQLiteStorage({ dbFolder: config.dbFolder });
26
23
  }
27
24
  if (config.postgreConfig) {
28
25
  (0, util_1.log)('Using PostgreSQL database', config.logger);
29
- const postgre = new postgre_1.PostgreStorage();
30
- storage = postgre;
31
- yield postgre.connect(config.postgreConfig);
32
- return;
26
+ storage = new postgre_1.PostgreStorage(config.postgreConfig);
33
27
  }
34
28
  if (config.mysqlConfig) {
35
29
  (0, util_1.log)('Using MySQL database', config.logger);
36
- const mysql = new mysql_1.MySQLStorage();
37
- storage = mysql;
38
- yield mysql.connect(config.mysqlConfig);
39
- return;
30
+ storage = new mysql_1.MySQLStorage(config.mysqlConfig);
40
31
  }
41
- throw new Error('Database is not configured');
32
+ if (!storage) {
33
+ throw new Error('Database is not configured');
34
+ }
35
+ yield storage.migrate();
42
36
  });
43
37
  }
44
38
  exports.initialize = initialize;
@@ -8,13 +8,16 @@ export interface MySQLStorageConfig {
8
8
  database?: string;
9
9
  port?: number;
10
10
  }
11
- export declare class MySQLStorage implements Storage<MySQLStorageConfig> {
12
- private connection;
11
+ export declare class MySQLStorage implements Storage {
12
+ private mysql;
13
13
  private _res?;
14
14
  private _rej?;
15
15
  private dbPromise;
16
- connect(config: MySQLStorageConfig): Promise<void>;
16
+ private config;
17
+ constructor(config: MySQLStorageConfig);
18
+ executeQuery<T>(command: (connection: any) => Promise<T>): Promise<T>;
17
19
  migrate(): Promise<void>;
20
+ addTables(connection: any): Promise<void>;
18
21
  saveCrowdinCredentials(credentials: CrowdinCredentials): Promise<void>;
19
22
  updateCrowdinCredentials(credentials: CrowdinCredentials): Promise<void>;
20
23
  getCrowdinCredentials(id: string): Promise<CrowdinCredentials | undefined>;
@@ -26,6 +29,7 @@ export declare class MySQLStorage implements Storage<MySQLStorageConfig> {
26
29
  getIntegrationCredentials(id: string): Promise<IntegrationCredentials | undefined>;
27
30
  getAllIntegrationCredentials(crowdinId: string): Promise<IntegrationCredentials[]>;
28
31
  deleteIntegrationCredentials(id: string): Promise<void>;
32
+ deleteAllIntegrationCredentials(crowdinId: string): Promise<void>;
29
33
  saveMetadata(id: string, metadata: any): Promise<void>;
30
34
  updateMetadata(id: string, metadata: any): Promise<void>;
31
35
  getMetadata(id: string): Promise<any>;
@@ -14,18 +14,35 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.MySQLStorage = void 0;
16
16
  class MySQLStorage {
17
- constructor() {
17
+ constructor(config) {
18
+ this.mysql = require('mysql2/promise');
18
19
  this.dbPromise = new Promise((res, rej) => {
19
20
  this._res = res;
20
21
  this._rej = rej;
21
22
  });
23
+ this.config = config;
22
24
  }
23
- connect(config) {
25
+ executeQuery(command) {
24
26
  return __awaiter(this, void 0, void 0, function* () {
27
+ let connection;
25
28
  try {
26
- const mysql = require('mysql2/promise');
27
- this.connection = yield mysql.createConnection(config);
28
- yield this.migrate();
29
+ connection = yield this.mysql.createConnection(this.config);
30
+ const res = yield command(connection);
31
+ yield connection.end();
32
+ return res;
33
+ }
34
+ catch (e) {
35
+ if (connection) {
36
+ yield connection.end();
37
+ }
38
+ throw e;
39
+ }
40
+ });
41
+ }
42
+ migrate() {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ try {
45
+ yield this.executeQuery(this.addTables);
29
46
  this._res && this._res();
30
47
  }
31
48
  catch (e) {
@@ -34,10 +51,9 @@ class MySQLStorage {
34
51
  }
35
52
  });
36
53
  }
37
- migrate() {
38
- var _a, _b, _c, _d;
54
+ addTables(connection) {
39
55
  return __awaiter(this, void 0, void 0, function* () {
40
- yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute(`
56
+ yield connection.execute(`
41
57
  create table if not exists crowdin_credentials
42
58
  (
43
59
  id varchar(255) primary key,
@@ -51,8 +67,8 @@ class MySQLStorage {
51
67
  expire varchar(255) not null,
52
68
  type varchar(255) not null
53
69
  )
54
- `));
55
- yield ((_b = this.connection) === null || _b === void 0 ? void 0 : _b.execute(`
70
+ `);
71
+ yield connection.execute(`
56
72
  create table if not exists integration_credentials
57
73
  (
58
74
  id varchar(255) primary key,
@@ -60,8 +76,8 @@ class MySQLStorage {
60
76
  config text,
61
77
  crowdin_id varchar(255) not null
62
78
  )
63
- `));
64
- yield ((_c = this.connection) === null || _c === void 0 ? void 0 : _c.execute(`
79
+ `);
80
+ yield connection.execute(`
65
81
  create table if not exists sync_settings
66
82
  (
67
83
  id int auto_increment primary key,
@@ -71,21 +87,20 @@ class MySQLStorage {
71
87
  type varchar(255) not null,
72
88
  provider varchar(255) not null
73
89
  )
74
- `));
75
- yield ((_d = this.connection) === null || _d === void 0 ? void 0 : _d.execute(`
90
+ `);
91
+ yield connection.execute(`
76
92
  create table if not exists app_metadata
77
93
  (
78
94
  id varchar(255) primary key,
79
95
  data text
80
96
  )
81
- `));
97
+ `);
82
98
  });
83
99
  }
84
100
  saveCrowdinCredentials(credentials) {
85
- var _a;
86
101
  return __awaiter(this, void 0, void 0, function* () {
87
102
  yield this.dbPromise;
88
- yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('INSERT INTO crowdin_credentials(id, app_secret, domain, user_id, organization_id, base_url, access_token, refresh_token, expire, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [
103
+ yield this.executeQuery(connection => connection.execute('INSERT INTO crowdin_credentials(id, app_secret, domain, user_id, organization_id, base_url, access_token, refresh_token, expire, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [
89
104
  credentials.id,
90
105
  credentials.appSecret,
91
106
  credentials.domain,
@@ -100,10 +115,9 @@ class MySQLStorage {
100
115
  });
101
116
  }
102
117
  updateCrowdinCredentials(credentials) {
103
- var _a;
104
118
  return __awaiter(this, void 0, void 0, function* () {
105
119
  yield this.dbPromise;
106
- yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('UPDATE crowdin_credentials SET app_secret = ?, domain = ?, user_id = ?, organization_id = ?, base_url = ?, access_token = ?, refresh_token = ?, expire = ? WHERE id = ?', [
120
+ yield this.executeQuery(connection => connection.execute('UPDATE crowdin_credentials SET app_secret = ?, domain = ?, user_id = ?, organization_id = ?, base_url = ?, access_token = ?, refresh_token = ?, expire = ? WHERE id = ?', [
107
121
  credentials.appSecret,
108
122
  credentials.domain,
109
123
  credentials.userId,
@@ -117,145 +131,157 @@ class MySQLStorage {
117
131
  });
118
132
  }
119
133
  getCrowdinCredentials(id) {
120
- var _a;
121
134
  return __awaiter(this, void 0, void 0, function* () {
122
135
  yield this.dbPromise;
123
- const [rows,] = yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('SELECT id, app_secret as "appSecret", domain, user_id as "userId", organization_id as "organizationId", base_url as "baseUrl", access_token as "accessToken", refresh_token as "refreshToken", expire, type FROM crowdin_credentials WHERE id = ?', [id]));
124
- return (rows || [])[0];
136
+ return this.executeQuery((connection) => __awaiter(this, void 0, void 0, function* () {
137
+ const [rows,] = yield connection.execute('SELECT id, app_secret as "appSecret", domain, user_id as "userId", organization_id as "organizationId", base_url as "baseUrl", access_token as "accessToken", refresh_token as "refreshToken", expire, type FROM crowdin_credentials WHERE id = ?', [id]);
138
+ return (rows || [])[0];
139
+ }));
125
140
  });
126
141
  }
127
142
  getAllCrowdinCredentials() {
128
- var _a;
129
143
  return __awaiter(this, void 0, void 0, function* () {
130
144
  yield this.dbPromise;
131
- const [rows] = yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('SELECT id, app_secret as "appSecret", domain, user_id as "userId", organization_id as "organizationId", base_url as "baseUrl", access_token as "accessToken", refresh_token as "refreshToken", expire, type FROM crowdin_credentials', []));
132
- return rows || [];
145
+ return this.executeQuery((connection) => __awaiter(this, void 0, void 0, function* () {
146
+ const [rows] = yield connection.execute('SELECT id, app_secret as "appSecret", domain, user_id as "userId", organization_id as "organizationId", base_url as "baseUrl", access_token as "accessToken", refresh_token as "refreshToken", expire, type FROM crowdin_credentials', []);
147
+ return rows || [];
148
+ }));
133
149
  });
134
150
  }
135
151
  deleteCrowdinCredentials(id) {
136
- var _a, _b, _c;
137
152
  return __awaiter(this, void 0, void 0, function* () {
138
153
  yield this.dbPromise;
139
- yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('DELETE FROM crowdin_credentials where id = ?', [id]));
140
- yield ((_b = this.connection) === null || _b === void 0 ? void 0 : _b.execute('DELETE FROM integration_credentials where crowdin_id = ?', [id]));
141
- yield ((_c = this.connection) === null || _c === void 0 ? void 0 : _c.execute('DELETE FROM sync_settings WHERE crowdin_id = ?', [id]));
154
+ yield this.executeQuery((connection) => __awaiter(this, void 0, void 0, function* () {
155
+ yield connection.execute('DELETE FROM crowdin_credentials where id = ?', [id]);
156
+ yield connection.execute('DELETE FROM integration_credentials where crowdin_id = ?', [id]);
157
+ yield connection.execute('DELETE FROM sync_settings WHERE crowdin_id = ?', [id]);
158
+ }));
142
159
  });
143
160
  }
144
161
  saveIntegrationCredentials(id, credentials, crowdinId) {
145
- var _a;
146
162
  return __awaiter(this, void 0, void 0, function* () {
147
163
  yield this.dbPromise;
148
- yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('INSERT INTO integration_credentials(id, credentials, crowdin_id) VALUES (?, ?, ?)', [id, credentials, crowdinId]));
164
+ yield this.executeQuery(connection => connection.execute('INSERT INTO integration_credentials(id, credentials, crowdin_id) VALUES (?, ?, ?)', [
165
+ id,
166
+ credentials,
167
+ crowdinId,
168
+ ]));
149
169
  });
150
170
  }
151
171
  updateIntegrationCredentials(id, credentials) {
152
- var _a;
153
172
  return __awaiter(this, void 0, void 0, function* () {
154
173
  yield this.dbPromise;
155
- yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('UPDATE integration_credentials SET credentials = ? WHERE id = ?', [
156
- credentials,
157
- id,
158
- ]));
174
+ yield this.executeQuery(connection => connection.execute('UPDATE integration_credentials SET credentials = ? WHERE id = ?', [credentials, id]));
159
175
  });
160
176
  }
161
177
  updateIntegrationConfig(id, config) {
162
- var _a;
163
178
  return __awaiter(this, void 0, void 0, function* () {
164
179
  yield this.dbPromise;
165
- yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('UPDATE integration_credentials SET config = ? WHERE id = ?', [config, id]));
180
+ yield this.executeQuery(connection => connection.execute('UPDATE integration_credentials SET config = ? WHERE id = ?', [config, id]));
166
181
  });
167
182
  }
168
183
  getIntegrationCredentials(id) {
169
- var _a;
170
184
  return __awaiter(this, void 0, void 0, function* () {
171
185
  yield this.dbPromise;
172
- const [rows,] = yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('SELECT id, credentials, config, crowdin_id as "crowdinId" FROM integration_credentials WHERE id = ?', [id]));
173
- return (rows || [])[0];
186
+ return this.executeQuery((connection) => __awaiter(this, void 0, void 0, function* () {
187
+ const [rows,] = yield connection.execute('SELECT id, credentials, config, crowdin_id as "crowdinId" FROM integration_credentials WHERE id = ?', [id]);
188
+ return (rows || [])[0];
189
+ }));
174
190
  });
175
191
  }
176
192
  getAllIntegrationCredentials(crowdinId) {
177
- var _a;
178
193
  return __awaiter(this, void 0, void 0, function* () {
179
194
  yield this.dbPromise;
180
- const [rows,] = yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('SELECT id, credentials, config, crowdin_id as "crowdinId" FROM integration_credentials WHERE crowdin_id = ?', [crowdinId]));
181
- return rows || [];
195
+ return this.executeQuery((connection) => __awaiter(this, void 0, void 0, function* () {
196
+ const [rows,] = yield connection.execute('SELECT id, credentials, config, crowdin_id as "crowdinId" FROM integration_credentials WHERE crowdin_id = ?', [crowdinId]);
197
+ return rows || [];
198
+ }));
182
199
  });
183
200
  }
184
201
  deleteIntegrationCredentials(id) {
185
- var _a, _b;
186
202
  return __awaiter(this, void 0, void 0, function* () {
187
203
  yield this.dbPromise;
188
- yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('DELETE FROM integration_credentials where id = ?', [id]));
189
- yield ((_b = this.connection) === null || _b === void 0 ? void 0 : _b.execute('DELETE FROM sync_settings where integration_id = ?', [id]));
204
+ yield this.executeQuery((connection) => __awaiter(this, void 0, void 0, function* () {
205
+ yield connection.execute('DELETE FROM integration_credentials where id = ?', [id]);
206
+ yield connection.execute('DELETE FROM sync_settings where integration_id = ?', [id]);
207
+ }));
208
+ });
209
+ }
210
+ deleteAllIntegrationCredentials(crowdinId) {
211
+ return __awaiter(this, void 0, void 0, function* () {
212
+ yield this.dbPromise;
213
+ yield this.executeQuery((connection) => __awaiter(this, void 0, void 0, function* () {
214
+ yield connection.execute('DELETE FROM integration_credentials where crowdin_id = ?', [crowdinId]);
215
+ yield connection.execute('DELETE FROM sync_settings where crowdin_id = ?', [crowdinId]);
216
+ }));
190
217
  });
191
218
  }
192
219
  saveMetadata(id, metadata) {
193
- var _a;
194
220
  return __awaiter(this, void 0, void 0, function* () {
195
221
  yield this.dbPromise;
196
- yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('INSERT INTO app_metadata(id, data) VALUES (?, ?)', [id, metadata]));
222
+ yield this.executeQuery(connection => connection.execute('INSERT INTO app_metadata(id, data) VALUES (?, ?)', [id, metadata]));
197
223
  });
198
224
  }
199
225
  updateMetadata(id, metadata) {
200
- var _a;
201
226
  return __awaiter(this, void 0, void 0, function* () {
202
227
  yield this.dbPromise;
203
- yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('UPDATE app_metadata SET data = ? WHERE id = ?', [id, metadata]));
228
+ yield this.executeQuery(connection => connection.execute('UPDATE app_metadata SET data = ? WHERE id = ?', [id, metadata]));
204
229
  });
205
230
  }
206
231
  getMetadata(id) {
207
- var _a;
208
232
  return __awaiter(this, void 0, void 0, function* () {
209
233
  yield this.dbPromise;
210
- const [rows] = yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('SELECT data FROM app_metadata WHERE id = ?', [id]));
211
- if (rows && rows[0]) {
212
- return JSON.parse(rows[0].data);
213
- }
234
+ return this.executeQuery((connection) => __awaiter(this, void 0, void 0, function* () {
235
+ const [rows] = yield connection.execute('SELECT data FROM app_metadata WHERE id = ?', [id]);
236
+ if (rows && rows[0]) {
237
+ return JSON.parse(rows[0].data);
238
+ }
239
+ }));
214
240
  });
215
241
  }
216
242
  deleteMetadata(id) {
217
- var _a;
218
243
  return __awaiter(this, void 0, void 0, function* () {
219
244
  yield this.dbPromise;
220
- yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('DELETE FROM app_metadata where id = ?', [id]));
245
+ yield this.executeQuery(connection => connection.execute('DELETE FROM app_metadata where id = ?', [id]));
221
246
  });
222
247
  }
223
248
  getSyncSettingsByProvider(integrationId, provider) {
224
- var _a;
225
249
  return __awaiter(this, void 0, void 0, function* () {
226
250
  yield this.dbPromise;
227
- const [rows,] = yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('SELECT id, files, integration_id as "integrationId", crowdin_id as "crowdinId", type, provider FROM sync_settings WHERE integration_id = ? AND provider = ?', [integrationId, provider]));
228
- return (rows || [])[0];
251
+ return this.executeQuery((connection) => __awaiter(this, void 0, void 0, function* () {
252
+ const [rows,] = yield connection.execute('SELECT id, files, integration_id as "integrationId", crowdin_id as "crowdinId", type, provider FROM sync_settings WHERE integration_id = ? AND provider = ?', [integrationId, provider]);
253
+ return (rows || [])[0];
254
+ }));
229
255
  });
230
256
  }
231
257
  getAllSyncSettingsByType(type) {
232
- var _a;
233
258
  return __awaiter(this, void 0, void 0, function* () {
234
259
  yield this.dbPromise;
235
- const [rows,] = yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('SELECT id, files, integration_id as "integrationId", crowdin_id as "crowdinId", type, provider FROM sync_settings WHERE type = ?', [type]));
236
- return rows || [];
260
+ return this.executeQuery((connection) => __awaiter(this, void 0, void 0, function* () {
261
+ const [rows,] = yield connection.execute('SELECT id, files, integration_id as "integrationId", crowdin_id as "crowdinId", type, provider FROM sync_settings WHERE type = ?', [type]);
262
+ return rows || [];
263
+ }));
237
264
  });
238
265
  }
239
266
  saveSyncSettings(files, integrationId, crowdinId, type, provider) {
240
- var _a;
241
267
  return __awaiter(this, void 0, void 0, function* () {
242
268
  yield this.dbPromise;
243
- yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('INSERT INTO sync_settings(files, integration_id, crowdin_id, type, provider) VALUES (?, , ?, ?, ?)', [files, integrationId, crowdinId, type, provider]));
269
+ yield this.executeQuery(connection => connection.execute('INSERT INTO sync_settings(files, integration_id, crowdin_id, type, provider) VALUES (?, , ?, ?, ?)', [files, integrationId, crowdinId, type, provider]));
244
270
  });
245
271
  }
246
272
  updateSyncSettings(files, integrationId, crowdinId, type, provider) {
247
- var _a;
248
273
  return __awaiter(this, void 0, void 0, function* () {
249
274
  yield this.dbPromise;
250
- yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('UPDATE sync_settings SET files = ? WHERE integration_id = ? AND crowdin_id = ? AND type = ? AND provider = ?', [files, integrationId, crowdinId, type, provider]));
275
+ yield this.executeQuery(connection => connection.execute('UPDATE sync_settings SET files = ? WHERE integration_id = ? AND crowdin_id = ? AND type = ? AND provider = ?', [files, integrationId, crowdinId, type, provider]));
251
276
  });
252
277
  }
253
278
  getSyncSettings(integrationId, crowdinId, type, provider) {
254
- var _a;
255
279
  return __awaiter(this, void 0, void 0, function* () {
256
280
  yield this.dbPromise;
257
- const [rows,] = yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('SELECT id, files, integration_id as "integrationId", crowdin_id as "crowdinId", type FROM sync_settings WHERE integration_id = ? AND crowdin_id = ? AND type = ? AND provider = ?', [integrationId, crowdinId, type, provider]));
258
- return (rows || [])[0];
281
+ return this.executeQuery((connection) => __awaiter(this, void 0, void 0, function* () {
282
+ const [rows,] = yield connection.execute('SELECT id, files, integration_id as "integrationId", crowdin_id as "crowdinId", type FROM sync_settings WHERE integration_id = ? AND crowdin_id = ? AND type = ? AND provider = ?', [integrationId, crowdinId, type, provider]);
283
+ return (rows || [])[0];
284
+ }));
259
285
  });
260
286
  }
261
287
  }
@@ -1,3 +1,4 @@
1
+ import { Client } from 'pg';
1
2
  import { Storage } from '.';
2
3
  import { CrowdinCredentials, IntegrationCredentials, IntegrationSyncSettings } from '../models';
3
4
  export interface PostgreStorageConfig {
@@ -12,13 +13,15 @@ export interface PostgreStorageConfig {
12
13
  idle_in_transaction_session_timeout?: number;
13
14
  connectionTimeoutMillis?: number;
14
15
  }
15
- export declare class PostgreStorage implements Storage<PostgreStorageConfig> {
16
- private client?;
16
+ export declare class PostgreStorage implements Storage {
17
+ private config;
17
18
  private _res?;
18
19
  private _rej?;
19
20
  private dbPromise;
20
- connect(config: PostgreStorageConfig): Promise<void>;
21
+ constructor(config: PostgreStorageConfig);
22
+ executeQuery<T>(command: (client: Client) => Promise<T>): Promise<T>;
21
23
  migrate(): Promise<void>;
24
+ addTables(client: Client): Promise<void>;
22
25
  saveCrowdinCredentials(credentials: CrowdinCredentials): Promise<void>;
23
26
  updateCrowdinCredentials(credentials: CrowdinCredentials): Promise<void>;
24
27
  getCrowdinCredentials(id: string): Promise<CrowdinCredentials | undefined>;
@@ -30,6 +33,7 @@ export declare class PostgreStorage implements Storage<PostgreStorageConfig> {
30
33
  getIntegrationCredentials(id: string): Promise<IntegrationCredentials | undefined>;
31
34
  getAllIntegrationCredentials(crowdinId: string): Promise<IntegrationCredentials[]>;
32
35
  deleteIntegrationCredentials(id: string): Promise<void>;
36
+ deleteAllIntegrationCredentials(crowdinId: string): Promise<void>;
33
37
  saveMetadata(id: string, metadata: any): Promise<void>;
34
38
  updateMetadata(id: string, metadata: any): Promise<void>;
35
39
  getMetadata(id: string): Promise<any>;
@@ -13,18 +13,32 @@ Object.defineProperty(exports, "__esModule", { value: true });
13
13
  exports.PostgreStorage = void 0;
14
14
  const pg_1 = require("pg");
15
15
  class PostgreStorage {
16
- constructor() {
16
+ constructor(config) {
17
17
  this.dbPromise = new Promise((res, rej) => {
18
18
  this._res = res;
19
19
  this._rej = rej;
20
20
  });
21
+ this.config = config;
21
22
  }
22
- connect(config) {
23
+ executeQuery(command) {
23
24
  return __awaiter(this, void 0, void 0, function* () {
24
- this.client = new pg_1.Client(config);
25
+ const client = new pg_1.Client(this.config);
25
26
  try {
26
- yield this.client.connect();
27
- yield this.migrate();
27
+ yield client.connect();
28
+ const res = yield command(client);
29
+ yield client.end();
30
+ return res;
31
+ }
32
+ catch (e) {
33
+ yield client.end();
34
+ throw e;
35
+ }
36
+ });
37
+ }
38
+ migrate() {
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ try {
41
+ yield this.executeQuery(this.addTables);
28
42
  this._res && this._res();
29
43
  }
30
44
  catch (e) {
@@ -33,10 +47,9 @@ class PostgreStorage {
33
47
  }
34
48
  });
35
49
  }
36
- migrate() {
37
- var _a, _b, _c, _d;
50
+ addTables(client) {
38
51
  return __awaiter(this, void 0, void 0, function* () {
39
- yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query(`
52
+ yield client.query(`
40
53
  create table if not exists crowdin_credentials
41
54
  (
42
55
  id varchar primary key,
@@ -50,8 +63,8 @@ class PostgreStorage {
50
63
  expire varchar not null,
51
64
  type varchar not null
52
65
  )
53
- `));
54
- yield ((_b = this.client) === null || _b === void 0 ? void 0 : _b.query(`
66
+ `);
67
+ yield client.query(`
55
68
  create table if not exists integration_credentials
56
69
  (
57
70
  id varchar primary key,
@@ -59,8 +72,8 @@ class PostgreStorage {
59
72
  config varchar,
60
73
  crowdin_id varchar not null
61
74
  )
62
- `));
63
- yield ((_c = this.client) === null || _c === void 0 ? void 0 : _c.query(`
75
+ `);
76
+ yield client.query(`
64
77
  create table if not exists sync_settings
65
78
  (
66
79
  id serial primary key,
@@ -70,21 +83,20 @@ class PostgreStorage {
70
83
  type varchar not null,
71
84
  provider varchar not null
72
85
  )
73
- `));
74
- yield ((_d = this.client) === null || _d === void 0 ? void 0 : _d.query(`
86
+ `);
87
+ yield client.query(`
75
88
  create table if not exists app_metadata
76
89
  (
77
90
  id varchar primary key,
78
91
  data varchar
79
92
  )
80
- `));
93
+ `);
81
94
  });
82
95
  }
83
96
  saveCrowdinCredentials(credentials) {
84
- var _a;
85
97
  return __awaiter(this, void 0, void 0, function* () {
86
98
  yield this.dbPromise;
87
- yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query('INSERT INTO crowdin_credentials(id, app_secret, domain, user_id, organization_id, base_url, access_token, refresh_token, expire, type) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)', [
99
+ yield this.executeQuery(client => client.query('INSERT INTO crowdin_credentials(id, app_secret, domain, user_id, organization_id, base_url, access_token, refresh_token, expire, type) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)', [
88
100
  credentials.id,
89
101
  credentials.appSecret,
90
102
  credentials.domain,
@@ -99,10 +111,9 @@ class PostgreStorage {
99
111
  });
100
112
  }
101
113
  updateCrowdinCredentials(credentials) {
102
- var _a;
103
114
  return __awaiter(this, void 0, void 0, function* () {
104
115
  yield this.dbPromise;
105
- yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query('UPDATE crowdin_credentials SET app_secret = $1, domain = $2, user_id = $3, organization_id = $4, base_url = $5, access_token = $6, refresh_token = $7, expire = $8 WHERE id = $9', [
116
+ yield this.executeQuery(client => client.query('UPDATE crowdin_credentials SET app_secret = $1, domain = $2, user_id = $3, organization_id = $4, base_url = $5, access_token = $6, refresh_token = $7, expire = $8 WHERE id = $9', [
106
117
  credentials.appSecret,
107
118
  credentials.domain,
108
119
  credentials.userId,
@@ -116,145 +127,157 @@ class PostgreStorage {
116
127
  });
117
128
  }
118
129
  getCrowdinCredentials(id) {
119
- var _a;
120
130
  return __awaiter(this, void 0, void 0, function* () {
121
131
  yield this.dbPromise;
122
- const res = yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query('SELECT id, app_secret as "appSecret", domain, user_id as "userId", organization_id as "organizationId", base_url as "baseUrl", access_token as "accessToken", refresh_token as "refreshToken", expire, type FROM crowdin_credentials WHERE id = $1', [id]));
123
- return res === null || res === void 0 ? void 0 : res.rows[0];
132
+ return this.executeQuery((client) => __awaiter(this, void 0, void 0, function* () {
133
+ const res = yield client.query('SELECT id, app_secret as "appSecret", domain, user_id as "userId", organization_id as "organizationId", base_url as "baseUrl", access_token as "accessToken", refresh_token as "refreshToken", expire, type FROM crowdin_credentials WHERE id = $1', [id]);
134
+ return res === null || res === void 0 ? void 0 : res.rows[0];
135
+ }));
124
136
  });
125
137
  }
126
138
  getAllCrowdinCredentials() {
127
- var _a;
128
139
  return __awaiter(this, void 0, void 0, function* () {
129
140
  yield this.dbPromise;
130
- const res = yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query('SELECT id, app_secret as "appSecret", domain, user_id as "userId", organization_id as "organizationId", base_url as "baseUrl", access_token as "accessToken", refresh_token as "refreshToken", expire, type FROM crowdin_credentials', []));
131
- return (res === null || res === void 0 ? void 0 : res.rows) || [];
141
+ return this.executeQuery((client) => __awaiter(this, void 0, void 0, function* () {
142
+ const res = yield client.query('SELECT id, app_secret as "appSecret", domain, user_id as "userId", organization_id as "organizationId", base_url as "baseUrl", access_token as "accessToken", refresh_token as "refreshToken", expire, type FROM crowdin_credentials', []);
143
+ return (res === null || res === void 0 ? void 0 : res.rows) || [];
144
+ }));
132
145
  });
133
146
  }
134
147
  deleteCrowdinCredentials(id) {
135
- var _a, _b, _c;
136
148
  return __awaiter(this, void 0, void 0, function* () {
137
149
  yield this.dbPromise;
138
- yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query('DELETE FROM crowdin_credentials where id = $1', [id]));
139
- yield ((_b = this.client) === null || _b === void 0 ? void 0 : _b.query('DELETE FROM integration_credentials where crowdin_id = $1', [id]));
140
- yield ((_c = this.client) === null || _c === void 0 ? void 0 : _c.query('DELETE FROM sync_settings WHERE crowdin_id = $1', [id]));
150
+ yield this.executeQuery((client) => __awaiter(this, void 0, void 0, function* () {
151
+ yield client.query('DELETE FROM crowdin_credentials where id = $1', [id]);
152
+ yield client.query('DELETE FROM integration_credentials where crowdin_id = $1', [id]);
153
+ yield client.query('DELETE FROM sync_settings WHERE crowdin_id = $1', [id]);
154
+ }));
141
155
  });
142
156
  }
143
157
  saveIntegrationCredentials(id, credentials, crowdinId) {
144
- var _a;
145
158
  return __awaiter(this, void 0, void 0, function* () {
146
159
  yield this.dbPromise;
147
- yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query('INSERT INTO integration_credentials(id, credentials, crowdin_id) VALUES ($1, $2, $3)', [id, credentials, crowdinId]));
160
+ yield this.executeQuery(client => client.query('INSERT INTO integration_credentials(id, credentials, crowdin_id) VALUES ($1, $2, $3)', [
161
+ id,
162
+ credentials,
163
+ crowdinId,
164
+ ]));
148
165
  });
149
166
  }
150
167
  updateIntegrationCredentials(id, credentials) {
151
- var _a;
152
168
  return __awaiter(this, void 0, void 0, function* () {
153
169
  yield this.dbPromise;
154
- yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query('UPDATE integration_credentials SET credentials = $1 WHERE id = $2', [
155
- credentials,
156
- id,
157
- ]));
170
+ yield this.executeQuery(client => client.query('UPDATE integration_credentials SET credentials = $1 WHERE id = $2', [credentials, id]));
158
171
  });
159
172
  }
160
173
  updateIntegrationConfig(id, config) {
161
- var _a;
162
174
  return __awaiter(this, void 0, void 0, function* () {
163
175
  yield this.dbPromise;
164
- yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query('UPDATE integration_credentials SET config = $1 WHERE id = $2', [config, id]));
176
+ yield this.executeQuery(client => client.query('UPDATE integration_credentials SET config = $1 WHERE id = $2', [config, id]));
165
177
  });
166
178
  }
167
179
  getIntegrationCredentials(id) {
168
- var _a;
169
180
  return __awaiter(this, void 0, void 0, function* () {
170
181
  yield this.dbPromise;
171
- const res = yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query('SELECT id, credentials, config, crowdin_id as "crowdinId" FROM integration_credentials WHERE id = $1', [id]));
172
- return res === null || res === void 0 ? void 0 : res.rows[0];
182
+ return this.executeQuery((client) => __awaiter(this, void 0, void 0, function* () {
183
+ const res = yield client.query('SELECT id, credentials, config, crowdin_id as "crowdinId" FROM integration_credentials WHERE id = $1', [id]);
184
+ return res === null || res === void 0 ? void 0 : res.rows[0];
185
+ }));
173
186
  });
174
187
  }
175
188
  getAllIntegrationCredentials(crowdinId) {
176
- var _a;
177
189
  return __awaiter(this, void 0, void 0, function* () {
178
190
  yield this.dbPromise;
179
- const res = yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query('SELECT id, credentials, config, crowdin_id as "crowdinId" FROM integration_credentials WHERE crowdin_id = $1', [crowdinId]));
180
- return (res === null || res === void 0 ? void 0 : res.rows) || [];
191
+ return this.executeQuery((client) => __awaiter(this, void 0, void 0, function* () {
192
+ const res = yield client.query('SELECT id, credentials, config, crowdin_id as "crowdinId" FROM integration_credentials WHERE crowdin_id = $1', [crowdinId]);
193
+ return (res === null || res === void 0 ? void 0 : res.rows) || [];
194
+ }));
181
195
  });
182
196
  }
183
197
  deleteIntegrationCredentials(id) {
184
- var _a, _b;
185
198
  return __awaiter(this, void 0, void 0, function* () {
186
199
  yield this.dbPromise;
187
- yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query('DELETE FROM integration_credentials where id = $1', [id]));
188
- yield ((_b = this.client) === null || _b === void 0 ? void 0 : _b.query('DELETE FROM sync_settings where integration_id = $1', [id]));
200
+ yield this.executeQuery((client) => __awaiter(this, void 0, void 0, function* () {
201
+ yield client.query('DELETE FROM integration_credentials where id = $1', [id]);
202
+ yield client.query('DELETE FROM sync_settings where integration_id = $1', [id]);
203
+ }));
204
+ });
205
+ }
206
+ deleteAllIntegrationCredentials(crowdinId) {
207
+ return __awaiter(this, void 0, void 0, function* () {
208
+ yield this.dbPromise;
209
+ yield this.executeQuery((client) => __awaiter(this, void 0, void 0, function* () {
210
+ yield client.query('DELETE FROM integration_credentials where crowdin_id = $1', [crowdinId]);
211
+ yield client.query('DELETE FROM sync_settings where crowdin_id = $1', [crowdinId]);
212
+ }));
189
213
  });
190
214
  }
191
215
  saveMetadata(id, metadata) {
192
- var _a;
193
216
  return __awaiter(this, void 0, void 0, function* () {
194
217
  yield this.dbPromise;
195
- yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query('INSERT INTO app_metadata(id, data) VALUES ($1, $2)', [id, metadata]));
218
+ yield this.executeQuery(client => client.query('INSERT INTO app_metadata(id, data) VALUES ($1, $2)', [id, metadata]));
196
219
  });
197
220
  }
198
221
  updateMetadata(id, metadata) {
199
- var _a;
200
222
  return __awaiter(this, void 0, void 0, function* () {
201
223
  yield this.dbPromise;
202
- yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query('UPDATE app_metadata SET data = $1 WHERE id = $2', [id, metadata]));
224
+ yield this.executeQuery(client => client.query('UPDATE app_metadata SET data = $1 WHERE id = $2', [id, metadata]));
203
225
  });
204
226
  }
205
227
  getMetadata(id) {
206
- var _a;
207
228
  return __awaiter(this, void 0, void 0, function* () {
208
229
  yield this.dbPromise;
209
- const res = yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query('SELECT data FROM app_metadata WHERE id = $1', [id]));
210
- if (res === null || res === void 0 ? void 0 : res.rows[0]) {
211
- return JSON.parse(res === null || res === void 0 ? void 0 : res.rows[0].data);
212
- }
230
+ return this.executeQuery((client) => __awaiter(this, void 0, void 0, function* () {
231
+ const res = yield client.query('SELECT data FROM app_metadata WHERE id = $1', [id]);
232
+ if (res === null || res === void 0 ? void 0 : res.rows[0]) {
233
+ return JSON.parse(res === null || res === void 0 ? void 0 : res.rows[0].data);
234
+ }
235
+ }));
213
236
  });
214
237
  }
215
238
  deleteMetadata(id) {
216
- var _a;
217
239
  return __awaiter(this, void 0, void 0, function* () {
218
240
  yield this.dbPromise;
219
- yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query('DELETE FROM app_metadata where id = $1', [id]));
241
+ yield this.executeQuery(client => client.query('DELETE FROM app_metadata where id = $1', [id]));
220
242
  });
221
243
  }
222
244
  getSyncSettingsByProvider(integrationId, provider) {
223
- var _a;
224
245
  return __awaiter(this, void 0, void 0, function* () {
225
246
  yield this.dbPromise;
226
- const res = yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query('SELECT id, files, integration_id as "integrationId", crowdin_id as "crowdinId", type, provider FROM sync_settings WHERE integration_id = $1 AND provider = $2', [integrationId, provider]));
227
- return res === null || res === void 0 ? void 0 : res.rows[0];
247
+ return this.executeQuery((client) => __awaiter(this, void 0, void 0, function* () {
248
+ const res = yield client.query('SELECT id, files, integration_id as "integrationId", crowdin_id as "crowdinId", type, provider FROM sync_settings WHERE integration_id = $1 AND provider = $2', [integrationId, provider]);
249
+ return res === null || res === void 0 ? void 0 : res.rows[0];
250
+ }));
228
251
  });
229
252
  }
230
253
  getAllSyncSettingsByType(type) {
231
- var _a;
232
254
  return __awaiter(this, void 0, void 0, function* () {
233
255
  yield this.dbPromise;
234
- const res = yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query('SELECT id, files, integration_id as "integrationId", crowdin_id as "crowdinId", type, provider FROM sync_settings WHERE type = $1', [type]));
235
- return (res === null || res === void 0 ? void 0 : res.rows) || [];
256
+ return this.executeQuery((client) => __awaiter(this, void 0, void 0, function* () {
257
+ const res = yield client.query('SELECT id, files, integration_id as "integrationId", crowdin_id as "crowdinId", type, provider FROM sync_settings WHERE type = $1', [type]);
258
+ return (res === null || res === void 0 ? void 0 : res.rows) || [];
259
+ }));
236
260
  });
237
261
  }
238
262
  saveSyncSettings(files, integrationId, crowdinId, type, provider) {
239
- var _a;
240
263
  return __awaiter(this, void 0, void 0, function* () {
241
264
  yield this.dbPromise;
242
- yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query('INSERT INTO sync_settings(files, integration_id, crowdin_id, type, provider) VALUES ($1, $2, $3, $4, $5)', [files, integrationId, crowdinId, type, provider]));
265
+ yield this.executeQuery(client => client.query('INSERT INTO sync_settings(files, integration_id, crowdin_id, type, provider) VALUES ($1, $2, $3, $4, $5)', [files, integrationId, crowdinId, type, provider]));
243
266
  });
244
267
  }
245
268
  updateSyncSettings(files, integrationId, crowdinId, type, provider) {
246
- var _a;
247
269
  return __awaiter(this, void 0, void 0, function* () {
248
270
  yield this.dbPromise;
249
- yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query('UPDATE sync_settings SET files = $1 WHERE integration_id = $2 AND crowdin_id = $3 AND type = $4 AND provider = $5', [files, integrationId, crowdinId, type, provider]));
271
+ yield this.executeQuery(client => client.query('UPDATE sync_settings SET files = $1 WHERE integration_id = $2 AND crowdin_id = $3 AND type = $4 AND provider = $5', [files, integrationId, crowdinId, type, provider]));
250
272
  });
251
273
  }
252
274
  getSyncSettings(integrationId, crowdinId, type, provider) {
253
- var _a;
254
275
  return __awaiter(this, void 0, void 0, function* () {
255
276
  yield this.dbPromise;
256
- const res = yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query('SELECT id, files, integration_id as "integrationId", crowdin_id as "crowdinId", type FROM sync_settings WHERE integration_id = $1 AND crowdin_id = $2 AND type = $3 AND provider = $4', [integrationId, crowdinId, type, provider]));
257
- return res === null || res === void 0 ? void 0 : res.rows[0];
277
+ return this.executeQuery((client) => __awaiter(this, void 0, void 0, function* () {
278
+ const res = yield client.query('SELECT id, files, integration_id as "integrationId", crowdin_id as "crowdinId", type FROM sync_settings WHERE integration_id = $1 AND crowdin_id = $2 AND type = $3 AND provider = $4', [integrationId, crowdinId, type, provider]);
279
+ return res === null || res === void 0 ? void 0 : res.rows[0];
280
+ }));
258
281
  });
259
282
  }
260
283
  }
@@ -3,17 +3,19 @@ import { CrowdinCredentials, IntegrationCredentials, IntegrationSyncSettings } f
3
3
  export interface SQLiteStorageConfig {
4
4
  dbFolder: string;
5
5
  }
6
- export declare class SQLiteStorage implements Storage<SQLiteStorageConfig> {
6
+ export declare class SQLiteStorage implements Storage {
7
7
  private db;
8
8
  private _res?;
9
9
  private _rej?;
10
10
  private dbPromise;
11
+ private config;
12
+ constructor(config: SQLiteStorageConfig);
11
13
  private _run;
12
14
  private run;
13
15
  private get;
14
16
  private each;
15
- private migrate;
16
- connect(config: SQLiteStorageConfig): Promise<void>;
17
+ private addColumns;
18
+ migrate(): Promise<void>;
17
19
  saveCrowdinCredentials(credentials: CrowdinCredentials): Promise<void>;
18
20
  updateCrowdinCredentials(credentials: CrowdinCredentials): Promise<void>;
19
21
  getCrowdinCredentials(id: string): Promise<CrowdinCredentials | undefined>;
@@ -25,6 +27,7 @@ export declare class SQLiteStorage implements Storage<SQLiteStorageConfig> {
25
27
  getIntegrationCredentials(id: string): Promise<IntegrationCredentials | undefined>;
26
28
  getAllIntegrationCredentials(crowdinId: string): Promise<IntegrationCredentials[]>;
27
29
  deleteIntegrationCredentials(id: string): Promise<void>;
30
+ deleteAllIntegrationCredentials(crowdinId: string): Promise<void>;
28
31
  saveMetadata(id: string, metadata: any): Promise<void>;
29
32
  updateMetadata(id: string, metadata: any): Promise<void>;
30
33
  getMetadata(id: string): Promise<any>;
@@ -16,11 +16,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.SQLiteStorage = void 0;
17
17
  const path_1 = require("path");
18
18
  class SQLiteStorage {
19
- constructor() {
19
+ constructor(config) {
20
20
  this.dbPromise = new Promise((res, rej) => {
21
21
  this._res = res;
22
22
  this._rej = rej;
23
23
  });
24
+ this.config = config;
24
25
  }
25
26
  _run(query, params) {
26
27
  return __awaiter(this, void 0, void 0, function* () {
@@ -91,7 +92,7 @@ class SQLiteStorage {
91
92
  });
92
93
  });
93
94
  }
94
- migrate() {
95
+ addColumns() {
95
96
  return __awaiter(this, void 0, void 0, function* () {
96
97
  const newColumns = ['app_secret', 'domain', 'user_id', 'organization_id', 'base_url'];
97
98
  const crowdinCredentialsInfo = yield this.each('PRAGMA table_info(crowdin_credentials);', []);
@@ -107,7 +108,7 @@ class SQLiteStorage {
107
108
  }
108
109
  });
109
110
  }
110
- connect(config) {
111
+ migrate() {
111
112
  return __awaiter(this, void 0, void 0, function* () {
112
113
  let _connection_res;
113
114
  let _connection_rej;
@@ -117,7 +118,7 @@ class SQLiteStorage {
117
118
  });
118
119
  const sqlite = require('sqlite3');
119
120
  //@ts-ignore
120
- this.db = new sqlite.Database((0, path_1.join)(config.dbFolder, 'app.sqlite'), error => {
121
+ this.db = new sqlite.Database((0, path_1.join)(this.config.dbFolder, 'app.sqlite'), error => {
121
122
  if (error) {
122
123
  _connection_rej(error);
123
124
  }
@@ -171,7 +172,7 @@ class SQLiteStorage {
171
172
  `, []);
172
173
  this._res && this._res();
173
174
  // TODO: temporary code
174
- yield this.migrate();
175
+ yield this.addColumns();
175
176
  }
176
177
  catch (e) {
177
178
  this._rej && this._rej(e);
@@ -253,6 +254,12 @@ class SQLiteStorage {
253
254
  yield this.run('DELETE FROM sync_settings where integration_id = ?', [id]);
254
255
  });
255
256
  }
257
+ deleteAllIntegrationCredentials(crowdinId) {
258
+ return __awaiter(this, void 0, void 0, function* () {
259
+ yield this.run('DELETE FROM integration_credentials where crowdin_id = ?', [crowdinId]);
260
+ yield this.run('DELETE FROM sync_settings where crowdin_id = ?', [crowdinId]);
261
+ });
262
+ }
256
263
  saveMetadata(id, metadata) {
257
264
  return this.run('INSERT INTO app_metadata(id, data) VALUES (?, ?)', [id, JSON.stringify(metadata)]);
258
265
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crowdin/app-project-module",
3
- "version": "0.20.1",
3
+ "version": "0.20.3",
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",