@crowdin/app-project-module
Version:
Module that generates for you all common endpoints for serving standalone Crowdin App
140 lines (139 loc) • 7.15 kB
JavaScript
;
/* eslint-disable @typescript-eslint/camelcase */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getStorage = exports.initialize = exports.TABLES = void 0;
const types_1 = require("../types");
const logger_1 = require("../util/logger");
const mysql_1 = require("./mysql");
const postgre_1 = require("./postgre");
const sqlite_1 = require("./sqlite");
const path_1 = __importStar(require("path"));
const fs_1 = __importDefault(require("fs"));
const child_process_1 = require("child_process");
const util = __importStar(require("node:util"));
exports.TABLES = {
crowdin_credentials: 'crowdin_credentials',
integration_credentials: 'integration_credentials',
sync_settings: 'sync_settings',
app_metadata: 'app_metadata',
files_snapshot: 'files_snapshot',
webhooks: 'webhooks',
user_errors: 'user_errors',
integration_settings: 'integration_settings',
job: 'job',
translation_file_cache: 'translation_file_cache',
unsynced_files: 'unsynced_files',
synced_data: 'synced_data',
};
let storage;
function initialize(config) {
return __awaiter(this, void 0, void 0, function* () {
if (config.postgreConfig) {
(0, logger_1.log)('Using PostgreSQL database');
let dumpDirectory = null;
if (config.migrateToPostgreFromSQLite) {
dumpDirectory = config.dbFolder;
createDumpForMigration(config);
}
storage = new postgre_1.PostgreStorage(config.postgreConfig, dumpDirectory);
}
else if (config.mysqlConfig) {
(0, logger_1.log)('Using MySQL database');
storage = new mysql_1.MySQLStorage(config.mysqlConfig);
}
else {
(0, logger_1.log)('Using SQLite database');
if (config.migrateToPostgreFromSQLite === false) {
(0, logger_1.log)('Try to get SQLite file from backup');
getSqLiteFileFromBackup(config);
}
storage = new sqlite_1.SQLiteStorage({ dbFolder: config.dbFolder });
}
yield storage.migrate();
});
}
exports.initialize = initialize;
function getStorage() {
if (!storage) {
throw new Error('Storage not initialized');
}
return storage;
}
exports.getStorage = getStorage;
function createDumpForMigration(config) {
const sqliteFilePath = (0, path_1.join)(config.dbFolder, types_1.storageFiles.SQLITE);
const backupFilePath = (0, path_1.join)(config.dbFolder, types_1.storageFiles.SQLITE_BACKUP);
if (!fs_1.default.existsSync(sqliteFilePath)) {
(0, logger_1.log)('SQLite database not found, skipping migration dump creation');
return;
}
(0, logger_1.log)('Creating dump for migration from SQLite to PostgreSQL');
for (const tableName in exports.TABLES) {
(0, logger_1.log)(`Creating dump for table ${tableName}`);
const dumpFileName = util.format(types_1.storageFiles.DUMP, tableName);
const dumpFilePath = path_1.default.join(config.dbFolder, dumpFileName);
(0, child_process_1.execSync)(`sqlite3 ${sqliteFilePath} ".output ${dumpFilePath}" ".dump ${tableName}" ".output stdout"`);
let modifiedContent = fs_1.default.readFileSync(dumpFilePath).toString();
// 1. Remove SQLite-specific PRAGMA statements
modifiedContent = modifiedContent.replace(/PRAGMA foreign_keys=OFF;\n/g, '');
// 2. Adjust transaction syntax for PostgreSQL
modifiedContent = modifiedContent.replace(/BEGIN TRANSACTION;\n/g, '');
modifiedContent = modifiedContent.replace(/COMMIT TRANSACTION;\n/g, '');
// 3. Ensure tables are only created if they don't already exist
modifiedContent = modifiedContent
.replace(/CREATE TABLE IF NOT EXISTS/g, 'CREATE TABLE') // Remove duplicate IF NOT EXISTS
.replace(/CREATE TABLE/g, 'CREATE TABLE IF NOT EXISTS'); // Add IF NOT EXISTS if missing
// 4. Add `ON CONFLICT DO NOTHING` to INSERT statements to handle conflicts gracefully
modifiedContent = modifiedContent.replace(/(INSERT INTO [^;]+)(;)/g, '$1 ON CONFLICT DO NOTHING;');
// 5. Convert SQLite-specific data types to PostgreSQL equivalents
modifiedContent = modifiedContent.replace(/integer not null primary key autoincrement/gi, 'serial primary key');
modifiedContent = modifiedContent.replace(/varchar not null primary key/gi, 'varchar primary key');
// 6. Remove SQLite-specific function replace()
modifiedContent = modifiedContent.replace(/replace\s*\(\s*'([^']*(?:'{2}[^']*)*)',\s*'\\n',\s*char\s*\(\s*10\s*\)\s*\)/gi, "'$1'");
fs_1.default.writeFileSync(dumpFilePath, modifiedContent, { encoding: 'utf8', flag: 'w' });
}
fs_1.default.renameSync(sqliteFilePath, backupFilePath);
}
function getSqLiteFileFromBackup(config) {
const sqliteFilePath = (0, path_1.join)(config.dbFolder, types_1.storageFiles.SQLITE);
const backupFilePath = (0, path_1.join)(config.dbFolder, types_1.storageFiles.SQLITE_BACKUP);
if (fs_1.default.existsSync(backupFilePath) && !fs_1.default.existsSync(sqliteFilePath)) {
(0, logger_1.log)('Restoring SQLite database from backup');
fs_1.default.renameSync(backupFilePath, sqliteFilePath);
}
}