@earbug/db-models 0.0.24 → 0.0.26

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.
@@ -0,0 +1,3 @@
1
+ export * from "./init-models";
2
+ export { initDb } from "./initDb";
3
+ export type { InitDbOptions } from "./initDb";
package/dist/index.js ADDED
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.initDb = void 0;
18
+ __exportStar(require("./init-models"), exports);
19
+ var initDb_1 = require("./initDb");
20
+ Object.defineProperty(exports, "initDb", { enumerable: true, get: function () { return initDb_1.initDb; } });
@@ -0,0 +1,7 @@
1
+ import { Sequelize, Options } from "sequelize";
2
+ export interface InitDbOptions {
3
+ pool?: Options["pool"];
4
+ retry?: Options["retry"];
5
+ logging?: Options["logging"];
6
+ }
7
+ export declare function initDb(options?: InitDbOptions): Sequelize;
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
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./init-models";
2
+ export { initDb } from "./initDb";
3
+ export type { InitDbOptions } from "./initDb";
@@ -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
+ }
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@earbug/db-models",
3
- "version": "0.0.24",
4
- "main": "dist/init-models.js",
5
- "types": "dist/init-models.d.ts",
3
+ "version": "0.0.26",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
6
  "scripts": {
7
7
  "generate-models": "sequelize-auto -l ts --cm=p --cf=p --cp=c -o \"./models\" -a ./model_options.json -d earbug_live -h localhost -u postgres -p 5432 -e postgres -T harvested_article article_to_artist_relation",
8
8
  "build": "tsc"