@crowdin/app-project-module 0.17.9 → 0.18.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.
- package/README.md +27 -0
- package/out/handlers/install.js +3 -3
- package/out/handlers/integration-login.js +3 -3
- package/out/handlers/integration-logout.js +1 -1
- package/out/handlers/settings-save.js +1 -1
- package/out/handlers/sync-settings-save.js +3 -3
- package/out/handlers/sync-settings.js +1 -1
- package/out/handlers/uninstall.js +1 -1
- package/out/index.js +9 -9
- package/out/middlewares/crowdin-client.js +1 -1
- package/out/middlewares/integration-credentials.js +1 -1
- package/out/middlewares/ui-module.js +1 -1
- package/out/models/index.d.ts +6 -1
- package/out/storage/index.d.ts +26 -22
- package/out/storage/index.js +29 -308
- package/out/storage/postgre.d.ts +42 -0
- package/out/storage/postgre.js +261 -0
- package/out/storage/sqlite.d.ts +37 -0
- package/out/storage/sqlite.js +297 -0
- package/out/util/connection.js +2 -2
- package/out/util/cron.js +5 -5
- package/out/util/index.js +1 -1
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -27,6 +27,9 @@ In both options you will need to provide Crowdin App configuration file. Please
|
|
|
27
27
|
- [Authorization](#authorization)
|
|
28
28
|
- [Custom login form](#customize-your-app-login-form)
|
|
29
29
|
- [OAuth2 login](#oauth2-support)
|
|
30
|
+
- [Storage](#storage)
|
|
31
|
+
- [SQLite](#sqlite)
|
|
32
|
+
- [PostgreSQL](#postgresql)
|
|
30
33
|
- [Settings window](#settings-window)
|
|
31
34
|
- [Info window](#info-window)
|
|
32
35
|
- [Background tasks](#background-tasks)
|
|
@@ -314,6 +317,28 @@ configuration.projectIntegration.oauthLogin = {
|
|
|
314
317
|
|
|
315
318
|
Please refer to jsdoc for more details.
|
|
316
319
|
|
|
320
|
+
## Storage
|
|
321
|
+
|
|
322
|
+
Module can be configured to use following storages:
|
|
323
|
+
|
|
324
|
+
### SQLite
|
|
325
|
+
|
|
326
|
+
```javascript
|
|
327
|
+
//specify folder where sqlite db file will be located
|
|
328
|
+
configuration.dbFolder = __dirname;
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
### PostgreSQL
|
|
332
|
+
|
|
333
|
+
```javascript
|
|
334
|
+
configuration.postgreConfig = {
|
|
335
|
+
host: 'localhost',
|
|
336
|
+
user: 'postgres',
|
|
337
|
+
password: 'password',
|
|
338
|
+
database: 'test'
|
|
339
|
+
};
|
|
340
|
+
```
|
|
341
|
+
|
|
317
342
|
## Settings window
|
|
318
343
|
|
|
319
344
|
It is also possible to define settings window for your app where users can customize integration flow.
|
|
@@ -480,6 +505,7 @@ const configuration = {
|
|
|
480
505
|
dbFolder: __dirname,
|
|
481
506
|
imagePath: __dirname + '/' + 'logo.png',
|
|
482
507
|
customFileFormat: {
|
|
508
|
+
filesFolder: __dirname,
|
|
483
509
|
type: 'type-xyz',
|
|
484
510
|
multilingual: false,
|
|
485
511
|
autoUploadTranslations: true, //useful when single language format
|
|
@@ -530,6 +556,7 @@ const configuration = {
|
|
|
530
556
|
dbFolder: __dirname,
|
|
531
557
|
imagePath: __dirname + '/' + 'logo.png',
|
|
532
558
|
customFileFormat: {
|
|
559
|
+
filesFolder: __dirname,
|
|
533
560
|
type: 'type-xyz',
|
|
534
561
|
stringsExport: true,
|
|
535
562
|
extensions: [
|
package/out/handlers/install.js
CHANGED
|
@@ -31,13 +31,13 @@ function handle(config) {
|
|
|
31
31
|
expire: (new Date().getTime() / 1000 + token.expiresIn).toString(),
|
|
32
32
|
type: event.domain ? models_1.AccountType.ENTERPRISE : models_1.AccountType.NORMAL,
|
|
33
33
|
};
|
|
34
|
-
const existingCredentials = yield (0, storage_1.
|
|
34
|
+
const existingCredentials = yield (0, storage_1.getStorage)().getCrowdinCredentials(credentials.id);
|
|
35
35
|
if (!!existingCredentials) {
|
|
36
|
-
yield (0, storage_1.
|
|
36
|
+
yield (0, storage_1.getStorage)().updateCrowdinCredentials(credentials);
|
|
37
37
|
(0, util_1.log)('An existing App has been reinstalled', config.logger);
|
|
38
38
|
}
|
|
39
39
|
else {
|
|
40
|
-
yield (0, storage_1.
|
|
40
|
+
yield (0, storage_1.getStorage)().saveCrowdinCredentials(credentials);
|
|
41
41
|
(0, util_1.log)('A new App has been installed', config.logger);
|
|
42
42
|
}
|
|
43
43
|
res.status(204).end();
|
|
@@ -18,12 +18,12 @@ function handle(config, integration) {
|
|
|
18
18
|
(0, util_1.log)('Checking the integration credentials', config.logger);
|
|
19
19
|
yield integration.checkConnection(req.body.credentials);
|
|
20
20
|
}
|
|
21
|
-
const existing = yield (0, storage_1.
|
|
21
|
+
const existing = yield (0, storage_1.getStorage)().getIntegrationCredentials(req.crowdinContext.clientId);
|
|
22
22
|
if (!!existing) {
|
|
23
23
|
(0, util_1.log)('Deleting old credentials', config.logger);
|
|
24
|
-
yield (0, storage_1.
|
|
24
|
+
yield (0, storage_1.getStorage)().deleteIntegrationCredentials(req.crowdinContext.clientId);
|
|
25
25
|
}
|
|
26
|
-
yield (0, storage_1.
|
|
26
|
+
yield (0, storage_1.getStorage)().saveIntegrationCredentials(req.crowdinContext.clientId, (0, util_1.encryptData)(config, JSON.stringify(req.body.credentials)), req.crowdinContext.crowdinId);
|
|
27
27
|
res.status(204).end();
|
|
28
28
|
}), config.onError);
|
|
29
29
|
}
|
|
@@ -15,7 +15,7 @@ const connection_1 = require("../util/connection");
|
|
|
15
15
|
function handle(config) {
|
|
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
|
-
yield (0, storage_1.
|
|
18
|
+
yield (0, storage_1.getStorage)().deleteIntegrationCredentials(req.crowdinContext.clientId);
|
|
19
19
|
(0, connection_1.clearCache)(req.crowdinContext.crowdinId);
|
|
20
20
|
res.status(204).end();
|
|
21
21
|
}), config.onError);
|
|
@@ -14,7 +14,7 @@ const util_1 = require("../util");
|
|
|
14
14
|
function handle(config) {
|
|
15
15
|
return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
16
16
|
(0, util_1.log)(`Saving settings ${JSON.stringify(req.body.config, null, 2)}`, config.logger);
|
|
17
|
-
yield (0, storage_1.
|
|
17
|
+
yield (0, storage_1.getStorage)().updateIntegrationConfig(req.crowdinContext.clientId, JSON.stringify(req.body.config));
|
|
18
18
|
res.status(204).end();
|
|
19
19
|
}), config.onError);
|
|
20
20
|
}
|
|
@@ -14,14 +14,14 @@ const util_1 = require("../util");
|
|
|
14
14
|
function handle(config) {
|
|
15
15
|
return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
16
16
|
const { files, type, provider } = req.body;
|
|
17
|
-
const existingSettings = yield (0, storage_1.
|
|
17
|
+
const existingSettings = yield (0, storage_1.getStorage)().getSyncSettings(req.crowdinContext.clientId, req.crowdinContext.crowdinId, type, provider);
|
|
18
18
|
if (existingSettings) {
|
|
19
19
|
(0, util_1.log)(`Updating sync settings for type ${type} and provider ${provider} ${JSON.stringify(files, null, 2)}`, config.logger);
|
|
20
|
-
yield (0, storage_1.
|
|
20
|
+
yield (0, storage_1.getStorage)().updateSyncSettings(JSON.stringify(files), req.crowdinContext.clientId, req.crowdinContext.crowdinId, type, provider);
|
|
21
21
|
}
|
|
22
22
|
else {
|
|
23
23
|
(0, util_1.log)(`Saving sync settings for type ${type} and provider ${provider} ${JSON.stringify(files, null, 2)}`, config.logger);
|
|
24
|
-
yield (0, storage_1.
|
|
24
|
+
yield (0, storage_1.getStorage)().saveSyncSettings(JSON.stringify(files), req.crowdinContext.clientId, req.crowdinContext.crowdinId, type, provider);
|
|
25
25
|
}
|
|
26
26
|
res.status(204).end();
|
|
27
27
|
}), config.onError);
|
|
@@ -16,7 +16,7 @@ function handle(config) {
|
|
|
16
16
|
let files = {};
|
|
17
17
|
const provider = req.params.provider;
|
|
18
18
|
(0, util_1.log)(`Loading sync settings for provider ${provider}`, config.logger);
|
|
19
|
-
const syncSettings = yield (0, storage_1.
|
|
19
|
+
const syncSettings = yield (0, storage_1.getStorage)().getSyncSettingsByProvider(req.crowdinContext.clientId, provider);
|
|
20
20
|
if (syncSettings) {
|
|
21
21
|
files = JSON.parse(syncSettings.files) || [];
|
|
22
22
|
}
|
|
@@ -16,7 +16,7 @@ function handle(config) {
|
|
|
16
16
|
const event = req.body;
|
|
17
17
|
(0, util_1.log)(`Recieved uninstall request ${JSON.stringify(event, null, 2)}`, config.logger);
|
|
18
18
|
const organization = (event.domain || event.organizationId).toString();
|
|
19
|
-
yield (0, storage_1.
|
|
19
|
+
yield (0, storage_1.getStorage)().deleteCrowdinCredentials(organization);
|
|
20
20
|
if (config.onUninstall) {
|
|
21
21
|
yield config.onUninstall(organization);
|
|
22
22
|
}
|
package/out/index.js
CHANGED
|
@@ -68,7 +68,7 @@ const defaults_1 = require("./util/defaults");
|
|
|
68
68
|
var models_1 = require("./models");
|
|
69
69
|
Object.defineProperty(exports, "Scope", { enumerable: true, get: function () { return models_1.Scope; } });
|
|
70
70
|
function addCrowdinEndpoints(app, config) {
|
|
71
|
-
storage.
|
|
71
|
+
storage.initialize(config);
|
|
72
72
|
app.use(express_1.default.json({ limit: '50mb' }));
|
|
73
73
|
app.use('/assets', express_1.default.static((0, path_1.join)(__dirname, 'static')));
|
|
74
74
|
app.set('views', (0, path_1.join)(__dirname, 'views'));
|
|
@@ -132,8 +132,8 @@ function addCrowdinEndpoints(app, config) {
|
|
|
132
132
|
}
|
|
133
133
|
}
|
|
134
134
|
if (config.customFileFormat) {
|
|
135
|
-
app.post('/process', (0, crowdin_client_1.default)(config), (0, process_1.default)(config, config.baseUrl, config.customFileFormat.filesFolder || config.dbFolder, config.customFileFormat));
|
|
136
|
-
app.get('/file/download', (0, download_1.default)(config, config.customFileFormat.filesFolder || config.dbFolder));
|
|
135
|
+
app.post('/process', (0, crowdin_client_1.default)(config), (0, process_1.default)(config, config.baseUrl, config.customFileFormat.filesFolder || config.dbFolder || '/', config.customFileFormat));
|
|
136
|
+
app.get('/file/download', (0, download_1.default)(config, config.customFileFormat.filesFolder || config.dbFolder || '/'));
|
|
137
137
|
}
|
|
138
138
|
if (config.customMT) {
|
|
139
139
|
app.post('/translate', (0, crowdin_client_1.default)(config), (0, translate_1.default)(config, config.customMT));
|
|
@@ -161,19 +161,19 @@ function addCrowdinEndpoints(app, config) {
|
|
|
161
161
|
app.use('/reports', (0, ui_module_1.default)(config), express_1.default.static(config.projectReports.uiPath));
|
|
162
162
|
}
|
|
163
163
|
return {
|
|
164
|
-
getMetadata: storage.getMetadata,
|
|
164
|
+
getMetadata: storage.getStorage().getMetadata,
|
|
165
165
|
saveMetadata: (id, metadata) => __awaiter(this, void 0, void 0, function* () {
|
|
166
|
-
const existing = yield storage.getMetadata(id);
|
|
166
|
+
const existing = yield storage.getStorage().getMetadata(id);
|
|
167
167
|
if (existing) {
|
|
168
|
-
yield storage.updateMetadata(id, metadata);
|
|
168
|
+
yield storage.getStorage().updateMetadata(id, metadata);
|
|
169
169
|
}
|
|
170
170
|
else {
|
|
171
|
-
yield storage.saveMetadata(id, metadata);
|
|
171
|
+
yield storage.getStorage().saveMetadata(id, metadata);
|
|
172
172
|
}
|
|
173
173
|
}),
|
|
174
|
-
deleteMetadata: storage.deleteMetadata,
|
|
174
|
+
deleteMetadata: storage.getStorage().deleteMetadata,
|
|
175
175
|
getUserSettings: (clientId) => __awaiter(this, void 0, void 0, function* () {
|
|
176
|
-
const integrationCredentials = yield storage.getIntegrationCredentials(clientId);
|
|
176
|
+
const integrationCredentials = yield storage.getStorage().getIntegrationCredentials(clientId);
|
|
177
177
|
if (integrationCredentials === null || integrationCredentials === void 0 ? void 0 : integrationCredentials.config) {
|
|
178
178
|
return JSON.parse(integrationCredentials.config);
|
|
179
179
|
}
|
|
@@ -35,7 +35,7 @@ function prepareCrowdinRequest(jwtToken, config, optional = false, checkSubscrip
|
|
|
35
35
|
crowdinId: `${jwtPayload.domain || jwtPayload.context.organization_id}`,
|
|
36
36
|
};
|
|
37
37
|
(0, util_1.log)('Loading crowdin credentials', config.logger);
|
|
38
|
-
const credentials = yield (0, storage_1.
|
|
38
|
+
const credentials = yield (0, storage_1.getStorage)().getCrowdinCredentials(context.crowdinId);
|
|
39
39
|
if (!credentials) {
|
|
40
40
|
if (optional) {
|
|
41
41
|
return { context };
|
|
@@ -16,7 +16,7 @@ function handle(config, integration, optional = false) {
|
|
|
16
16
|
return (0, util_1.runAsyncWrapper)((req, res, next) => __awaiter(this, void 0, void 0, function* () {
|
|
17
17
|
const clientId = req.crowdinContext.clientId;
|
|
18
18
|
(0, util_1.log)(`Loading integration credentials for client ${clientId}`, config.logger);
|
|
19
|
-
const integrationCredentials = yield (0, storage_1.
|
|
19
|
+
const integrationCredentials = yield (0, storage_1.getStorage)().getIntegrationCredentials(clientId);
|
|
20
20
|
if (!integrationCredentials) {
|
|
21
21
|
if (optional) {
|
|
22
22
|
return next();
|
|
@@ -23,7 +23,7 @@ function handle(config) {
|
|
|
23
23
|
const jwtPayload = yield (0, crowdin_apps_functions_1.validateJwtToken)(tokenJwt, config.clientSecret);
|
|
24
24
|
const id = `${jwtPayload.domain || jwtPayload.context.organization_id}`;
|
|
25
25
|
(0, util_1.log)('Loading crowdin credentials', config.logger);
|
|
26
|
-
const credentials = yield (0, storage_1.
|
|
26
|
+
const credentials = yield (0, storage_1.getStorage)().getCrowdinCredentials(id);
|
|
27
27
|
if (!credentials) {
|
|
28
28
|
throw new Error("Can't find organization by id");
|
|
29
29
|
}
|
package/out/models/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import Crowdin, { LanguagesModel, SourceFilesModel, SourceStringsModel } from '@crowdin/crowdin-api-client';
|
|
2
2
|
import { JwtPayload } from '@crowdin/crowdin-apps-functions';
|
|
3
3
|
import { Request } from 'express';
|
|
4
|
+
import { PostgreStorageConfig } from '../storage/postgre';
|
|
4
5
|
export interface Config extends ImagePath {
|
|
5
6
|
/**
|
|
6
7
|
* client id that we received when registering the app
|
|
@@ -45,7 +46,11 @@ export interface Config extends ImagePath {
|
|
|
45
46
|
/**
|
|
46
47
|
* folder where module will create sqlite db file to persist credentials (e.g. {@example __dirname})
|
|
47
48
|
*/
|
|
48
|
-
dbFolder
|
|
49
|
+
dbFolder?: string;
|
|
50
|
+
/**
|
|
51
|
+
* config to configure PostgreSQL as a storage
|
|
52
|
+
*/
|
|
53
|
+
postgreConfig?: PostgreStorageConfig;
|
|
49
54
|
/**
|
|
50
55
|
* integration module logic
|
|
51
56
|
*/
|
package/out/storage/index.d.ts
CHANGED
|
@@ -1,22 +1,26 @@
|
|
|
1
|
-
import { CrowdinCredentials, IntegrationCredentials, IntegrationSyncSettings } from '../models';
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
1
|
+
import { Config, CrowdinCredentials, IntegrationCredentials, IntegrationSyncSettings } from '../models';
|
|
2
|
+
export interface Storage<T> {
|
|
3
|
+
connect(config: T): Promise<void>;
|
|
4
|
+
saveCrowdinCredentials(credentials: CrowdinCredentials): Promise<void>;
|
|
5
|
+
updateCrowdinCredentials(credentials: CrowdinCredentials): Promise<void>;
|
|
6
|
+
getCrowdinCredentials(id: string): Promise<CrowdinCredentials | undefined>;
|
|
7
|
+
getAllCrowdinCredentials(): Promise<CrowdinCredentials[]>;
|
|
8
|
+
deleteCrowdinCredentials(id: string): Promise<void>;
|
|
9
|
+
saveIntegrationCredentials(id: string, credentials: any, crowdinId: string): Promise<void>;
|
|
10
|
+
updateIntegrationCredentials(id: string, credentials: any): Promise<void>;
|
|
11
|
+
updateIntegrationConfig(id: string, config: any): Promise<void>;
|
|
12
|
+
getIntegrationCredentials(id: string): Promise<IntegrationCredentials | undefined>;
|
|
13
|
+
getAllIntegrationCredentials(crowdinId: string): Promise<IntegrationCredentials[]>;
|
|
14
|
+
deleteIntegrationCredentials(id: string): Promise<void>;
|
|
15
|
+
saveMetadata(id: string, metadata: any): Promise<void>;
|
|
16
|
+
updateMetadata(id: string, metadata: any): Promise<void>;
|
|
17
|
+
getMetadata(id: string): Promise<any | undefined>;
|
|
18
|
+
deleteMetadata(id: string): Promise<void>;
|
|
19
|
+
getSyncSettingsByProvider(integrationId: string, provider: string): Promise<IntegrationSyncSettings | undefined>;
|
|
20
|
+
getAllSyncSettingsByType(type: string): Promise<IntegrationSyncSettings[]>;
|
|
21
|
+
saveSyncSettings(files: any, integrationId: string, crowdinId: string, type: string, provider: string): Promise<void>;
|
|
22
|
+
updateSyncSettings(files: any, integrationId: string, crowdinId: string, type: string, provider: string): Promise<void>;
|
|
23
|
+
getSyncSettings(integrationId: string, crowdinId: string, type: string, provider: string): Promise<IntegrationSyncSettings | undefined>;
|
|
24
|
+
}
|
|
25
|
+
export declare function initialize(config: Config): Promise<void>;
|
|
26
|
+
export declare function getStorage(): Storage<any>;
|
package/out/storage/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/* eslint-disable @typescript-eslint/camelcase */
|
|
3
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
5
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -9,311 +8,33 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
10
9
|
});
|
|
11
10
|
};
|
|
12
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
13
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
14
|
-
};
|
|
15
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
let
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (err) {
|
|
46
|
-
rej(err);
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
res();
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
function get(query, params) {
|
|
56
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
-
yield dbPromise;
|
|
58
|
-
return new Promise((res, rej) => {
|
|
59
|
-
db.get(query, params, (err, row) => {
|
|
60
|
-
if (err) {
|
|
61
|
-
rej(err);
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
res(row);
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
function each(query, params) {
|
|
71
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
72
|
-
yield dbPromise;
|
|
73
|
-
return new Promise((res, rej) => {
|
|
74
|
-
const result = [];
|
|
75
|
-
db.each(query, params, (err, row) => {
|
|
76
|
-
if (err) {
|
|
77
|
-
rej(err);
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
result.push(row);
|
|
81
|
-
}
|
|
82
|
-
}, () => res(result));
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
function migrate() {
|
|
87
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
88
|
-
const newColumns = ['app_secret', 'domain', 'user_id', 'organization_id', 'base_url'];
|
|
89
|
-
const crowdinCredentialsInfo = yield each('PRAGMA table_info(crowdin_credentials);', []);
|
|
90
|
-
crowdinCredentialsInfo.map((columnInfo) => __awaiter(this, void 0, void 0, function* () {
|
|
91
|
-
const index = newColumns.indexOf(columnInfo.name);
|
|
92
|
-
if (~index) {
|
|
93
|
-
newColumns.splice(index, 1);
|
|
94
|
-
}
|
|
95
|
-
}));
|
|
96
|
-
for (const column of newColumns) {
|
|
97
|
-
yield run(`ALTER TABLE crowdin_credentials ADD COLUMN ${column} varchar null;`, []);
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
function connect(folder) {
|
|
102
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
103
|
-
let _connection_res;
|
|
104
|
-
let _connection_rej;
|
|
105
|
-
const connectionPromise = new Promise((res, rej) => {
|
|
106
|
-
_connection_res = res;
|
|
107
|
-
_connection_rej = rej;
|
|
108
|
-
});
|
|
109
|
-
db = new sqlite3_1.default.Database((0, path_1.join)(folder, 'app.sqlite'), error => {
|
|
110
|
-
if (error) {
|
|
111
|
-
_connection_rej(error);
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
_connection_res();
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
try {
|
|
118
|
-
yield connectionPromise;
|
|
119
|
-
yield _run(`
|
|
120
|
-
create table if not exists crowdin_credentials
|
|
121
|
-
(
|
|
122
|
-
id varchar not null primary key,
|
|
123
|
-
app_secret varchar null,
|
|
124
|
-
domain varchar null,
|
|
125
|
-
user_id varchar null,
|
|
126
|
-
organization_id varchar null,
|
|
127
|
-
base_url varchar null,
|
|
128
|
-
access_token varchar not null,
|
|
129
|
-
refresh_token varchar not null,
|
|
130
|
-
expire varchar not null,
|
|
131
|
-
type varchar not null
|
|
132
|
-
);
|
|
133
|
-
`, []);
|
|
134
|
-
yield _run(`
|
|
135
|
-
create table if not exists integration_credentials
|
|
136
|
-
(
|
|
137
|
-
id varchar not null primary key,
|
|
138
|
-
credentials varchar not null,
|
|
139
|
-
config varchar null,
|
|
140
|
-
crowdin_id varchar not null
|
|
141
|
-
);
|
|
142
|
-
`, []);
|
|
143
|
-
yield _run(`
|
|
144
|
-
create table if not exists sync_settings
|
|
145
|
-
(
|
|
146
|
-
id integer not null primary key autoincrement,
|
|
147
|
-
files varchar null,
|
|
148
|
-
integration_id varchar not null,
|
|
149
|
-
crowdin_id varchar not null,
|
|
150
|
-
type varchar not null,
|
|
151
|
-
provider varchar not null
|
|
152
|
-
);
|
|
153
|
-
`, []);
|
|
154
|
-
yield _run(`
|
|
155
|
-
create table if not exists app_metadata
|
|
156
|
-
(
|
|
157
|
-
id varchar not null primary key,
|
|
158
|
-
data varchar null
|
|
159
|
-
);
|
|
160
|
-
`, []);
|
|
161
|
-
_res();
|
|
162
|
-
// TODO: temporary code
|
|
163
|
-
yield migrate();
|
|
164
|
-
}
|
|
165
|
-
catch (e) {
|
|
166
|
-
_rej(e);
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
exports.connect = connect;
|
|
171
|
-
function saveCrowdinCredentials(credentials) {
|
|
172
|
-
return run('INSERT INTO crowdin_credentials(id, app_secret, domain, user_id, organization_id, base_url, access_token, refresh_token, expire, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [
|
|
173
|
-
credentials.id,
|
|
174
|
-
credentials.appSecret,
|
|
175
|
-
credentials.domain,
|
|
176
|
-
credentials.userId,
|
|
177
|
-
credentials.organizationId,
|
|
178
|
-
credentials.baseUrl,
|
|
179
|
-
credentials.accessToken,
|
|
180
|
-
credentials.refreshToken,
|
|
181
|
-
credentials.expire,
|
|
182
|
-
credentials.type,
|
|
183
|
-
]);
|
|
184
|
-
}
|
|
185
|
-
exports.saveCrowdinCredentials = saveCrowdinCredentials;
|
|
186
|
-
function updateCrowdinCredentials(credentials) {
|
|
187
|
-
return run('UPDATE crowdin_credentials SET app_secret = ?, domain = ?, user_id = ?, organization_id = ?, base_url = ?, access_token = ?, refresh_token = ?, expire = ? WHERE id = ?', [
|
|
188
|
-
credentials.appSecret,
|
|
189
|
-
credentials.domain,
|
|
190
|
-
credentials.userId,
|
|
191
|
-
credentials.organizationId,
|
|
192
|
-
credentials.baseUrl,
|
|
193
|
-
credentials.accessToken,
|
|
194
|
-
credentials.refreshToken,
|
|
195
|
-
credentials.expire,
|
|
196
|
-
credentials.id,
|
|
197
|
-
]);
|
|
198
|
-
}
|
|
199
|
-
exports.updateCrowdinCredentials = updateCrowdinCredentials;
|
|
200
|
-
function getCrowdinCredentials(id) {
|
|
201
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
202
|
-
const row = yield get('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]);
|
|
203
|
-
if (row) {
|
|
204
|
-
return row;
|
|
205
|
-
}
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
exports.getCrowdinCredentials = getCrowdinCredentials;
|
|
209
|
-
function getAllCrowdinCredentials() {
|
|
210
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
211
|
-
return each('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', []);
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
exports.getAllCrowdinCredentials = getAllCrowdinCredentials;
|
|
215
|
-
function deleteCrowdinCredentials(id) {
|
|
216
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
217
|
-
yield run('DELETE FROM crowdin_credentials where id = ?', [id]);
|
|
218
|
-
yield run('DELETE FROM integration_credentials where crowdin_id = ?', [id]);
|
|
219
|
-
yield run('DELETE FROM sync_settings WHERE crowdin_id = ?', [id]);
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
exports.deleteCrowdinCredentials = deleteCrowdinCredentials;
|
|
223
|
-
function saveIntegrationCredentials(id, credentials, crowdinId) {
|
|
224
|
-
return run('INSERT INTO integration_credentials(id, credentials, crowdin_id) VALUES (?, ?, ?)', [
|
|
225
|
-
id,
|
|
226
|
-
credentials,
|
|
227
|
-
crowdinId,
|
|
228
|
-
]);
|
|
229
|
-
}
|
|
230
|
-
exports.saveIntegrationCredentials = saveIntegrationCredentials;
|
|
231
|
-
function updateIntegrationCredentials(id, credentials) {
|
|
232
|
-
return run('UPDATE integration_credentials SET credentials = ? WHERE id = ?', [credentials, id]);
|
|
233
|
-
}
|
|
234
|
-
exports.updateIntegrationCredentials = updateIntegrationCredentials;
|
|
235
|
-
function updateIntegrationConfig(id, config) {
|
|
236
|
-
return run('UPDATE integration_credentials SET config = ? WHERE id = ?', [config, id]);
|
|
237
|
-
}
|
|
238
|
-
exports.updateIntegrationConfig = updateIntegrationConfig;
|
|
239
|
-
function getIntegrationCredentials(id) {
|
|
240
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
241
|
-
const row = yield get('SELECT id, credentials, config, crowdin_id as crowdinId FROM integration_credentials WHERE id = ?', [id]);
|
|
242
|
-
if (row) {
|
|
243
|
-
return row;
|
|
244
|
-
}
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
exports.getIntegrationCredentials = getIntegrationCredentials;
|
|
248
|
-
function getAllIntegrationCredentials(crowdinId) {
|
|
249
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
250
|
-
return each('SELECT id, credentials, config, crowdin_id as crowdinId FROM integration_credentials WHERE crowdin_id = ?', [crowdinId]);
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
exports.getAllIntegrationCredentials = getAllIntegrationCredentials;
|
|
254
|
-
function deleteIntegrationCredentials(id) {
|
|
255
|
-
return run('DELETE FROM integration_credentials where id = ?', [id]);
|
|
256
|
-
return run('DELETE FROM sync_settings where integration_id = ?', [id]);
|
|
257
|
-
}
|
|
258
|
-
exports.deleteIntegrationCredentials = deleteIntegrationCredentials;
|
|
259
|
-
function saveMetadata(id, metadata) {
|
|
260
|
-
return run('INSERT INTO app_metadata(id, data) VALUES (?, ?)', [id, JSON.stringify(metadata)]);
|
|
261
|
-
}
|
|
262
|
-
exports.saveMetadata = saveMetadata;
|
|
263
|
-
function updateMetadata(id, metadata) {
|
|
264
|
-
return run('UPDATE app_metadata SET data = ? WHERE id = ?', [JSON.stringify(metadata), id]);
|
|
265
|
-
}
|
|
266
|
-
exports.updateMetadata = updateMetadata;
|
|
267
|
-
function getMetadata(id) {
|
|
268
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
269
|
-
const row = yield get('SELECT data FROM app_metadata WHERE id = ?', [id]);
|
|
270
|
-
if (row) {
|
|
271
|
-
return JSON.parse(row.data);
|
|
272
|
-
}
|
|
273
|
-
});
|
|
274
|
-
}
|
|
275
|
-
exports.getMetadata = getMetadata;
|
|
276
|
-
function deleteMetadata(id) {
|
|
277
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
278
|
-
yield run('DELETE FROM app_metadata where id = ?', [id]);
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
exports.deleteMetadata = deleteMetadata;
|
|
282
|
-
function getSyncSettingsByProvider(integrationId, provider) {
|
|
283
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
284
|
-
const row = yield get('SELECT id, files, integration_id as integrationId, crowdin_id as crowdinId, type, provider FROM sync_settings WHERE integration_id = ? AND provider = ?', [integrationId, provider]);
|
|
285
|
-
if (row) {
|
|
286
|
-
return row;
|
|
287
|
-
}
|
|
288
|
-
});
|
|
289
|
-
}
|
|
290
|
-
exports.getSyncSettingsByProvider = getSyncSettingsByProvider;
|
|
291
|
-
function getAllSyncSettingsByType(type) {
|
|
292
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
293
|
-
return each('SELECT id, files, integration_id as integrationId, crowdin_id as crowdinId, type, provider FROM sync_settings WHERE type = ?', [type]);
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
exports.getAllSyncSettingsByType = getAllSyncSettingsByType;
|
|
297
|
-
function saveSyncSettings(files, integrationId, crowdinId, type, provider) {
|
|
298
|
-
return run('INSERT INTO sync_settings(files, integration_id, crowdin_id, type, provider) VALUES (?, ?, ?, ?, ?)', [
|
|
299
|
-
files,
|
|
300
|
-
integrationId,
|
|
301
|
-
crowdinId,
|
|
302
|
-
type,
|
|
303
|
-
provider,
|
|
304
|
-
]);
|
|
305
|
-
}
|
|
306
|
-
exports.saveSyncSettings = saveSyncSettings;
|
|
307
|
-
function updateSyncSettings(files, integrationId, crowdinId, type, provider) {
|
|
308
|
-
return run('UPDATE sync_settings SET files = ? WHERE integration_id = ? AND crowdin_id = ? AND type = ? AND provider = ?', [files, integrationId, crowdinId, type, provider]);
|
|
309
|
-
}
|
|
310
|
-
exports.updateSyncSettings = updateSyncSettings;
|
|
311
|
-
function getSyncSettings(integrationId, crowdinId, type, provider) {
|
|
312
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
313
|
-
const row = yield get('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]);
|
|
314
|
-
if (row) {
|
|
315
|
-
return row;
|
|
316
|
-
}
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
|
-
exports.getSyncSettings = getSyncSettings;
|
|
12
|
+
exports.getStorage = exports.initialize = void 0;
|
|
13
|
+
const util_1 = require("../util");
|
|
14
|
+
const postgre_1 = require("./postgre");
|
|
15
|
+
const sqlite_1 = require("./sqlite");
|
|
16
|
+
let storage;
|
|
17
|
+
function initialize(config) {
|
|
18
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
+
if (config.dbFolder) {
|
|
20
|
+
(0, util_1.log)('Using SQLite database', config.logger);
|
|
21
|
+
const sqlite = new sqlite_1.SQLiteStorage();
|
|
22
|
+
storage = sqlite;
|
|
23
|
+
yield sqlite.connect({ dbFolder: config.dbFolder });
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (config.postgreConfig) {
|
|
27
|
+
(0, util_1.log)('Using PostgreSQL database', config.logger);
|
|
28
|
+
const postgre = new postgre_1.PostgreStorage();
|
|
29
|
+
storage = postgre;
|
|
30
|
+
yield postgre.connect(config.postgreConfig);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
throw new Error('Database is not configured');
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
exports.initialize = initialize;
|
|
37
|
+
function getStorage() {
|
|
38
|
+
return storage;
|
|
39
|
+
}
|
|
40
|
+
exports.getStorage = getStorage;
|