@crowdin/app-project-module
Version:
Module that generates for you all common endpoints for serving standalone Crowdin App
787 lines (786 loc) • 35.9 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SQLiteStorage = void 0;
const crypto_1 = require("crypto");
const path_1 = require("path");
const types_1 = require("../modules/integration/util/types");
const types_2 = require("../types");
class SQLiteStorage {
constructor(config) {
this.db = null;
this.tables = {
crowdin_credentials: `(
id varchar not null primary key,
app_secret varchar null,
domain varchar null,
user_id varchar null,
agent_id varchar null,
organization_id varchar null,
base_url varchar null,
access_token varchar not null,
refresh_token varchar not null,
expire varchar not null,
type varchar not null
)`,
integration_credentials: `(
id varchar not null primary key,
credentials varchar not null,
crowdin_id varchar not null,
managers varchar null
)`,
sync_settings: `(
id integer not null primary key autoincrement,
files varchar null,
integration_id varchar not null,
crowdin_id varchar not null,
type varchar not null,
provider varchar not null
)`,
app_metadata: `(
id varchar not null primary key,
data varchar null,
crowdin_id varchar null
)`,
files_snapshot: `(
id integer not null primary key autoincrement,
integration_id varchar not null,
crowdin_id varchar not null,
files varchar null,
provider varchar not null
)`,
webhooks: `(
id integer not null primary key autoincrement,
file_id varchar not null,
integration_id varchar not null,
crowdin_id varchar not null,
provider varchar not null
)`,
user_errors: `(
id integer not null primary key autoincrement,
action varchar not null,
message varchar not null,
data varchar null,
created_at varchar not null,
crowdin_id varchar not null,
integration_id varchar null
)`,
integration_settings: `(
id integer not null primary key autoincrement,
integration_id varchar not null,
crowdin_id varchar not null,
config varchar null
)`,
job: `(
id varchar not null primary key,
integration_id varchar not null,
crowdin_id varchar not null,
type varchar not null,
title varchar null,
progress integer DEFAULT 0,
status varchar DEFAULT '${types_1.JobStatus.CREATED}',
payload varchar null,
info varchar null,
data varchar null,
attempt varchar DEFAULT 0,
errors varchar null,
processed_entities varchar null,
initiated_by varchar null,
created_at varchar not null,
updated_at varchar null,
finished_at varchar null
)`,
translation_file_cache: `(
id integer not null primary key autoincrement,
integration_id varchar not null,
crowdin_id varchar not null,
file_id integer not null,
language_id varchar not null,
etag varchar
)`,
unsynced_files: `(
id integer not null primary key autoincrement,
integration_id varchar not null,
crowdin_id varchar not null,
files varchar null
)`,
synced_data: `(
id integer not null primary key autoincrement,
files varchar null,
integration_id varchar not null,
crowdin_id varchar not null,
type varchar not null,
updated_at varchar null
)`,
};
this.config = config;
}
addColumn(tableName, column, defaultValue) {
const columns = this.db.pragma(`table_info(${tableName})`);
const exists = columns.some((col) => col.name === column);
if (!exists) {
this.db.prepare(`ALTER TABLE ${tableName} ADD COLUMN ${column} varchar ${defaultValue}`).run();
}
}
updateTables() {
this.addColumn('job', 'initiated_by', 'null');
}
migrate(skipOnboarding) {
return __awaiter(this, void 0, void 0, function* () {
this.db = require('better-sqlite3')((0, path_1.join)(this.config.dbFolder, types_2.storageFiles.SQLITE));
if (!skipOnboarding) {
try {
for (const [tableName, schema] of Object.entries(this.tables)) {
this.db.prepare(`CREATE TABLE IF NOT EXISTS ${tableName} ${schema}`).run();
}
this.updateTables();
}
catch (error) {
console.error('Error during database migration:', error);
throw error;
}
}
});
}
saveCrowdinCredentials(credentials) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare(`
INSERT INTO crowdin_credentials
(id, app_secret, domain, user_id, agent_id, organization_id, base_url, access_token, refresh_token, expire, type)
VALUES
(, , , , , , , , , , )
`).run(credentials);
});
}
updateCrowdinCredentials(credentials) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare(`
UPDATE crowdin_credentials
SET
app_secret = ,
domain = ,
user_id = ,
agent_id = ,
organization_id = ,
base_url = ,
access_token = ,
refresh_token = ,
expire = ,
type =
WHERE id =
`).run(credentials);
});
}
getCrowdinCredentials(id) {
return __awaiter(this, void 0, void 0, function* () {
return this.db.prepare(`
SELECT id, app_secret as appSecret, domain, user_id as userId, agent_id as agentId, organization_id as organizationId, base_url as baseUrl, access_token as accessToken, refresh_token as refreshToken, expire, type
FROM crowdin_credentials
WHERE id = ?
`).get(id);
});
}
getAllCrowdinCredentials() {
return __awaiter(this, void 0, void 0, function* () {
return this.db.prepare(`
SELECT id, app_secret as appSecret, domain, user_id as userId, agent_id as agentId, organization_id as organizationId, base_url as baseUrl, access_token as accessToken, refresh_token as refreshToken, expire, type
FROM crowdin_credentials
`).all();
});
}
deleteCrowdinCredentials(id) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare('DELETE FROM crowdin_credentials where id = ?').run(id);
this.db.prepare('DELETE FROM integration_credentials where crowdin_id = ?').run(id);
this.db.prepare('DELETE FROM sync_settings WHERE crowdin_id = ?').run(id);
this.db.prepare('DELETE FROM app_metadata WHERE crowdin_id = ?').run(id);
this.db.prepare('DELETE FROM files_snapshot WHERE crowdin_id = ?').run(id);
this.db.prepare('DELETE FROM webhooks WHERE crowdin_id = ?').run(id);
this.db.prepare('DELETE FROM user_errors WHERE crowdin_id = ?').run(id);
this.db.prepare('DELETE FROM integration_settings WHERE crowdin_id = ?').run(id);
this.db.prepare('DELETE FROM job WHERE crowdin_id = ?').run(id);
this.db.prepare('DELETE FROM translation_file_cache WHERE crowdin_id = ?').run(id);
this.db.prepare('DELETE FROM unsynced_files WHERE crowdin_id = ?').run(id);
this.db.prepare('DELETE FROM synced_data WHERE crowdin_id = ?').run(id);
});
}
saveIntegrationCredentials(id, credentials, crowdinId) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare(`
INSERT INTO integration_credentials
(id, credentials, crowdin_id)
VALUES
(, , )
`).run({ id, credentials, crowdinId });
});
}
updateIntegrationCredentials(id, credentials) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare(`
UPDATE integration_credentials
SET credentials =
WHERE id =
`).run({ id, credentials });
});
}
updateIntegrationManagers(id, managers) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare(`
UPDATE integration_credentials
SET managers =
WHERE id =
`).run({ id, managers });
});
}
getIntegrationCredentials(id) {
return __awaiter(this, void 0, void 0, function* () {
return this.db.prepare(`
SELECT id, credentials, crowdin_id as crowdinId, managers
FROM integration_credentials
WHERE id = ?
`).get(id);
});
}
getAllIntegrationCredentials(crowdinId) {
return __awaiter(this, void 0, void 0, function* () {
return this.db.prepare(`
SELECT id, credentials, crowdin_id as crowdinId, managers
FROM integration_credentials
WHERE crowdin_id = ?`).all(crowdinId);
});
}
deleteIntegrationCredentials(id) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare('DELETE FROM integration_credentials where id = ?').run(id);
this.db.prepare('DELETE FROM sync_settings where integration_id = ?').run(id);
this.db.prepare('DELETE FROM files_snapshot where integration_id = ?').run(id);
this.db.prepare('DELETE FROM webhooks where integration_id = ?').run(id);
this.db.prepare('DELETE FROM user_errors where integration_id = ?').run(id);
this.db.prepare('DELETE FROM integration_settings where integration_id = ?').run(id);
this.db.prepare('DELETE FROM job where integration_id = ?').run(id);
this.db.prepare('DELETE FROM translation_file_cache where integration_id = ?').run(id);
this.db.prepare('DELETE FROM unsynced_files where integration_id = ?').run(id);
this.db.prepare('DELETE FROM synced_data where integration_id = ?').run(id);
});
}
deleteAllIntegrationCredentials(crowdinId) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare('DELETE FROM integration_credentials where crowdin_id = ?').run(crowdinId);
this.db.prepare('DELETE FROM sync_settings where crowdin_id = ?').run(crowdinId);
this.db.prepare('DELETE FROM files_snapshot where crowdin_id = ?').run(crowdinId);
this.db.prepare('DELETE FROM webhooks where crowdin_id = ?').run(crowdinId);
this.db.prepare('DELETE FROM user_errors where crowdin_id = ?').run(crowdinId);
this.db.prepare('DELETE FROM job where crowdin_id = ?').run(crowdinId);
this.db.prepare('DELETE FROM unsynced_files where crowdin_id = ?').run(crowdinId);
this.db.prepare('DELETE FROM synced_data where crowdin_id = ?').run(crowdinId);
});
}
saveMetadata(id, metadata, crowdinId) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare(`
INSERT INTO app_metadata
(id, data, crowdin_id)
VALUES
(, , )
`).run({ id, data: JSON.stringify(metadata), crowdinId });
});
}
updateMetadata(id, metadata, crowdinId) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare(`
UPDATE app_metadata
SET data =
WHERE id = AND crowdin_id =
`).run({ id, data: JSON.stringify(metadata), crowdinId });
});
}
getMetadata(id) {
return __awaiter(this, void 0, void 0, function* () {
const row = this.db.prepare('SELECT data FROM app_metadata WHERE id = ?').get(id);
return row ? JSON.parse(row.data) : undefined;
});
}
getAllMetadata() {
return __awaiter(this, void 0, void 0, function* () {
return this.db.prepare('SELECT * FROM app_metadata').all();
});
}
deleteMetadata(id) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare('DELETE FROM app_metadata WHERE id = ?').run(id);
});
}
getSyncSettingsByProvider(integrationId, provider) {
return __awaiter(this, void 0, void 0, function* () {
return this.db.prepare(`
SELECT id, files, integration_id as integrationId, crowdin_id as crowdinId, type, provider
FROM sync_settings
WHERE integration_id = ? AND provider = ?
`).get(integrationId, provider);
});
}
getSyncSettingsBySchedule(type, schedule) {
return __awaiter(this, void 0, void 0, function* () {
return this.db.prepare(`
SELECT s.id, s.files, s.integration_id as integrationId, s.crowdin_id as crowdinId, s.type, s.provider
FROM sync_settings s
INNER JOIN integration_settings i ON s.integration_id = i.integration_id
WHERE s.type = ?
AND CASE
WHEN i.config IS NULL THEN 0
ELSE json_extract(i.config, '$.schedule') = ?
END
`).all(type, schedule);
});
}
saveSyncSettings(files, integrationId, crowdinId, type, provider) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare(`
INSERT INTO sync_settings
(files, integration_id, crowdin_id, type, provider)
VALUES
(, , , , )
`).run({ files, integrationId, crowdinId, type, provider });
});
}
updateSyncSettings(files, integrationId, crowdinId, type, provider) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare(`
UPDATE sync_settings
SET files =
WHERE integration_id = AND crowdin_id = AND type = AND provider =
`).run({ files, integrationId, crowdinId, type, provider });
});
}
getSyncSettings(integrationId, crowdinId, type, provider) {
return __awaiter(this, void 0, void 0, function* () {
return this.db.prepare(`
SELECT id, files, integration_id as integrationId, crowdin_id as crowdinId, type, provider
FROM sync_settings
WHERE integration_id = ? AND crowdin_id = ? AND type = ? AND provider = ?
`).get(integrationId, crowdinId, type, provider);
});
}
saveFilesSnapshot(files, integrationId, crowdinId, provider) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare(`
INSERT INTO files_snapshot
(files, integration_id, crowdin_id, provider)
VALUES
(, , , )
`).run({ files, integrationId, crowdinId, provider });
});
}
updateFilesSnapshot(files, integrationId, crowdinId, provider) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare(`
UPDATE files_snapshot
SET files =
WHERE integration_id = AND crowdin_id = AND provider =
`).run({ files, integrationId, crowdinId, provider });
});
}
getFilesSnapshot(integrationId, crowdinId, provider) {
return __awaiter(this, void 0, void 0, function* () {
return this.db.prepare(`
SELECT files, integration_id as integrationId, crowdin_id as crowdinId, provider
FROM files_snapshot
WHERE integration_id = ? AND crowdin_id = ? AND provider = ?
`).get(integrationId, crowdinId, provider);
});
}
getAllWebhooks(integrationId, crowdinId, provider) {
return __awaiter(this, void 0, void 0, function* () {
return this.db.prepare(`
SELECT id, file_id as fileId, integration_id as integrationId, crowdin_id as crowdinId, provider
FROM webhooks
WHERE integration_id = ? AND crowdin_id = ? AND provider = ?
`).all(integrationId, crowdinId, provider);
});
}
getWebhooks(fileId, integrationId, crowdinId, provider) {
return __awaiter(this, void 0, void 0, function* () {
return this.db.prepare(`
SELECT id, file_id as fileId, integration_id as integrationId, crowdin_id as crowdinId, provider
FROM webhooks
WHERE file_id = ? AND integration_id = ? AND crowdin_id = ? AND provider = ?
`).get(fileId, integrationId, crowdinId, provider);
});
}
saveWebhooks(fileId, integrationId, crowdinId, provider) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare(`
INSERT INTO webhooks
(file_id, integration_id, crowdin_id, provider)
VALUES
(, , , )
`).run({ fileId, integrationId, crowdinId, provider });
});
}
deleteWebhooks(fileIds, integrationId, crowdinId, provider) {
return __awaiter(this, void 0, void 0, function* () {
if (!fileIds.length) {
return;
}
this.db.prepare(`
DELETE FROM webhooks
WHERE file_id IN (${fileIds.map(() => '?').join(',')})
AND integration_id = ? AND crowdin_id = ? AND provider = ?
`).run([...fileIds, integrationId, crowdinId, provider]);
});
}
getAllUserErrors(crowdinId, integrationId) {
return __awaiter(this, void 0, void 0, function* () {
let whereIntegrationCondition = 'integration_id is NULL';
const params = [crowdinId];
if (integrationId) {
whereIntegrationCondition = 'integration_id = ?';
params.push(integrationId);
}
return this.db.prepare(`
SELECT id, action, message, data, created_at as createdAt, crowdin_id as crowdinId, integration_id as integrationId
FROM user_errors
WHERE crowdin_id = ? AND ${whereIntegrationCondition}
`).all(...params);
});
}
saveUserError(action, message, data, createdAt, crowdinId, integrationId) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare(`
INSERT INTO user_errors
(action, message, data, created_at, crowdin_id, integration_id)
VALUES
(, , , , , )
`).run({ action, message, data, createdAt, crowdinId, integrationId });
});
}
deleteUserErrors(date, crowdinId, integrationId) {
return __awaiter(this, void 0, void 0, function* () {
let whereIntegrationCondition = 'integration_id is NULL';
const params = [date, crowdinId];
if (integrationId) {
whereIntegrationCondition = 'integration_id = ?';
params.push(integrationId);
}
this.db.prepare(`
DELETE FROM user_errors
WHERE created_at < ? AND crowdin_id = ? AND ${whereIntegrationCondition}
`).run(...params);
});
}
deleteAllUsersErrorsOlderThan(date) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare('DELETE FROM user_errors WHERE created_at < ?').run(date);
});
}
saveIntegrationConfig(integrationId, crowdinId, config) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare(`
INSERT OR REPLACE INTO integration_settings
(integration_id, crowdin_id, config)
VALUES
(, , )
`).run({ integrationId, crowdinId, config });
});
}
getAllIntegrationConfigs(crowdinId) {
return __awaiter(this, void 0, void 0, function* () {
return this.db.prepare(`
SELECT config, integration_id as integrationId
FROM integration_settings
WHERE crowdin_id = ?
`).all(crowdinId);
});
}
getIntegrationConfig(integrationId) {
return __awaiter(this, void 0, void 0, function* () {
return this.db.prepare(`
SELECT config
FROM integration_settings
WHERE integration_id = ?
`).get(integrationId);
});
}
updateIntegrationConfig(integrationId, config) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare(`
UPDATE integration_settings
SET config =
WHERE integration_id =
`).run({ integrationId, config });
});
}
createJob(params) {
return __awaiter(this, void 0, void 0, function* () {
const id = (0, crypto_1.randomUUID)();
this.db.prepare(`
INSERT INTO job
(id, integration_id, crowdin_id, type, title, payload, initiated_by, created_at)
VALUES
(, , , , , , , )
`).run(Object.assign({ id, createdAt: Date.now().toString() }, params));
return id;
});
}
updateJob(_a) {
return __awaiter(this, arguments, void 0, function* ({ id, progress, status, info, data, attempt, errors, processedEntities, }) {
const updateFields = ['updated_at = ?'];
const updateParams = [Date.now().toString()];
if (progress) {
updateFields.push('progress = ?');
updateParams.push(Math.round(progress));
if (progress >= 100) {
updateFields.push('finished_at = ?');
updateParams.push(Date.now().toString());
}
}
if (status) {
updateFields.push('status = ?');
updateParams.push(status);
if (!updateFields.includes('finished_at = ?') && [types_1.JobStatus.FAILED, types_1.JobStatus.CANCELED].includes(status)) {
updateFields.push('finished_at = ?');
updateParams.push(Date.now().toString());
}
}
if (data) {
updateFields.push('data = ?');
updateParams.push(data);
}
if (info) {
updateFields.push('info = ?');
updateParams.push(info);
}
if (attempt) {
updateFields.push('attempt = ?');
updateParams.push(attempt);
}
if (errors) {
updateFields.push('errors = ?');
updateParams.push(JSON.stringify(errors));
}
if (processedEntities) {
updateFields.push('processed_entities = ?');
updateParams.push(processedEntities);
}
updateParams.push(id);
const query = `
UPDATE job
SET ${updateFields.join(', ')}
WHERE id = ?
`;
this.db.prepare(query).run(...updateParams);
});
}
getJob(params) {
return __awaiter(this, void 0, void 0, function* () {
return this.db.prepare(`
SELECT id, integration_id as integrationId, crowdin_id as crowdinId, type, payload, progress, status,
title, info, data, attempt, errors, processed_entities as processedEntities, initiated_by as initiatedBy, created_at as createdAt, updated_at as updatedAt, finished_at as finishedAt
FROM job
WHERE id = ?
`).get(params.id);
});
}
getActiveJobs(params) {
return __awaiter(this, void 0, void 0, function* () {
return this.db.prepare(`
SELECT id, integration_id as integrationId, crowdin_id as crowdinId, type, payload, progress, status, title, info, data, attempt, errors, processed_entities as processedEntities, initiated_by as initiatedBy, created_at as createdAt, updated_at as updatedAt, finished_at as finishedAt
FROM job
WHERE integration_id = ? AND crowdin_id = ? AND finished_at is NULL
`).all(params.integrationId, params.crowdinId);
});
}
deleteFinishedJobs() {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare('DELETE FROM job WHERE finished_at IS NOT NULL').run();
});
}
getAllInProgressJobs() {
return __awaiter(this, void 0, void 0, function* () {
return this.db.prepare(`
SELECT id, integration_id as integrationId, crowdin_id as crowdinId, type, payload, progress, status, title, info, data, attempt, errors, processed_entities as processedEntities, initiated_by as initiatedBy, created_at as createdAt, updated_at as updatedAt, finished_at as finishedAt
FROM job
WHERE status IN (?,?) AND finished_at is NULL
`).all(types_1.JobStatus.IN_PROGRESS, types_1.JobStatus.CREATED);
});
}
getAllJobs(_a) {
return __awaiter(this, arguments, void 0, function* ({ integrationId, crowdinId, limit, offset }) {
return this.db.prepare(`
SELECT
id,
integration_id as integrationId,
crowdin_id as crowdinId,
type,
payload,
progress,
status,
title,
info,
data,
attempt,
initiated_by as initiatedBy,
created_at as createdAt,
updated_at as updatedAt,
finished_at as finishedAt
FROM job
WHERE integration_id = ? AND crowdin_id = ?
ORDER BY created_at DESC
LIMIT ? OFFSET ?
`).all(integrationId, crowdinId, limit, offset);
});
}
saveTranslationCache(params) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
this.db.prepare(`
INSERT INTO translation_file_cache
(integration_id, crowdin_id, file_id, language_id, etag)
VALUES
(, , , , )
`).run(Object.assign(Object.assign({}, params), { etag: (_a = params.etag) !== null && _a !== void 0 ? _a : null }));
});
}
getFileTranslationCache(params) {
return __awaiter(this, void 0, void 0, function* () {
return this.db.prepare(`
SELECT integration_id as integrationId, crowdin_id as crowdinId, file_id as fileId, language_id as languageId, etag
FROM translation_file_cache
WHERE integration_id = ? AND crowdin_id = ? AND file_id = ?
`).all(params.integrationId, params.crowdinId, params.fileId);
});
}
getFileTranslationCacheByLanguage(params) {
return __awaiter(this, void 0, void 0, function* () {
return this.db.prepare(`
SELECT integration_id as integrationId, crowdin_id as crowdinId, file_id as fileId, language_id as languageId, etag
FROM translation_file_cache
WHERE integration_id = ? AND crowdin_id = ? AND file_id = ? AND language_id = ?
`).get(params.integrationId, params.crowdinId, params.fileId, params.languageId);
});
}
updateTranslationCache(params) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare(`
UPDATE translation_file_cache
SET etag = ?
WHERE integration_id = ? AND crowdin_id = ? AND file_id = ? AND language_id = ?
`).run(params.etag, params.integrationId, params.crowdinId, params.fileId, params.languageId);
});
}
saveUnsyncedFiles(params) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare(`
INSERT INTO unsynced_files
(integration_id, crowdin_id, files)
VALUES
(, , )
`).run(params);
});
}
getUnsyncedFiles(params) {
return __awaiter(this, void 0, void 0, function* () {
return this.db.prepare(`
SELECT files
FROM unsynced_files
WHERE integration_id = ? AND crowdin_id = ?
`).get(params.integrationId, params.crowdinId);
});
}
updateUnsyncedFiles(params) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare(`
UPDATE unsynced_files
SET files = ?
WHERE integration_id = ? AND crowdin_id = ?
`).run(params.files, params.integrationId, params.crowdinId);
});
}
registerCustomTable(tableName, schema) {
return __awaiter(this, void 0, void 0, function* () {
const columnsDefinition = Object.entries(schema)
.map(([columnName, columnType]) => `${columnName} ${columnType}`)
.join(', ');
const createTableQuery = `CREATE TABLE IF NOT EXISTS ${tableName} (${columnsDefinition})`;
this.db.prepare(createTableQuery).run();
});
}
insertRecord(tableName, data) {
return __awaiter(this, void 0, void 0, function* () {
const columns = Object.keys(data).join(', ');
const placeholders = Object.keys(data)
.map(() => '?')
.join(', ');
const values = Object.values(data);
const insertQuery = `INSERT INTO ${tableName} (${columns}) VALUES (${placeholders})`;
this.db.prepare(insertQuery).run(...values);
});
}
selectRecords(tableName_1) {
return __awaiter(this, arguments, void 0, function* (tableName, options = {}, params) {
var _a;
const columns = ((_a = options.columns) === null || _a === void 0 ? void 0 : _a.length) ? options.columns.join(', ') : '*';
const distinctKeyword = options.distinct ? 'DISTINCT ' : '';
const whereClause = options.whereClause ? ` ${options.whereClause}` : '';
const orderByClause = options.orderBy ? ` ORDER BY ${options.orderBy}` : '';
const limitClause = options.limit ? ` LIMIT ${options.limit}` : '';
const offsetClause = options.offset ? ` OFFSET ${options.offset}` : '';
const query = `SELECT ${distinctKeyword}${columns} FROM ${tableName}${whereClause}${orderByClause}${limitClause}${offsetClause};`;
return this.db.prepare(query).all(...(params || []));
});
}
updateRecord(tableName, data, whereClause, params) {
return __awaiter(this, void 0, void 0, function* () {
const setClause = Object.keys(data)
.map((key) => `${key} = ?`)
.join(', ');
const values = Object.values(data);
const query = `UPDATE ${tableName} SET ${setClause} ${whereClause};`;
this.db.prepare(query).run(...values, ...(params || []));
});
}
deleteRecord(tableName, whereClause, params) {
return __awaiter(this, void 0, void 0, function* () {
const query = `DELETE FROM ${tableName} ${whereClause};`;
this.db.prepare(query).run(...(params || []));
});
}
saveSyncedData(files, integrationId, crowdinId, type) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare(`
INSERT INTO synced_data
(files, integration_id, crowdin_id, type, updated_at)
VALUES
(, , , , )
`).run({ files, integrationId, crowdinId, type, updatedAt: Date.now().toString() });
});
}
updateSyncedData(files, integrationId, crowdinId, type) {
return __awaiter(this, void 0, void 0, function* () {
this.db.prepare(`
UPDATE synced_data
SET files = , updated_at =
WHERE integration_id = AND crowdin_id = AND type =
`).run({ files, integrationId, crowdinId, type, updatedAt: Date.now().toString() });
});
}
getSyncedData(integrationId, crowdinId, type) {
return __awaiter(this, void 0, void 0, function* () {
const query = `
SELECT id, files, integration_id as integrationId, crowdin_id as crowdinId, type, updated_at as updatedAt
FROM synced_data
WHERE integration_id = AND crowdin_id = AND type =
`;
return this.db.prepare(query).get({ integrationId, crowdinId, type });
});
}
}
exports.SQLiteStorage = SQLiteStorage;