@earbug/db-models 0.0.24 → 0.0.25
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/dist/initDb.d.ts +7 -0
- package/dist/initDb.js +42 -0
- package/models/initDb.ts +60 -0
- package/package.json +1 -1
package/dist/initDb.d.ts
ADDED
package/dist/initDb.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.initDb = initDb;
|
|
4
|
+
const sequelize_1 = require("sequelize");
|
|
5
|
+
const init_models_1 = require("./init-models");
|
|
6
|
+
const REQUIRED_ENV_VARS = ["PG_DATABASE", "PG_USER", "PG_HOST"];
|
|
7
|
+
function initDb(options = {}) {
|
|
8
|
+
var _a, _b, _c;
|
|
9
|
+
const missing = REQUIRED_ENV_VARS.filter((v) => !process.env[v]);
|
|
10
|
+
if (missing.length > 0) {
|
|
11
|
+
throw new Error(`Missing required environment variables: ${missing.join(", ")}`);
|
|
12
|
+
}
|
|
13
|
+
console.log(`Initializing DB connection to ${process.env.PG_HOST}:${process.env.PG_PORT || "5432"}/${process.env.PG_DATABASE}...`);
|
|
14
|
+
const sequelize = new sequelize_1.Sequelize(process.env.PG_DATABASE, process.env.PG_USER, process.env.PG_PASSWORD, {
|
|
15
|
+
host: process.env.PG_HOST,
|
|
16
|
+
port: parseInt(process.env.PG_PORT || "5432"),
|
|
17
|
+
dialect: "postgres",
|
|
18
|
+
schema: "eb",
|
|
19
|
+
minifyAliases: true,
|
|
20
|
+
logging: (_a = options.logging) !== null && _a !== void 0 ? _a : false,
|
|
21
|
+
pool: (_b = options.pool) !== null && _b !== void 0 ? _b : {
|
|
22
|
+
max: 10,
|
|
23
|
+
min: 1,
|
|
24
|
+
acquire: 30000,
|
|
25
|
+
idle: 10000,
|
|
26
|
+
evict: 1000,
|
|
27
|
+
},
|
|
28
|
+
retry: (_c = options.retry) !== null && _c !== void 0 ? _c : {
|
|
29
|
+
max: 2,
|
|
30
|
+
match: [
|
|
31
|
+
/ConnectionError/,
|
|
32
|
+
/ConnectionRefused/,
|
|
33
|
+
/ConnectionAcquireTimeout/,
|
|
34
|
+
/ECONNRESET/,
|
|
35
|
+
/ETIMEDOUT/,
|
|
36
|
+
],
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
(0, init_models_1.initModels)(sequelize);
|
|
40
|
+
console.log("DB connection initialized.");
|
|
41
|
+
return sequelize;
|
|
42
|
+
}
|
package/models/initDb.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Sequelize, Options } from "sequelize";
|
|
2
|
+
import { initModels } from "./init-models";
|
|
3
|
+
|
|
4
|
+
const REQUIRED_ENV_VARS = ["PG_DATABASE", "PG_USER", "PG_HOST"] as const;
|
|
5
|
+
|
|
6
|
+
export interface InitDbOptions {
|
|
7
|
+
pool?: Options["pool"];
|
|
8
|
+
retry?: Options["retry"];
|
|
9
|
+
logging?: Options["logging"];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function initDb(options: InitDbOptions = {}): Sequelize {
|
|
13
|
+
const missing = REQUIRED_ENV_VARS.filter((v) => !process.env[v]);
|
|
14
|
+
if (missing.length > 0) {
|
|
15
|
+
throw new Error(
|
|
16
|
+
`Missing required environment variables: ${missing.join(", ")}`,
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
console.log(
|
|
21
|
+
`Initializing DB connection to ${process.env.PG_HOST}:${process.env.PG_PORT || "5432"}/${process.env.PG_DATABASE}...`,
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
const sequelize = new Sequelize(
|
|
25
|
+
process.env.PG_DATABASE!,
|
|
26
|
+
process.env.PG_USER!,
|
|
27
|
+
process.env.PG_PASSWORD,
|
|
28
|
+
{
|
|
29
|
+
host: process.env.PG_HOST,
|
|
30
|
+
port: parseInt(process.env.PG_PORT || "5432"),
|
|
31
|
+
dialect: "postgres",
|
|
32
|
+
schema: "eb",
|
|
33
|
+
minifyAliases: true,
|
|
34
|
+
logging: options.logging ?? false,
|
|
35
|
+
pool: options.pool ?? {
|
|
36
|
+
max: 10,
|
|
37
|
+
min: 1,
|
|
38
|
+
acquire: 30000,
|
|
39
|
+
idle: 10000,
|
|
40
|
+
evict: 1000,
|
|
41
|
+
},
|
|
42
|
+
retry: options.retry ?? {
|
|
43
|
+
max: 2,
|
|
44
|
+
match: [
|
|
45
|
+
/ConnectionError/,
|
|
46
|
+
/ConnectionRefused/,
|
|
47
|
+
/ConnectionAcquireTimeout/,
|
|
48
|
+
/ECONNRESET/,
|
|
49
|
+
/ETIMEDOUT/,
|
|
50
|
+
],
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
initModels(sequelize);
|
|
56
|
+
|
|
57
|
+
console.log("DB connection initialized.");
|
|
58
|
+
|
|
59
|
+
return sequelize;
|
|
60
|
+
}
|