@rvoh/dream 0.35.0 → 0.35.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/dist/cjs/src/Dream.js +12 -22
- package/dist/cjs/src/bin/helpers/sync.js +2 -3
- package/dist/cjs/src/bin/index.js +17 -17
- package/dist/cjs/src/db/DreamDbConnection.js +10 -16
- package/dist/cjs/src/dream-application/index.js +22 -0
- package/dist/cjs/src/helpers/areEqual.js +29 -0
- package/dist/cjs/src/helpers/db/createDb.js +3 -3
- package/dist/cjs/src/helpers/db/dropDb.js +8 -5
- package/dist/cjs/src/helpers/notEqual.js +7 -0
- package/dist/cjs/src/helpers/uniq.js +3 -4
- package/dist/esm/src/Dream.js +12 -22
- package/dist/esm/src/bin/helpers/sync.js +2 -3
- package/dist/esm/src/bin/index.js +17 -17
- package/dist/esm/src/db/DreamDbConnection.js +10 -16
- package/dist/esm/src/dream-application/index.js +22 -0
- package/dist/esm/src/helpers/areEqual.js +26 -0
- package/dist/esm/src/helpers/db/createDb.js +3 -3
- package/dist/esm/src/helpers/db/dropDb.js +8 -5
- package/dist/esm/src/helpers/notEqual.js +4 -0
- package/dist/esm/src/helpers/uniq.js +3 -4
- package/dist/types/src/Dream.d.ts +3 -2
- package/dist/types/src/db/DreamDbConnection.d.ts +1 -0
- package/dist/types/src/dream/Query.d.ts +2 -2
- package/dist/types/src/dream-application/index.d.ts +5 -0
- package/dist/types/src/helpers/areEqual.d.ts +1 -0
- package/dist/types/src/helpers/notEqual.d.ts +1 -0
- package/dist/types/src/types/query.d.ts +38 -0
- package/docs/assets/search.js +1 -1
- package/docs/classes/Benchmark.html +2 -2
- package/docs/classes/CalendarDate.html +2 -2
- package/docs/classes/CreateOrFindByFailedToCreateAndFind.html +3 -3
- package/docs/classes/Decorators.html +19 -19
- package/docs/classes/Dream.html +129 -128
- package/docs/classes/DreamApplication.html +9 -5
- package/docs/classes/DreamBin.html +2 -2
- package/docs/classes/DreamCLI.html +4 -4
- package/docs/classes/DreamImporter.html +2 -2
- package/docs/classes/DreamLogos.html +2 -2
- package/docs/classes/DreamMigrationHelpers.html +7 -7
- package/docs/classes/DreamSerializer.html +2 -2
- package/docs/classes/DreamTransaction.html +2 -2
- package/docs/classes/Encrypt.html +2 -2
- package/docs/classes/Env.html +2 -2
- package/docs/classes/GlobalNameNotSet.html +3 -3
- package/docs/classes/NonLoadedAssociation.html +3 -3
- package/docs/classes/Query.html +51 -51
- package/docs/classes/Range.html +2 -2
- package/docs/classes/RecordNotFound.html +3 -3
- package/docs/classes/ValidationError.html +3 -3
- package/docs/functions/Attribute.html +1 -1
- package/docs/functions/RendersMany.html +1 -1
- package/docs/functions/RendersOne.html +1 -1
- package/docs/functions/ReplicaSafe.html +1 -1
- package/docs/functions/STI.html +1 -1
- package/docs/functions/SoftDelete.html +1 -1
- package/docs/functions/camelize.html +1 -1
- package/docs/functions/capitalize.html +1 -1
- package/docs/functions/closeAllDbConnections.html +1 -1
- package/docs/functions/compact.html +1 -1
- package/docs/functions/debug.html +1 -1
- package/docs/functions/dreamDbConnections.html +1 -1
- package/docs/functions/dreamPath.html +1 -1
- package/docs/functions/generateDream.html +1 -1
- package/docs/functions/globalClassNameFromFullyQualifiedModelName.html +1 -1
- package/docs/functions/hyphenize.html +1 -1
- package/docs/functions/inferSerializerFromDreamClassOrViewModelClass.html +1 -1
- package/docs/functions/inferSerializerFromDreamOrViewModel.html +1 -1
- package/docs/functions/isEmpty.html +1 -1
- package/docs/functions/loadRepl.html +1 -1
- package/docs/functions/lookupClassByGlobalName.html +1 -1
- package/docs/functions/pascalize.html +1 -1
- package/docs/functions/pgErrorType.html +1 -1
- package/docs/functions/range-1.html +1 -1
- package/docs/functions/relativeDreamPath.html +1 -1
- package/docs/functions/round.html +1 -1
- package/docs/functions/serializerNameFromFullyQualifiedModelName.html +1 -1
- package/docs/functions/sharedPathPrefix.html +1 -1
- package/docs/functions/snakeify.html +1 -1
- package/docs/functions/sort.html +1 -1
- package/docs/functions/sortBy.html +1 -1
- package/docs/functions/standardizeFullyQualifiedModelName.html +1 -1
- package/docs/functions/uncapitalize.html +1 -1
- package/docs/functions/uniq.html +1 -1
- package/docs/functions/untypedDb.html +1 -1
- package/docs/functions/validateColumn.html +1 -1
- package/docs/functions/validateTable.html +1 -1
- package/docs/interfaces/AttributeStatement.html +2 -2
- package/docs/interfaces/DecoratorContext.html +2 -2
- package/docs/interfaces/DreamApplicationInitOptions.html +2 -2
- package/docs/interfaces/DreamApplicationOpts.html +2 -2
- package/docs/interfaces/DreamSerializerAssociationStatement.html +2 -2
- package/docs/interfaces/EncryptOptions.html +2 -2
- package/docs/interfaces/OpenapiSchemaProperties.html +1 -1
- package/docs/interfaces/OpenapiSchemaPropertiesShorthand.html +1 -1
- package/docs/interfaces/OpenapiTypeFieldObject.html +1 -1
- package/docs/types/Camelized.html +1 -1
- package/docs/types/CommonOpenapiSchemaObjectFields.html +1 -1
- package/docs/types/DateTime.html +1 -1
- package/docs/types/DbConnectionType.html +1 -1
- package/docs/types/DreamAssociationMetadata.html +1 -1
- package/docs/types/DreamAttributes.html +1 -1
- package/docs/types/DreamClassColumn.html +1 -1
- package/docs/types/DreamColumn.html +1 -1
- package/docs/types/DreamColumnNames.html +1 -1
- package/docs/types/DreamLogLevel.html +1 -1
- package/docs/types/DreamLogger.html +1 -1
- package/docs/types/DreamOrViewModelSerializerKey.html +1 -1
- package/docs/types/DreamParamSafeAttributes.html +1 -1
- package/docs/types/DreamParamSafeColumnNames.html +1 -1
- package/docs/types/DreamSerializerKey.html +1 -1
- package/docs/types/DreamSerializers.html +1 -1
- package/docs/types/DreamTableSchema.html +1 -1
- package/docs/types/DreamVirtualColumns.html +1 -1
- package/docs/types/EncryptAlgorithm.html +1 -1
- package/docs/types/Hyphenized.html +1 -1
- package/docs/types/IdType.html +1 -1
- package/docs/types/OpenapiAllTypes.html +1 -1
- package/docs/types/OpenapiFormats.html +1 -1
- package/docs/types/OpenapiNumberFormats.html +1 -1
- package/docs/types/OpenapiPrimitiveTypes.html +1 -1
- package/docs/types/OpenapiSchemaArray.html +1 -1
- package/docs/types/OpenapiSchemaArrayShorthand.html +1 -1
- package/docs/types/OpenapiSchemaBase.html +1 -1
- package/docs/types/OpenapiSchemaBody.html +1 -1
- package/docs/types/OpenapiSchemaBodyShorthand.html +1 -1
- package/docs/types/OpenapiSchemaCommonFields.html +1 -1
- package/docs/types/OpenapiSchemaExpressionAllOf.html +1 -1
- package/docs/types/OpenapiSchemaExpressionAnyOf.html +1 -1
- package/docs/types/OpenapiSchemaExpressionOneOf.html +1 -1
- package/docs/types/OpenapiSchemaExpressionRef.html +1 -1
- package/docs/types/OpenapiSchemaExpressionRefSchemaShorthand.html +1 -1
- package/docs/types/OpenapiSchemaInteger.html +1 -1
- package/docs/types/OpenapiSchemaNull.html +1 -1
- package/docs/types/OpenapiSchemaNumber.html +1 -1
- package/docs/types/OpenapiSchemaObject.html +1 -1
- package/docs/types/OpenapiSchemaObjectAllOf.html +1 -1
- package/docs/types/OpenapiSchemaObjectAllOfShorthand.html +1 -1
- package/docs/types/OpenapiSchemaObjectAnyOf.html +1 -1
- package/docs/types/OpenapiSchemaObjectAnyOfShorthand.html +1 -1
- package/docs/types/OpenapiSchemaObjectBase.html +1 -1
- package/docs/types/OpenapiSchemaObjectBaseShorthand.html +1 -1
- package/docs/types/OpenapiSchemaObjectOneOf.html +1 -1
- package/docs/types/OpenapiSchemaObjectOneOfShorthand.html +1 -1
- package/docs/types/OpenapiSchemaObjectShorthand.html +1 -1
- package/docs/types/OpenapiSchemaPartialSegment.html +1 -1
- package/docs/types/OpenapiSchemaPrimitiveGeneric.html +1 -1
- package/docs/types/OpenapiSchemaShorthandExpressionAllOf.html +1 -1
- package/docs/types/OpenapiSchemaShorthandExpressionAnyOf.html +1 -1
- package/docs/types/OpenapiSchemaShorthandExpressionOneOf.html +1 -1
- package/docs/types/OpenapiSchemaShorthandExpressionSerializableRef.html +1 -1
- package/docs/types/OpenapiSchemaShorthandExpressionSerializerRef.html +1 -1
- package/docs/types/OpenapiSchemaShorthandPrimitiveGeneric.html +1 -1
- package/docs/types/OpenapiSchemaString.html +1 -1
- package/docs/types/OpenapiShorthandAllTypes.html +1 -1
- package/docs/types/OpenapiShorthandPrimitiveTypes.html +1 -1
- package/docs/types/OpenapiTypeField.html +1 -1
- package/docs/types/Pascalized.html +1 -1
- package/docs/types/PrimaryKeyType.html +1 -1
- package/docs/types/RoundingPrecision.html +1 -1
- package/docs/types/SerializableClassOrSerializerCallback.html +1 -1
- package/docs/types/SerializableDreamClassOrViewModelClass.html +1 -1
- package/docs/types/SerializableDreamOrViewModel.html +1 -1
- package/docs/types/SerializableTypes.html +1 -1
- package/docs/types/Snakeified.html +1 -1
- package/docs/types/Timestamp.html +1 -1
- package/docs/types/UpdateableAssociationProperties.html +1 -1
- package/docs/types/UpdateableProperties.html +1 -1
- package/docs/types/ValidationType.html +1 -1
- package/docs/types/ViewModelSerializerKey.html +1 -1
- package/docs/types/WhereStatementForDream.html +1 -1
- package/docs/types/WhereStatementForDreamClass.html +1 -1
- package/docs/variables/DateTime-1.html +1 -1
- package/docs/variables/DreamConst.html +1 -1
- package/docs/variables/TRIGRAM_OPERATORS.html +1 -1
- package/docs/variables/openapiPrimitiveTypes-1.html +1 -1
- package/docs/variables/openapiShorthandPrimitiveTypes-1.html +1 -1
- package/docs/variables/ops.html +1 -1
- package/docs/variables/primaryKeyTypes.html +1 -1
- package/package.json +2 -2
- package/dist/cjs/src/db/ConnectionConfRetriever.js +0 -22
- package/dist/esm/src/db/ConnectionConfRetriever.js +0 -19
- package/dist/types/src/db/ConnectionConfRetriever.d.ts +0 -6
package/dist/cjs/src/Dream.js
CHANGED
|
@@ -46,6 +46,7 @@ const DateTime_js_1 = require("./helpers/DateTime.js");
|
|
|
46
46
|
const cachedTypeForAttribute_js_1 = require("./helpers/db/cachedTypeForAttribute.js");
|
|
47
47
|
const isJsonColumn_js_1 = require("./helpers/db/types/isJsonColumn.js");
|
|
48
48
|
const inferSerializerFromDreamOrViewModel_js_1 = require("./helpers/inferSerializerFromDreamOrViewModel.js");
|
|
49
|
+
const notEqual_js_1 = require("./helpers/notEqual.js");
|
|
49
50
|
const typechecks_js_1 = require("./helpers/typechecks.js");
|
|
50
51
|
class Dream {
|
|
51
52
|
DB;
|
|
@@ -1233,18 +1234,7 @@ class Dream {
|
|
|
1233
1234
|
* @returns A Kysely query. Depending on the type passed, it will return either a SelectQueryBuilder, DeleteQueryBuilder, UpdateQueryBuilder, or an InsertQueryBuilder
|
|
1234
1235
|
*/
|
|
1235
1236
|
static toKysely(type) {
|
|
1236
|
-
|
|
1237
|
-
case 'select':
|
|
1238
|
-
return this.query().dbFor('select').selectFrom(this.table);
|
|
1239
|
-
case 'delete':
|
|
1240
|
-
return this.query().dbFor('delete').deleteFrom(this.table);
|
|
1241
|
-
case 'update':
|
|
1242
|
-
return this.query().dbFor('update').updateTable(this.table);
|
|
1243
|
-
case 'insert':
|
|
1244
|
-
return this.query().dbFor('insert').insertInto(this.table);
|
|
1245
|
-
default:
|
|
1246
|
-
throw new Error('never');
|
|
1247
|
-
}
|
|
1237
|
+
return this.query().toKysely(type);
|
|
1248
1238
|
}
|
|
1249
1239
|
/**
|
|
1250
1240
|
* Applies transaction to a new Query scoped
|
|
@@ -2249,13 +2239,8 @@ class Dream {
|
|
|
2249
2239
|
this.constructor.columns().forEach(column => {
|
|
2250
2240
|
const was = this.previousValueForAttribute(column);
|
|
2251
2241
|
const now = this[column];
|
|
2252
|
-
if (was
|
|
2253
|
-
;
|
|
2254
|
-
obj[column] = {
|
|
2255
|
-
was,
|
|
2256
|
-
now,
|
|
2257
|
-
};
|
|
2258
|
-
}
|
|
2242
|
+
if ((0, notEqual_js_1.default)(was, now))
|
|
2243
|
+
obj[column] = { was, now };
|
|
2259
2244
|
});
|
|
2260
2245
|
return obj;
|
|
2261
2246
|
}
|
|
@@ -2289,7 +2274,7 @@ class Dream {
|
|
|
2289
2274
|
* @returns Returns the previous value for an attribute
|
|
2290
2275
|
*/
|
|
2291
2276
|
previousValueForAttribute(columnName) {
|
|
2292
|
-
if (this.frozenAttributes[columnName]
|
|
2277
|
+
if ((0, notEqual_js_1.default)(this.frozenAttributes[columnName], this[columnName]))
|
|
2293
2278
|
return this.frozenAttributes[columnName];
|
|
2294
2279
|
return this.attributesFromBeforeLastSave[columnName];
|
|
2295
2280
|
}
|
|
@@ -2305,7 +2290,7 @@ class Dream {
|
|
|
2305
2290
|
const changes = this.changes();
|
|
2306
2291
|
const now = changes?.[columnName]?.now;
|
|
2307
2292
|
const was = changes?.[columnName]?.was;
|
|
2308
|
-
return this.isPersisted && now
|
|
2293
|
+
return this.isPersisted && (0, notEqual_js_1.default)(now, was);
|
|
2309
2294
|
}
|
|
2310
2295
|
/**
|
|
2311
2296
|
* Returns true if the columnName provided has
|
|
@@ -2488,7 +2473,12 @@ class Dream {
|
|
|
2488
2473
|
* @returns A boolean
|
|
2489
2474
|
*/
|
|
2490
2475
|
equals(other) {
|
|
2491
|
-
|
|
2476
|
+
if (!other?.isDreamInstance)
|
|
2477
|
+
return false;
|
|
2478
|
+
return this.comparisonKey === other.comparisonKey;
|
|
2479
|
+
}
|
|
2480
|
+
get comparisonKey() {
|
|
2481
|
+
return `${this.constructor.globalName}:${this.primaryKeyValue}`;
|
|
2492
2482
|
}
|
|
2493
2483
|
/**
|
|
2494
2484
|
* @internal
|
|
@@ -5,17 +5,16 @@ const fs = require("fs/promises");
|
|
|
5
5
|
const path = require("path");
|
|
6
6
|
const index_js_1 = require("../../cli/index.js");
|
|
7
7
|
const colorize_js_1 = require("../../cli/logger/loggable/colorize.js");
|
|
8
|
-
const ConnectionConfRetriever_js_1 = require("../../db/ConnectionConfRetriever.js");
|
|
9
8
|
const index_js_2 = require("../../dream-application/index.js");
|
|
9
|
+
const autoGeneratedFileDisclaimer_js_1 = require("../../helpers/cli/autoGeneratedFileDisclaimer.js");
|
|
10
10
|
const compact_js_1 = require("../../helpers/compact.js");
|
|
11
11
|
const EnvInternal_js_1 = require("../../helpers/EnvInternal.js");
|
|
12
12
|
const dreamPath_js_1 = require("../../helpers/path/dreamPath.js");
|
|
13
13
|
const snakeify_js_1 = require("../../helpers/snakeify.js");
|
|
14
14
|
const sspawn_js_1 = require("../../helpers/sspawn.js");
|
|
15
|
-
const autoGeneratedFileDisclaimer_js_1 = require("../../helpers/cli/autoGeneratedFileDisclaimer.js");
|
|
16
15
|
async function writeSyncFile() {
|
|
17
|
-
const dbConf = new ConnectionConfRetriever_js_1.default().getConnectionConf('primary');
|
|
18
16
|
const dreamApp = index_js_2.default.getOrFail();
|
|
17
|
+
const dbConf = dreamApp.dbConnectionConfig('primary');
|
|
19
18
|
const dbSyncFilePath = path.join((0, dreamPath_js_1.default)('types'), 'db.ts');
|
|
20
19
|
const absoluteDbSyncPath = path.join(dreamApp.projectRoot, dbSyncFilePath);
|
|
21
20
|
await (0, sspawn_js_1.default)(`kysely-codegen --dialect=postgres --url=postgres://${dbConf.user}:${dbConf.password}@${dbConf.host}:${dbConf.port}/${dbConf.name} --out-file=${absoluteDbSyncPath}`, {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const index_js_1 = require("../cli/index.js");
|
|
4
|
-
const ConnectionConfRetriever_js_1 = require("../db/ConnectionConfRetriever.js");
|
|
5
4
|
const index_js_2 = require("../dream-application/index.js");
|
|
6
5
|
const EnvInternal_js_1 = require("../helpers/EnvInternal.js");
|
|
7
6
|
const SchemaBuilder_js_1 = require("../helpers/cli/SchemaBuilder.js");
|
|
@@ -29,8 +28,8 @@ class DreamBin {
|
|
|
29
28
|
await new SchemaBuilder_js_1.default().build();
|
|
30
29
|
}
|
|
31
30
|
static async dbCreate() {
|
|
32
|
-
const
|
|
33
|
-
const primaryDbConf =
|
|
31
|
+
const dreamApp = index_js_2.default.getOrFail();
|
|
32
|
+
const primaryDbConf = dreamApp.dbConnectionConfig('primary');
|
|
34
33
|
index_js_1.default.logger.logStartProgress(`creating ${primaryDbConf.name}...`);
|
|
35
34
|
await (0, createDb_js_1.default)('primary');
|
|
36
35
|
index_js_1.default.logger.logEndProgress();
|
|
@@ -44,8 +43,8 @@ class DreamBin {
|
|
|
44
43
|
// }
|
|
45
44
|
}
|
|
46
45
|
static async dbDrop() {
|
|
47
|
-
const
|
|
48
|
-
const primaryDbConf =
|
|
46
|
+
const dreamApp = index_js_2.default.getOrFail();
|
|
47
|
+
const primaryDbConf = dreamApp.dbConnectionConfig('primary');
|
|
49
48
|
index_js_1.default.logger.logStartProgress(`dropping ${primaryDbConf.name}...`);
|
|
50
49
|
await (0, dropDb_js_1.default)('primary');
|
|
51
50
|
index_js_1.default.logger.logEndProgress();
|
|
@@ -59,24 +58,24 @@ class DreamBin {
|
|
|
59
58
|
// }
|
|
60
59
|
}
|
|
61
60
|
static async dbMigrate() {
|
|
62
|
-
const
|
|
63
|
-
const primaryDbConf =
|
|
61
|
+
const dreamApp = index_js_2.default.getOrFail();
|
|
62
|
+
const primaryDbConf = dreamApp.dbConnectionConfig('primary');
|
|
64
63
|
index_js_1.default.logger.logStartProgress(`migrating ${primaryDbConf.name}...`);
|
|
65
64
|
await (0, runMigration_js_1.default)({ mode: 'migrate' });
|
|
66
|
-
await this.duplicateDatabase();
|
|
67
65
|
index_js_1.default.logger.logEndProgress();
|
|
66
|
+
await this.duplicateDatabase();
|
|
68
67
|
}
|
|
69
68
|
static async dbRollback(opts) {
|
|
70
|
-
const
|
|
71
|
-
const primaryDbConf =
|
|
69
|
+
const dreamApp = index_js_2.default.getOrFail();
|
|
70
|
+
const primaryDbConf = dreamApp.dbConnectionConfig('primary');
|
|
72
71
|
index_js_1.default.logger.logStartProgress(`rolling back ${primaryDbConf.name}...`);
|
|
73
72
|
let step = opts.steps;
|
|
74
73
|
while (step > 0) {
|
|
75
74
|
await (0, runMigration_js_1.default)({ mode: 'rollback' });
|
|
76
75
|
step -= 1;
|
|
77
76
|
}
|
|
78
|
-
await this.duplicateDatabase();
|
|
79
77
|
index_js_1.default.logger.logEndProgress();
|
|
78
|
+
await this.duplicateDatabase();
|
|
80
79
|
}
|
|
81
80
|
static async generateDream(fullyQualifiedModelName, columnsWithTypes, options) {
|
|
82
81
|
await (0, generateDream_js_1.default)({ fullyQualifiedModelName, columnsWithTypes, options });
|
|
@@ -96,26 +95,27 @@ class DreamBin {
|
|
|
96
95
|
index_js_1.default.logger.logEndProgress();
|
|
97
96
|
}
|
|
98
97
|
static async duplicateDatabase() {
|
|
99
|
-
const
|
|
98
|
+
const dreamApp = index_js_2.default.getOrFail();
|
|
99
|
+
const parallelTests = dreamApp.parallelTests;
|
|
100
100
|
if (!parallelTests)
|
|
101
101
|
return;
|
|
102
|
-
|
|
103
|
-
const dbConf =
|
|
102
|
+
index_js_1.default.logger.logStartProgress(`duplicating db for parallel tests...`);
|
|
103
|
+
const dbConf = dreamApp.dbConnectionConfig('primary');
|
|
104
104
|
const client = await (0, loadPgClient_js_1.default)({ useSystemDb: true });
|
|
105
105
|
if (EnvInternal_js_1.default.boolean('DREAM_CORE_DEVELOPMENT')) {
|
|
106
106
|
const replicaTestWorkerDatabaseName = `replica_test_${dbConf.name}`;
|
|
107
|
-
index_js_1.default.logger.logContinueProgress(`creating fake replica test database ${replicaTestWorkerDatabaseName}
|
|
107
|
+
index_js_1.default.logger.logContinueProgress(`creating fake replica test database ${replicaTestWorkerDatabaseName}...`, { logPrefix: ' ├ [db]', logPrefixColor: 'cyan' });
|
|
108
108
|
await client.query(`DROP DATABASE IF EXISTS ${replicaTestWorkerDatabaseName};`);
|
|
109
109
|
await client.query(`CREATE DATABASE ${replicaTestWorkerDatabaseName} TEMPLATE ${dbConf.name};`);
|
|
110
110
|
}
|
|
111
111
|
for (let i = 2; i <= parallelTests; i++) {
|
|
112
112
|
const workerDatabaseName = `${dbConf.name}_${i}`;
|
|
113
|
-
|
|
114
|
-
index_js_1.default.logger.logContinueProgress(`creating duplicate test database ${workerDatabaseName} for concurrent tests...`);
|
|
113
|
+
index_js_1.default.logger.logContinueProgress(`creating duplicate test database ${workerDatabaseName} for concurrent tests...`, { logPrefix: ' ├ [db]', logPrefixColor: 'cyan' });
|
|
115
114
|
await client.query(`DROP DATABASE IF EXISTS ${workerDatabaseName};`);
|
|
116
115
|
await client.query(`CREATE DATABASE ${workerDatabaseName} TEMPLATE ${dbConf.name};`);
|
|
117
116
|
}
|
|
118
117
|
await client.end();
|
|
118
|
+
index_js_1.default.logger.logEndProgress();
|
|
119
119
|
}
|
|
120
120
|
}
|
|
121
121
|
exports.default = DreamBin;
|
|
@@ -13,16 +13,16 @@ exports.closeAllDbConnections = closeAllDbConnections;
|
|
|
13
13
|
const pg_1 = require("pg");
|
|
14
14
|
const kysely_1 = require("kysely");
|
|
15
15
|
const index_js_1 = require("../dream-application/index.js");
|
|
16
|
-
const ConnectionConfRetriever_js_1 = require("./ConnectionConfRetriever.js");
|
|
17
16
|
let connections = {};
|
|
18
17
|
class DreamDbConnection {
|
|
19
18
|
static getConnection(connectionType) {
|
|
20
|
-
const
|
|
19
|
+
const dreamApp = index_js_1.default.getOrFail();
|
|
20
|
+
const connectionName = this.getConnectionTypeName(connectionType);
|
|
21
21
|
const connection = connections[connectionName];
|
|
22
22
|
if (connection) {
|
|
23
23
|
return connection;
|
|
24
24
|
}
|
|
25
|
-
const connectionConf =
|
|
25
|
+
const connectionConf = dreamApp.dbConnectionConfig(connectionType);
|
|
26
26
|
const dbConn = new kysely_1.Kysely({
|
|
27
27
|
log(event) {
|
|
28
28
|
const dreamApp = index_js_1.default.getOrFail();
|
|
@@ -34,7 +34,7 @@ class DreamDbConnection {
|
|
|
34
34
|
pool: new pg_1.default.Pool({
|
|
35
35
|
user: connectionConf.user || '',
|
|
36
36
|
password: connectionConf.password || '',
|
|
37
|
-
database:
|
|
37
|
+
database: dreamApp.dbName(connectionType),
|
|
38
38
|
host: connectionConf.host || 'localhost',
|
|
39
39
|
port: connectionConf.port || 5432,
|
|
40
40
|
ssl: connectionConf.useSsl ? sslConfig(connectionConf) : false,
|
|
@@ -42,7 +42,7 @@ class DreamDbConnection {
|
|
|
42
42
|
}),
|
|
43
43
|
plugins: [new kysely_1.CamelCasePlugin({ underscoreBetweenUppercaseLetters: true })],
|
|
44
44
|
});
|
|
45
|
-
connections[getConnectionTypeName(connectionType)] = dbConn;
|
|
45
|
+
connections[this.getConnectionTypeName(connectionType)] = dbConn;
|
|
46
46
|
return dbConn;
|
|
47
47
|
}
|
|
48
48
|
static async dropAllConnections() {
|
|
@@ -51,19 +51,13 @@ class DreamDbConnection {
|
|
|
51
51
|
delete connections[key];
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
|
+
static getConnectionTypeName(connectionType) {
|
|
55
|
+
return index_js_1.default.getOrFail().parallelDatabasesEnabled
|
|
56
|
+
? `${connectionType}_${process.env.VITEST_POOL_ID}`
|
|
57
|
+
: connectionType;
|
|
58
|
+
}
|
|
54
59
|
}
|
|
55
60
|
exports.default = DreamDbConnection;
|
|
56
|
-
function getConnectionTypeName(connectionType) {
|
|
57
|
-
return parallelDatabasesEnabled() ? `${connectionType}_${process.env.JEST_WORKER_ID}` : connectionType;
|
|
58
|
-
}
|
|
59
|
-
function getDatabaseName(dbName) {
|
|
60
|
-
return parallelDatabasesEnabled() ? `${dbName}_${process.env.JEST_WORKER_ID}` : dbName;
|
|
61
|
-
}
|
|
62
|
-
function parallelDatabasesEnabled() {
|
|
63
|
-
return (!!index_js_1.default.getOrFail().parallelTests &&
|
|
64
|
-
!Number.isNaN(Number(process.env.JEST_WORKER_ID)) &&
|
|
65
|
-
Number(process.env.JEST_WORKER_ID) > 1);
|
|
66
|
-
}
|
|
67
61
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
68
62
|
function sslConfig(connectionConf) {
|
|
69
63
|
// TODO: properly configure (https://rvohealth.atlassian.net/browse/PDTC-2914)
|
|
@@ -228,6 +228,28 @@ Try setting it to something valid, like:
|
|
|
228
228
|
get serializers() {
|
|
229
229
|
return (0, importSerializers_js_1.getSerializersOrFail)();
|
|
230
230
|
}
|
|
231
|
+
dbName(connection) {
|
|
232
|
+
const conf = this.dbConnectionConfig(connection);
|
|
233
|
+
return this.parallelDatabasesEnabled ? `${conf.name}_${process.env.VITEST_POOL_ID}` : conf.name;
|
|
234
|
+
}
|
|
235
|
+
dbConnectionConfig(connection) {
|
|
236
|
+
const conf = this.dbCredentials?.[connection] || this.dbCredentials?.primary;
|
|
237
|
+
if (!conf)
|
|
238
|
+
throw new Error(`
|
|
239
|
+
Cannot find a connection config given the following connection and node environment:
|
|
240
|
+
connection: ${connection}
|
|
241
|
+
NODE_ENV: ${EnvInternal_js_1.default.nodeEnv}
|
|
242
|
+
`);
|
|
243
|
+
return conf;
|
|
244
|
+
}
|
|
245
|
+
get hasReplicaConfig() {
|
|
246
|
+
return !!this.dbCredentials.replica;
|
|
247
|
+
}
|
|
248
|
+
get parallelDatabasesEnabled() {
|
|
249
|
+
return (!!this.parallelTests &&
|
|
250
|
+
!Number.isNaN(Number(process.env.VITEST_POOL_ID)) &&
|
|
251
|
+
Number(process.env.VITEST_POOL_ID) > 1);
|
|
252
|
+
}
|
|
231
253
|
async load(resourceType, resourcePath, importCb) {
|
|
232
254
|
switch (resourceType) {
|
|
233
255
|
case 'models':
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = areEqual;
|
|
4
|
+
const CalendarDate_js_1 = require("./CalendarDate.js");
|
|
5
|
+
const DateTime_js_1 = require("./DateTime.js");
|
|
6
|
+
function areEqual(a, b) {
|
|
7
|
+
return areEqualOrUndefined(a, b) ?? areEqualOrUndefined(b, a) ?? JSON.stringify(a) === JSON.stringify(b);
|
|
8
|
+
}
|
|
9
|
+
function areEqualOrUndefined(a, b) {
|
|
10
|
+
if (a === null)
|
|
11
|
+
return b === null;
|
|
12
|
+
if (a === undefined)
|
|
13
|
+
return b === undefined;
|
|
14
|
+
if (typeof a === 'boolean')
|
|
15
|
+
return a === b;
|
|
16
|
+
if (typeof a === 'number')
|
|
17
|
+
return a === b;
|
|
18
|
+
if (typeof a === 'string')
|
|
19
|
+
return a === b;
|
|
20
|
+
if (a instanceof DateTime_js_1.DateTime)
|
|
21
|
+
return b instanceof DateTime_js_1.DateTime && a.equals(b);
|
|
22
|
+
if (a instanceof CalendarDate_js_1.default)
|
|
23
|
+
return b instanceof CalendarDate_js_1.default && a.equals(b);
|
|
24
|
+
if (Array.isArray(a))
|
|
25
|
+
return Array.isArray(b) && a.length === b.length && !a.find((value, index) => !areEqual(value, b[index]));
|
|
26
|
+
if (a?.isDreamInstance)
|
|
27
|
+
return a.equals(b);
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.default = createDb;
|
|
4
|
-
const
|
|
4
|
+
const index_js_1 = require("../../dream-application/index.js");
|
|
5
5
|
const EnvInternal_js_1 = require("../EnvInternal.js");
|
|
6
6
|
const loadPgClient_js_1 = require("./loadPgClient.js");
|
|
7
7
|
async function createDb(connection, dbName) {
|
|
@@ -9,8 +9,8 @@ async function createDb(connection, dbName) {
|
|
|
9
9
|
// so there is no way to drop in production
|
|
10
10
|
if (EnvInternal_js_1.default.isProduction)
|
|
11
11
|
return false;
|
|
12
|
-
const
|
|
13
|
-
const dbConf =
|
|
12
|
+
const dreamApp = index_js_1.default.getOrFail();
|
|
13
|
+
const dbConf = dreamApp.dbConnectionConfig(connection);
|
|
14
14
|
dbName ||= dbConf.name || null;
|
|
15
15
|
if (!dbName)
|
|
16
16
|
throw new Error('Must either pass a dbName to the create function, or else ensure that DB_NAME is set in the env');
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.default = dropDb;
|
|
4
|
-
const ConnectionConfRetriever_js_1 = require("../../db/ConnectionConfRetriever.js");
|
|
5
4
|
const index_js_1 = require("../../dream-application/index.js");
|
|
6
5
|
const EnvInternal_js_1 = require("../EnvInternal.js");
|
|
7
6
|
const loadPgClient_js_1 = require("./loadPgClient.js");
|
|
7
|
+
const index_js_2 = require("../../cli/index.js");
|
|
8
8
|
async function dropDb(connection, dbName) {
|
|
9
9
|
// this was only ever written to clear the db between tests or in development,
|
|
10
10
|
// so there is no way to drop in production
|
|
11
11
|
if (EnvInternal_js_1.default.isProduction)
|
|
12
12
|
return false;
|
|
13
|
-
const
|
|
14
|
-
const dbConf =
|
|
13
|
+
const dreamApp = index_js_1.default.getOrFail();
|
|
14
|
+
const dbConf = dreamApp.dbConnectionConfig(connection);
|
|
15
15
|
dbName ||= dbConf.name || null;
|
|
16
16
|
if (!dbName)
|
|
17
17
|
throw new Error('Must either pass a dbName to the drop function, or else ensure that DB_NAME is set in the env');
|
|
@@ -25,12 +25,15 @@ async function maybeDropDuplicateDatabases(client, dbName) {
|
|
|
25
25
|
return;
|
|
26
26
|
if (EnvInternal_js_1.default.boolean('DREAM_CORE_DEVELOPMENT')) {
|
|
27
27
|
const replicaTestWorkerDatabaseName = `replica_test_${dbName}`;
|
|
28
|
-
|
|
28
|
+
index_js_2.default.logger.logContinueProgress(`dropping fake replica test database ${replicaTestWorkerDatabaseName}`, { logPrefix: ' ├ [db]', logPrefixColor: 'cyan' });
|
|
29
29
|
await client.query(`DROP DATABASE IF EXISTS ${replicaTestWorkerDatabaseName};`);
|
|
30
30
|
}
|
|
31
31
|
for (let i = 2; i <= parallelTests; i++) {
|
|
32
32
|
const workerDatabaseName = `${dbName}_${i}`;
|
|
33
|
-
|
|
33
|
+
index_js_2.default.logger.logContinueProgress(`dropping duplicate test database ${workerDatabaseName}`, {
|
|
34
|
+
logPrefix: ' ├ [db]',
|
|
35
|
+
logPrefixColor: 'cyan',
|
|
36
|
+
});
|
|
34
37
|
await client.query(`DROP DATABASE IF EXISTS ${workerDatabaseName};`);
|
|
35
38
|
}
|
|
36
39
|
}
|
|
@@ -6,15 +6,14 @@ function uniq(arr, toKey = undefined) {
|
|
|
6
6
|
return uniqWith(arr, toKey);
|
|
7
7
|
return uniqWith(arr, a => String(a));
|
|
8
8
|
}
|
|
9
|
-
function dreamKey(dream) {
|
|
10
|
-
return `${dream.constructor.globalName}:${dream.primaryKeyValue}`;
|
|
11
|
-
}
|
|
12
9
|
function uniqWith(arr, toKey) {
|
|
13
10
|
const map = arr.reduce((acc, val) => {
|
|
14
11
|
// Prefix with underscore to ensure that the values cannot be interpreted as integers.
|
|
15
12
|
// If they can be interpreted as integers, then the keys are ordered not by the
|
|
16
13
|
// order in which they were added, but in ascending numerical order.
|
|
17
|
-
const key = val?.isDreamInstance
|
|
14
|
+
const key = val?.isDreamInstance
|
|
15
|
+
? val.comparisonKey
|
|
16
|
+
: `_${toKey(val)}`;
|
|
18
17
|
acc[key] ||= val;
|
|
19
18
|
return acc;
|
|
20
19
|
}, {});
|
package/dist/esm/src/Dream.js
CHANGED
|
@@ -44,6 +44,7 @@ import { DateTime } from './helpers/DateTime.js';
|
|
|
44
44
|
import cachedTypeForAttribute from './helpers/db/cachedTypeForAttribute.js';
|
|
45
45
|
import isJsonColumn from './helpers/db/types/isJsonColumn.js';
|
|
46
46
|
import inferSerializerFromDreamOrViewModel from './helpers/inferSerializerFromDreamOrViewModel.js';
|
|
47
|
+
import notEqual from './helpers/notEqual.js';
|
|
47
48
|
import { isString } from './helpers/typechecks.js';
|
|
48
49
|
export default class Dream {
|
|
49
50
|
DB;
|
|
@@ -1231,18 +1232,7 @@ export default class Dream {
|
|
|
1231
1232
|
* @returns A Kysely query. Depending on the type passed, it will return either a SelectQueryBuilder, DeleteQueryBuilder, UpdateQueryBuilder, or an InsertQueryBuilder
|
|
1232
1233
|
*/
|
|
1233
1234
|
static toKysely(type) {
|
|
1234
|
-
|
|
1235
|
-
case 'select':
|
|
1236
|
-
return this.query().dbFor('select').selectFrom(this.table);
|
|
1237
|
-
case 'delete':
|
|
1238
|
-
return this.query().dbFor('delete').deleteFrom(this.table);
|
|
1239
|
-
case 'update':
|
|
1240
|
-
return this.query().dbFor('update').updateTable(this.table);
|
|
1241
|
-
case 'insert':
|
|
1242
|
-
return this.query().dbFor('insert').insertInto(this.table);
|
|
1243
|
-
default:
|
|
1244
|
-
throw new Error('never');
|
|
1245
|
-
}
|
|
1235
|
+
return this.query().toKysely(type);
|
|
1246
1236
|
}
|
|
1247
1237
|
/**
|
|
1248
1238
|
* Applies transaction to a new Query scoped
|
|
@@ -2247,13 +2237,8 @@ export default class Dream {
|
|
|
2247
2237
|
this.constructor.columns().forEach(column => {
|
|
2248
2238
|
const was = this.previousValueForAttribute(column);
|
|
2249
2239
|
const now = this[column];
|
|
2250
|
-
if (was
|
|
2251
|
-
;
|
|
2252
|
-
obj[column] = {
|
|
2253
|
-
was,
|
|
2254
|
-
now,
|
|
2255
|
-
};
|
|
2256
|
-
}
|
|
2240
|
+
if (notEqual(was, now))
|
|
2241
|
+
obj[column] = { was, now };
|
|
2257
2242
|
});
|
|
2258
2243
|
return obj;
|
|
2259
2244
|
}
|
|
@@ -2287,7 +2272,7 @@ export default class Dream {
|
|
|
2287
2272
|
* @returns Returns the previous value for an attribute
|
|
2288
2273
|
*/
|
|
2289
2274
|
previousValueForAttribute(columnName) {
|
|
2290
|
-
if (this.frozenAttributes[columnName]
|
|
2275
|
+
if (notEqual(this.frozenAttributes[columnName], this[columnName]))
|
|
2291
2276
|
return this.frozenAttributes[columnName];
|
|
2292
2277
|
return this.attributesFromBeforeLastSave[columnName];
|
|
2293
2278
|
}
|
|
@@ -2303,7 +2288,7 @@ export default class Dream {
|
|
|
2303
2288
|
const changes = this.changes();
|
|
2304
2289
|
const now = changes?.[columnName]?.now;
|
|
2305
2290
|
const was = changes?.[columnName]?.was;
|
|
2306
|
-
return this.isPersisted && now
|
|
2291
|
+
return this.isPersisted && notEqual(now, was);
|
|
2307
2292
|
}
|
|
2308
2293
|
/**
|
|
2309
2294
|
* Returns true if the columnName provided has
|
|
@@ -2486,7 +2471,12 @@ export default class Dream {
|
|
|
2486
2471
|
* @returns A boolean
|
|
2487
2472
|
*/
|
|
2488
2473
|
equals(other) {
|
|
2489
|
-
|
|
2474
|
+
if (!other?.isDreamInstance)
|
|
2475
|
+
return false;
|
|
2476
|
+
return this.comparisonKey === other.comparisonKey;
|
|
2477
|
+
}
|
|
2478
|
+
get comparisonKey() {
|
|
2479
|
+
return `${this.constructor.globalName}:${this.primaryKeyValue}`;
|
|
2490
2480
|
}
|
|
2491
2481
|
/**
|
|
2492
2482
|
* @internal
|
|
@@ -2,17 +2,16 @@ import * as fs from 'fs/promises';
|
|
|
2
2
|
import * as path from 'path';
|
|
3
3
|
import DreamCLI from '../../cli/index.js';
|
|
4
4
|
import colorize from '../../cli/logger/loggable/colorize.js';
|
|
5
|
-
import ConnectionConfRetriever from '../../db/ConnectionConfRetriever.js';
|
|
6
5
|
import DreamApplication from '../../dream-application/index.js';
|
|
6
|
+
import autogeneratedFileDisclaimer from '../../helpers/cli/autoGeneratedFileDisclaimer.js';
|
|
7
7
|
import compact from '../../helpers/compact.js';
|
|
8
8
|
import EnvInternal from '../../helpers/EnvInternal.js';
|
|
9
9
|
import dreamPath from '../../helpers/path/dreamPath.js';
|
|
10
10
|
import snakeify from '../../helpers/snakeify.js';
|
|
11
11
|
import sspawn from '../../helpers/sspawn.js';
|
|
12
|
-
import autogeneratedFileDisclaimer from '../../helpers/cli/autoGeneratedFileDisclaimer.js';
|
|
13
12
|
export default async function writeSyncFile() {
|
|
14
|
-
const dbConf = new ConnectionConfRetriever().getConnectionConf('primary');
|
|
15
13
|
const dreamApp = DreamApplication.getOrFail();
|
|
14
|
+
const dbConf = dreamApp.dbConnectionConfig('primary');
|
|
16
15
|
const dbSyncFilePath = path.join(dreamPath('types'), 'db.ts');
|
|
17
16
|
const absoluteDbSyncPath = path.join(dreamApp.projectRoot, dbSyncFilePath);
|
|
18
17
|
await sspawn(`kysely-codegen --dialect=postgres --url=postgres://${dbConf.user}:${dbConf.password}@${dbConf.host}:${dbConf.port}/${dbConf.name} --out-file=${absoluteDbSyncPath}`, {
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import DreamCLI from '../cli/index.js';
|
|
2
|
-
import ConnectionConfRetriever from '../db/ConnectionConfRetriever.js';
|
|
3
2
|
import DreamApplication from '../dream-application/index.js';
|
|
4
3
|
import EnvInternal from '../helpers/EnvInternal.js';
|
|
5
4
|
import SchemaBuilder from '../helpers/cli/SchemaBuilder.js';
|
|
@@ -27,8 +26,8 @@ export default class DreamBin {
|
|
|
27
26
|
await new SchemaBuilder().build();
|
|
28
27
|
}
|
|
29
28
|
static async dbCreate() {
|
|
30
|
-
const
|
|
31
|
-
const primaryDbConf =
|
|
29
|
+
const dreamApp = DreamApplication.getOrFail();
|
|
30
|
+
const primaryDbConf = dreamApp.dbConnectionConfig('primary');
|
|
32
31
|
DreamCLI.logger.logStartProgress(`creating ${primaryDbConf.name}...`);
|
|
33
32
|
await createDb('primary');
|
|
34
33
|
DreamCLI.logger.logEndProgress();
|
|
@@ -42,8 +41,8 @@ export default class DreamBin {
|
|
|
42
41
|
// }
|
|
43
42
|
}
|
|
44
43
|
static async dbDrop() {
|
|
45
|
-
const
|
|
46
|
-
const primaryDbConf =
|
|
44
|
+
const dreamApp = DreamApplication.getOrFail();
|
|
45
|
+
const primaryDbConf = dreamApp.dbConnectionConfig('primary');
|
|
47
46
|
DreamCLI.logger.logStartProgress(`dropping ${primaryDbConf.name}...`);
|
|
48
47
|
await _dropDb('primary');
|
|
49
48
|
DreamCLI.logger.logEndProgress();
|
|
@@ -57,24 +56,24 @@ export default class DreamBin {
|
|
|
57
56
|
// }
|
|
58
57
|
}
|
|
59
58
|
static async dbMigrate() {
|
|
60
|
-
const
|
|
61
|
-
const primaryDbConf =
|
|
59
|
+
const dreamApp = DreamApplication.getOrFail();
|
|
60
|
+
const primaryDbConf = dreamApp.dbConnectionConfig('primary');
|
|
62
61
|
DreamCLI.logger.logStartProgress(`migrating ${primaryDbConf.name}...`);
|
|
63
62
|
await runMigration({ mode: 'migrate' });
|
|
64
|
-
await this.duplicateDatabase();
|
|
65
63
|
DreamCLI.logger.logEndProgress();
|
|
64
|
+
await this.duplicateDatabase();
|
|
66
65
|
}
|
|
67
66
|
static async dbRollback(opts) {
|
|
68
|
-
const
|
|
69
|
-
const primaryDbConf =
|
|
67
|
+
const dreamApp = DreamApplication.getOrFail();
|
|
68
|
+
const primaryDbConf = dreamApp.dbConnectionConfig('primary');
|
|
70
69
|
DreamCLI.logger.logStartProgress(`rolling back ${primaryDbConf.name}...`);
|
|
71
70
|
let step = opts.steps;
|
|
72
71
|
while (step > 0) {
|
|
73
72
|
await runMigration({ mode: 'rollback' });
|
|
74
73
|
step -= 1;
|
|
75
74
|
}
|
|
76
|
-
await this.duplicateDatabase();
|
|
77
75
|
DreamCLI.logger.logEndProgress();
|
|
76
|
+
await this.duplicateDatabase();
|
|
78
77
|
}
|
|
79
78
|
static async generateDream(fullyQualifiedModelName, columnsWithTypes, options) {
|
|
80
79
|
await generateDream({ fullyQualifiedModelName, columnsWithTypes, options });
|
|
@@ -94,25 +93,26 @@ export default class DreamBin {
|
|
|
94
93
|
DreamCLI.logger.logEndProgress();
|
|
95
94
|
}
|
|
96
95
|
static async duplicateDatabase() {
|
|
97
|
-
const
|
|
96
|
+
const dreamApp = DreamApplication.getOrFail();
|
|
97
|
+
const parallelTests = dreamApp.parallelTests;
|
|
98
98
|
if (!parallelTests)
|
|
99
99
|
return;
|
|
100
|
-
|
|
101
|
-
const dbConf =
|
|
100
|
+
DreamCLI.logger.logStartProgress(`duplicating db for parallel tests...`);
|
|
101
|
+
const dbConf = dreamApp.dbConnectionConfig('primary');
|
|
102
102
|
const client = await loadPgClient({ useSystemDb: true });
|
|
103
103
|
if (EnvInternal.boolean('DREAM_CORE_DEVELOPMENT')) {
|
|
104
104
|
const replicaTestWorkerDatabaseName = `replica_test_${dbConf.name}`;
|
|
105
|
-
DreamCLI.logger.logContinueProgress(`creating fake replica test database ${replicaTestWorkerDatabaseName}
|
|
105
|
+
DreamCLI.logger.logContinueProgress(`creating fake replica test database ${replicaTestWorkerDatabaseName}...`, { logPrefix: ' ├ [db]', logPrefixColor: 'cyan' });
|
|
106
106
|
await client.query(`DROP DATABASE IF EXISTS ${replicaTestWorkerDatabaseName};`);
|
|
107
107
|
await client.query(`CREATE DATABASE ${replicaTestWorkerDatabaseName} TEMPLATE ${dbConf.name};`);
|
|
108
108
|
}
|
|
109
109
|
for (let i = 2; i <= parallelTests; i++) {
|
|
110
110
|
const workerDatabaseName = `${dbConf.name}_${i}`;
|
|
111
|
-
|
|
112
|
-
DreamCLI.logger.logContinueProgress(`creating duplicate test database ${workerDatabaseName} for concurrent tests...`);
|
|
111
|
+
DreamCLI.logger.logContinueProgress(`creating duplicate test database ${workerDatabaseName} for concurrent tests...`, { logPrefix: ' ├ [db]', logPrefixColor: 'cyan' });
|
|
113
112
|
await client.query(`DROP DATABASE IF EXISTS ${workerDatabaseName};`);
|
|
114
113
|
await client.query(`CREATE DATABASE ${workerDatabaseName} TEMPLATE ${dbConf.name};`);
|
|
115
114
|
}
|
|
116
115
|
await client.end();
|
|
116
|
+
DreamCLI.logger.logEndProgress();
|
|
117
117
|
}
|
|
118
118
|
}
|
|
@@ -9,16 +9,16 @@
|
|
|
9
9
|
import pg from 'pg';
|
|
10
10
|
import { CamelCasePlugin, Kysely, PostgresDialect } from 'kysely';
|
|
11
11
|
import DreamApplication from '../dream-application/index.js';
|
|
12
|
-
import ConnectionConfRetriever from './ConnectionConfRetriever.js';
|
|
13
12
|
let connections = {};
|
|
14
13
|
export default class DreamDbConnection {
|
|
15
14
|
static getConnection(connectionType) {
|
|
16
|
-
const
|
|
15
|
+
const dreamApp = DreamApplication.getOrFail();
|
|
16
|
+
const connectionName = this.getConnectionTypeName(connectionType);
|
|
17
17
|
const connection = connections[connectionName];
|
|
18
18
|
if (connection) {
|
|
19
19
|
return connection;
|
|
20
20
|
}
|
|
21
|
-
const connectionConf =
|
|
21
|
+
const connectionConf = dreamApp.dbConnectionConfig(connectionType);
|
|
22
22
|
const dbConn = new Kysely({
|
|
23
23
|
log(event) {
|
|
24
24
|
const dreamApp = DreamApplication.getOrFail();
|
|
@@ -30,7 +30,7 @@ export default class DreamDbConnection {
|
|
|
30
30
|
pool: new pg.Pool({
|
|
31
31
|
user: connectionConf.user || '',
|
|
32
32
|
password: connectionConf.password || '',
|
|
33
|
-
database:
|
|
33
|
+
database: dreamApp.dbName(connectionType),
|
|
34
34
|
host: connectionConf.host || 'localhost',
|
|
35
35
|
port: connectionConf.port || 5432,
|
|
36
36
|
ssl: connectionConf.useSsl ? sslConfig(connectionConf) : false,
|
|
@@ -38,7 +38,7 @@ export default class DreamDbConnection {
|
|
|
38
38
|
}),
|
|
39
39
|
plugins: [new CamelCasePlugin({ underscoreBetweenUppercaseLetters: true })],
|
|
40
40
|
});
|
|
41
|
-
connections[getConnectionTypeName(connectionType)] = dbConn;
|
|
41
|
+
connections[this.getConnectionTypeName(connectionType)] = dbConn;
|
|
42
42
|
return dbConn;
|
|
43
43
|
}
|
|
44
44
|
static async dropAllConnections() {
|
|
@@ -47,17 +47,11 @@ export default class DreamDbConnection {
|
|
|
47
47
|
delete connections[key];
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return parallelDatabasesEnabled() ? `${dbName}_${process.env.JEST_WORKER_ID}` : dbName;
|
|
56
|
-
}
|
|
57
|
-
function parallelDatabasesEnabled() {
|
|
58
|
-
return (!!DreamApplication.getOrFail().parallelTests &&
|
|
59
|
-
!Number.isNaN(Number(process.env.JEST_WORKER_ID)) &&
|
|
60
|
-
Number(process.env.JEST_WORKER_ID) > 1);
|
|
50
|
+
static getConnectionTypeName(connectionType) {
|
|
51
|
+
return DreamApplication.getOrFail().parallelDatabasesEnabled
|
|
52
|
+
? `${connectionType}_${process.env.VITEST_POOL_ID}`
|
|
53
|
+
: connectionType;
|
|
54
|
+
}
|
|
61
55
|
}
|
|
62
56
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
63
57
|
function sslConfig(connectionConf) {
|