@rvoh/dream 1.4.2 → 1.5.0
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/CHANGELOG.md +66 -0
- package/dist/cjs/src/Dream.js +12 -16
- package/dist/cjs/src/bin/index.js +23 -7
- package/dist/cjs/src/cli/index.js +18 -3
- package/dist/cjs/src/db/ConnectedToDB.js +0 -8
- package/dist/cjs/src/db/DreamDbConnection.js +25 -36
- package/dist/cjs/src/db/helpers/dbTypesFilenameForConnection.js +6 -0
- package/dist/cjs/src/{bin/helpers/sync.js → db/helpers/syncDbTypesFiles.js} +8 -5
- package/dist/cjs/src/db/index.js +3 -2
- package/dist/cjs/src/decorators/field/sortable/helpers/setPosition.js +17 -2
- package/dist/cjs/src/dream/Query.js +9 -6
- package/dist/cjs/src/dream/QueryDriver/Base.js +111 -7
- package/dist/cjs/src/dream/QueryDriver/Kysely.js +213 -86
- package/dist/cjs/src/dream/QueryDriver/Postgres.js +162 -0
- package/dist/cjs/src/{helpers/db → dream/QueryDriver/helpers/kysely}/runMigration.js +20 -16
- package/dist/cjs/src/{helpers/db → dream/QueryDriver/helpers/pg}/createDb.js +5 -5
- package/dist/cjs/src/{helpers/db → dream/QueryDriver/helpers/pg}/dropDb.js +6 -6
- package/dist/cjs/src/{helpers/db → dream/QueryDriver/helpers/pg}/loadPgClient.js +5 -3
- package/dist/cjs/src/dream/internal/destroyDream.js +3 -10
- package/dist/cjs/src/dream/internal/saveDream.js +1 -1
- package/dist/cjs/src/dream/internal/similarity/SimilarityBuilder.js +9 -3
- package/dist/cjs/src/dream-app/index.js +47 -64
- package/dist/cjs/src/helpers/cli/SchemaBuilder.js +30 -42
- package/dist/cjs/src/helpers/cli/generateDream.js +2 -0
- package/dist/cjs/src/helpers/cli/generateDreamContent.js +5 -3
- package/dist/cjs/src/helpers/cli/generateFactoryContent.js +29 -0
- package/dist/cjs/src/helpers/cli/generateMigration.js +7 -5
- package/dist/cjs/src/helpers/cli/generateMigrationContent.js +48 -15
- package/dist/cjs/src/helpers/db/primaryKeyType.js +4 -22
- package/dist/cjs/src/helpers/sqlAttributes.js +4 -1
- package/dist/cjs/src/index.js +8 -2
- package/dist/esm/src/Dream.js +10 -14
- package/dist/esm/src/bin/index.js +23 -7
- package/dist/esm/src/cli/index.js +18 -3
- package/dist/esm/src/db/ConnectedToDB.js +0 -8
- package/dist/esm/src/db/DreamDbConnection.js +22 -34
- package/dist/esm/src/db/helpers/dbTypesFilenameForConnection.js +3 -0
- package/dist/esm/src/{bin/helpers/sync.js → db/helpers/syncDbTypesFiles.js} +7 -4
- package/dist/esm/src/db/index.js +3 -2
- package/dist/esm/src/decorators/field/sortable/helpers/setPosition.js +16 -2
- package/dist/esm/src/dream/Query.js +9 -6
- package/dist/esm/src/dream/QueryDriver/Base.js +111 -7
- package/dist/esm/src/dream/QueryDriver/Kysely.js +215 -88
- package/dist/esm/src/dream/QueryDriver/Postgres.js +162 -0
- package/dist/esm/src/{helpers/db → dream/QueryDriver/helpers/kysely}/runMigration.js +14 -10
- package/dist/esm/src/{helpers/db → dream/QueryDriver/helpers/pg}/createDb.js +5 -5
- package/dist/esm/src/{helpers/db → dream/QueryDriver/helpers/pg}/dropDb.js +6 -6
- package/dist/esm/src/{helpers/db → dream/QueryDriver/helpers/pg}/loadPgClient.js +5 -3
- package/dist/esm/src/dream/internal/destroyDream.js +3 -10
- package/dist/esm/src/dream/internal/saveDream.js +1 -1
- package/dist/esm/src/dream/internal/similarity/SimilarityBuilder.js +9 -3
- package/dist/esm/src/dream-app/index.js +44 -61
- package/dist/esm/src/helpers/cli/SchemaBuilder.js +24 -36
- package/dist/esm/src/helpers/cli/generateDream.js +2 -0
- package/dist/esm/src/helpers/cli/generateDreamContent.js +5 -3
- package/dist/esm/src/helpers/cli/generateFactoryContent.js +29 -0
- package/dist/esm/src/helpers/cli/generateMigration.js +7 -5
- package/dist/esm/src/helpers/cli/generateMigrationContent.js +48 -15
- package/dist/esm/src/helpers/db/primaryKeyType.js +4 -22
- package/dist/esm/src/helpers/sqlAttributes.js +4 -1
- package/dist/esm/src/index.js +3 -0
- package/dist/types/src/Dream.d.ts +5 -3
- package/dist/types/src/bin/index.d.ts +3 -1
- package/dist/types/src/db/ConnectedToDB.d.ts +0 -2
- package/dist/types/src/db/DreamDbConnection.d.ts +7 -3
- package/dist/types/src/db/helpers/dbTypesFilenameForConnection.d.ts +1 -0
- package/dist/types/src/db/helpers/syncDbTypesFiles.d.ts +1 -0
- package/dist/types/src/db/index.d.ts +2 -1
- package/dist/types/src/decorators/field/sortable/helpers/setPosition.d.ts +2 -0
- package/dist/types/src/dream/Query.d.ts +5 -20
- package/dist/types/src/dream/QueryDriver/Base.d.ts +74 -13
- package/dist/types/src/dream/QueryDriver/Kysely.d.ts +75 -9
- package/dist/types/src/dream/QueryDriver/Postgres.d.ts +32 -0
- package/dist/types/src/dream/QueryDriver/helpers/kysely/foreignKeyTypeFromPrimaryKey.d.ts +2 -0
- package/dist/types/src/dream/QueryDriver/helpers/kysely/runMigration.d.ts +9 -0
- package/dist/types/src/dream/QueryDriver/helpers/pg/createDb.d.ts +2 -0
- package/dist/types/src/dream/QueryDriver/helpers/pg/dropDb.d.ts +2 -0
- package/dist/types/src/dream/QueryDriver/helpers/pg/loadPgClient.d.ts +5 -0
- package/dist/types/src/dream/internal/destroyOptions.d.ts +3 -3
- package/dist/types/src/dream-app/index.d.ts +11 -6
- package/dist/types/src/helpers/cli/SchemaBuilder.d.ts +26 -0
- package/dist/types/src/helpers/cli/generateDream.d.ts +1 -0
- package/dist/types/src/helpers/cli/generateDreamContent.d.ts +2 -1
- package/dist/types/src/helpers/cli/generateMigration.d.ts +2 -1
- package/dist/types/src/helpers/cli/generateMigrationContent.d.ts +2 -1
- package/dist/types/src/helpers/db/primaryKeyType.d.ts +1 -1
- package/dist/types/src/index.d.ts +3 -0
- package/dist/types/src/types/dream.d.ts +4 -4
- package/dist/types/src/types/dream.ts +8 -9
- package/docs/assets/navigation.js +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/classes/Benchmark.html +2 -2
- package/docs/classes/CalendarDate.html +2 -2
- package/docs/classes/CheckConstraintViolation.html +3 -3
- package/docs/classes/CliFileWriter.html +2 -2
- package/docs/classes/CreateOrFindByFailedToCreateAndFind.html +3 -3
- package/docs/classes/DataTypeColumnTypeMismatch.html +3 -3
- package/docs/classes/Decorators.html +19 -19
- package/docs/classes/Dream.html +117 -115
- package/docs/classes/DreamApp.html +8 -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/DreamSerializerBuilder.html +8 -8
- 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/KyselyQueryDriver.html +163 -0
- package/docs/classes/NonLoadedAssociation.html +3 -3
- package/docs/classes/NotNullViolation.html +3 -3
- package/docs/classes/ObjectSerializerBuilder.html +8 -8
- package/docs/classes/PostgresQueryDriver.html +165 -0
- package/docs/classes/Query.html +59 -87
- package/docs/classes/QueryDriverBase.html +156 -0
- package/docs/classes/Range.html +2 -2
- package/docs/classes/RecordNotFound.html +3 -3
- package/docs/classes/ValidationError.html +3 -3
- package/docs/functions/DreamSerializer.html +1 -1
- package/docs/functions/ObjectSerializer.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/cloneDeepSafe.html +1 -1
- package/docs/functions/closeAllDbConnections.html +1 -1
- package/docs/functions/compact.html +1 -1
- package/docs/functions/dreamDbConnections.html +1 -1
- package/docs/functions/dreamPath.html +1 -1
- package/docs/functions/expandStiClasses.html +1 -1
- package/docs/functions/generateDream.html +1 -1
- package/docs/functions/globalClassNameFromFullyQualifiedModelName.html +1 -1
- package/docs/functions/groupBy.html +1 -1
- package/docs/functions/hyphenize.html +1 -1
- package/docs/functions/inferSerializerFromDreamOrViewModel.html +1 -1
- package/docs/functions/inferSerializersFromDreamClassOrViewModelClass.html +1 -1
- package/docs/functions/intersection.html +1 -1
- package/docs/functions/isDreamSerializer.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/normalizeUnicode.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/sortObjectByKey.html +1 -1
- package/docs/functions/sortObjectByValue.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/hierarchy.html +1 -0
- package/docs/interfaces/BelongsToStatement.html +2 -2
- package/docs/interfaces/DecoratorContext.html +2 -2
- package/docs/interfaces/DreamAppInitOptions.html +2 -2
- package/docs/interfaces/DreamAppOpts.html +2 -2
- package/docs/interfaces/EncryptOptions.html +2 -2
- package/docs/interfaces/InternalAnyTypedSerializerRendersMany.html +2 -2
- package/docs/interfaces/InternalAnyTypedSerializerRendersOne.html +2 -2
- package/docs/interfaces/OpenapiDescription.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/interfaces/SerializerRendererOpts.html +2 -2
- package/docs/modules.html +3 -0
- 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/DbTypes.html +1 -1
- package/docs/types/DreamAppAllowedPackageManagersEnum.html +1 -1
- package/docs/types/DreamAssociationMetadata.html +1 -1
- package/docs/types/DreamAttributes.html +1 -1
- package/docs/types/DreamClassAssociationAndStatement.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/DreamModelSerializerType.html +1 -1
- package/docs/types/DreamOrViewModelClassSerializerKey.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/DreamSerializable.html +1 -1
- package/docs/types/DreamSerializableArray.html +1 -1
- package/docs/types/DreamSerializerKey.html +1 -1
- package/docs/types/DreamSerializers.html +1 -1
- package/docs/types/DreamVirtualColumns.html +1 -1
- package/docs/types/EncryptAlgorithm.html +1 -1
- package/docs/types/HasManyStatement.html +1 -1
- package/docs/types/HasOneStatement.html +1 -1
- package/docs/types/Hyphenized.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/OpenapiPrimitiveBaseTypes.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/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/OpenapiShorthandPrimitiveBaseTypes.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/RoundingPrecision.html +1 -1
- package/docs/types/SerializerCasing.html +1 -1
- package/docs/types/SimpleObjectSerializerType.html +1 -1
- package/docs/types/Snakeified.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/ViewModel.html +1 -1
- package/docs/types/ViewModelClass.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/DreamAppAllowedPackageManagersEnumValues.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 +3 -2
- package/dist/cjs/src/helpers/db/truncateDb.js +0 -27
- package/dist/esm/src/helpers/db/truncateDb.js +0 -24
- package/dist/types/src/bin/helpers/sync.d.ts +0 -1
- package/dist/types/src/helpers/db/createDb.d.ts +0 -2
- package/dist/types/src/helpers/db/dropDb.d.ts +0 -2
- package/dist/types/src/helpers/db/foreignKeyTypeFromPrimaryKey.d.ts +0 -2
- package/dist/types/src/helpers/db/loadPgClient.d.ts +0 -4
- package/dist/types/src/helpers/db/runMigration.d.ts +0 -6
- package/dist/types/src/helpers/db/truncateDb.d.ts +0 -1
- /package/dist/cjs/src/{helpers/db → dream/QueryDriver/helpers/kysely}/foreignKeyTypeFromPrimaryKey.js +0 -0
- /package/dist/esm/src/{helpers/db → dream/QueryDriver/helpers/kysely}/foreignKeyTypeFromPrimaryKey.js +0 -0
|
@@ -1,6 +1,168 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
// after building for esm, importing pg using the following:
|
|
4
|
+
//
|
|
5
|
+
// import * as pg from 'pg'
|
|
6
|
+
//
|
|
7
|
+
// will crash. This is difficult to discover, since it only happens
|
|
8
|
+
// when being imported from our esm build.
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
10
|
+
// @ts-ignore
|
|
11
|
+
const pg_1 = require("pg");
|
|
12
|
+
const kysely_1 = require("kysely");
|
|
13
|
+
const index_js_1 = require("../../cli/index.js");
|
|
14
|
+
const dataTypes_js_1 = require("../../db/dataTypes.js");
|
|
15
|
+
const index_js_2 = require("../../dream-app/index.js");
|
|
16
|
+
const camelize_js_1 = require("../../helpers/camelize.js");
|
|
17
|
+
const customPgParsers_js_1 = require("../../helpers/customPgParsers.js");
|
|
18
|
+
const EnvInternal_js_1 = require("../../helpers/EnvInternal.js");
|
|
19
|
+
const createDb_js_1 = require("./helpers/pg/createDb.js");
|
|
20
|
+
const dropDb_js_1 = require("./helpers/pg/dropDb.js");
|
|
21
|
+
const loadPgClient_js_1 = require("./helpers/pg/loadPgClient.js");
|
|
3
22
|
const Kysely_js_1 = require("./Kysely.js");
|
|
23
|
+
const pgTypes = pg_1.default.types;
|
|
4
24
|
class PostgresQueryDriver extends Kysely_js_1.default {
|
|
25
|
+
/**
|
|
26
|
+
* create the database. Must respond to the NODE_ENV value.
|
|
27
|
+
*/
|
|
28
|
+
static async dbCreate(connectionName) {
|
|
29
|
+
const dreamApp = index_js_2.default.getOrFail();
|
|
30
|
+
const primaryDbConf = dreamApp.dbConnectionConfig(connectionName, 'primary');
|
|
31
|
+
index_js_1.default.logger.logStartProgress(`creating ${primaryDbConf.name}...`);
|
|
32
|
+
await (0, createDb_js_1.default)(connectionName, 'primary');
|
|
33
|
+
index_js_1.default.logger.logEndProgress();
|
|
34
|
+
// TODO: add support for creating replicas. Began doing it below, but it is very tricky,
|
|
35
|
+
// and we don't need it at the moment, so kicking off for future development when we have more time
|
|
36
|
+
// to flesh this out.
|
|
37
|
+
// if (connectionRetriever.hasReplicaConfig(connectionName)) {
|
|
38
|
+
// const replicaDbConf = connectionRetriever.getConnectionConf('replica')
|
|
39
|
+
// console.log(`creating ${process.env[replicaDbConf.name]}`)
|
|
40
|
+
// await createDb('replica')
|
|
41
|
+
// }
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* delete the database. Must respond to the NODE_ENV value.
|
|
45
|
+
*/
|
|
46
|
+
static async dbDrop(connectionName) {
|
|
47
|
+
const dreamApp = index_js_2.default.getOrFail();
|
|
48
|
+
const primaryDbConf = dreamApp.dbConnectionConfig(connectionName, 'primary');
|
|
49
|
+
index_js_1.default.logger.logStartProgress(`dropping ${primaryDbConf.name}...`);
|
|
50
|
+
await (0, dropDb_js_1.default)(connectionName, 'primary');
|
|
51
|
+
index_js_1.default.logger.logEndProgress();
|
|
52
|
+
// TODO: add support for dropping replicas. Began doing it below, but it is very tricky,
|
|
53
|
+
// and we don't need it at the moment, so kicking off for future development when we have more time
|
|
54
|
+
// to flesh this out.
|
|
55
|
+
// if (connectionRetriever.hasReplicaConfig(connectionName)) {
|
|
56
|
+
// const replicaDbConf = connectionRetriever.getConnectionConf('replica')
|
|
57
|
+
// console.log(`dropping ${process.env[replicaDbConf.name]}`)
|
|
58
|
+
// await _dropDb('replica')
|
|
59
|
+
// }
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* @internal
|
|
63
|
+
*
|
|
64
|
+
* this method is called when dream is initializing, and is used
|
|
65
|
+
* to configure the database to utilize custom type parsers for
|
|
66
|
+
* a variety of data types.
|
|
67
|
+
*
|
|
68
|
+
* @param connectionName - the name of the connection you are doing this for
|
|
69
|
+
* @returns void
|
|
70
|
+
*/
|
|
71
|
+
static async setDatabaseTypeParsers(connectionName) {
|
|
72
|
+
const kyselyDb = this.dbFor(connectionName, 'primary');
|
|
73
|
+
pgTypes.setTypeParser(pgTypes.builtins.DATE, customPgParsers_js_1.parsePostgresDate);
|
|
74
|
+
pgTypes.setTypeParser(pgTypes.builtins.TIMESTAMP, customPgParsers_js_1.parsePostgresDatetime);
|
|
75
|
+
pgTypes.setTypeParser(pgTypes.builtins.TIMESTAMPTZ, customPgParsers_js_1.parsePostgresDatetime);
|
|
76
|
+
pgTypes.setTypeParser(pgTypes.builtins.NUMERIC, customPgParsers_js_1.parsePostgresDecimal);
|
|
77
|
+
pgTypes.setTypeParser(pgTypes.builtins.INT8, customPgParsers_js_1.parsePostgresBigint);
|
|
78
|
+
const textArrayOid = await (0, customPgParsers_js_1.findCorrespondingArrayOid)(kyselyDb, pgTypes.builtins.TEXT);
|
|
79
|
+
if (textArrayOid) {
|
|
80
|
+
let oid;
|
|
81
|
+
const textArrayParser = pgTypes.getTypeParser(textArrayOid);
|
|
82
|
+
function transformPostgresArray(transformer) {
|
|
83
|
+
return (value) => textArrayParser(value).map(str => transformer(str));
|
|
84
|
+
}
|
|
85
|
+
const enumArrayOids = await (0, customPgParsers_js_1.findEnumArrayOids)(kyselyDb);
|
|
86
|
+
enumArrayOids.forEach((enumArrayOid) => pgTypes.setTypeParser(enumArrayOid, textArrayParser));
|
|
87
|
+
oid = await (0, customPgParsers_js_1.findCitextArrayOid)(kyselyDb);
|
|
88
|
+
if (oid)
|
|
89
|
+
pgTypes.setTypeParser(oid, textArrayParser);
|
|
90
|
+
oid = await (0, customPgParsers_js_1.findCorrespondingArrayOid)(kyselyDb, pgTypes.builtins.UUID);
|
|
91
|
+
if (oid)
|
|
92
|
+
pgTypes.setTypeParser(oid, textArrayParser);
|
|
93
|
+
oid = await (0, customPgParsers_js_1.findCorrespondingArrayOid)(kyselyDb, pgTypes.builtins.DATE);
|
|
94
|
+
if (oid)
|
|
95
|
+
pgTypes.setTypeParser(oid, transformPostgresArray(customPgParsers_js_1.parsePostgresDate));
|
|
96
|
+
oid = await (0, customPgParsers_js_1.findCorrespondingArrayOid)(kyselyDb, pgTypes.builtins.TIMESTAMP);
|
|
97
|
+
if (oid)
|
|
98
|
+
pgTypes.setTypeParser(oid, transformPostgresArray(customPgParsers_js_1.parsePostgresDatetime));
|
|
99
|
+
oid = await (0, customPgParsers_js_1.findCorrespondingArrayOid)(kyselyDb, pgTypes.builtins.TIMESTAMPTZ);
|
|
100
|
+
if (oid)
|
|
101
|
+
pgTypes.setTypeParser(oid, transformPostgresArray(customPgParsers_js_1.parsePostgresDatetime));
|
|
102
|
+
oid = await (0, customPgParsers_js_1.findCorrespondingArrayOid)(kyselyDb, pgTypes.builtins.NUMERIC);
|
|
103
|
+
if (oid)
|
|
104
|
+
pgTypes.setTypeParser(oid, transformPostgresArray(customPgParsers_js_1.parsePostgresDecimal));
|
|
105
|
+
oid = await (0, customPgParsers_js_1.findCorrespondingArrayOid)(kyselyDb, pgTypes.builtins.INT8);
|
|
106
|
+
if (oid)
|
|
107
|
+
pgTypes.setTypeParser(oid, transformPostgresArray(customPgParsers_js_1.parsePostgresBigint));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* @internal
|
|
112
|
+
*
|
|
113
|
+
* this is used by the SchemaBuilder to store column data permanently
|
|
114
|
+
* within the types/dream.ts file.
|
|
115
|
+
*/
|
|
116
|
+
static async getColumnData(connectionName, tableName, associationData) {
|
|
117
|
+
const db = this.dbFor(connectionName, 'primary');
|
|
118
|
+
const sqlQuery = (0, kysely_1.sql) `SELECT column_name, udt_name::regtype, is_nullable, data_type FROM information_schema.columns WHERE table_name = ${tableName}`;
|
|
119
|
+
const columnToDBTypeMap = await sqlQuery.execute(db);
|
|
120
|
+
const rows = columnToDBTypeMap.rows;
|
|
121
|
+
const columnData = {};
|
|
122
|
+
rows.forEach(row => {
|
|
123
|
+
const isEnum = ['USER-DEFINED', 'ARRAY'].includes(row.dataType) && !(0, dataTypes_js_1.isPrimitiveDataType)(row.udtName);
|
|
124
|
+
const isArray = ['ARRAY'].includes(row.dataType);
|
|
125
|
+
const associationMetadata = associationData[row.columnName];
|
|
126
|
+
columnData[(0, camelize_js_1.default)(row.columnName)] = {
|
|
127
|
+
dbType: row.udtName,
|
|
128
|
+
allowNull: row.isNullable === 'YES',
|
|
129
|
+
enumType: isEnum ? this.enumType(row) : null,
|
|
130
|
+
enumValues: isEnum ? `${this.enumType(row)}Values` : null,
|
|
131
|
+
isArray,
|
|
132
|
+
foreignKey: associationMetadata?.foreignKey || null,
|
|
133
|
+
};
|
|
134
|
+
});
|
|
135
|
+
return Object.keys(columnData)
|
|
136
|
+
.sort()
|
|
137
|
+
.reduce((acc, key) => {
|
|
138
|
+
if (columnData[key] === undefined)
|
|
139
|
+
return acc;
|
|
140
|
+
acc[key] = columnData[key];
|
|
141
|
+
return acc;
|
|
142
|
+
}, {});
|
|
143
|
+
}
|
|
144
|
+
static async duplicateDatabase(connectionName) {
|
|
145
|
+
const dreamApp = index_js_2.default.getOrFail();
|
|
146
|
+
const parallelTests = dreamApp.parallelTests;
|
|
147
|
+
if (!parallelTests)
|
|
148
|
+
return;
|
|
149
|
+
index_js_1.default.logger.logStartProgress(`duplicating db for parallel tests...`);
|
|
150
|
+
const dbConf = dreamApp.dbConnectionConfig(connectionName, 'primary');
|
|
151
|
+
const client = await (0, loadPgClient_js_1.default)({ useSystemDb: true, connectionName });
|
|
152
|
+
if (EnvInternal_js_1.default.boolean('DREAM_CORE_DEVELOPMENT')) {
|
|
153
|
+
const replicaTestWorkerDatabaseName = `replica_test_${dbConf.name}`;
|
|
154
|
+
index_js_1.default.logger.logContinueProgress(`creating fake replica test database ${replicaTestWorkerDatabaseName}...`, { logPrefix: ' ├ [db]', logPrefixColor: 'cyan' });
|
|
155
|
+
await client.query(`DROP DATABASE IF EXISTS ${replicaTestWorkerDatabaseName};`);
|
|
156
|
+
await client.query(`CREATE DATABASE ${replicaTestWorkerDatabaseName} TEMPLATE ${dbConf.name};`);
|
|
157
|
+
}
|
|
158
|
+
for (let i = 2; i <= parallelTests; i++) {
|
|
159
|
+
const workerDatabaseName = `${dbConf.name}_${i}`;
|
|
160
|
+
index_js_1.default.logger.logContinueProgress(`creating duplicate test database ${workerDatabaseName} for concurrent tests...`, { logPrefix: ' ├ [db]', logPrefixColor: 'cyan' });
|
|
161
|
+
await client.query(`DROP DATABASE IF EXISTS ${workerDatabaseName};`);
|
|
162
|
+
await client.query(`CREATE DATABASE ${workerDatabaseName} TEMPLATE ${dbConf.name};`);
|
|
163
|
+
}
|
|
164
|
+
await client.end();
|
|
165
|
+
index_js_1.default.logger.logEndProgress();
|
|
166
|
+
}
|
|
5
167
|
}
|
|
6
168
|
exports.default = PostgresQueryDriver;
|
|
@@ -4,15 +4,19 @@ exports.default = runMigration;
|
|
|
4
4
|
const kysely_1 = require("kysely");
|
|
5
5
|
const fs = require("node:fs/promises");
|
|
6
6
|
const path = require("node:path");
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const index_js_2 = require("
|
|
11
|
-
const index_js_3 = require("
|
|
12
|
-
async function runMigration({ mode = 'migrate'
|
|
13
|
-
const dreamApp =
|
|
14
|
-
const migrationFolder =
|
|
15
|
-
|
|
7
|
+
const colorize_js_1 = require("../../../../cli/logger/loggable/colorize.js");
|
|
8
|
+
const DreamDbConnection_js_1 = require("../../../../db/DreamDbConnection.js");
|
|
9
|
+
const index_js_1 = require("../../../../db/index.js");
|
|
10
|
+
const index_js_2 = require("../../../../dream-app/index.js");
|
|
11
|
+
const index_js_3 = require("../../../../cli/index.js");
|
|
12
|
+
async function runMigration({ connectionName, mode = 'migrate', dialectProvider, }) {
|
|
13
|
+
const dreamApp = index_js_2.default.getOrFail();
|
|
14
|
+
const migrationFolder = connectionName === 'default'
|
|
15
|
+
? path.join(dreamApp.projectRoot, dreamApp.paths.db, 'migrations')
|
|
16
|
+
: path.join(dreamApp.projectRoot, dreamApp.paths.db, 'migrations', connectionName);
|
|
17
|
+
// Ensure the migration folder exists
|
|
18
|
+
await fs.mkdir(migrationFolder, { recursive: true });
|
|
19
|
+
const kyselyDb = (0, index_js_1.default)(connectionName, 'primary', dialectProvider);
|
|
16
20
|
const migrator = new kysely_1.Migrator({
|
|
17
21
|
db: kyselyDb,
|
|
18
22
|
allowUnorderedMigrations: true,
|
|
@@ -28,7 +32,7 @@ async function runMigration({ mode = 'migrate' } = {}) {
|
|
|
28
32
|
else if (mode === 'rollback') {
|
|
29
33
|
await rollback(migrator);
|
|
30
34
|
}
|
|
31
|
-
await DreamDbConnection_js_1.
|
|
35
|
+
await (0, DreamDbConnection_js_1.closeAllConnectionsForConnectionName)(connectionName);
|
|
32
36
|
}
|
|
33
37
|
async function migrate(migrator) {
|
|
34
38
|
let nextMigrationRequiringNewTransaction = await findNextMigrationRequiringNewTransaction(migrator);
|
|
@@ -74,9 +78,9 @@ async function rollback(migrator) {
|
|
|
74
78
|
await handleError(error, 'rollback');
|
|
75
79
|
}
|
|
76
80
|
async function handleError(error, mode) {
|
|
77
|
-
await DreamDbConnection_js_1.
|
|
78
|
-
|
|
79
|
-
|
|
81
|
+
await (0, DreamDbConnection_js_1.closeAllDbConnections)();
|
|
82
|
+
index_js_2.default.logWithLevel('error', `failed to ${migratedActionCurrentTense(mode)}`);
|
|
83
|
+
index_js_2.default.logWithLevel('error', error);
|
|
80
84
|
process.exit(1);
|
|
81
85
|
}
|
|
82
86
|
function migratedActionCurrentTense(mode) {
|
|
@@ -88,12 +92,12 @@ function migratedActionPastTense(mode) {
|
|
|
88
92
|
function logResults(results, mode) {
|
|
89
93
|
results?.forEach(it => {
|
|
90
94
|
if (it.status === 'Success') {
|
|
91
|
-
|
|
95
|
+
index_js_3.default.logger.logContinueProgress((0, colorize_js_1.default)(`[db]`, { color: 'cyan' }) +
|
|
92
96
|
` migration "${it.migrationName}" was ${migratedActionPastTense(mode)} successfully`, { logPrefixColor: 'cyan' });
|
|
93
97
|
}
|
|
94
98
|
else if (it.status === 'Error') {
|
|
95
|
-
|
|
96
|
-
|
|
99
|
+
index_js_3.default.logger.logContinueProgress(JSON.stringify(it, null, 2));
|
|
100
|
+
index_js_3.default.logger.logContinueProgress((0, colorize_js_1.default)(`failed to ${migratedActionCurrentTense(mode)} migration "${it.migrationName}"`, {
|
|
97
101
|
color: 'redBright',
|
|
98
102
|
}));
|
|
99
103
|
}
|
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.default = createDb;
|
|
4
|
-
const index_js_1 = require("
|
|
5
|
-
const EnvInternal_js_1 = require("
|
|
4
|
+
const index_js_1 = require("../../../../dream-app/index.js");
|
|
5
|
+
const EnvInternal_js_1 = require("../../../../helpers/EnvInternal.js");
|
|
6
6
|
const loadPgClient_js_1 = require("./loadPgClient.js");
|
|
7
|
-
async function createDb(
|
|
7
|
+
async function createDb(connectionName, connectionType, dbName) {
|
|
8
8
|
// this was only ever written to clear the db between tests or in development,
|
|
9
9
|
// so there is no way to drop in production
|
|
10
10
|
if (EnvInternal_js_1.default.isProduction)
|
|
11
11
|
return false;
|
|
12
12
|
const dreamApp = index_js_1.default.getOrFail();
|
|
13
|
-
const dbConf = dreamApp.dbConnectionConfig(
|
|
13
|
+
const dbConf = dreamApp.dbConnectionConfig(connectionName, connectionType);
|
|
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');
|
|
17
|
-
const client = await (0, loadPgClient_js_1.default)({ useSystemDb: true });
|
|
17
|
+
const client = await (0, loadPgClient_js_1.default)({ useSystemDb: true, connectionName });
|
|
18
18
|
await client.query(`CREATE DATABASE ${dbName};`);
|
|
19
19
|
await client.end();
|
|
20
20
|
}
|
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.default = dropDb;
|
|
4
|
-
const index_js_1 = require("
|
|
5
|
-
const index_js_2 = require("
|
|
6
|
-
const EnvInternal_js_1 = require("
|
|
4
|
+
const index_js_1 = require("../../../../cli/index.js");
|
|
5
|
+
const index_js_2 = require("../../../../dream-app/index.js");
|
|
6
|
+
const EnvInternal_js_1 = require("../../../../helpers/EnvInternal.js");
|
|
7
7
|
const loadPgClient_js_1 = require("./loadPgClient.js");
|
|
8
|
-
async function dropDb(connection, dbName) {
|
|
8
|
+
async function dropDb(connectionName, 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
13
|
const dreamApp = index_js_2.default.getOrFail();
|
|
14
|
-
const dbConf = dreamApp.dbConnectionConfig(connection);
|
|
14
|
+
const dbConf = dreamApp.dbConnectionConfig(connectionName, 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');
|
|
18
|
-
const client = await (0, loadPgClient_js_1.default)({ useSystemDb: true });
|
|
18
|
+
const client = await (0, loadPgClient_js_1.default)({ useSystemDb: true, connectionName });
|
|
19
19
|
await maybeDropDuplicateDatabases(client, dbName);
|
|
20
20
|
await client.query(`DROP DATABASE IF EXISTS ${dbName};`);
|
|
21
21
|
}
|
|
@@ -10,10 +10,12 @@ exports.default = loadPgClient;
|
|
|
10
10
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
11
11
|
// @ts-ignore
|
|
12
12
|
const pg_1 = require("pg");
|
|
13
|
-
const index_js_1 = require("
|
|
14
|
-
async function loadPgClient({ useSystemDb }
|
|
13
|
+
const index_js_1 = require("../../../../dream-app/index.js");
|
|
14
|
+
async function loadPgClient({ connectionName, useSystemDb, }) {
|
|
15
15
|
const dreamconf = index_js_1.default.getOrFail();
|
|
16
|
-
const creds = dreamconf.
|
|
16
|
+
const creds = dreamconf.dbCredentialsFor(connectionName)?.primary;
|
|
17
|
+
if (!creds)
|
|
18
|
+
throw new Error(`failed to load db credentials for connection: ${connectionName}`);
|
|
17
19
|
const client = new pg_1.default.Client({
|
|
18
20
|
host: creds.host || 'localhost',
|
|
19
21
|
port: creds.port,
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.default = destroyDream;
|
|
4
|
+
const index_js_1 = require("../../dream-app/index.js");
|
|
4
5
|
const destroyAssociatedRecords_js_1 = require("./destroyAssociatedRecords.js");
|
|
5
6
|
const runHooksFor_js_1 = require("./runHooksFor.js");
|
|
6
|
-
const softDeleteDream_js_1 = require("./softDeleteDream.js");
|
|
7
7
|
/**
|
|
8
8
|
* @internal
|
|
9
9
|
*
|
|
@@ -59,13 +59,6 @@ function shouldSoftDelete(dream, reallyDestroy) {
|
|
|
59
59
|
* deleting by one of the beforeDestroy model hooks
|
|
60
60
|
*/
|
|
61
61
|
async function maybeDestroyDream(dream, txn, reallyDestroy) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
else if (!dream['_preventDeletion']) {
|
|
66
|
-
await txn.kyselyTransaction
|
|
67
|
-
.deleteFrom(dream.table)
|
|
68
|
-
.where(dream['_primaryKey'], '=', dream.primaryKeyValue())
|
|
69
|
-
.execute();
|
|
70
|
-
}
|
|
62
|
+
const dbDriverClass = index_js_1.default.getOrFail().dbConnectionQueryDriverClass(dream.connectionName);
|
|
63
|
+
await dbDriverClass.destroyDream(dream, txn, reallyDestroy);
|
|
71
64
|
}
|
|
@@ -31,7 +31,7 @@ async function saveDream(dream, txn = null, { skipHooks = false } = {}) {
|
|
|
31
31
|
// BeforeSave/Update actions may clear all the data that we intended to save, leaving us with
|
|
32
32
|
// an invalid update command. The Sortable decorator is an example of this.
|
|
33
33
|
if (!alreadyPersisted || hasUnsavedData) {
|
|
34
|
-
const data = await Query_js_1.default.dbDriverClass().saveDream(dream, txn);
|
|
34
|
+
const data = await Query_js_1.default.dbDriverClass(dream.connectionName || 'default').saveDream(dream, txn);
|
|
35
35
|
dream['isPersisted'] = true;
|
|
36
36
|
dream.setAttributes(data);
|
|
37
37
|
}
|
|
@@ -11,6 +11,7 @@ const namespaceColumn_js_1 = require("../../../helpers/namespaceColumn.js");
|
|
|
11
11
|
const constants_js_1 = require("../../constants.js");
|
|
12
12
|
const similaritySelectSql_js_1 = require("./similaritySelectSql.js");
|
|
13
13
|
const similarityWhereSql_js_1 = require("./similarityWhereSql.js");
|
|
14
|
+
const setPosition_js_1 = require("../../../decorators/field/sortable/helpers/setPosition.js");
|
|
14
15
|
class SimilarityBuilder extends ConnectedToDB_js_1.default {
|
|
15
16
|
whereStatement;
|
|
16
17
|
whereNotStatement;
|
|
@@ -217,8 +218,9 @@ class SimilarityBuilder extends ConnectedToDB_js_1.default {
|
|
|
217
218
|
const schema = this.dreamClass.prototype.schema;
|
|
218
219
|
const primaryKeyName = this.dreamClass.primaryKey;
|
|
219
220
|
const { tableName, tableAlias, columnName } = similarityStatement;
|
|
221
|
+
const queryDriverClass = (0, setPosition_js_1.getPostgresQueryDriver)(this.dreamInstance.connectionName);
|
|
220
222
|
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
221
|
-
const { ref } =
|
|
223
|
+
const { ref } = queryDriverClass.dbFor(this.dreamInstance.connectionName, this.dbConnectionType('select')).dynamic;
|
|
222
224
|
const validatedTableAlias = (0, validateTableAlias_js_1.default)(tableAlias);
|
|
223
225
|
const validatedPrimaryKey = (0, validateColumn_js_1.default)(schema, tableName, primaryKeyName);
|
|
224
226
|
const nestedQuery = this.buildNestedSelectQuery({
|
|
@@ -248,7 +250,9 @@ class SimilarityBuilder extends ConnectedToDB_js_1.default {
|
|
|
248
250
|
statementType,
|
|
249
251
|
});
|
|
250
252
|
const trigramSearchAlias = this.similaritySearchId(tableAlias, columnName);
|
|
251
|
-
const
|
|
253
|
+
const queryDriverClass = (0, setPosition_js_1.getPostgresQueryDriver)(this.dreamInstance.connectionName);
|
|
254
|
+
const selectQuery = queryDriverClass
|
|
255
|
+
.dbFor(this.dreamInstance.connectionName, this.dbConnectionType('select'))
|
|
252
256
|
.selectFrom(validatedTableAlias)
|
|
253
257
|
.select(`${trigramSearchAlias}.trigram_search_id`)
|
|
254
258
|
.innerJoin(nestedQuery.as(trigramSearchAlias), join => join.onRef((0, namespaceColumn_js_1.default)(validatedPrimaryKey, validatedTableAlias), '=', (0, namespaceColumn_js_1.default)('trigram_search_id', trigramSearchAlias)));
|
|
@@ -260,7 +264,9 @@ class SimilarityBuilder extends ConnectedToDB_js_1.default {
|
|
|
260
264
|
const { columnName, opsStatement, tableName } = similarityStatement;
|
|
261
265
|
const validatedTable = (0, validateTable_js_1.default)(schema, tableName);
|
|
262
266
|
const validatedPrimaryKey = (0, validateColumn_js_1.default)(schema, tableName, primaryKeyName);
|
|
263
|
-
|
|
267
|
+
const queryDriverClass = (0, setPosition_js_1.getPostgresQueryDriver)(this.dreamInstance.connectionName);
|
|
268
|
+
let nestedQuery = queryDriverClass
|
|
269
|
+
.dbFor(this.dreamInstance.connectionName, this.dbConnectionType('select'))
|
|
264
270
|
.selectFrom(tableName)
|
|
265
271
|
.select(eb => {
|
|
266
272
|
const tableNameRef = eb.ref(validatedTable);
|
|
@@ -1,29 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.GeneratorImportStyles = exports.DreamAppAllowedPackageManagersEnumValues = void 0;
|
|
4
|
-
// after building for esm, importing pg using the following:
|
|
5
|
-
//
|
|
6
|
-
// import * as pg from 'pg'
|
|
7
|
-
//
|
|
8
|
-
// will crash. This is difficult to discover, since it only happens
|
|
9
|
-
// when being imported from our esm build.
|
|
10
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
11
|
-
// @ts-ignore
|
|
12
|
-
const pg_1 = require("pg");
|
|
13
4
|
const util = require("node:util");
|
|
14
|
-
const index_js_1 = require("../db/index.js");
|
|
15
5
|
const validateTable_js_1 = require("../db/validators/validateTable.js");
|
|
16
|
-
const
|
|
6
|
+
const Query_js_1 = require("../dream/Query.js");
|
|
7
|
+
const index_js_1 = require("../encrypt/index.js");
|
|
17
8
|
const DreamAppInitMissingCallToLoadModels_js_1 = require("../errors/dream-app/DreamAppInitMissingCallToLoadModels.js");
|
|
18
9
|
const DreamAppInitMissingMissingProjectRoot_js_1 = require("../errors/dream-app/DreamAppInitMissingMissingProjectRoot.js");
|
|
19
10
|
const CalendarDate_js_1 = require("../helpers/CalendarDate.js");
|
|
20
|
-
const customPgParsers_js_1 = require("../helpers/customPgParsers.js");
|
|
21
11
|
const DateTime_js_1 = require("../helpers/DateTime.js");
|
|
22
12
|
const EnvInternal_js_1 = require("../helpers/EnvInternal.js");
|
|
23
13
|
const cache_js_1 = require("./cache.js");
|
|
24
14
|
const importModels_js_1 = require("./helpers/importers/importModels.js");
|
|
25
15
|
const importSerializers_js_1 = require("./helpers/importers/importSerializers.js");
|
|
26
|
-
const
|
|
16
|
+
const Postgres_js_1 = require("../dream/QueryDriver/Postgres.js");
|
|
27
17
|
// this needs to be done top-level to ensure proper configuration
|
|
28
18
|
DateTime_js_1.Settings.defaultZone = 'UTC';
|
|
29
19
|
class DreamApp {
|
|
@@ -47,7 +37,7 @@ class DreamApp {
|
|
|
47
37
|
(0, importSerializers_js_1.setCachedSerializers)({});
|
|
48
38
|
(0, cache_js_1.cacheDreamApp)(dreamApp);
|
|
49
39
|
if (!EnvInternal_js_1.default.boolean('BYPASS_DB_CONNECTIONS_DURING_INIT'))
|
|
50
|
-
await this.setDatabaseTypeParsers();
|
|
40
|
+
await this.setDatabaseTypeParsers(dreamApp);
|
|
51
41
|
await deferCb?.(dreamApp);
|
|
52
42
|
for (const plugin of dreamApp.plugins) {
|
|
53
43
|
await plugin(dreamApp);
|
|
@@ -90,51 +80,18 @@ class DreamApp {
|
|
|
90
80
|
*
|
|
91
81
|
*
|
|
92
82
|
*/
|
|
93
|
-
static async setDatabaseTypeParsers() {
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
pgTypes.setTypeParser(pgTypes.builtins.TIMESTAMPTZ, customPgParsers_js_1.parsePostgresDatetime);
|
|
98
|
-
pgTypes.setTypeParser(pgTypes.builtins.NUMERIC, customPgParsers_js_1.parsePostgresDecimal);
|
|
99
|
-
pgTypes.setTypeParser(pgTypes.builtins.INT8, customPgParsers_js_1.parsePostgresBigint);
|
|
100
|
-
const textArrayOid = await (0, customPgParsers_js_1.findCorrespondingArrayOid)(kyselyDb, pgTypes.builtins.TEXT);
|
|
101
|
-
if (textArrayOid) {
|
|
102
|
-
let oid;
|
|
103
|
-
const textArrayParser = pgTypes.getTypeParser(textArrayOid);
|
|
104
|
-
function transformPostgresArray(transformer) {
|
|
105
|
-
return (value) => textArrayParser(value).map(str => transformer(str));
|
|
106
|
-
}
|
|
107
|
-
const enumArrayOids = await (0, customPgParsers_js_1.findEnumArrayOids)(kyselyDb);
|
|
108
|
-
enumArrayOids.forEach((enumArrayOid) => pgTypes.setTypeParser(enumArrayOid, textArrayParser));
|
|
109
|
-
oid = await (0, customPgParsers_js_1.findCitextArrayOid)(kyselyDb);
|
|
110
|
-
if (oid)
|
|
111
|
-
pgTypes.setTypeParser(oid, textArrayParser);
|
|
112
|
-
oid = await (0, customPgParsers_js_1.findCorrespondingArrayOid)(kyselyDb, pgTypes.builtins.UUID);
|
|
113
|
-
if (oid)
|
|
114
|
-
pgTypes.setTypeParser(oid, textArrayParser);
|
|
115
|
-
oid = await (0, customPgParsers_js_1.findCorrespondingArrayOid)(kyselyDb, pgTypes.builtins.DATE);
|
|
116
|
-
if (oid)
|
|
117
|
-
pgTypes.setTypeParser(oid, transformPostgresArray(customPgParsers_js_1.parsePostgresDate));
|
|
118
|
-
oid = await (0, customPgParsers_js_1.findCorrespondingArrayOid)(kyselyDb, pgTypes.builtins.TIMESTAMP);
|
|
119
|
-
if (oid)
|
|
120
|
-
pgTypes.setTypeParser(oid, transformPostgresArray(customPgParsers_js_1.parsePostgresDatetime));
|
|
121
|
-
oid = await (0, customPgParsers_js_1.findCorrespondingArrayOid)(kyselyDb, pgTypes.builtins.TIMESTAMPTZ);
|
|
122
|
-
if (oid)
|
|
123
|
-
pgTypes.setTypeParser(oid, transformPostgresArray(customPgParsers_js_1.parsePostgresDatetime));
|
|
124
|
-
oid = await (0, customPgParsers_js_1.findCorrespondingArrayOid)(kyselyDb, pgTypes.builtins.NUMERIC);
|
|
125
|
-
if (oid)
|
|
126
|
-
pgTypes.setTypeParser(oid, transformPostgresArray(customPgParsers_js_1.parsePostgresDecimal));
|
|
127
|
-
oid = await (0, customPgParsers_js_1.findCorrespondingArrayOid)(kyselyDb, pgTypes.builtins.INT8);
|
|
128
|
-
if (oid)
|
|
129
|
-
pgTypes.setTypeParser(oid, transformPostgresArray(customPgParsers_js_1.parsePostgresBigint));
|
|
83
|
+
static async setDatabaseTypeParsers(dreamApp) {
|
|
84
|
+
for (const connectionName of Object.keys(dreamApp._dbCredentials)) {
|
|
85
|
+
const dbDriverClass = Query_js_1.default.dbDriverClass(connectionName);
|
|
86
|
+
await dbDriverClass.setDatabaseTypeParsers(connectionName);
|
|
130
87
|
}
|
|
131
88
|
}
|
|
132
89
|
static checkKey(encryptionIdentifier, key, algorithm) {
|
|
133
|
-
if (!
|
|
90
|
+
if (!index_js_1.default.validateKey(key, algorithm))
|
|
134
91
|
console.warn(`
|
|
135
92
|
Your current key value for ${encryptionIdentifier} encryption is invalid.
|
|
136
93
|
Try setting it to something valid, like:
|
|
137
|
-
${
|
|
94
|
+
${index_js_1.default.generateKey(algorithm)}
|
|
138
95
|
|
|
139
96
|
(This was done by calling:
|
|
140
97
|
Encrypt.generateKey('${algorithm}')
|
|
@@ -168,10 +125,15 @@ Try setting it to something valid, like:
|
|
|
168
125
|
get specialHooks() {
|
|
169
126
|
return this._specialHooks;
|
|
170
127
|
}
|
|
171
|
-
_dbCredentials;
|
|
128
|
+
_dbCredentials = {};
|
|
172
129
|
get dbCredentials() {
|
|
173
130
|
return this._dbCredentials;
|
|
174
131
|
}
|
|
132
|
+
dbCredentialsFor(connectionName) {
|
|
133
|
+
if (this._dbCredentials[connectionName])
|
|
134
|
+
return this._dbCredentials[connectionName];
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
175
137
|
_encryption;
|
|
176
138
|
get encryption() {
|
|
177
139
|
return this._encryption;
|
|
@@ -227,7 +189,7 @@ Try setting it to something valid, like:
|
|
|
227
189
|
loadedModels = false;
|
|
228
190
|
constructor(opts) {
|
|
229
191
|
if (opts?.db)
|
|
230
|
-
this._dbCredentials = opts.db;
|
|
192
|
+
this._dbCredentials['default'] = opts.db;
|
|
231
193
|
if (opts?.primaryKeyType)
|
|
232
194
|
this._primaryKeyType = opts.primaryKeyType;
|
|
233
195
|
if (opts?.projectRoot)
|
|
@@ -254,22 +216,38 @@ Try setting it to something valid, like:
|
|
|
254
216
|
get serializers() {
|
|
255
217
|
return (0, importSerializers_js_1.getSerializersOrFail)();
|
|
256
218
|
}
|
|
257
|
-
dbName(connection) {
|
|
258
|
-
const conf = this.dbConnectionConfig(connection);
|
|
219
|
+
dbName(connectionName, connection) {
|
|
220
|
+
const conf = this.dbConnectionConfig(connectionName, connection);
|
|
259
221
|
return this.parallelDatabasesEnabled ? `${conf.name}_${process.env.VITEST_POOL_ID}` : conf.name;
|
|
260
222
|
}
|
|
261
|
-
dbConnectionConfig(connection) {
|
|
262
|
-
const conf = this.
|
|
263
|
-
if (!conf)
|
|
223
|
+
dbConnectionConfig(connectionName, connection) {
|
|
224
|
+
const conf = this.dbCredentialsFor(connectionName)?.[connection] || this.dbCredentialsFor(connectionName)?.primary;
|
|
225
|
+
if (!conf) {
|
|
264
226
|
throw new Error(`
|
|
265
227
|
Cannot find a connection config given the following connection and node environment:
|
|
228
|
+
connectionName: ${connectionName}
|
|
266
229
|
connection: ${connection}
|
|
267
230
|
NODE_ENV: ${EnvInternal_js_1.default.nodeEnv}
|
|
268
231
|
`);
|
|
232
|
+
}
|
|
269
233
|
return conf;
|
|
270
234
|
}
|
|
271
|
-
|
|
272
|
-
|
|
235
|
+
dbConnectionQueryDriverClass(connectionName) {
|
|
236
|
+
const conf = this.dbCredentialsFor(connectionName);
|
|
237
|
+
if (!conf) {
|
|
238
|
+
throw new Error(`
|
|
239
|
+
Cannot find a db credentials for the given connectionName:
|
|
240
|
+
connectionName: ${connectionName}
|
|
241
|
+
NODE_ENV: ${EnvInternal_js_1.default.nodeEnv}
|
|
242
|
+
`);
|
|
243
|
+
}
|
|
244
|
+
return (conf.queryDriverClass) || (Postgres_js_1.default);
|
|
245
|
+
}
|
|
246
|
+
dbConnectionKeys() {
|
|
247
|
+
return Object.keys(this.dbCredentials);
|
|
248
|
+
}
|
|
249
|
+
hasReplicaConfig(connectionName) {
|
|
250
|
+
return !!this.dbCredentials[connectionName]?.replica;
|
|
273
251
|
}
|
|
274
252
|
get parallelDatabasesEnabled() {
|
|
275
253
|
return (!!this.parallelTests &&
|
|
@@ -290,10 +268,15 @@ Try setting it to something valid, like:
|
|
|
290
268
|
plugin(cb) {
|
|
291
269
|
this._plugins.push(cb);
|
|
292
270
|
}
|
|
293
|
-
set(applyOption, options) {
|
|
271
|
+
set(applyOption, options, secondaryOptions) {
|
|
294
272
|
switch (applyOption) {
|
|
295
273
|
case 'db':
|
|
296
|
-
|
|
274
|
+
if (typeof options === 'string') {
|
|
275
|
+
this._dbCredentials[options] = secondaryOptions;
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
this._dbCredentials['default'] = options;
|
|
279
|
+
}
|
|
297
280
|
break;
|
|
298
281
|
case 'encryption':
|
|
299
282
|
this._encryption = options;
|