vr-migrations 1.0.29 → 1.0.31
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/db.d.ts
CHANGED
|
@@ -1,12 +1,22 @@
|
|
|
1
1
|
import { Sequelize } from "sequelize";
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
export interface DatabaseConfig {
|
|
3
|
+
DB_HOST?: string;
|
|
4
|
+
DB_PORT?: number;
|
|
5
|
+
DB_USER?: string;
|
|
6
|
+
DB_PASSWORD?: string;
|
|
7
|
+
DB_NAME?: string;
|
|
8
|
+
DATABASE_URL?: string;
|
|
9
9
|
NODE_ENV?: string;
|
|
10
|
-
|
|
10
|
+
pool?: {
|
|
11
|
+
max?: number;
|
|
12
|
+
min?: number;
|
|
13
|
+
acquire?: number;
|
|
14
|
+
idle?: number;
|
|
15
|
+
};
|
|
16
|
+
logging?: boolean | ((sql: string, timing?: number) => void);
|
|
17
|
+
dialectOptions?: any;
|
|
18
|
+
}
|
|
19
|
+
export declare function getSequelize(): Sequelize;
|
|
20
|
+
export declare function initializeDatabase(config: DatabaseConfig): Promise<Sequelize>;
|
|
11
21
|
export declare function closeDatabase(): Promise<void>;
|
|
12
22
|
export { Model, InferAttributes, InferCreationAttributes, DataTypes, CreationOptional, ModelStatic, ModelAttributes, NonAttribute, Op, } from "sequelize";
|
package/dist/db.js
CHANGED
|
@@ -4,6 +4,7 @@ exports.Op = exports.DataTypes = exports.Model = void 0;
|
|
|
4
4
|
exports.getSequelize = getSequelize;
|
|
5
5
|
exports.initializeDatabase = initializeDatabase;
|
|
6
6
|
exports.closeDatabase = closeDatabase;
|
|
7
|
+
// Updated vr-migrations/index.ts
|
|
7
8
|
const sequelize_1 = require("sequelize");
|
|
8
9
|
let _sequelize = null;
|
|
9
10
|
let _isInitialized = false;
|
|
@@ -16,29 +17,67 @@ function getSequelize() {
|
|
|
16
17
|
async function initializeDatabase(config) {
|
|
17
18
|
if (_isInitialized)
|
|
18
19
|
return _sequelize;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
20
|
+
const isProduction = config.NODE_ENV === "production";
|
|
21
|
+
const isStaging = config.NODE_ENV === "staging";
|
|
22
|
+
// Determine if we should use SSL (Neon requires SSL)
|
|
23
|
+
const shouldUseSSL = isProduction || isStaging || config.DATABASE_URL?.includes("neon.tech");
|
|
24
|
+
// Default pool configuration
|
|
25
|
+
const poolConfig = config.pool || {
|
|
26
|
+
max: 20,
|
|
27
|
+
min: 2,
|
|
28
|
+
acquire: 120000,
|
|
29
|
+
idle: 10000,
|
|
30
|
+
};
|
|
31
|
+
// Default dialect options
|
|
32
|
+
let dialectOptions = config.dialectOptions || {};
|
|
33
|
+
// Add SSL for production/staging/Neon
|
|
34
|
+
if (shouldUseSSL) {
|
|
35
|
+
dialectOptions = {
|
|
36
|
+
...dialectOptions,
|
|
37
|
+
ssl: {
|
|
38
|
+
require: true,
|
|
39
|
+
rejectUnauthorized: false, // Required for Neon
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
// Option 1: Use DATABASE_URL if provided
|
|
44
|
+
if (config.DATABASE_URL) {
|
|
45
|
+
console.log(`📊 Initializing database with connection URL for ${config.NODE_ENV || "default"} environment`);
|
|
46
|
+
_sequelize = new sequelize_1.Sequelize(config.DATABASE_URL, {
|
|
47
|
+
dialect: "postgres",
|
|
48
|
+
logging: config.logging !== undefined ? config.logging : false,
|
|
49
|
+
pool: poolConfig,
|
|
50
|
+
dialectOptions,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
// Option 2: Use individual connection parameters
|
|
54
|
+
else if (config.DB_HOST && config.DB_USER && config.DB_NAME) {
|
|
55
|
+
console.log(`📊 Initializing database with connection parameters for ${config.NODE_ENV || "default"} environment`);
|
|
56
|
+
_sequelize = new sequelize_1.Sequelize({
|
|
57
|
+
dialect: "postgres",
|
|
58
|
+
host: config.DB_HOST,
|
|
59
|
+
port: config.DB_PORT || 5432,
|
|
60
|
+
username: config.DB_USER,
|
|
61
|
+
password: config.DB_PASSWORD,
|
|
62
|
+
database: config.DB_NAME,
|
|
63
|
+
logging: config.logging !== undefined ? config.logging : false,
|
|
64
|
+
pool: poolConfig,
|
|
65
|
+
dialectOptions,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
throw new Error("Invalid database configuration. Provide either DATABASE_URL or (DB_HOST, DB_USER, DB_NAME)");
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
await _sequelize.authenticate();
|
|
73
|
+
console.log(`✅ Database connection established successfully`);
|
|
74
|
+
_isInitialized = true;
|
|
75
|
+
return _sequelize;
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
console.error("❌ Database connection failed:", error);
|
|
79
|
+
throw error;
|
|
80
|
+
}
|
|
42
81
|
}
|
|
43
82
|
async function closeDatabase() {
|
|
44
83
|
if (_sequelize) {
|
|
@@ -24,7 +24,7 @@ module.exports = {
|
|
|
24
24
|
key: "id",
|
|
25
25
|
},
|
|
26
26
|
onUpdate: "CASCADE",
|
|
27
|
-
onDelete: "
|
|
27
|
+
onDelete: "SET NULL", // ← This is SET NULL, not CASCADE
|
|
28
28
|
},
|
|
29
29
|
adminId: {
|
|
30
30
|
type: Sequelize.UUID,
|
|
@@ -34,7 +34,7 @@ module.exports = {
|
|
|
34
34
|
key: "id",
|
|
35
35
|
},
|
|
36
36
|
onUpdate: "CASCADE",
|
|
37
|
-
onDelete: "
|
|
37
|
+
onDelete: "SET NULL", // ← This is SET NULL, not CASCADE
|
|
38
38
|
},
|
|
39
39
|
reason: {
|
|
40
40
|
type: Sequelize.TEXT,
|
|
@@ -24,7 +24,7 @@ module.exports = {
|
|
|
24
24
|
key: "id",
|
|
25
25
|
},
|
|
26
26
|
onUpdate: "CASCADE",
|
|
27
|
-
onDelete: "
|
|
27
|
+
onDelete: "SET NULL", // ← This is SET NULL, not CASCADE
|
|
28
28
|
},
|
|
29
29
|
adminId: {
|
|
30
30
|
type: Sequelize.UUID,
|
|
@@ -34,7 +34,7 @@ module.exports = {
|
|
|
34
34
|
key: "id",
|
|
35
35
|
},
|
|
36
36
|
onUpdate: "CASCADE",
|
|
37
|
-
onDelete: "
|
|
37
|
+
onDelete: "SET NULL", // ← This is SET NULL, not CASCADE
|
|
38
38
|
},
|
|
39
39
|
reason: {
|
|
40
40
|
type: Sequelize.TEXT,
|