@rvoh/dream 1.4.1 → 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 +166 -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 +59 -65
- package/dist/cjs/src/helpers/cli/SchemaBuilder.js +30 -42
- package/dist/cjs/src/helpers/cli/addImportSuffix.js +17 -0
- 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/path/relativeDreamPath.js +4 -3
- 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 +55 -61
- package/dist/esm/src/helpers/cli/SchemaBuilder.js +24 -36
- package/dist/esm/src/helpers/cli/addImportSuffix.js +14 -0
- 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/path/relativeDreamPath.js +4 -3
- 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 +16 -7
- package/dist/types/src/helpers/cli/SchemaBuilder.d.ts +26 -0
- package/dist/types/src/helpers/cli/addImportSuffix.d.ts +1 -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 +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/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 +4 -3
- 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,5 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const CalendarDate_js_1 = require("../../helpers/CalendarDate.js");
|
|
4
|
+
const DateTime_js_1 = require("../../helpers/DateTime.js");
|
|
3
5
|
class QueryDriverBase {
|
|
4
6
|
query;
|
|
5
7
|
dreamClass;
|
|
@@ -24,7 +26,9 @@ class QueryDriverBase {
|
|
|
24
26
|
* migrate the database. Must respond to the NODE_ENV value.
|
|
25
27
|
*/
|
|
26
28
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
27
|
-
static async migrate(
|
|
29
|
+
static async migrate(
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
31
|
+
connectionName) {
|
|
28
32
|
throw new Error('override migrate in child class');
|
|
29
33
|
}
|
|
30
34
|
/**
|
|
@@ -38,14 +42,18 @@ class QueryDriverBase {
|
|
|
38
42
|
* create the database. Must respond to the NODE_ENV value.
|
|
39
43
|
*/
|
|
40
44
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
41
|
-
static async dbCreate(
|
|
45
|
+
static async dbCreate(
|
|
46
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
47
|
+
connectionName) {
|
|
42
48
|
throw new Error('override dbCreate on child class');
|
|
43
49
|
}
|
|
44
50
|
/**
|
|
45
51
|
* delete the database. Must respond to the NODE_ENV value.
|
|
46
52
|
*/
|
|
47
53
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
48
|
-
static async dbDrop(
|
|
54
|
+
static async dbDrop(
|
|
55
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
56
|
+
connectionName) {
|
|
49
57
|
throw new Error('override dbDrop on child class');
|
|
50
58
|
}
|
|
51
59
|
/**
|
|
@@ -56,8 +64,14 @@ class QueryDriverBase {
|
|
|
56
64
|
* is sorted by date in the file tree, and, more importantly, so
|
|
57
65
|
* they can be run in order by your migration runner.
|
|
58
66
|
*/
|
|
59
|
-
// eslint-disable-next-line @typescript-eslint/require-await
|
|
60
|
-
static async generateMigration(
|
|
67
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
68
|
+
static async generateMigration(
|
|
69
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
70
|
+
connectionName,
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
72
|
+
migrationName,
|
|
73
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
74
|
+
columnsWithTypes) {
|
|
61
75
|
throw new Error('override generateMigration in child class');
|
|
62
76
|
}
|
|
63
77
|
/**
|
|
@@ -76,8 +90,10 @@ class QueryDriverBase {
|
|
|
76
90
|
* but this will likely need to be overridden to tailor to your custom
|
|
77
91
|
* database engine.
|
|
78
92
|
*/
|
|
79
|
-
// eslint-disable-next-line @typescript-eslint/require-await
|
|
80
|
-
static async sync(_
|
|
93
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
94
|
+
static async sync(connectionName, _,
|
|
95
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
96
|
+
options = {}) {
|
|
81
97
|
throw new Error('override sync on child class');
|
|
82
98
|
}
|
|
83
99
|
/**
|
|
@@ -94,6 +110,79 @@ class QueryDriverBase {
|
|
|
94
110
|
toKysely(type) {
|
|
95
111
|
throw new Error('implement toKysely in child class (if it makes sense)');
|
|
96
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* Builds a new DreamTransaction instance, provides
|
|
115
|
+
* the instance to the provided callback.
|
|
116
|
+
*
|
|
117
|
+
* ```ts
|
|
118
|
+
* await ApplicationModel.transaction(async txn => {
|
|
119
|
+
* const user = await User.txn(txn).create({ email: 'how@yadoin' })
|
|
120
|
+
* await Pet.txn(txn).create({ user })
|
|
121
|
+
* })
|
|
122
|
+
* ```
|
|
123
|
+
*
|
|
124
|
+
* @param callback - A callback function to call. The transaction provided to the callback can be passed to subsequent database calls within the transaction callback
|
|
125
|
+
* @returns void
|
|
126
|
+
*/
|
|
127
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
128
|
+
static async transaction(dreamInstance, callback) {
|
|
129
|
+
throw new Error('implement transaction in child class');
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* @internal
|
|
133
|
+
*
|
|
134
|
+
* returns the foreign key type based on the primary key received.
|
|
135
|
+
* gives the driver the opportunity to switch i.e. bigserial to bigint.
|
|
136
|
+
*/
|
|
137
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
138
|
+
static foreignKeyTypeFromPrimaryKey(primaryKey) {
|
|
139
|
+
throw new Error('implement foreignKeyTypeFromPrimaryKey in child class');
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* @internal
|
|
143
|
+
*
|
|
144
|
+
* used to return the computed primary key type based
|
|
145
|
+
* on the primaryKeyType set in the DreamApp class.
|
|
146
|
+
*/
|
|
147
|
+
static primaryKeyType() {
|
|
148
|
+
throw new Error('implement primaryKeyType in child class');
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* @internal
|
|
152
|
+
*
|
|
153
|
+
* this method is called when dream is initializing, and is used
|
|
154
|
+
* to configure the database to utilize custom type parsers for
|
|
155
|
+
* a variety of data types.
|
|
156
|
+
*
|
|
157
|
+
* @param connectionName - the name of the connection you are doing this for
|
|
158
|
+
* @returns void
|
|
159
|
+
*/
|
|
160
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
161
|
+
static async setDatabaseTypeParsers(connectionName) { }
|
|
162
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
163
|
+
static async duplicateDatabase(connectionName) { }
|
|
164
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
165
|
+
static async getColumnData(
|
|
166
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
167
|
+
connectionName,
|
|
168
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
169
|
+
tableName,
|
|
170
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
171
|
+
associationData) {
|
|
172
|
+
throw new Error('implement getColumnData in child class');
|
|
173
|
+
}
|
|
174
|
+
static serializeDbType(type, val) {
|
|
175
|
+
switch (type) {
|
|
176
|
+
case 'datetime':
|
|
177
|
+
case 'date':
|
|
178
|
+
if (val instanceof DateTime_js_1.DateTime || val instanceof CalendarDate_js_1.default) {
|
|
179
|
+
return val.toSQL();
|
|
180
|
+
}
|
|
181
|
+
throw new Error(`unrecognized value found when trying to serialize for date/datetime: ${val}`);
|
|
182
|
+
default:
|
|
183
|
+
return val;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
97
186
|
/**
|
|
98
187
|
* @internal
|
|
99
188
|
*
|
|
@@ -249,6 +338,21 @@ class QueryDriverBase {
|
|
|
249
338
|
static async saveDream(dream, txn = null) {
|
|
250
339
|
throw new Error('implement saveDream in child class');
|
|
251
340
|
}
|
|
341
|
+
/**
|
|
342
|
+
* destroys a dream, possibly implementing soft delete if reallyDestroy is false
|
|
343
|
+
* and the record being deleted implements soft delete.
|
|
344
|
+
*
|
|
345
|
+
* @param dream - the dream instance you wish to destroy
|
|
346
|
+
* @param txn - a transaction to encapsulate, consistently provided by underlying dream mechanisms
|
|
347
|
+
* @param reallyDestroy - whether or not to reallyDestroy. If false, soft delete will be attempted when relevant
|
|
348
|
+
*/
|
|
349
|
+
// eslint-disable-next-line @typescript-eslint/require-await, @typescript-eslint/no-unused-vars
|
|
350
|
+
static async destroyDream(dream, txn, reallyDestroy) {
|
|
351
|
+
throw new Error('implement destroyDream in child class');
|
|
352
|
+
}
|
|
353
|
+
static get syncDialect() {
|
|
354
|
+
return 'postgres';
|
|
355
|
+
}
|
|
252
356
|
/**
|
|
253
357
|
* Returns the sql that would be executed by this Query
|
|
254
358
|
*
|
|
@@ -1,11 +1,20 @@
|
|
|
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");
|
|
3
12
|
const kysely_1 = require("kysely");
|
|
4
13
|
const pluralize_esm_1 = require("pluralize-esm");
|
|
5
|
-
const sync_js_1 = require("../../bin/helpers/sync.js");
|
|
6
14
|
const CliFileWriter_js_1 = require("../../cli/CliFileWriter.js");
|
|
7
15
|
const index_js_1 = require("../../cli/index.js");
|
|
8
16
|
const errors_js_1 = require("../../db/errors.js");
|
|
17
|
+
const syncDbTypesFiles_js_1 = require("../../db/helpers/syncDbTypesFiles.js");
|
|
9
18
|
const index_js_2 = require("../../db/index.js");
|
|
10
19
|
const associationToGetterSetterProp_js_1 = require("../../decorators/field/association/associationToGetterSetterProp.js");
|
|
11
20
|
const PackageManager_js_1 = require("../../dream-app/helpers/PackageManager.js");
|
|
@@ -30,16 +39,13 @@ const generateMigration_js_1 = require("../../helpers/cli/generateMigration.js")
|
|
|
30
39
|
const SchemaBuilder_js_1 = require("../../helpers/cli/SchemaBuilder.js");
|
|
31
40
|
const compact_js_1 = require("../../helpers/compact.js");
|
|
32
41
|
const DateTime_js_1 = require("../../helpers/DateTime.js");
|
|
33
|
-
const createDb_js_1 = require("../../helpers/db/createDb.js");
|
|
34
|
-
const dropDb_js_1 = require("../../helpers/db/dropDb.js");
|
|
35
|
-
const loadPgClient_js_1 = require("../../helpers/db/loadPgClient.js");
|
|
36
|
-
const runMigration_js_1 = require("../../helpers/db/runMigration.js");
|
|
37
42
|
const EnvInternal_js_1 = require("../../helpers/EnvInternal.js");
|
|
38
43
|
const groupBy_js_1 = require("../../helpers/groupBy.js");
|
|
39
44
|
const isEmpty_js_1 = require("../../helpers/isEmpty.js");
|
|
40
45
|
const namespaceColumn_js_1 = require("../../helpers/namespaceColumn.js");
|
|
41
46
|
const normalizeUnicode_js_1 = require("../../helpers/normalizeUnicode.js");
|
|
42
47
|
const objectPathsToArrays_js_1 = require("../../helpers/objectPathsToArrays.js");
|
|
48
|
+
const pascalize_js_1 = require("../../helpers/pascalize.js");
|
|
43
49
|
const protectAgainstPollutingAssignment_js_1 = require("../../helpers/protectAgainstPollutingAssignment.js");
|
|
44
50
|
const range_js_1 = require("../../helpers/range.js");
|
|
45
51
|
const snakeify_js_1 = require("../../helpers/snakeify.js");
|
|
@@ -48,6 +54,7 @@ const uniq_js_1 = require("../../helpers/uniq.js");
|
|
|
48
54
|
const curried_ops_statement_js_1 = require("../../ops/curried-ops-statement.js");
|
|
49
55
|
const ops_statement_js_1 = require("../../ops/ops-statement.js");
|
|
50
56
|
const constants_js_1 = require("../constants.js");
|
|
57
|
+
const DreamTransaction_js_1 = require("../DreamTransaction.js");
|
|
51
58
|
const throughAssociationHasOptionsBesidesThroughAndSource_js_1 = require("../internal/associations/throughAssociationHasOptionsBesidesThroughAndSource.js");
|
|
52
59
|
const associationStringToNameAndAlias_js_1 = require("../internal/associationStringToNameAndAlias.js");
|
|
53
60
|
const executeDatabaseQuery_js_1 = require("../internal/executeDatabaseQuery.js");
|
|
@@ -57,6 +64,8 @@ const SimilarityBuilder_js_1 = require("../internal/similarity/SimilarityBuilder
|
|
|
57
64
|
const sqlResultToDreamInstance_js_1 = require("../internal/sqlResultToDreamInstance.js");
|
|
58
65
|
const Query_js_1 = require("../Query.js");
|
|
59
66
|
const Base_js_1 = require("./Base.js");
|
|
67
|
+
const runMigration_js_1 = require("./helpers/kysely/runMigration.js");
|
|
68
|
+
const softDeleteDream_js_1 = require("../internal/softDeleteDream.js");
|
|
60
69
|
class KyselyQueryDriver extends Base_js_1.default {
|
|
61
70
|
// ATTENTION FRED
|
|
62
71
|
// stop trying to make this async. You never learn...
|
|
@@ -67,48 +76,101 @@ class KyselyQueryDriver extends Base_js_1.default {
|
|
|
67
76
|
// you are attempting to support. By default, dream is postgres-only, so our internal _db
|
|
68
77
|
// function will return a kysely instance, tightly coupled to a postgres-specific connection.
|
|
69
78
|
dbFor(sqlCommandType) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
79
|
+
const constructor = this.constructor;
|
|
80
|
+
return constructor.dbFor(this.dreamInstance.connectionName, this.dbConnectionType(sqlCommandType), this.dreamTransaction);
|
|
81
|
+
}
|
|
82
|
+
// if you are attempting to leverage a kysely-based connection using a different driver,
|
|
83
|
+
// you would want to extend this KyselyQueryDriver class, and then change this method
|
|
84
|
+
// to return a custom kysely instance, tailored to the specific database connection
|
|
85
|
+
// you are attempting to support. By default, dream is postgres-only, so our internal _db
|
|
86
|
+
// function will return a kysely instance, tightly coupled to a postgres-specific connection.
|
|
87
|
+
static dbFor(connectionName, dbConnectionType, dreamTransaction) {
|
|
88
|
+
if (dreamTransaction?.kyselyTransaction)
|
|
89
|
+
return dreamTransaction?.kyselyTransaction;
|
|
90
|
+
return (0, index_js_2.default)(connectionName || 'default', dbConnectionType, this.dialectProvider(connectionName, dbConnectionType));
|
|
91
|
+
}
|
|
92
|
+
static dialectProvider(connectionName, dbConnectionType) {
|
|
93
|
+
return (connectionConf) => new kysely_1.PostgresDialect({
|
|
94
|
+
pool: new pg_1.default.Pool({
|
|
95
|
+
user: connectionConf.user || '',
|
|
96
|
+
password: connectionConf.password || '',
|
|
97
|
+
database: index_js_3.default.getOrFail().dbName(connectionName, dbConnectionType),
|
|
98
|
+
host: connectionConf.host || 'localhost',
|
|
99
|
+
port: connectionConf.port || 5432,
|
|
100
|
+
ssl: connectionConf.useSsl ? defaultPostgresSslConfig(connectionConf) : false,
|
|
101
|
+
}),
|
|
102
|
+
});
|
|
73
103
|
}
|
|
74
104
|
/**
|
|
75
105
|
* migrate the database. Must respond to the NODE_ENV value.
|
|
76
106
|
*/
|
|
77
|
-
static async migrate() {
|
|
107
|
+
static async migrate(connectionName) {
|
|
78
108
|
const dreamApp = index_js_3.default.getOrFail();
|
|
79
|
-
const primaryDbConf = dreamApp.dbConnectionConfig('primary');
|
|
109
|
+
const primaryDbConf = dreamApp.dbConnectionConfig(connectionName, 'primary');
|
|
80
110
|
index_js_1.default.logger.logStartProgress(`migrating ${primaryDbConf.name}...`);
|
|
81
|
-
await (0, runMigration_js_1.default)({
|
|
111
|
+
await (0, runMigration_js_1.default)({
|
|
112
|
+
connectionName,
|
|
113
|
+
mode: 'migrate',
|
|
114
|
+
dialectProvider: this.dialectProvider(connectionName, 'primary'),
|
|
115
|
+
});
|
|
82
116
|
index_js_1.default.logger.logEndProgress();
|
|
83
|
-
await this.duplicateDatabase();
|
|
117
|
+
await this.duplicateDatabase(connectionName);
|
|
84
118
|
}
|
|
85
119
|
/**
|
|
86
120
|
* rollback the database. Must respond to the NODE_ENV value.
|
|
87
121
|
*/
|
|
88
122
|
static async rollback(opts) {
|
|
89
123
|
const dreamApp = index_js_3.default.getOrFail();
|
|
90
|
-
const primaryDbConf = dreamApp.dbConnectionConfig('primary');
|
|
124
|
+
const primaryDbConf = dreamApp.dbConnectionConfig(opts.connectionName || 'default', 'primary');
|
|
91
125
|
index_js_1.default.logger.logStartProgress(`rolling back ${primaryDbConf.name}...`);
|
|
92
126
|
let step = opts.steps;
|
|
93
127
|
while (step > 0) {
|
|
94
|
-
await (0, runMigration_js_1.default)({
|
|
128
|
+
await (0, runMigration_js_1.default)({
|
|
129
|
+
connectionName: opts.connectionName,
|
|
130
|
+
mode: 'rollback',
|
|
131
|
+
dialectProvider: this.dialectProvider(opts.connectionName, 'primary'),
|
|
132
|
+
});
|
|
95
133
|
step -= 1;
|
|
96
134
|
}
|
|
97
135
|
index_js_1.default.logger.logEndProgress();
|
|
98
|
-
await this.duplicateDatabase();
|
|
136
|
+
await this.duplicateDatabase(opts.connectionName);
|
|
99
137
|
}
|
|
100
|
-
|
|
101
|
-
|
|
138
|
+
/**
|
|
139
|
+
* This should build a new migration file in the migrations folder
|
|
140
|
+
* of your application. This will then need to be read and run
|
|
141
|
+
* whenever the `migrate` method is called. The filename should
|
|
142
|
+
* contain a timestamp at the front of the filename, so that it
|
|
143
|
+
* is sorted by date in the file tree, and, more importantly, so
|
|
144
|
+
* they can be run in order by your migration runner.
|
|
145
|
+
*/
|
|
146
|
+
static async generateMigration(connectionName, migrationName, columnsWithTypes) {
|
|
147
|
+
await (0, generateMigration_js_1.default)({ migrationName, columnsWithTypes, connectionName });
|
|
102
148
|
}
|
|
103
|
-
|
|
149
|
+
/**
|
|
150
|
+
* defines the syncing behavior for dream and psychic,
|
|
151
|
+
* which is run whenever the `sync` command is called.
|
|
152
|
+
* This is an important step, and will be incredibly
|
|
153
|
+
* comlpex to override. You will need to do the following
|
|
154
|
+
* when overriding this method:
|
|
155
|
+
*
|
|
156
|
+
* 1. introspect the db and use it to generate a db.ts file in the
|
|
157
|
+
* same shape as the existing one. Currently, the process for generating
|
|
158
|
+
* this file is extremely complex and messy, and will be difficult
|
|
159
|
+
* to achieve.
|
|
160
|
+
* 2. generate a types/dream.ts file in the same shape as the existing
|
|
161
|
+
* one. This is normally done using `await new SchemaBuilder().build()`,
|
|
162
|
+
* but this will likely need to be overridden to tailor to your custom
|
|
163
|
+
* database engine.
|
|
164
|
+
*/
|
|
165
|
+
static async sync(connectionName, onSync, options = {}) {
|
|
104
166
|
try {
|
|
105
167
|
if (!options?.schemaOnly) {
|
|
106
|
-
await index_js_1.default.logger.logProgress(
|
|
107
|
-
await (0,
|
|
168
|
+
await index_js_1.default.logger.logProgress(`introspecting db for connection: ${connectionName}...`, async () => {
|
|
169
|
+
await (0, syncDbTypesFiles_js_1.default)(connectionName);
|
|
108
170
|
});
|
|
109
171
|
}
|
|
110
|
-
const schemaBuilder = new SchemaBuilder_js_1.default();
|
|
111
|
-
await index_js_1.default.logger.logProgress(
|
|
172
|
+
const schemaBuilder = new SchemaBuilder_js_1.default(connectionName);
|
|
173
|
+
await index_js_1.default.logger.logProgress(`building dream schema for connection ${connectionName}...`, async () => {
|
|
112
174
|
await schemaBuilder.build();
|
|
113
175
|
});
|
|
114
176
|
if (schemaBuilder.hasForeignKeyError && !options?.schemaOnly) {
|
|
@@ -125,6 +187,7 @@ class KyselyQueryDriver extends Base_js_1.default {
|
|
|
125
187
|
});
|
|
126
188
|
});
|
|
127
189
|
}
|
|
190
|
+
await SchemaBuilder_js_1.default.buildGlobalTypes();
|
|
128
191
|
if (!options?.schemaOnly) {
|
|
129
192
|
// intentionally leaving logs off here, since it allows other
|
|
130
193
|
// onSync handlers to determine their own independent logging approach
|
|
@@ -138,41 +201,68 @@ class KyselyQueryDriver extends Base_js_1.default {
|
|
|
138
201
|
});
|
|
139
202
|
}
|
|
140
203
|
}
|
|
204
|
+
static get syncDialect() {
|
|
205
|
+
return 'postgres';
|
|
206
|
+
}
|
|
141
207
|
/**
|
|
142
|
-
*
|
|
208
|
+
* @internal
|
|
209
|
+
*
|
|
210
|
+
* returns the foreign key type based on the primary key received.
|
|
211
|
+
* gives the driver the opportunity to switch i.e. bigserial to bigint.
|
|
143
212
|
*/
|
|
144
|
-
static
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
// and we don't need it at the moment, so kicking off for future development when we have more time
|
|
152
|
-
// to flesh this out.
|
|
153
|
-
// if (connectionRetriever.hasReplicaConfig()) {
|
|
154
|
-
// const replicaDbConf = connectionRetriever.getConnectionConf('replica')
|
|
155
|
-
// console.log(`creating ${process.env[replicaDbConf.name]}`)
|
|
156
|
-
// await createDb('replica')
|
|
157
|
-
// }
|
|
213
|
+
static foreignKeyTypeFromPrimaryKey(primaryKey) {
|
|
214
|
+
switch (primaryKey) {
|
|
215
|
+
case 'bigserial':
|
|
216
|
+
return 'bigint';
|
|
217
|
+
default:
|
|
218
|
+
return primaryKey;
|
|
219
|
+
}
|
|
158
220
|
}
|
|
159
221
|
/**
|
|
160
|
-
*
|
|
222
|
+
* @internal
|
|
223
|
+
*
|
|
224
|
+
* used to return the computed primary key type based
|
|
225
|
+
* on the primaryKeyType set in the DreamApp class.
|
|
161
226
|
*/
|
|
162
|
-
static
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
227
|
+
static primaryKeyType() {
|
|
228
|
+
const dreamconf = index_js_3.default.getOrFail();
|
|
229
|
+
switch (dreamconf.primaryKeyType) {
|
|
230
|
+
case 'bigint':
|
|
231
|
+
case 'bigserial':
|
|
232
|
+
case 'uuid':
|
|
233
|
+
case 'integer':
|
|
234
|
+
return dreamconf.primaryKeyType;
|
|
235
|
+
default: {
|
|
236
|
+
// protection so that if a new EncryptAlgorithm is ever added, this will throw a type error at build time
|
|
237
|
+
const _never = dreamconf.primaryKeyType;
|
|
238
|
+
throw new Error(`
|
|
239
|
+
ATTENTION!
|
|
240
|
+
|
|
241
|
+
unrecognized primary key type "${_never}" found in .dream.yml.
|
|
242
|
+
please use one of the allowed primary key types:
|
|
243
|
+
${constants_js_1.primaryKeyTypes.join(', ')}
|
|
244
|
+
`);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* destroys a dream, possibly implementing soft delete if reallyDestroy is false
|
|
250
|
+
* and the record being deleted implements soft delete.
|
|
251
|
+
*
|
|
252
|
+
* @param dream - the dream instance you wish to destroy
|
|
253
|
+
* @param txn - a transaction to encapsulate, consistently provided by underlying dream mechanisms
|
|
254
|
+
* @param reallyDestroy - whether or not to reallyDestroy. If false, soft delete will be attempted when relevant
|
|
255
|
+
*/
|
|
256
|
+
static async destroyDream(dream, txn, reallyDestroy) {
|
|
257
|
+
if (shouldSoftDelete(dream, reallyDestroy)) {
|
|
258
|
+
await (0, softDeleteDream_js_1.default)(dream, txn);
|
|
259
|
+
}
|
|
260
|
+
else if (!dream['_preventDeletion']) {
|
|
261
|
+
await txn.kyselyTransaction
|
|
262
|
+
.deleteFrom(dream.table)
|
|
263
|
+
.where(dream['_primaryKey'], '=', dream.primaryKeyValue())
|
|
264
|
+
.execute();
|
|
265
|
+
}
|
|
176
266
|
}
|
|
177
267
|
/**
|
|
178
268
|
* Converts the given dream class into a Kysely query, enabling
|
|
@@ -202,6 +292,41 @@ class KyselyQueryDriver extends Base_js_1.default {
|
|
|
202
292
|
}
|
|
203
293
|
}
|
|
204
294
|
}
|
|
295
|
+
/**
|
|
296
|
+
* Builds a new DreamTransaction instance, provides
|
|
297
|
+
* the instance to the provided callback.
|
|
298
|
+
*
|
|
299
|
+
* ```ts
|
|
300
|
+
* await ApplicationModel.transaction(async txn => {
|
|
301
|
+
* const user = await User.txn(txn).create({ email: 'how@yadoin' })
|
|
302
|
+
* await Pet.txn(txn).create({ user })
|
|
303
|
+
* })
|
|
304
|
+
* ```
|
|
305
|
+
*
|
|
306
|
+
* @param callback - A callback function to call. The transaction provided to the callback can be passed to subsequent database calls within the transaction callback
|
|
307
|
+
* @returns void
|
|
308
|
+
*/
|
|
309
|
+
static async transaction(dreamInstance, callback) {
|
|
310
|
+
const dreamTransaction = new DreamTransaction_js_1.default();
|
|
311
|
+
let callbackResponse = undefined;
|
|
312
|
+
await this.dbFor(dreamInstance.connectionName || 'default', 'primary')
|
|
313
|
+
.transaction()
|
|
314
|
+
.execute(async (kyselyTransaction) => {
|
|
315
|
+
dreamTransaction.kyselyTransaction = kyselyTransaction;
|
|
316
|
+
callbackResponse = (await callback(dreamTransaction));
|
|
317
|
+
});
|
|
318
|
+
await dreamTransaction.runAfterCommitHooks(dreamTransaction);
|
|
319
|
+
return callbackResponse;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* @internal
|
|
323
|
+
*
|
|
324
|
+
* this is used by getColumnData to serialize enums
|
|
325
|
+
*/
|
|
326
|
+
static enumType(row) {
|
|
327
|
+
const enumName = (0, pascalize_js_1.default)(row.udtName.replace(/\[\]$/, ''));
|
|
328
|
+
return enumName;
|
|
329
|
+
}
|
|
205
330
|
/**
|
|
206
331
|
* @internal
|
|
207
332
|
*
|
|
@@ -246,7 +371,7 @@ class KyselyQueryDriver extends Base_js_1.default {
|
|
|
246
371
|
const driverClass = this.constructor;
|
|
247
372
|
return (await new driverClass(query)['executeJoinLoad']())[0] || null;
|
|
248
373
|
}
|
|
249
|
-
const kyselyQuery = new
|
|
374
|
+
const kyselyQuery = new this.constructor(this.query.limit(1)).buildSelect();
|
|
250
375
|
const results = await (0, executeDatabaseQuery_js_1.default)(kyselyQuery, 'executeTakeFirst');
|
|
251
376
|
if (results) {
|
|
252
377
|
const theFirst = this.dbResultToDreamInstance(results, this.dreamClass);
|
|
@@ -295,7 +420,7 @@ class KyselyQueryDriver extends Base_js_1.default {
|
|
|
295
420
|
async max(columnName) {
|
|
296
421
|
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
297
422
|
const { max } = this.dbFor('select').fn;
|
|
298
|
-
let kyselyQuery = new
|
|
423
|
+
let kyselyQuery = new this.constructor(this.query).buildSelect({
|
|
299
424
|
bypassSelectAll: true,
|
|
300
425
|
bypassOrder: true,
|
|
301
426
|
});
|
|
@@ -318,7 +443,7 @@ class KyselyQueryDriver extends Base_js_1.default {
|
|
|
318
443
|
async min(columnName) {
|
|
319
444
|
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
320
445
|
const { min } = this.dbFor('select').fn;
|
|
321
|
-
let kyselyQuery = new
|
|
446
|
+
let kyselyQuery = new this.constructor(this.query).buildSelect({
|
|
322
447
|
bypassSelectAll: true,
|
|
323
448
|
bypassOrder: true,
|
|
324
449
|
});
|
|
@@ -340,7 +465,10 @@ class KyselyQueryDriver extends Base_js_1.default {
|
|
|
340
465
|
const { count } = this.dbFor('select').fn;
|
|
341
466
|
const distinctColumn = this.query['distinctColumn'];
|
|
342
467
|
const query = this.query.clone({ distinctColumn: null });
|
|
343
|
-
let kyselyQuery = new
|
|
468
|
+
let kyselyQuery = new this.constructor(query).buildSelect({
|
|
469
|
+
bypassSelectAll: true,
|
|
470
|
+
bypassOrder: true,
|
|
471
|
+
});
|
|
344
472
|
const countClause = distinctColumn
|
|
345
473
|
? count((0, kysely_1.sql) `DISTINCT ${distinctColumn}`)
|
|
346
474
|
: count(query['namespaceColumn'](query.dreamInstance['_primaryKey']));
|
|
@@ -356,7 +484,7 @@ class KyselyQueryDriver extends Base_js_1.default {
|
|
|
356
484
|
* @returns An array of plucked values
|
|
357
485
|
*/
|
|
358
486
|
async pluck(...fields) {
|
|
359
|
-
let kyselyQuery = new
|
|
487
|
+
let kyselyQuery = new this.constructor(this.query['removeAllDefaultScopesExceptOnAssociations']()).buildSelect({
|
|
360
488
|
bypassSelectAll: true,
|
|
361
489
|
});
|
|
362
490
|
const aliases = [];
|
|
@@ -418,7 +546,8 @@ class KyselyQueryDriver extends Base_js_1.default {
|
|
|
418
546
|
* subsequent model hooks that are fired.
|
|
419
547
|
*/
|
|
420
548
|
static async saveDream(dream, txn = null) {
|
|
421
|
-
const
|
|
549
|
+
const connectionName = dream.connectionName || 'default';
|
|
550
|
+
const db = txn?.kyselyTransaction ?? this.dbFor(connectionName, 'primary');
|
|
422
551
|
const sqlifiedAttributes = (0, sqlAttributes_js_1.default)(dream);
|
|
423
552
|
try {
|
|
424
553
|
if (dream.isPersisted) {
|
|
@@ -509,7 +638,7 @@ class KyselyQueryDriver extends Base_js_1.default {
|
|
|
509
638
|
buildDelete() {
|
|
510
639
|
const kyselyQuery = this.dbFor('delete').deleteFrom(this.query['baseSqlAlias']);
|
|
511
640
|
const results = this.attachLimitAndOrderStatementsToNonSelectQuery(kyselyQuery);
|
|
512
|
-
return new
|
|
641
|
+
return new this.constructor(results.clone).buildCommon(results.kyselyQuery);
|
|
513
642
|
}
|
|
514
643
|
buildSelect({ bypassSelectAll = false, bypassOrder = false, columns, } = {}) {
|
|
515
644
|
let kyselyQuery;
|
|
@@ -518,7 +647,9 @@ class KyselyQueryDriver extends Base_js_1.default {
|
|
|
518
647
|
const query = connectionOverride
|
|
519
648
|
? this.query['baseSelectQuery'].connection(connectionOverride)
|
|
520
649
|
: this.query['baseSelectQuery'];
|
|
521
|
-
kyselyQuery = new
|
|
650
|
+
kyselyQuery = new this.constructor(query).buildSelect({
|
|
651
|
+
bypassSelectAll: true,
|
|
652
|
+
});
|
|
522
653
|
}
|
|
523
654
|
else {
|
|
524
655
|
const from = this.query['baseSqlAlias'] === this.query['tableName']
|
|
@@ -535,7 +666,7 @@ class KyselyQueryDriver extends Base_js_1.default {
|
|
|
535
666
|
});
|
|
536
667
|
if (this.query['orderStatements'].length && !bypassOrder) {
|
|
537
668
|
this.query['orderStatements'].forEach(orderStatement => {
|
|
538
|
-
kyselyQuery = kyselyQuery.orderBy(this.namespaceColumn(orderStatement.column),
|
|
669
|
+
kyselyQuery = kyselyQuery.orderBy(this.namespaceColumn(orderStatement.column), this.orderByDirection(orderStatement.direction));
|
|
539
670
|
});
|
|
540
671
|
}
|
|
541
672
|
if (this.query['limitStatement'])
|
|
@@ -555,13 +686,16 @@ class KyselyQueryDriver extends Base_js_1.default {
|
|
|
555
686
|
kyselyQuery = kyselyQuery.clearOrderBy();
|
|
556
687
|
return kyselyQuery;
|
|
557
688
|
}
|
|
689
|
+
orderByDirection(direction) {
|
|
690
|
+
return (0, orderByDirection_js_1.default)(direction);
|
|
691
|
+
}
|
|
558
692
|
buildUpdate(attributes) {
|
|
559
693
|
let kyselyQuery = this.dbFor('update')
|
|
560
694
|
.updateTable(this.query['tableName'])
|
|
561
695
|
.set(attributes);
|
|
562
696
|
kyselyQuery = this.conditionallyAttachSimilarityColumnsToUpdate(kyselyQuery);
|
|
563
697
|
const results = this.attachLimitAndOrderStatementsToNonSelectQuery(kyselyQuery);
|
|
564
|
-
return new
|
|
698
|
+
return new this.constructor(results.clone).buildCommon(results.kyselyQuery);
|
|
565
699
|
}
|
|
566
700
|
/**
|
|
567
701
|
* @internal
|
|
@@ -612,29 +746,6 @@ class KyselyQueryDriver extends Base_js_1.default {
|
|
|
612
746
|
async hydratePreload(dream) {
|
|
613
747
|
await this.applyPreload(this.query['preloadStatements'], this.query['preloadOnStatements'], dream);
|
|
614
748
|
}
|
|
615
|
-
static async duplicateDatabase() {
|
|
616
|
-
const dreamApp = index_js_3.default.getOrFail();
|
|
617
|
-
const parallelTests = dreamApp.parallelTests;
|
|
618
|
-
if (!parallelTests)
|
|
619
|
-
return;
|
|
620
|
-
index_js_1.default.logger.logStartProgress(`duplicating db for parallel tests...`);
|
|
621
|
-
const dbConf = dreamApp.dbConnectionConfig('primary');
|
|
622
|
-
const client = await (0, loadPgClient_js_1.default)({ useSystemDb: true });
|
|
623
|
-
if (EnvInternal_js_1.default.boolean('DREAM_CORE_DEVELOPMENT')) {
|
|
624
|
-
const replicaTestWorkerDatabaseName = `replica_test_${dbConf.name}`;
|
|
625
|
-
index_js_1.default.logger.logContinueProgress(`creating fake replica test database ${replicaTestWorkerDatabaseName}...`, { logPrefix: ' ├ [db]', logPrefixColor: 'cyan' });
|
|
626
|
-
await client.query(`DROP DATABASE IF EXISTS ${replicaTestWorkerDatabaseName};`);
|
|
627
|
-
await client.query(`CREATE DATABASE ${replicaTestWorkerDatabaseName} TEMPLATE ${dbConf.name};`);
|
|
628
|
-
}
|
|
629
|
-
for (let i = 2; i <= parallelTests; i++) {
|
|
630
|
-
const workerDatabaseName = `${dbConf.name}_${i}`;
|
|
631
|
-
index_js_1.default.logger.logContinueProgress(`creating duplicate test database ${workerDatabaseName} for concurrent tests...`, { logPrefix: ' ├ [db]', logPrefixColor: 'cyan' });
|
|
632
|
-
await client.query(`DROP DATABASE IF EXISTS ${workerDatabaseName};`);
|
|
633
|
-
await client.query(`CREATE DATABASE ${workerDatabaseName} TEMPLATE ${dbConf.name};`);
|
|
634
|
-
}
|
|
635
|
-
await client.end();
|
|
636
|
-
index_js_1.default.logger.logEndProgress();
|
|
637
|
-
}
|
|
638
749
|
aliasWhereStatements(whereStatements, alias) {
|
|
639
750
|
return whereStatements.map(whereStatement => this.aliasWhereStatement(whereStatement, alias));
|
|
640
751
|
}
|
|
@@ -682,7 +793,9 @@ class KyselyQueryDriver extends Base_js_1.default {
|
|
|
682
793
|
*/
|
|
683
794
|
async executeJoinLoad(options = {}) {
|
|
684
795
|
const query = this.query['limit'](null).offset(null);
|
|
685
|
-
let kyselyQuery = new
|
|
796
|
+
let kyselyQuery = new this.constructor(query).buildSelect({
|
|
797
|
+
bypassSelectAll: true,
|
|
798
|
+
});
|
|
686
799
|
const aliasToDreamClassesMap = {
|
|
687
800
|
[this.query['baseSqlAlias']]: this.dreamClass,
|
|
688
801
|
...this.joinStatementsToDreamClassesMap(this.query['leftJoinStatements']),
|
|
@@ -1867,3 +1980,17 @@ const associationStringToAssociationAndMaybeAlias = function ({ dreamClass, asso
|
|
|
1867
1980
|
alias,
|
|
1868
1981
|
};
|
|
1869
1982
|
};
|
|
1983
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1984
|
+
function defaultPostgresSslConfig(connectionConf) {
|
|
1985
|
+
// TODO: properly configure (https://rvohealth.atlassian.net/browse/PDTC-2914)
|
|
1986
|
+
return {
|
|
1987
|
+
rejectUnauthorized: false,
|
|
1988
|
+
// ca: fs.readFileSync('/path/to/server-certificates/root.crt').toString(),
|
|
1989
|
+
// key: fs.readFileSync('/path/to/client-key/postgresql.key').toString(),
|
|
1990
|
+
// cert: fs.readFileSync('/path/to/client-certificates/postgresql.crt').toString(),
|
|
1991
|
+
};
|
|
1992
|
+
}
|
|
1993
|
+
function shouldSoftDelete(dream, reallyDestroy) {
|
|
1994
|
+
const dreamClass = dream.constructor;
|
|
1995
|
+
return dreamClass['softDelete'] && !reallyDestroy;
|
|
1996
|
+
}
|