@grouparoo/core
Version:
The Grouparoo Core
195 lines (194 loc) • 7.96 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT = void 0;
const fs_1 = __importDefault(require("fs"));
const url_1 = require("url");
const path_1 = require("path");
const pluginDetails_1 = require("../modules/pluginDetails");
const actionhero_1 = require("actionhero");
const cls_hooked_1 = __importDefault(require("cls-hooked"));
const sequelize_1 = __importDefault(require("sequelize"));
const namespace = "sequelize";
// Opt Into CLS
// Learn more @ https://sequelize.org/master/manual/transactions.html and https://github.com/Jeff-Lewis/cls-hooked
// @ts-ignore
sequelize_1.default.useCLS(cls_hooked_1.default.createNamespace("grouparoo-cls"));
// we want BIGINTs to be returned as JS integer types
require("pg").defaults.parseInt8 = true;
exports.DEFAULT = {
[namespace]: (config) => {
var _a, _b, _c, _d, _e;
const env = (_a = process.env.NODE_ENV) !== null && _a !== void 0 ? _a : "development";
let storage; //only for sqlite
let dialect = process.env.DB_DIALECT;
let host = process.env.DB_HOST;
let port = process.env.DB_PORT;
let database = process.env.DB_DATABASE;
let username = process.env.DB_USER;
let password = process.env.DB_PASS || undefined;
let schema = process.env.DB_SCHEMA || undefined;
let ssl = false;
// if your environment provides database information via a single JDBC-style URL
// like mysql://username:password@hostname:port/default_schema
let parsed = {};
const connectionURL = process.env.DATABASE_URL || process.env.MYSQL_URL || process.env.PG_URL;
if (connectionURL)
parsed = new url_1.URL(connectionURL);
if (parsed.protocol)
dialect = parsed.protocol.slice(0, -1).toLowerCase();
if (dialect === "postgresql")
dialect = "postgres";
if (dialect === "psql")
dialect = "postgres";
/** POSTGRES */
if (dialect === "postgres") {
if (parsed.username)
username = parsed.username;
if (parsed.password)
password = parsed.password;
if (parsed.hostname)
host = parsed.hostname;
if (parsed.port)
port = parsed.port;
if (parsed.pathname) {
database = parsed.pathname.substring(1);
if (database.includes("/")) {
const dbAndSchema = database.split("/");
if (dbAndSchema[0].trim() !== "")
database = dbAndSchema[0];
if (dbAndSchema[1].trim() !== "")
schema = dbAndSchema[1];
}
}
const search_ssl = (_b = parsed.searchParams) === null || _b === void 0 ? void 0 : _b.get("ssl");
const search_sslmode = (_c = parsed.searchParams) === null || _c === void 0 ? void 0 : _c.get("sslmode");
if (search_ssl)
ssl = search_ssl === "true";
if (search_sslmode) {
ssl = search_sslmode === "true" || search_sslmode === "required";
}
if (((_d = process.env.DATABASE_SSL) === null || _d === void 0 ? void 0 : _d.toLowerCase()) === "true") {
ssl = true;
}
if (((_e = process.env.DATABASE_SSL_SELF_SIGNED) === null || _e === void 0 ? void 0 : _e.toLowerCase()) === "true") {
ssl = { rejectUnauthorized: false };
}
}
/** SQLITE */
if (dialect === "sqlite") {
storage = ":memory:";
if (parsed.hostname || parsed.pathname) {
storage = `${parsed.hostname}${parsed.pathname}`;
}
if (env === "test" && !parsed.hostname && !parsed.pathname) {
storage = (0, path_1.join)((0, pluginDetails_1.getParentPath)(), `${database}.sqlite`);
}
if (storage === "memory")
storage = ":memory:";
// without a starting "/" we assume relative locations are against project root
if (storage !== ":memory:" && !(0, path_1.isAbsolute)(storage)) {
storage = (0, path_1.join)((0, pluginDetails_1.getParentPath)(), storage);
}
}
/** Query Logging */
function logging(message, time) {
if (typeof message !== "string")
return;
(0, actionhero_1.log)(message, "debug", { time });
}
/** Migration */
function shouldAutoMigrate() {
const runMode = process.env.GROUPAROO_RUN_MODE;
const workers = parseInt(process.env.WORKERS || "0");
if (["cli:apply"].includes(runMode)) {
// doing it in the same transaction elsewhere
return false;
}
if (["test", "development"].includes(env)) {
// working locally, make sure to migrate
return true;
}
if (["cli:config"].includes(runMode)) {
// always migrate because don't have other workers
return true;
}
if (workers > 0) {
// in production, background servers do the migration
return true;
}
return false;
}
/** Load plugin migrations */
const { plugins } = (0, pluginDetails_1.getPluginManifest)();
const pluginMigrations = [];
for (const plugin of plugins) {
const path = (0, path_1.join)(plugin.path, "dist", "migrations");
if (fs_1.default.existsSync(path)) {
pluginMigrations.push(path);
}
}
return {
_toExpand: false,
logging,
benchmark: true,
autoMigrate: shouldAutoMigrate(),
dialect: dialect,
port: parseInt(port),
database: database,
host: host,
username: username,
password: password,
schema: schema,
searchPath: schema,
models: [(0, path_1.join)(__dirname, "..", "models")],
migrations: [(0, path_1.join)(__dirname, "..", "migrations"), ...pluginMigrations],
migrationLogLevel: storage === ":memory:" ? "debug" : "info",
storage,
dialectOptions: { ssl, prependSearchPath: schema !== undefined },
transactionType: "DEFERRED",
pool: {
max: dialect === "sqlite"
? 1
: Math.max(parseInt(process.env.SEQUELIZE_POOL_SIZE || "0"), parseInt(process.env.WORKERS || "0") + 1, 1),
min: 1,
acquire: 30000,
idle: 10000,
evict: 1000,
},
retry: {
match: [
sequelize_1.default.ConnectionError,
sequelize_1.default.DatabaseError,
sequelize_1.default.TimeoutError,
sequelize_1.default.ConnectionTimedOutError,
],
backoffBase: dialect === "sqlite" ? 1000 : 100,
backoffExponent: dialect === "sqlite" ? 1.5 : 1.1,
max: 5,
timeout: undefined,
},
};
},
};
// for the sequelize CLI tool
module.exports.development = exports.DEFAULT.sequelize({
env: "development",
process: {
env: "development",
},
});
module.exports.staging = exports.DEFAULT.sequelize({
env: "staging",
process: {
env: "staging",
},
});
module.exports.production = exports.DEFAULT.sequelize({
env: "production",
process: {
env: "production",
},
});