@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,11 +1,11 @@
|
|
|
1
1
|
import pluralize from 'pluralize-esm';
|
|
2
|
+
import Query from '../../dream/Query.js';
|
|
2
3
|
import InvalidDecimalFieldPassedToGenerator from '../../errors/InvalidDecimalFieldPassedToGenerator.js';
|
|
3
4
|
import compact from '../compact.js';
|
|
4
|
-
import foreignKeyTypeFromPrimaryKey from '../db/foreignKeyTypeFromPrimaryKey.js';
|
|
5
5
|
import snakeify from '../snakeify.js';
|
|
6
6
|
const STI_TYPE_COLUMN_NAME = 'type';
|
|
7
7
|
const COLUMNS_TO_INDEX = [STI_TYPE_COLUMN_NAME];
|
|
8
|
-
export default function generateMigrationContent({ table, columnsWithTypes = [], primaryKeyType = 'bigserial', createOrAlter = 'create', stiChildClassName, } = {}) {
|
|
8
|
+
export default function generateMigrationContent({ connectionName = 'default', table, columnsWithTypes = [], primaryKeyType = 'bigserial', createOrAlter = 'create', stiChildClassName, } = {}) {
|
|
9
9
|
const altering = createOrAlter === 'alter';
|
|
10
10
|
let requireCitextExtension = false;
|
|
11
11
|
const checkConstraints = [];
|
|
@@ -37,7 +37,7 @@ export default function generateMigrationContent({ table, columnsWithTypes = [],
|
|
|
37
37
|
}
|
|
38
38
|
switch (attributeType) {
|
|
39
39
|
case 'belongs_to':
|
|
40
|
-
columnDefs.push(generateBelongsToStr(attributeName, {
|
|
40
|
+
columnDefs.push(generateBelongsToStr(connectionName, attributeName, {
|
|
41
41
|
primaryKeyType,
|
|
42
42
|
optional,
|
|
43
43
|
}));
|
|
@@ -46,15 +46,27 @@ export default function generateMigrationContent({ table, columnsWithTypes = [],
|
|
|
46
46
|
case 'enum':
|
|
47
47
|
columnDefs.push(generateEnumStr(attributeName, { descriptors, optional }));
|
|
48
48
|
break;
|
|
49
|
+
case 'enum[]':
|
|
50
|
+
columnDefs.push(generateEnumStr(attributeName, { descriptors, optional, asArray: true }));
|
|
51
|
+
break;
|
|
49
52
|
case 'decimal':
|
|
50
53
|
columnDefs.push(generateDecimalStr(attributeName, { descriptors, optional }));
|
|
51
54
|
break;
|
|
55
|
+
case 'decimal[]':
|
|
56
|
+
columnDefs.push(generateDecimalStr(attributeName, { descriptors, optional, asArray: true }));
|
|
57
|
+
break;
|
|
58
|
+
// array case for booleans can be handled with the default block.
|
|
59
|
+
// the only thing that is customized for a boolean field is the default
|
|
60
|
+
// value, which doesn't need to be special for boolean[]
|
|
52
61
|
case 'boolean':
|
|
53
62
|
columnDefs.push(generateBooleanStr(attributeName, { optional }));
|
|
54
63
|
break;
|
|
55
64
|
case 'encrypted':
|
|
56
65
|
columnDefs.push(generateColumnStr(`encrypted_${attributeName}`, 'text', descriptors, { optional }));
|
|
57
66
|
break;
|
|
67
|
+
// TODO: determine if we need to support encrypted[] in the future
|
|
68
|
+
case 'encrypted[]':
|
|
69
|
+
throw new Error('the "encrypted[]" column type is not supported');
|
|
58
70
|
default:
|
|
59
71
|
if (sqlAttributeType !== undefined) {
|
|
60
72
|
columnDefs.push(generateColumnStr(attributeName, sqlAttributeType, descriptors, { optional }));
|
|
@@ -126,19 +138,25 @@ function getAttributeType(attributeType, descriptors) {
|
|
|
126
138
|
switch (attributeType) {
|
|
127
139
|
case 'string':
|
|
128
140
|
return `varchar(${descriptors[0] || 255})`;
|
|
141
|
+
case 'string[]':
|
|
142
|
+
return `varchar(${descriptors[0] || 255})[]`;
|
|
129
143
|
case 'enum':
|
|
130
144
|
return enumAttributeType(descriptors);
|
|
145
|
+
case 'enum[]':
|
|
146
|
+
return enumAttributeType(descriptors, true);
|
|
131
147
|
case 'datetime':
|
|
132
148
|
return 'timestamp';
|
|
133
149
|
default:
|
|
134
150
|
return attributeType;
|
|
135
151
|
}
|
|
136
152
|
}
|
|
137
|
-
function enumAttributeType(descriptors) {
|
|
138
|
-
|
|
153
|
+
function enumAttributeType(descriptors, asArray = false) {
|
|
154
|
+
const suffix = asArray ? '[]' : '';
|
|
155
|
+
return `sql\`${descriptors[0]}_enum${suffix}\``;
|
|
139
156
|
}
|
|
157
|
+
const ENUM_OR_ENUM_ARRAY_REGEX = /:enum:.*:|:enum\[\]:.*:/;
|
|
140
158
|
function generateEnumStatements(columnsWithTypes) {
|
|
141
|
-
const enumStatements = columnsWithTypes.filter(attribute =>
|
|
159
|
+
const enumStatements = columnsWithTypes.filter(attribute => ENUM_OR_ENUM_ARRAY_REGEX.test(attribute));
|
|
142
160
|
const finalStatements = compact(enumStatements.map(statement => {
|
|
143
161
|
const [, , enumName, ...descriptors] = statement.split(':');
|
|
144
162
|
optionalFromDescriptors(descriptors);
|
|
@@ -156,7 +174,7 @@ function generateEnumStatements(columnsWithTypes) {
|
|
|
156
174
|
return finalStatements.length ? finalStatements.join('\n\n') + '\n\n' : '';
|
|
157
175
|
}
|
|
158
176
|
function generateEnumDropStatements(columnsWithTypes) {
|
|
159
|
-
const enumStatements = columnsWithTypes.filter(attribute =>
|
|
177
|
+
const enumStatements = columnsWithTypes.filter(attribute => ENUM_OR_ENUM_ARRAY_REGEX.test(attribute));
|
|
160
178
|
const finalStatements = compact(enumStatements.map(statement => {
|
|
161
179
|
const [, , enumName, ...descriptors] = statement.split(':');
|
|
162
180
|
optionalFromDescriptors(descriptors);
|
|
@@ -170,17 +188,22 @@ function generateEnumDropStatements(columnsWithTypes) {
|
|
|
170
188
|
function generateBooleanStr(attributeName, { optional }) {
|
|
171
189
|
return `.addColumn('${attributeName}', 'boolean'${optional ? '' : ', col => col.notNull().defaultTo(false)'})`;
|
|
172
190
|
}
|
|
173
|
-
function generateEnumStr(attributeName, { descriptors, optional }) {
|
|
174
|
-
const computedAttributeType = enumAttributeType(descriptors);
|
|
191
|
+
function generateEnumStr(attributeName, { descriptors, optional, asArray = false }) {
|
|
192
|
+
const computedAttributeType = enumAttributeType(descriptors, asArray);
|
|
175
193
|
if (attributeName === undefined)
|
|
176
194
|
return '';
|
|
177
|
-
|
|
195
|
+
const columnModifiers = asArray ? "col.notNull().defaultTo('{}')" : 'col.notNull()';
|
|
196
|
+
return `.addColumn('${attributeName}', ${computedAttributeType}${optional ? '' : `, col => ${columnModifiers}`})`;
|
|
178
197
|
}
|
|
179
|
-
function generateDecimalStr(attributeName, { descriptors, optional }) {
|
|
198
|
+
function generateDecimalStr(attributeName, { descriptors, optional, asArray = false }) {
|
|
180
199
|
const [scale, precision] = descriptors[0]?.split(',') || [null, null];
|
|
181
200
|
if (!scale || !precision)
|
|
182
201
|
throw new InvalidDecimalFieldPassedToGenerator(attributeName);
|
|
183
|
-
|
|
202
|
+
const columnModifiers = asArray ? "col.notNull().defaultTo('{}')" : 'col.notNull()';
|
|
203
|
+
const decimalStatement = asArray
|
|
204
|
+
? `sql\`decimal(${scale}, ${precision})[]\``
|
|
205
|
+
: `'decimal(${scale}, ${precision})'`;
|
|
206
|
+
return `.addColumn('${attributeName}', ${decimalStatement}${optional ? '' : `, col => ${columnModifiers}`})`;
|
|
184
207
|
}
|
|
185
208
|
function generateColumnStr(attributeName, attributeType, descriptors, { optional }) {
|
|
186
209
|
let returnStr = `.addColumn('${attributeName}', ${attributeTypeString(attributeType)}`;
|
|
@@ -188,12 +211,15 @@ function generateColumnStr(attributeName, attributeType, descriptors, { optional
|
|
|
188
211
|
const providedDefault = providedDefaultArg?.replace(/^default\(/, '')?.replace(/\)$/, '');
|
|
189
212
|
const notNull = !optional;
|
|
190
213
|
const hasExtraValues = providedDefault || notNull;
|
|
214
|
+
const isArray = /\[\]$/.test(attributeType);
|
|
191
215
|
if (hasExtraValues)
|
|
192
216
|
returnStr += ', col => col';
|
|
193
217
|
if (notNull)
|
|
194
218
|
returnStr += '.notNull()';
|
|
195
219
|
if (providedDefault)
|
|
196
220
|
returnStr += `.defaultTo('${providedDefault}')`;
|
|
221
|
+
else if (isArray)
|
|
222
|
+
returnStr += `.defaultTo('{}')`;
|
|
197
223
|
returnStr = `${returnStr})`;
|
|
198
224
|
if (attributeName === STI_TYPE_COLUMN_NAME)
|
|
199
225
|
returnStr = `// CONSIDER: when using type for STI, always use an enum
|
|
@@ -206,6 +232,7 @@ function attributeTypeString(attributeType) {
|
|
|
206
232
|
const attributeTypesRequiringSql = ['citext'];
|
|
207
233
|
if (attributeTypesRequiringSql.includes(attributeType))
|
|
208
234
|
return `sql\`${attributeType}\``;
|
|
235
|
+
const isArray = /\[\]$/.test(attributeType);
|
|
209
236
|
switch (attributeType) {
|
|
210
237
|
case 'varbit':
|
|
211
238
|
case 'bitvarying':
|
|
@@ -213,11 +240,17 @@ function attributeTypeString(attributeType) {
|
|
|
213
240
|
case 'txid_snapshot':
|
|
214
241
|
return "'txid_snapshot'";
|
|
215
242
|
default:
|
|
216
|
-
|
|
243
|
+
if (isArray) {
|
|
244
|
+
return `sql\`${attributeType.replace(/_/g, ' ')}\``;
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
return `'${attributeType.replace(/_/g, ' ')}'`;
|
|
248
|
+
}
|
|
217
249
|
}
|
|
218
250
|
}
|
|
219
|
-
function generateBelongsToStr(associationName, { primaryKeyType, optional = false, }) {
|
|
220
|
-
const
|
|
251
|
+
function generateBelongsToStr(connectionName, associationName, { primaryKeyType, optional = false, }) {
|
|
252
|
+
const dbDriverClass = Query.dbDriverClass(connectionName);
|
|
253
|
+
const dataType = dbDriverClass.foreignKeyTypeFromPrimaryKey(primaryKeyType);
|
|
221
254
|
const references = pluralize(associationName.replace(/\//g, '_').replace(/_id$/, ''));
|
|
222
255
|
return `.addColumn('${associationNameToForeignKey(associationName)}', '${dataType}', col => col.references('${references}.id').onDelete('restrict')${optional ? '' : '.notNull()'})`;
|
|
223
256
|
}
|
|
@@ -1,23 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
switch (dreamconf.primaryKeyType) {
|
|
6
|
-
case 'bigint':
|
|
7
|
-
case 'bigserial':
|
|
8
|
-
case 'uuid':
|
|
9
|
-
case 'integer':
|
|
10
|
-
return dreamconf.primaryKeyType;
|
|
11
|
-
default: {
|
|
12
|
-
// protection so that if a new EncryptAlgorithm is ever added, this will throw a type error at build time
|
|
13
|
-
const _never = dreamconf.primaryKeyType;
|
|
14
|
-
throw new Error(`
|
|
15
|
-
ATTENTION!
|
|
16
|
-
|
|
17
|
-
unrecognized primary key type "${_never}" found in .dream.yml.
|
|
18
|
-
please use one of the allowed primary key types:
|
|
19
|
-
${primaryKeyTypes.join(', ')}
|
|
20
|
-
`);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
1
|
+
import Query from '../../dream/Query.js';
|
|
2
|
+
export default function primaryKeyType(connectionName) {
|
|
3
|
+
const dbDriverClass = Query.dbDriverClass(connectionName);
|
|
4
|
+
return dbDriverClass.primaryKeyType();
|
|
23
5
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import DreamApp from '../dream-app/index.js';
|
|
1
2
|
import CalendarDate from './CalendarDate.js';
|
|
2
3
|
import { DateTime } from './DateTime.js';
|
|
3
4
|
import isDatetimeOrDatetimeArrayColumn from './db/types/isDatetimeOrDatetimeArrayColumn.js';
|
|
@@ -6,6 +7,7 @@ import normalizeUnicode from './normalizeUnicode.js';
|
|
|
6
7
|
export default function sqlAttributes(dream) {
|
|
7
8
|
const attributes = dream.dirtyAttributes();
|
|
8
9
|
const dreamClass = dream.constructor;
|
|
10
|
+
const queryDriverClass = DreamApp.getOrFail().dbConnectionQueryDriverClass(dream.connectionName);
|
|
9
11
|
return Object.keys(attributes).reduce((result, key) => {
|
|
10
12
|
let val = attributes[key];
|
|
11
13
|
if (val === undefined)
|
|
@@ -23,13 +25,14 @@ export default function sqlAttributes(dream) {
|
|
|
23
25
|
val = normalizeUnicode(val);
|
|
24
26
|
}
|
|
25
27
|
if (val instanceof DateTime || val instanceof CalendarDate) {
|
|
28
|
+
const dateOrDatetime = val instanceof DateTime ? 'datetime' : 'date';
|
|
26
29
|
// Converting toJSDate resulted in the correct timezone, but even with process.env.TZ=UTC,
|
|
27
30
|
// Kysely inserted into the database with the machine timezone, which can shift the date
|
|
28
31
|
// (e.g., toJSDate resulted in a JS Date that formats as "1987-04-07T00:00:00.000Z", but
|
|
29
32
|
// Kysely inserted "1907-04-06" into the database). By converting to an SQL string before
|
|
30
33
|
// handing off to Kysely, we bypass Javascript dates altogether, sending the string into the
|
|
31
34
|
// database for storage as a date or datetime.
|
|
32
|
-
result[key] =
|
|
35
|
+
result[key] = queryDriverClass.serializeDbType(dateOrDatetime, val);
|
|
33
36
|
}
|
|
34
37
|
else {
|
|
35
38
|
result[key] = val;
|
package/dist/esm/src/index.js
CHANGED
|
@@ -18,6 +18,9 @@ export { default as Dream } from './Dream.js';
|
|
|
18
18
|
export { DreamConst, openapiPrimitiveTypes, openapiShorthandPrimitiveTypes, } from './dream/constants.js';
|
|
19
19
|
export { default as DreamTransaction } from './dream/DreamTransaction.js';
|
|
20
20
|
export { default as Query } from './dream/Query.js';
|
|
21
|
+
export { default as QueryDriverBase } from './dream/QueryDriver/Base.js';
|
|
22
|
+
export { default as KyselyQueryDriver } from './dream/QueryDriver/Kysely.js';
|
|
23
|
+
export { default as PostgresQueryDriver } from './dream/QueryDriver/Postgres.js';
|
|
21
24
|
export { default as Encrypt } from './encrypt/index.js';
|
|
22
25
|
export { default as NonLoadedAssociation } from './errors/associations/NonLoadedAssociation.js';
|
|
23
26
|
export { default as CreateOrFindByFailedToCreateAndFind } from './errors/CreateOrFindByFailedToCreateAndFind.js';
|
|
@@ -8,7 +8,7 @@ import LoadBuilder from './dream/LoadBuilder.js';
|
|
|
8
8
|
import Query from './dream/Query.js';
|
|
9
9
|
import { PassthroughOnClause, WhereStatement } from './types/associations/shared.js';
|
|
10
10
|
import { AssociationTableNames, DbConnectionType } from './types/db.js';
|
|
11
|
-
import { AllDefaultScopeNames, AssociationNameToDream, AttributeKeys, CreateOrFindByExtraOpts, DefaultOrNamedScopeName, DreamAssociationNames, DreamAssociationNamesWithoutRequiredOnClauses, DreamAttributes, DreamColumnNames, DreamParamSafeColumnNames, DreamPrimaryKeyType, DreamSerializerKey, JoinAndStatements, NextPreloadArgumentType, OrderDir, PassthroughColumnNames, PluckEachArgs, PrimaryKeyForFind, TableColumnNames, UpdateableAssociationProperties, UpdateableProperties, UpdateablePropertiesForClass, UpdateOrCreateByExtraOpts } from './types/dream.js';
|
|
11
|
+
import { AllDefaultScopeNames, AssociationNameToDream, AttributeKeys, CreateOrFindByExtraOpts, DefaultOrNamedScopeName, DreamAssociationNames, DreamAssociationNamesWithoutRequiredOnClauses, DreamAttributes, DreamColumnNames, DreamParamSafeColumnNames, DreamPrimaryKeyType, DreamSerializerKey, GlobalModelNames, GlobalSerializerName, JoinAndStatements, NextPreloadArgumentType, OrderDir, PassthroughColumnNames, PluckEachArgs, PrimaryKeyForFind, TableColumnNames, UpdateableAssociationProperties, UpdateableProperties, UpdateablePropertiesForClass, UpdateOrCreateByExtraOpts } from './types/dream.js';
|
|
12
12
|
import { BaseModelColumnTypes, DefaultQueryTypeOptions, FindEachOpts, LoadForModifierFn, PaginatedDreamQueryOptions, PaginatedDreamQueryResult, QueryWithJoinedAssociationsType, QueryWithJoinedAssociationsTypeAndNoPreload } from './types/query.js';
|
|
13
13
|
import { DreamModelSerializerType, SimpleObjectSerializerType } from './types/serializer.js';
|
|
14
14
|
import { ValidationType } from './types/validation.js';
|
|
@@ -50,7 +50,9 @@ export default class Dream {
|
|
|
50
50
|
*/
|
|
51
51
|
private static globallyInitializingDecorators;
|
|
52
52
|
get schema(): any;
|
|
53
|
-
get
|
|
53
|
+
get connectionName(): any;
|
|
54
|
+
get connectionTypeConfig(): any;
|
|
55
|
+
get globalTypeConfig(): any;
|
|
54
56
|
/**
|
|
55
57
|
* Determines if the provided Dream class is the same as or a subclass of this Dream class.
|
|
56
58
|
* This method is particularly useful for runtime type checking and works with STI (Single Table Inheritance) classes.
|
|
@@ -564,7 +566,7 @@ export default class Dream {
|
|
|
564
566
|
*
|
|
565
567
|
* @returns the found model or serializer, depending on what you are looking for
|
|
566
568
|
*/
|
|
567
|
-
static lookup<T extends typeof Dream,
|
|
569
|
+
static lookup<T extends typeof Dream, LookupName extends GlobalSerializerName<InstanceType<T>> | GlobalModelNames<InstanceType<T>> = GlobalSerializerName<InstanceType<T>> | GlobalModelNames<InstanceType<T>>>(this: T, lookupName: LookupName): typeof Dream | DreamModelSerializerType | SimpleObjectSerializerType | undefined;
|
|
568
570
|
/**
|
|
569
571
|
* Prevents a specific default scope from applying when
|
|
570
572
|
* the Query is executed
|
|
@@ -11,10 +11,12 @@ export default class DreamBin {
|
|
|
11
11
|
static generateDream(fullyQualifiedModelName: string, columnsWithTypes: string[], options: {
|
|
12
12
|
serializer: boolean;
|
|
13
13
|
stiBaseSerializer: boolean;
|
|
14
|
+
connectionName: string;
|
|
14
15
|
}): Promise<void>;
|
|
15
16
|
static generateStiChild(fullyQualifiedModelName: string, fullyQualifiedParentName: string, columnsWithTypes: string[], options: {
|
|
16
17
|
serializer: boolean;
|
|
18
|
+
connectionName: string;
|
|
17
19
|
}): Promise<void>;
|
|
18
|
-
static generateMigration(migrationName: string, columnsWithTypes: string[]): Promise<void>;
|
|
20
|
+
static generateMigration(migrationName: string, columnsWithTypes: string[], connectionName: string): Promise<void>;
|
|
19
21
|
private static buildDocs;
|
|
20
22
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Kysely, Transaction as KyselyTransaction } from 'kysely';
|
|
2
1
|
import Dream from '../Dream.js';
|
|
3
2
|
import DreamTransaction from '../dream/DreamTransaction.js';
|
|
4
3
|
import { DbConnectionType } from '../types/db.js';
|
|
@@ -16,7 +15,6 @@ export default class ConnectedToDB<DreamInstance extends Dream> {
|
|
|
16
15
|
protected readonly innerJoinDreamClasses: readonly (typeof Dream)[];
|
|
17
16
|
constructor(dreamInstance: DreamInstance, opts?: ConnectedToDBOpts);
|
|
18
17
|
dbConnectionType(sqlCommandType: SqlCommandType): DbConnectionType;
|
|
19
|
-
dbFor(sqlCommandType: SqlCommandType): Kysely<DreamInstance['DB']> | KyselyTransaction<DreamInstance['DB']>;
|
|
20
18
|
private isReplicaSafe;
|
|
21
19
|
}
|
|
22
20
|
export interface ConnectedToDBOpts {
|
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import { Kysely } from 'kysely';
|
|
2
|
+
import { SingleDbCredential } from '../dream-app/index.js';
|
|
2
3
|
import { DbConnectionType } from '../types/db.js';
|
|
3
4
|
export default class DreamDbConnection {
|
|
4
|
-
static getConnection<DB>(connectionType: DbConnectionType): Kysely<DB>;
|
|
5
|
-
static dropAllConnections(): Promise<void>;
|
|
5
|
+
static getConnection<DB>(connectionName: string, connectionType: DbConnectionType, dialectProvider: DialectProviderCb): Kysely<DB>;
|
|
6
6
|
private static getConnectionTypeName;
|
|
7
7
|
}
|
|
8
8
|
export declare function dreamDbConnections(): {
|
|
9
|
-
[key: string]:
|
|
9
|
+
[key: string]: {
|
|
10
|
+
[key: string]: Kysely<any>;
|
|
11
|
+
};
|
|
10
12
|
};
|
|
11
13
|
export declare function closeAllDbConnections(): Promise<void>;
|
|
14
|
+
export declare function closeAllConnectionsForConnectionName(connectionName: string): Promise<PromiseSettledResult<void>[]>;
|
|
15
|
+
export type DialectProviderCb = (connectionConf: SingleDbCredential) => any;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function dbTypesFilenameForConnection(connectionName: string): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function syncDbTypesFiles(connectionName: string): Promise<void>;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Kysely } from 'kysely';
|
|
2
|
+
import { SingleDbCredential } from '../dream-app/index.js';
|
|
2
3
|
import Dream from '../Dream.js';
|
|
3
4
|
import { DbConnectionType } from '../types/db.js';
|
|
4
|
-
export default function db<T extends Dream, DB extends T['DB'] = T['DB']>(connectionType?: DbConnectionType): Kysely<DB>;
|
|
5
|
+
export default function db<T extends Dream, DB extends T['DB'] = T['DB']>(connectionName?: string, connectionType?: DbConnectionType, dialectProvider?: (connectionConf: SingleDbCredential) => any): Kysely<DB>;
|
|
@@ -2,6 +2,7 @@ import { SelectQueryBuilder, UpdateQueryBuilder } from 'kysely';
|
|
|
2
2
|
import Dream from '../../../../Dream.js';
|
|
3
3
|
import DreamTransaction from '../../../../dream/DreamTransaction.js';
|
|
4
4
|
import Query from '../../../../dream/Query.js';
|
|
5
|
+
import PostgresQueryDriver from '../../../../dream/QueryDriver/Postgres.js';
|
|
5
6
|
export default function setPosition({ position, previousPosition, dream, positionField, scope, query, txn, }: {
|
|
6
7
|
dream: Dream;
|
|
7
8
|
position: number;
|
|
@@ -12,3 +13,4 @@ export default function setPosition({ position, previousPosition, dream, positio
|
|
|
12
13
|
txn: DreamTransaction<any> | undefined;
|
|
13
14
|
}): Promise<void>;
|
|
14
15
|
export declare function applySortableScopesToQuery<QB extends UpdateQueryBuilder<any, string, string, any> | SelectQueryBuilder<any, any, any>>(dream: Dream, kyselyQuery: QB, whereValueCB: (column: string) => any, scope?: string | string[]): QB;
|
|
16
|
+
export declare function getPostgresQueryDriver(connectionName: string): typeof PostgresQueryDriver;
|
|
@@ -6,7 +6,7 @@ import { AllDefaultScopeNames, DefaultScopeName, DreamColumnNames, DreamConstruc
|
|
|
6
6
|
import { DefaultQueryTypeOptions, ExtendQueryType, LoadForModifierFn, NamespacedOrBaseModelColumnTypes, PaginatedDreamQueryOptions, PaginatedDreamQueryResult, QueryToKyselyDBType, QueryToKyselyTableNamesType } from '../types/query.js';
|
|
7
7
|
import { JoinedAssociation, JoinedAssociationsTypeFromAssociations, QueryTypeOptions, VariadicJoinsArgs, VariadicLeftJoinLoadArgs, VariadicLoadArgs } from '../types/variadic.js';
|
|
8
8
|
import DreamTransaction from './DreamTransaction.js';
|
|
9
|
-
import
|
|
9
|
+
import QueryDriverBase from './QueryDriver/Base.js';
|
|
10
10
|
export default class Query<DreamInstance extends Dream, QueryTypeOpts extends Readonly<QueryTypeOptions> = DefaultQueryTypeOptions<DreamInstance>> {
|
|
11
11
|
/**
|
|
12
12
|
* @internal
|
|
@@ -705,9 +705,7 @@ export default class Query<DreamInstance extends Dream, QueryTypeOpts extends Re
|
|
|
705
705
|
* @returns An object representing the underlying sql statement
|
|
706
706
|
*
|
|
707
707
|
*/
|
|
708
|
-
sql(): import("kysely").CompiledQuery<
|
|
709
|
-
[x: string]: any;
|
|
710
|
-
}>;
|
|
708
|
+
sql(): import("kysely").CompiledQuery<object>;
|
|
711
709
|
/**
|
|
712
710
|
* Converts the given dream class into a Kysely query, enabling
|
|
713
711
|
* you to build custom queries using the Kysely API
|
|
@@ -843,22 +841,9 @@ export default class Query<DreamInstance extends Dream, QueryTypeOpts extends Re
|
|
|
843
841
|
all(options?: {
|
|
844
842
|
columns?: DreamColumnNames<DreamInstance>[];
|
|
845
843
|
}): Promise<DreamInstance[]>;
|
|
846
|
-
static dbDriverClass<T extends Dream>():
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
rollback(opts: {
|
|
850
|
-
steps: number;
|
|
851
|
-
}): Promise<void>;
|
|
852
|
-
generateMigration(migrationName: string, columnsWithTypes: string[]): Promise<void>;
|
|
853
|
-
sync(onSync: () => Promise<void> | void, options?: {
|
|
854
|
-
schemaOnly?: boolean;
|
|
855
|
-
}): Promise<void>;
|
|
856
|
-
dbCreate(): Promise<void>;
|
|
857
|
-
dbDrop(): Promise<void>;
|
|
858
|
-
saveDream(dream: Dream, txn?: DreamTransaction<Dream> | null): Promise<any>;
|
|
859
|
-
duplicateDatabase(): Promise<void>;
|
|
860
|
-
};
|
|
861
|
-
dbDriverInstance(query?: Query<DreamInstance, any>): PostgresQueryDriver<DreamInstance>;
|
|
844
|
+
static dbDriverClass<T extends Dream>(connectionName: string): typeof QueryDriverBase<T>;
|
|
845
|
+
dbDriverInstance(query?: Query<DreamInstance, any>): QueryDriverBase<DreamInstance>;
|
|
846
|
+
get connectionName(): any;
|
|
862
847
|
/**
|
|
863
848
|
* Paginates the results of your query, accepting a pageSize and page argument,
|
|
864
849
|
* which it uses to segment your query into pages, leveraging limit and offset
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { DeleteQueryBuilder, SelectQueryBuilder, UpdateQueryBuilder } from 'kysely';
|
|
1
|
+
import { CompiledQuery, DeleteQueryBuilder, SelectQueryBuilder, UpdateQueryBuilder } from 'kysely';
|
|
2
2
|
import Dream from '../../Dream.js';
|
|
3
|
+
import { SchemaBuilderAssociationData, SchemaBuilderColumnData } from '../../helpers/cli/SchemaBuilder.js';
|
|
3
4
|
import { AssociationStatement } from '../../types/associations/shared.js';
|
|
4
|
-
import {
|
|
5
|
+
import { DbConnectionType } from '../../types/db.js';
|
|
6
|
+
import { DreamColumnNames, DreamConstructorType, DreamTableSchema, PrimaryKeyType } from '../../types/dream.js';
|
|
5
7
|
import { PreloadedDreamsAndWhatTheyPointTo, QueryToKyselyDBType, QueryToKyselyTableNamesType } from '../../types/query.js';
|
|
6
8
|
import DreamTransaction from '../DreamTransaction.js';
|
|
7
9
|
import Query from '../Query.js';
|
|
8
|
-
import PostgresQueryDriver from './Postgres.js';
|
|
9
|
-
import { DbConnectionType } from '../../types/db.js';
|
|
10
10
|
export default class QueryDriverBase<DreamInstance extends Dream> {
|
|
11
11
|
query: Query<DreamInstance, any>;
|
|
12
12
|
protected readonly dreamClass: DreamConstructorType<DreamInstance>;
|
|
@@ -23,21 +23,22 @@ export default class QueryDriverBase<DreamInstance extends Dream> {
|
|
|
23
23
|
/**
|
|
24
24
|
* migrate the database. Must respond to the NODE_ENV value.
|
|
25
25
|
*/
|
|
26
|
-
static migrate(): Promise<void>;
|
|
26
|
+
static migrate(connectionName: string): Promise<void>;
|
|
27
27
|
/**
|
|
28
28
|
* rollback the database. Must respond to the NODE_ENV value.
|
|
29
29
|
*/
|
|
30
30
|
static rollback(_: {
|
|
31
|
+
connectionName: string;
|
|
31
32
|
steps: number;
|
|
32
33
|
}): Promise<void>;
|
|
33
34
|
/**
|
|
34
35
|
* create the database. Must respond to the NODE_ENV value.
|
|
35
36
|
*/
|
|
36
|
-
static dbCreate(): Promise<void>;
|
|
37
|
+
static dbCreate(connectionName: string): Promise<void>;
|
|
37
38
|
/**
|
|
38
39
|
* delete the database. Must respond to the NODE_ENV value.
|
|
39
40
|
*/
|
|
40
|
-
static dbDrop(): Promise<void>;
|
|
41
|
+
static dbDrop(connectionName: string): Promise<void>;
|
|
41
42
|
/**
|
|
42
43
|
* This should build a new migration file in the migrations folder
|
|
43
44
|
* of your application. This will then need to be read and run
|
|
@@ -46,7 +47,7 @@ export default class QueryDriverBase<DreamInstance extends Dream> {
|
|
|
46
47
|
* is sorted by date in the file tree, and, more importantly, so
|
|
47
48
|
* they can be run in order by your migration runner.
|
|
48
49
|
*/
|
|
49
|
-
static generateMigration(migrationName: string, columnsWithTypes: string[]): Promise<void>;
|
|
50
|
+
static generateMigration(connectionName: string, migrationName: string, columnsWithTypes: string[]): Promise<void>;
|
|
50
51
|
/**
|
|
51
52
|
* defines the syncing behavior for dream and psychic,
|
|
52
53
|
* which is run whenever the `sync` command is called.
|
|
@@ -63,7 +64,9 @@ export default class QueryDriverBase<DreamInstance extends Dream> {
|
|
|
63
64
|
* but this will likely need to be overridden to tailor to your custom
|
|
64
65
|
* database engine.
|
|
65
66
|
*/
|
|
66
|
-
static sync(_: () => Promise<void> | void
|
|
67
|
+
static sync(connectionName: string, _: () => Promise<void> | void, options?: {
|
|
68
|
+
schemaOnly?: boolean;
|
|
69
|
+
}): Promise<void>;
|
|
67
70
|
/**
|
|
68
71
|
* Converts the given dream class into a Kysely query, enabling
|
|
69
72
|
* you to build custom queries using the Kysely API
|
|
@@ -76,6 +79,53 @@ export default class QueryDriverBase<DreamInstance extends Dream> {
|
|
|
76
79
|
* @returns A Kysely query. Depending on the type passed, it will return either a SelectQueryBuilder, DeleteQueryBuilder, or an UpdateQueryBuilder
|
|
77
80
|
*/
|
|
78
81
|
toKysely<QueryType extends 'select' | 'delete' | 'update', DbType = QueryToKyselyDBType<Query<DreamInstance>>, TableNames = QueryToKyselyTableNamesType<Query<DreamInstance>>, ToKyselyReturnType = QueryType extends 'select' ? SelectQueryBuilder<DbType, TableNames & keyof DbType, unknown> : QueryType extends 'delete' ? DeleteQueryBuilder<DbType, TableNames & keyof DbType, unknown> : QueryType extends 'update' ? UpdateQueryBuilder<DbType, TableNames & keyof DbType, TableNames & keyof DbType, unknown> : never>(type: QueryType): ToKyselyReturnType;
|
|
82
|
+
/**
|
|
83
|
+
* Builds a new DreamTransaction instance, provides
|
|
84
|
+
* the instance to the provided callback.
|
|
85
|
+
*
|
|
86
|
+
* ```ts
|
|
87
|
+
* await ApplicationModel.transaction(async txn => {
|
|
88
|
+
* const user = await User.txn(txn).create({ email: 'how@yadoin' })
|
|
89
|
+
* await Pet.txn(txn).create({ user })
|
|
90
|
+
* })
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
* @param callback - A callback function to call. The transaction provided to the callback can be passed to subsequent database calls within the transaction callback
|
|
94
|
+
* @returns void
|
|
95
|
+
*/
|
|
96
|
+
static transaction<DreamInstance extends Dream, CB extends (txn: DreamTransaction<DreamInstance>) => unknown, RetType extends ReturnType<CB>>(dreamInstance: DreamInstance, callback: CB): Promise<RetType>;
|
|
97
|
+
/**
|
|
98
|
+
* @internal
|
|
99
|
+
*
|
|
100
|
+
* returns the foreign key type based on the primary key received.
|
|
101
|
+
* gives the driver the opportunity to switch i.e. bigserial to bigint.
|
|
102
|
+
*/
|
|
103
|
+
static foreignKeyTypeFromPrimaryKey(primaryKey: PrimaryKeyType): PrimaryKeyType;
|
|
104
|
+
/**
|
|
105
|
+
* @internal
|
|
106
|
+
*
|
|
107
|
+
* used to return the computed primary key type based
|
|
108
|
+
* on the primaryKeyType set in the DreamApp class.
|
|
109
|
+
*/
|
|
110
|
+
static primaryKeyType(): void;
|
|
111
|
+
/**
|
|
112
|
+
* @internal
|
|
113
|
+
*
|
|
114
|
+
* this method is called when dream is initializing, and is used
|
|
115
|
+
* to configure the database to utilize custom type parsers for
|
|
116
|
+
* a variety of data types.
|
|
117
|
+
*
|
|
118
|
+
* @param connectionName - the name of the connection you are doing this for
|
|
119
|
+
* @returns void
|
|
120
|
+
*/
|
|
121
|
+
static setDatabaseTypeParsers(connectionName: string): Promise<void>;
|
|
122
|
+
static duplicateDatabase(connectionName: string): Promise<void>;
|
|
123
|
+
static getColumnData(connectionName: string, tableName: string, associationData: {
|
|
124
|
+
[key: string]: SchemaBuilderAssociationData;
|
|
125
|
+
}): Promise<{
|
|
126
|
+
[key: string]: SchemaBuilderColumnData;
|
|
127
|
+
}>;
|
|
128
|
+
static serializeDbType(type: GenericDbType, val: any): any;
|
|
79
129
|
/**
|
|
80
130
|
* @internal
|
|
81
131
|
*
|
|
@@ -100,7 +150,7 @@ export default class QueryDriverBase<DreamInstance extends Dream> {
|
|
|
100
150
|
*
|
|
101
151
|
* @returns A dream instance or null
|
|
102
152
|
*/
|
|
103
|
-
takeOne(this:
|
|
153
|
+
takeOne(this: QueryDriverBase<DreamInstance>): Promise<DreamInstance | null>;
|
|
104
154
|
/**
|
|
105
155
|
* Retrieves an array containing all records matching the Query.
|
|
106
156
|
* Be careful using this, since it will attempt to pull every
|
|
@@ -176,7 +226,7 @@ export default class QueryDriverBase<DreamInstance extends Dream> {
|
|
|
176
226
|
* @param selection - the column to use for your nested Query
|
|
177
227
|
* @returns A Kysely SelectQueryBuilder instance
|
|
178
228
|
*/
|
|
179
|
-
nestedSelect<SimpleFieldType extends keyof DreamColumnNames<DreamInstance>, PluckThroughFieldType>(this:
|
|
229
|
+
nestedSelect<SimpleFieldType extends keyof DreamColumnNames<DreamInstance>, PluckThroughFieldType>(this: QueryDriverBase<DreamInstance>, selection: SimpleFieldType | PluckThroughFieldType): SelectQueryBuilder<any, any, any>;
|
|
180
230
|
/**
|
|
181
231
|
* executes provided query instance as a deletion query.
|
|
182
232
|
* @returns the number of deleted rows
|
|
@@ -193,7 +243,17 @@ export default class QueryDriverBase<DreamInstance extends Dream> {
|
|
|
193
243
|
* to encapsulate the persisting of the dream, as well as any
|
|
194
244
|
* subsequent model hooks that are fired.
|
|
195
245
|
*/
|
|
196
|
-
static saveDream(dream: Dream, txn?: DreamTransaction<Dream> | null): Promise<
|
|
246
|
+
static saveDream(dream: Dream, txn?: DreamTransaction<Dream> | null): Promise<any>;
|
|
247
|
+
/**
|
|
248
|
+
* destroys a dream, possibly implementing soft delete if reallyDestroy is false
|
|
249
|
+
* and the record being deleted implements soft delete.
|
|
250
|
+
*
|
|
251
|
+
* @param dream - the dream instance you wish to destroy
|
|
252
|
+
* @param txn - a transaction to encapsulate, consistently provided by underlying dream mechanisms
|
|
253
|
+
* @param reallyDestroy - whether or not to reallyDestroy. If false, soft delete will be attempted when relevant
|
|
254
|
+
*/
|
|
255
|
+
static destroyDream(dream: Dream, txn: DreamTransaction<Dream>, reallyDestroy: boolean): Promise<void>;
|
|
256
|
+
static get syncDialect(): string;
|
|
197
257
|
/**
|
|
198
258
|
* Returns the sql that would be executed by this Query
|
|
199
259
|
*
|
|
@@ -226,7 +286,7 @@ export default class QueryDriverBase<DreamInstance extends Dream> {
|
|
|
226
286
|
* @returns An object representing the underlying sql statement
|
|
227
287
|
*
|
|
228
288
|
*/
|
|
229
|
-
sql():
|
|
289
|
+
sql(): CompiledQuery<object>;
|
|
230
290
|
/**
|
|
231
291
|
* @internal
|
|
232
292
|
*
|
|
@@ -240,3 +300,4 @@ export default class QueryDriverBase<DreamInstance extends Dream> {
|
|
|
240
300
|
*/
|
|
241
301
|
hydratePreload(this: QueryDriverBase<DreamInstance>, dream: Dream): Promise<void>;
|
|
242
302
|
}
|
|
303
|
+
export type GenericDbType = 'datetime' | 'date';
|