@lobb-js/core 0.26.0 → 0.26.2
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/package.json
CHANGED
|
@@ -35,11 +35,27 @@ export class DatabaseSyncManager {
|
|
|
35
35
|
// Startup tasks run first — applies any idempotent DB fixes before schema reading begins.
|
|
36
36
|
await runCoreDbSetup();
|
|
37
37
|
|
|
38
|
+
const dbSchemaDiff = await this.getDbDifferences(specificColleciton);
|
|
39
|
+
|
|
40
|
+
// Pass 1: additive ops only (add-collection, add-field) — always safe, no data loss possible.
|
|
41
|
+
// Running these before migrations means any new collection defined in config already exists
|
|
42
|
+
// when migration code tries to seed it.
|
|
43
|
+
const additiveOps = dbSchemaDiff.filter(
|
|
44
|
+
(op) => op.op === "add-collection" || op.op === "add-field",
|
|
45
|
+
);
|
|
46
|
+
await this.applyDbSchemaDiff(additiveOps, forceSync);
|
|
47
|
+
|
|
48
|
+
// Migrations run after additive schema changes so they can safely insert into new collections.
|
|
38
49
|
const migrationsManager = new MigrationsManager();
|
|
39
50
|
await migrationsManager.init();
|
|
40
51
|
|
|
41
|
-
|
|
42
|
-
|
|
52
|
+
// Pass 2: everything else (remove-collection, remove-field, alter-field, indexes).
|
|
53
|
+
// Destructive ops run after migrations so a migration can clear data before applyDbSchemaDiff
|
|
54
|
+
// would otherwise refuse to drop a column/table that still has rows.
|
|
55
|
+
const remainingOps = dbSchemaDiff.filter(
|
|
56
|
+
(op) => op.op !== "add-collection" && op.op !== "add-field",
|
|
57
|
+
);
|
|
58
|
+
await this.applyDbSchemaDiff(remainingOps, forceSync);
|
|
43
59
|
}
|
|
44
60
|
|
|
45
61
|
private async applyDbSchemaDiff(ops: SchemaDiffOp[], forceSync: boolean) {
|
|
@@ -38,13 +38,43 @@ export class PGDriver extends DatabaseDriver {
|
|
|
38
38
|
|
|
39
39
|
public async createConnection() {
|
|
40
40
|
const default_pool_connections = (cpus().length * 2) + 4;
|
|
41
|
+
const { host, port } = this.databaseConfig;
|
|
41
42
|
|
|
42
|
-
|
|
43
|
-
|
|
43
|
+
try {
|
|
44
|
+
// Ensure the database exists before connecting
|
|
45
|
+
await this.ensureDatabaseExists();
|
|
46
|
+
} catch (err: any) {
|
|
47
|
+
const addr = `${host}:${port}`;
|
|
48
|
+
if (err.code === "ESERVFAIL" || err.code === "ENOTFOUND" || err.code === "EAI_AGAIN") {
|
|
49
|
+
throw new LobbError({
|
|
50
|
+
code: "INTERNAL_SERVER_ERROR",
|
|
51
|
+
message: `Cannot connect to database: failed to resolve host "${host}". Make sure the database server is running and reachable at ${addr}.`,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
if (err.code === "ECONNREFUSED") {
|
|
55
|
+
throw new LobbError({
|
|
56
|
+
code: "INTERNAL_SERVER_ERROR",
|
|
57
|
+
message: `Cannot connect to database: connection refused at ${addr}. Make sure the database server is running.`,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
if (err.code === "ETIMEDOUT") {
|
|
61
|
+
throw new LobbError({
|
|
62
|
+
code: "INTERNAL_SERVER_ERROR",
|
|
63
|
+
message: `Cannot connect to database: connection timed out at ${addr}. Make sure the database server is reachable.`,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
if ((err as any).code === "28P01") {
|
|
67
|
+
throw new LobbError({
|
|
68
|
+
code: "INTERNAL_SERVER_ERROR",
|
|
69
|
+
message: `Cannot connect to database: authentication failed for user "${this.databaseConfig.username}" at ${addr}. Check your database credentials.`,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
throw err;
|
|
73
|
+
}
|
|
44
74
|
|
|
45
75
|
// Now connect to the actual target database
|
|
46
76
|
this.pool = new Pool({
|
|
47
|
-
host
|
|
77
|
+
host,
|
|
48
78
|
port: this.databaseConfig.port,
|
|
49
79
|
user: this.databaseConfig.username,
|
|
50
80
|
password: this.databaseConfig.password,
|