@earbug/db-models 0.0.23 → 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/AppUser.d.ts CHANGED
@@ -23,7 +23,7 @@ export interface AppUserAttributes {
23
23
  createDate: Date;
24
24
  updateDate: Date;
25
25
  syncContacts?: boolean;
26
- stateId?: number;
26
+ stateId: number;
27
27
  lastName?: string;
28
28
  autoApproveFollowers: boolean;
29
29
  lastHarvested?: Date;
@@ -41,7 +41,7 @@ export declare class AppUser extends Model<AppUserAttributes, AppUserCreationAtt
41
41
  createDate: Date;
42
42
  updateDate: Date;
43
43
  syncContacts?: boolean;
44
- stateId?: number;
44
+ stateId: number;
45
45
  lastName?: string;
46
46
  autoApproveFollowers: boolean;
47
47
  lastHarvested?: Date;
package/dist/AppUser.js CHANGED
@@ -65,7 +65,7 @@ class AppUser extends sequelize_1.Model {
65
65
  },
66
66
  stateId: {
67
67
  type: sequelize_1.DataTypes.SMALLINT,
68
- allowNull: true,
68
+ allowNull: false,
69
69
  defaultValue: 0,
70
70
  references: {
71
71
  model: 'state',
@@ -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
+ }
package/models/AppUser.ts CHANGED
@@ -24,7 +24,7 @@ export interface AppUserAttributes {
24
24
  createDate: Date;
25
25
  updateDate: Date;
26
26
  syncContacts?: boolean;
27
- stateId?: number;
27
+ stateId: number;
28
28
  lastName?: string;
29
29
  autoApproveFollowers: boolean;
30
30
  lastHarvested?: Date;
@@ -44,7 +44,7 @@ export class AppUser extends Model<AppUserAttributes, AppUserCreationAttributes>
44
44
  createDate!: Date;
45
45
  updateDate!: Date;
46
46
  syncContacts?: boolean;
47
- stateId?: number;
47
+ stateId!: number;
48
48
  lastName?: string;
49
49
  autoApproveFollowers!: boolean;
50
50
  lastHarvested?: Date;
@@ -304,7 +304,7 @@ export class AppUser extends Model<AppUserAttributes, AppUserCreationAttributes>
304
304
  },
305
305
  stateId: {
306
306
  type: DataTypes.SMALLINT,
307
- allowNull: true,
307
+ allowNull: false,
308
308
  defaultValue: 0,
309
309
  references: {
310
310
  model: 'state',
@@ -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,6 +1,6 @@
1
1
  {
2
2
  "name": "@earbug/db-models",
3
- "version": "0.0.23",
3
+ "version": "0.0.25",
4
4
  "main": "dist/init-models.js",
5
5
  "types": "dist/init-models.d.ts",
6
6
  "scripts": {