@rvoh/dream 1.4.2 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/src/Dream.js +12 -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/db/migration-helpers/DreamMigrationHelpers.js +36 -0
- 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 +22 -17
- 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/db/migration-helpers/DreamMigrationHelpers.js +36 -0
- 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 +16 -11
- 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/db/migration-helpers/DreamMigrationHelpers.d.ts +36 -0
- 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 +19 -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/CHANGELOG.md +0 -100
- 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
|
@@ -47,27 +47,56 @@ function generateFactoryContent({ fullyQualifiedModelName, columnsWithTypes, })
|
|
|
47
47
|
attributeDefaults.push(`${attributeVariable}: \`${fullyQualifiedModelName} ${attributeVariable} ${counterVariableIncremented ? '${counter}' : '${++counter}'}\`,`);
|
|
48
48
|
counterVariableIncremented = true;
|
|
49
49
|
break;
|
|
50
|
+
case 'string[]':
|
|
51
|
+
case 'text[]':
|
|
52
|
+
case 'citext[]':
|
|
53
|
+
attributeDefaults.push(`${attributeVariable}: [\`${fullyQualifiedModelName} ${attributeVariable} ${counterVariableIncremented ? '${counter}' : '${++counter}'}\`],`);
|
|
54
|
+
counterVariableIncremented = true;
|
|
55
|
+
break;
|
|
50
56
|
case 'enum':
|
|
51
57
|
attributeDefaults.push(`${attributeVariable}: '${(descriptors.at(-1) || '<tbd>').split(',')[0]}',`);
|
|
52
58
|
break;
|
|
59
|
+
case 'enum[]':
|
|
60
|
+
attributeDefaults.push(`${attributeVariable}: ['${(descriptors.at(-1) || '<tbd>').split(',')[0]}'],`);
|
|
61
|
+
break;
|
|
53
62
|
case 'integer':
|
|
54
63
|
attributeDefaults.push(`${attributeVariable}: 1,`);
|
|
55
64
|
break;
|
|
65
|
+
case 'integer[]':
|
|
66
|
+
attributeDefaults.push(`${attributeVariable}: [1],`);
|
|
67
|
+
break;
|
|
56
68
|
case 'bigint':
|
|
57
69
|
attributeDefaults.push(`${attributeVariable}: '11111111111111111',`);
|
|
58
70
|
break;
|
|
71
|
+
case 'bigint[]':
|
|
72
|
+
attributeDefaults.push(`${attributeVariable}: ['11111111111111111'],`);
|
|
73
|
+
break;
|
|
59
74
|
case 'decimal':
|
|
60
75
|
attributeDefaults.push(`${attributeVariable}: 1.1,`);
|
|
61
76
|
break;
|
|
77
|
+
case 'decimal[]':
|
|
78
|
+
attributeDefaults.push(`${attributeVariable}: [1.1],`);
|
|
79
|
+
break;
|
|
62
80
|
case 'date':
|
|
63
81
|
dreamImports.push('CalendarDate');
|
|
64
82
|
attributeDefaults.push(`${attributeVariable}: CalendarDate.today(),`);
|
|
65
83
|
break;
|
|
84
|
+
case 'date[]':
|
|
85
|
+
dreamImports.push('CalendarDate');
|
|
86
|
+
attributeDefaults.push(`${attributeVariable}: [CalendarDate.today()],`);
|
|
87
|
+
break;
|
|
66
88
|
case 'datetime':
|
|
67
89
|
dreamImports.push('DateTime');
|
|
68
90
|
attributeDefaults.push(`${attributeVariable}: DateTime.now(),`);
|
|
69
91
|
break;
|
|
92
|
+
case 'datetime[]':
|
|
93
|
+
dreamImports.push('DateTime');
|
|
94
|
+
attributeDefaults.push(`${attributeVariable}: [DateTime.now()],`);
|
|
95
|
+
break;
|
|
70
96
|
default:
|
|
97
|
+
if (/\[\]$/.test(attributeType)) {
|
|
98
|
+
attributeDefaults.push(`${attributeVariable}: [],`);
|
|
99
|
+
}
|
|
71
100
|
// noop
|
|
72
101
|
}
|
|
73
102
|
}
|
|
@@ -13,15 +13,17 @@ const dreamFileAndDirPaths_js_1 = require("../path/dreamFileAndDirPaths.js");
|
|
|
13
13
|
const dreamPath_js_1 = require("../path/dreamPath.js");
|
|
14
14
|
const snakeify_js_1 = require("../snakeify.js");
|
|
15
15
|
const generateStiMigrationContent_js_1 = require("./generateStiMigrationContent.js");
|
|
16
|
-
async function generateMigration({ migrationName, columnsWithTypes, fullyQualifiedModelName, fullyQualifiedParentName, }) {
|
|
17
|
-
const { relFilePath, absFilePath } =
|
|
16
|
+
async function generateMigration({ migrationName, columnsWithTypes, connectionName, fullyQualifiedModelName, fullyQualifiedParentName, }) {
|
|
17
|
+
const { relFilePath, absFilePath } = connectionName === 'default'
|
|
18
|
+
? (0, dreamFileAndDirPaths_js_1.default)(path.join((0, dreamPath_js_1.default)('db'), 'migrations'), `${(0, migrationVersion_js_1.default)()}-${(0, hyphenize_js_1.default)(migrationName).replace(/\//g, '-')}.ts`)
|
|
19
|
+
: (0, dreamFileAndDirPaths_js_1.default)(path.join((0, dreamPath_js_1.default)('db'), 'migrations', connectionName), `${(0, migrationVersion_js_1.default)()}-${(0, hyphenize_js_1.default)(migrationName).replace(/\//g, '-')}.ts`);
|
|
18
20
|
const isSTI = !!fullyQualifiedParentName;
|
|
19
21
|
let finalContent = '';
|
|
20
22
|
if (isSTI) {
|
|
21
23
|
finalContent = (0, generateStiMigrationContent_js_1.default)({
|
|
22
24
|
table: (0, snakeify_js_1.default)((0, pluralize_esm_1.default)((0, pascalizePath_js_1.default)(fullyQualifiedParentName))),
|
|
23
25
|
columnsWithTypes,
|
|
24
|
-
primaryKeyType: (0, primaryKeyType_js_1.default)(),
|
|
26
|
+
primaryKeyType: (0, primaryKeyType_js_1.default)(connectionName),
|
|
25
27
|
stiChildClassName: (0, pascalizePath_js_1.default)(fullyQualifiedModelName),
|
|
26
28
|
});
|
|
27
29
|
}
|
|
@@ -29,7 +31,7 @@ async function generateMigration({ migrationName, columnsWithTypes, fullyQualifi
|
|
|
29
31
|
finalContent = (0, generateMigrationContent_js_1.default)({
|
|
30
32
|
table: (0, snakeify_js_1.default)((0, pluralize_esm_1.default)((0, pascalizePath_js_1.default)(fullyQualifiedModelName))),
|
|
31
33
|
columnsWithTypes,
|
|
32
|
-
primaryKeyType: (0, primaryKeyType_js_1.default)(),
|
|
34
|
+
primaryKeyType: (0, primaryKeyType_js_1.default)(connectionName),
|
|
33
35
|
});
|
|
34
36
|
}
|
|
35
37
|
else {
|
|
@@ -37,7 +39,7 @@ async function generateMigration({ migrationName, columnsWithTypes, fullyQualifi
|
|
|
37
39
|
finalContent = (0, generateMigrationContent_js_1.default)({
|
|
38
40
|
table: tableName ? (0, pluralize_esm_1.default)((0, snakeify_js_1.default)(tableName)) : '<table-name>',
|
|
39
41
|
columnsWithTypes,
|
|
40
|
-
primaryKeyType: (0, primaryKeyType_js_1.default)(),
|
|
42
|
+
primaryKeyType: (0, primaryKeyType_js_1.default)(connectionName),
|
|
41
43
|
createOrAlter: 'alter',
|
|
42
44
|
});
|
|
43
45
|
}
|
|
@@ -3,13 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.default = generateMigrationContent;
|
|
4
4
|
exports.optionalFromDescriptors = optionalFromDescriptors;
|
|
5
5
|
const pluralize_esm_1 = require("pluralize-esm");
|
|
6
|
+
const Query_js_1 = require("../../dream/Query.js");
|
|
6
7
|
const InvalidDecimalFieldPassedToGenerator_js_1 = require("../../errors/InvalidDecimalFieldPassedToGenerator.js");
|
|
7
8
|
const compact_js_1 = require("../compact.js");
|
|
8
|
-
const foreignKeyTypeFromPrimaryKey_js_1 = require("../db/foreignKeyTypeFromPrimaryKey.js");
|
|
9
9
|
const snakeify_js_1 = require("../snakeify.js");
|
|
10
10
|
const STI_TYPE_COLUMN_NAME = 'type';
|
|
11
11
|
const COLUMNS_TO_INDEX = [STI_TYPE_COLUMN_NAME];
|
|
12
|
-
function generateMigrationContent({ table, columnsWithTypes = [], primaryKeyType = 'bigserial', createOrAlter = 'create', stiChildClassName, } = {}) {
|
|
12
|
+
function generateMigrationContent({ connectionName = 'default', table, columnsWithTypes = [], primaryKeyType = 'bigserial', createOrAlter = 'create', stiChildClassName, } = {}) {
|
|
13
13
|
const altering = createOrAlter === 'alter';
|
|
14
14
|
let requireCitextExtension = false;
|
|
15
15
|
const checkConstraints = [];
|
|
@@ -41,7 +41,7 @@ function generateMigrationContent({ table, columnsWithTypes = [], primaryKeyType
|
|
|
41
41
|
}
|
|
42
42
|
switch (attributeType) {
|
|
43
43
|
case 'belongs_to':
|
|
44
|
-
columnDefs.push(generateBelongsToStr(attributeName, {
|
|
44
|
+
columnDefs.push(generateBelongsToStr(connectionName, attributeName, {
|
|
45
45
|
primaryKeyType,
|
|
46
46
|
optional,
|
|
47
47
|
}));
|
|
@@ -50,15 +50,27 @@ function generateMigrationContent({ table, columnsWithTypes = [], primaryKeyType
|
|
|
50
50
|
case 'enum':
|
|
51
51
|
columnDefs.push(generateEnumStr(attributeName, { descriptors, optional }));
|
|
52
52
|
break;
|
|
53
|
+
case 'enum[]':
|
|
54
|
+
columnDefs.push(generateEnumStr(attributeName, { descriptors, optional, asArray: true }));
|
|
55
|
+
break;
|
|
53
56
|
case 'decimal':
|
|
54
57
|
columnDefs.push(generateDecimalStr(attributeName, { descriptors, optional }));
|
|
55
58
|
break;
|
|
59
|
+
case 'decimal[]':
|
|
60
|
+
columnDefs.push(generateDecimalStr(attributeName, { descriptors, optional, asArray: true }));
|
|
61
|
+
break;
|
|
62
|
+
// array case for booleans can be handled with the default block.
|
|
63
|
+
// the only thing that is customized for a boolean field is the default
|
|
64
|
+
// value, which doesn't need to be special for boolean[]
|
|
56
65
|
case 'boolean':
|
|
57
66
|
columnDefs.push(generateBooleanStr(attributeName, { optional }));
|
|
58
67
|
break;
|
|
59
68
|
case 'encrypted':
|
|
60
69
|
columnDefs.push(generateColumnStr(`encrypted_${attributeName}`, 'text', descriptors, { optional }));
|
|
61
70
|
break;
|
|
71
|
+
// TODO: determine if we need to support encrypted[] in the future
|
|
72
|
+
case 'encrypted[]':
|
|
73
|
+
throw new Error('the "encrypted[]" column type is not supported');
|
|
62
74
|
default:
|
|
63
75
|
if (sqlAttributeType !== undefined) {
|
|
64
76
|
columnDefs.push(generateColumnStr(attributeName, sqlAttributeType, descriptors, { optional }));
|
|
@@ -130,19 +142,25 @@ function getAttributeType(attributeType, descriptors) {
|
|
|
130
142
|
switch (attributeType) {
|
|
131
143
|
case 'string':
|
|
132
144
|
return `varchar(${descriptors[0] || 255})`;
|
|
145
|
+
case 'string[]':
|
|
146
|
+
return `varchar(${descriptors[0] || 255})[]`;
|
|
133
147
|
case 'enum':
|
|
134
148
|
return enumAttributeType(descriptors);
|
|
149
|
+
case 'enum[]':
|
|
150
|
+
return enumAttributeType(descriptors, true);
|
|
135
151
|
case 'datetime':
|
|
136
152
|
return 'timestamp';
|
|
137
153
|
default:
|
|
138
154
|
return attributeType;
|
|
139
155
|
}
|
|
140
156
|
}
|
|
141
|
-
function enumAttributeType(descriptors) {
|
|
142
|
-
|
|
157
|
+
function enumAttributeType(descriptors, asArray = false) {
|
|
158
|
+
const suffix = asArray ? '[]' : '';
|
|
159
|
+
return `sql\`${descriptors[0]}_enum${suffix}\``;
|
|
143
160
|
}
|
|
161
|
+
const ENUM_OR_ENUM_ARRAY_REGEX = /:enum:.*:|:enum\[\]:.*:/;
|
|
144
162
|
function generateEnumStatements(columnsWithTypes) {
|
|
145
|
-
const enumStatements = columnsWithTypes.filter(attribute =>
|
|
163
|
+
const enumStatements = columnsWithTypes.filter(attribute => ENUM_OR_ENUM_ARRAY_REGEX.test(attribute));
|
|
146
164
|
const finalStatements = (0, compact_js_1.default)(enumStatements.map(statement => {
|
|
147
165
|
const [, , enumName, ...descriptors] = statement.split(':');
|
|
148
166
|
optionalFromDescriptors(descriptors);
|
|
@@ -160,7 +178,7 @@ function generateEnumStatements(columnsWithTypes) {
|
|
|
160
178
|
return finalStatements.length ? finalStatements.join('\n\n') + '\n\n' : '';
|
|
161
179
|
}
|
|
162
180
|
function generateEnumDropStatements(columnsWithTypes) {
|
|
163
|
-
const enumStatements = columnsWithTypes.filter(attribute =>
|
|
181
|
+
const enumStatements = columnsWithTypes.filter(attribute => ENUM_OR_ENUM_ARRAY_REGEX.test(attribute));
|
|
164
182
|
const finalStatements = (0, compact_js_1.default)(enumStatements.map(statement => {
|
|
165
183
|
const [, , enumName, ...descriptors] = statement.split(':');
|
|
166
184
|
optionalFromDescriptors(descriptors);
|
|
@@ -174,17 +192,22 @@ function generateEnumDropStatements(columnsWithTypes) {
|
|
|
174
192
|
function generateBooleanStr(attributeName, { optional }) {
|
|
175
193
|
return `.addColumn('${attributeName}', 'boolean'${optional ? '' : ', col => col.notNull().defaultTo(false)'})`;
|
|
176
194
|
}
|
|
177
|
-
function generateEnumStr(attributeName, { descriptors, optional }) {
|
|
178
|
-
const computedAttributeType = enumAttributeType(descriptors);
|
|
195
|
+
function generateEnumStr(attributeName, { descriptors, optional, asArray = false }) {
|
|
196
|
+
const computedAttributeType = enumAttributeType(descriptors, asArray);
|
|
179
197
|
if (attributeName === undefined)
|
|
180
198
|
return '';
|
|
181
|
-
|
|
199
|
+
const columnModifiers = asArray ? "col.notNull().defaultTo('{}')" : 'col.notNull()';
|
|
200
|
+
return `.addColumn('${attributeName}', ${computedAttributeType}${optional ? '' : `, col => ${columnModifiers}`})`;
|
|
182
201
|
}
|
|
183
|
-
function generateDecimalStr(attributeName, { descriptors, optional }) {
|
|
202
|
+
function generateDecimalStr(attributeName, { descriptors, optional, asArray = false }) {
|
|
184
203
|
const [scale, precision] = descriptors[0]?.split(',') || [null, null];
|
|
185
204
|
if (!scale || !precision)
|
|
186
205
|
throw new InvalidDecimalFieldPassedToGenerator_js_1.default(attributeName);
|
|
187
|
-
|
|
206
|
+
const columnModifiers = asArray ? "col.notNull().defaultTo('{}')" : 'col.notNull()';
|
|
207
|
+
const decimalStatement = asArray
|
|
208
|
+
? `sql\`decimal(${scale}, ${precision})[]\``
|
|
209
|
+
: `'decimal(${scale}, ${precision})'`;
|
|
210
|
+
return `.addColumn('${attributeName}', ${decimalStatement}${optional ? '' : `, col => ${columnModifiers}`})`;
|
|
188
211
|
}
|
|
189
212
|
function generateColumnStr(attributeName, attributeType, descriptors, { optional }) {
|
|
190
213
|
let returnStr = `.addColumn('${attributeName}', ${attributeTypeString(attributeType)}`;
|
|
@@ -192,12 +215,15 @@ function generateColumnStr(attributeName, attributeType, descriptors, { optional
|
|
|
192
215
|
const providedDefault = providedDefaultArg?.replace(/^default\(/, '')?.replace(/\)$/, '');
|
|
193
216
|
const notNull = !optional;
|
|
194
217
|
const hasExtraValues = providedDefault || notNull;
|
|
218
|
+
const isArray = /\[\]$/.test(attributeType);
|
|
195
219
|
if (hasExtraValues)
|
|
196
220
|
returnStr += ', col => col';
|
|
197
221
|
if (notNull)
|
|
198
222
|
returnStr += '.notNull()';
|
|
199
223
|
if (providedDefault)
|
|
200
224
|
returnStr += `.defaultTo('${providedDefault}')`;
|
|
225
|
+
else if (isArray)
|
|
226
|
+
returnStr += `.defaultTo('{}')`;
|
|
201
227
|
returnStr = `${returnStr})`;
|
|
202
228
|
if (attributeName === STI_TYPE_COLUMN_NAME)
|
|
203
229
|
returnStr = `// CONSIDER: when using type for STI, always use an enum
|
|
@@ -210,6 +236,7 @@ function attributeTypeString(attributeType) {
|
|
|
210
236
|
const attributeTypesRequiringSql = ['citext'];
|
|
211
237
|
if (attributeTypesRequiringSql.includes(attributeType))
|
|
212
238
|
return `sql\`${attributeType}\``;
|
|
239
|
+
const isArray = /\[\]$/.test(attributeType);
|
|
213
240
|
switch (attributeType) {
|
|
214
241
|
case 'varbit':
|
|
215
242
|
case 'bitvarying':
|
|
@@ -217,11 +244,17 @@ function attributeTypeString(attributeType) {
|
|
|
217
244
|
case 'txid_snapshot':
|
|
218
245
|
return "'txid_snapshot'";
|
|
219
246
|
default:
|
|
220
|
-
|
|
247
|
+
if (isArray) {
|
|
248
|
+
return `sql\`${attributeType.replace(/_/g, ' ')}\``;
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
return `'${attributeType.replace(/_/g, ' ')}'`;
|
|
252
|
+
}
|
|
221
253
|
}
|
|
222
254
|
}
|
|
223
|
-
function generateBelongsToStr(associationName, { primaryKeyType, optional = false, }) {
|
|
224
|
-
const
|
|
255
|
+
function generateBelongsToStr(connectionName, associationName, { primaryKeyType, optional = false, }) {
|
|
256
|
+
const dbDriverClass = Query_js_1.default.dbDriverClass(connectionName);
|
|
257
|
+
const dataType = dbDriverClass.foreignKeyTypeFromPrimaryKey(primaryKeyType);
|
|
225
258
|
const references = (0, pluralize_esm_1.default)(associationName.replace(/\//g, '_').replace(/_id$/, ''));
|
|
226
259
|
return `.addColumn('${associationNameToForeignKey(associationName)}', '${dataType}', col => col.references('${references}.id').onDelete('restrict')${optional ? '' : '.notNull()'})`;
|
|
227
260
|
}
|
|
@@ -1,26 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.default = primaryKeyType;
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
switch (dreamconf.primaryKeyType) {
|
|
9
|
-
case 'bigint':
|
|
10
|
-
case 'bigserial':
|
|
11
|
-
case 'uuid':
|
|
12
|
-
case 'integer':
|
|
13
|
-
return dreamconf.primaryKeyType;
|
|
14
|
-
default: {
|
|
15
|
-
// protection so that if a new EncryptAlgorithm is ever added, this will throw a type error at build time
|
|
16
|
-
const _never = dreamconf.primaryKeyType;
|
|
17
|
-
throw new Error(`
|
|
18
|
-
ATTENTION!
|
|
19
|
-
|
|
20
|
-
unrecognized primary key type "${_never}" found in .dream.yml.
|
|
21
|
-
please use one of the allowed primary key types:
|
|
22
|
-
${constants_js_1.primaryKeyTypes.join(', ')}
|
|
23
|
-
`);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
4
|
+
const Query_js_1 = require("../../dream/Query.js");
|
|
5
|
+
function primaryKeyType(connectionName) {
|
|
6
|
+
const dbDriverClass = Query_js_1.default.dbDriverClass(connectionName);
|
|
7
|
+
return dbDriverClass.primaryKeyType();
|
|
26
8
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.default = sqlAttributes;
|
|
4
|
+
const index_js_1 = require("../dream-app/index.js");
|
|
4
5
|
const CalendarDate_js_1 = require("./CalendarDate.js");
|
|
5
6
|
const DateTime_js_1 = require("./DateTime.js");
|
|
6
7
|
const isDatetimeOrDatetimeArrayColumn_js_1 = require("./db/types/isDatetimeOrDatetimeArrayColumn.js");
|
|
@@ -9,6 +10,7 @@ const normalizeUnicode_js_1 = require("./normalizeUnicode.js");
|
|
|
9
10
|
function sqlAttributes(dream) {
|
|
10
11
|
const attributes = dream.dirtyAttributes();
|
|
11
12
|
const dreamClass = dream.constructor;
|
|
13
|
+
const queryDriverClass = index_js_1.default.getOrFail().dbConnectionQueryDriverClass(dream.connectionName);
|
|
12
14
|
return Object.keys(attributes).reduce((result, key) => {
|
|
13
15
|
let val = attributes[key];
|
|
14
16
|
if (val === undefined)
|
|
@@ -26,13 +28,14 @@ function sqlAttributes(dream) {
|
|
|
26
28
|
val = (0, normalizeUnicode_js_1.default)(val);
|
|
27
29
|
}
|
|
28
30
|
if (val instanceof DateTime_js_1.DateTime || val instanceof CalendarDate_js_1.default) {
|
|
31
|
+
const dateOrDatetime = val instanceof DateTime_js_1.DateTime ? 'datetime' : 'date';
|
|
29
32
|
// Converting toJSDate resulted in the correct timezone, but even with process.env.TZ=UTC,
|
|
30
33
|
// Kysely inserted into the database with the machine timezone, which can shift the date
|
|
31
34
|
// (e.g., toJSDate resulted in a JS Date that formats as "1987-04-07T00:00:00.000Z", but
|
|
32
35
|
// Kysely inserted "1907-04-06" into the database). By converting to an SQL string before
|
|
33
36
|
// handing off to Kysely, we bypass Javascript dates altogether, sending the string into the
|
|
34
37
|
// database for storage as a date or datetime.
|
|
35
|
-
result[key] =
|
|
38
|
+
result[key] = queryDriverClass.serializeDbType(dateOrDatetime, val);
|
|
36
39
|
}
|
|
37
40
|
else {
|
|
38
41
|
result[key] = val;
|
package/dist/cjs/src/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.ObjectSerializerBuilder = exports.DreamSerializerBuilder = exports.ObjectSerializer = exports.DreamSerializer = exports.serializerNameFromFullyQualifiedModelName = exports.isDreamSerializer = exports.inferSerializersFromDreamClassOrViewModelClass = exports.inferSerializerFromDreamOrViewModel = exports.ops = exports.uniq = exports.uncapitalize = exports.expandStiClasses = exports.standardizeFullyQualifiedModelName = exports.sortObjectByValue = exports.sortObjectByKey = exports.sortBy = exports.sort = exports.snakeify = exports.round = exports.range = exports.Range = exports.sharedPathPrefix = exports.relativeDreamPath = exports.dreamPath = exports.pascalize = void 0;
|
|
3
|
+
exports.intersection = exports.hyphenize = exports.groupBy = exports.globalClassNameFromFullyQualifiedModelName = exports.Env = exports.DreamLogos = exports.DateTime = exports.compact = exports.cloneDeepSafe = exports.generateDream = exports.capitalize = exports.camelize = exports.CalendarDate = exports.Benchmark = exports.ValidationError = exports.RecordNotFound = exports.GlobalNameNotSet = exports.NotNullViolation = exports.DataTypeColumnTypeMismatch = exports.CheckConstraintViolation = exports.CreateOrFindByFailedToCreateAndFind = exports.NonLoadedAssociation = exports.Encrypt = exports.PostgresQueryDriver = exports.KyselyQueryDriver = exports.QueryDriverBase = exports.Query = exports.DreamTransaction = exports.openapiShorthandPrimitiveTypes = exports.openapiPrimitiveTypes = exports.DreamConst = exports.Dream = exports.DreamAppAllowedPackageManagersEnumValues = exports.DreamApp = exports.lookupClassByGlobalName = exports.DreamImporter = exports.Decorators = exports.STI = exports.SoftDelete = exports.ReplicaSafe = exports.validateTable = exports.validateColumn = exports.DreamMigrationHelpers = exports.untypedDb = exports.pgErrorType = exports.dreamDbConnections = exports.closeAllDbConnections = exports.DreamCLI = exports.CliFileWriter = exports.DreamBin = void 0;
|
|
4
|
+
exports.ObjectSerializerBuilder = exports.DreamSerializerBuilder = exports.ObjectSerializer = exports.DreamSerializer = exports.serializerNameFromFullyQualifiedModelName = exports.isDreamSerializer = exports.inferSerializersFromDreamClassOrViewModelClass = exports.inferSerializerFromDreamOrViewModel = exports.ops = exports.uniq = exports.uncapitalize = exports.expandStiClasses = exports.standardizeFullyQualifiedModelName = exports.sortObjectByValue = exports.sortObjectByKey = exports.sortBy = exports.sort = exports.snakeify = exports.round = exports.range = exports.Range = exports.sharedPathPrefix = exports.relativeDreamPath = exports.dreamPath = exports.pascalize = exports.normalizeUnicode = exports.loadRepl = exports.isEmpty = void 0;
|
|
5
5
|
var index_js_1 = require("./bin/index.js");
|
|
6
6
|
Object.defineProperty(exports, "DreamBin", { enumerable: true, get: function () { return index_js_1.default; } });
|
|
7
7
|
var CliFileWriter_js_1 = require("./cli/CliFileWriter.js");
|
|
@@ -46,6 +46,12 @@ var DreamTransaction_js_1 = require("./dream/DreamTransaction.js");
|
|
|
46
46
|
Object.defineProperty(exports, "DreamTransaction", { enumerable: true, get: function () { return DreamTransaction_js_1.default; } });
|
|
47
47
|
var Query_js_1 = require("./dream/Query.js");
|
|
48
48
|
Object.defineProperty(exports, "Query", { enumerable: true, get: function () { return Query_js_1.default; } });
|
|
49
|
+
var Base_js_1 = require("./dream/QueryDriver/Base.js");
|
|
50
|
+
Object.defineProperty(exports, "QueryDriverBase", { enumerable: true, get: function () { return Base_js_1.default; } });
|
|
51
|
+
var Kysely_js_1 = require("./dream/QueryDriver/Kysely.js");
|
|
52
|
+
Object.defineProperty(exports, "KyselyQueryDriver", { enumerable: true, get: function () { return Kysely_js_1.default; } });
|
|
53
|
+
var Postgres_js_1 = require("./dream/QueryDriver/Postgres.js");
|
|
54
|
+
Object.defineProperty(exports, "PostgresQueryDriver", { enumerable: true, get: function () { return Postgres_js_1.default; } });
|
|
49
55
|
var index_js_5 = require("./encrypt/index.js");
|
|
50
56
|
Object.defineProperty(exports, "Encrypt", { enumerable: true, get: function () { return index_js_5.default; } });
|
|
51
57
|
var NonLoadedAssociation_js_1 = require("./errors/associations/NonLoadedAssociation.js");
|
package/dist/esm/src/Dream.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import yoctocolors from 'yoctocolors';
|
|
2
2
|
import { pgErrorType, UNIQUE_VIOLATION } from './db/errors.js';
|
|
3
|
-
import db from './db/index.js';
|
|
4
3
|
import associationToGetterSetterProp from './decorators/field/association/associationToGetterSetterProp.js';
|
|
5
4
|
import { blankAssociationsFactory } from './decorators/field/association/shared.js';
|
|
6
5
|
import { blankHooksFactory } from './decorators/field/lifecycle/shared.js';
|
|
@@ -8,7 +7,6 @@ import resortAllRecords from './decorators/field/sortable/helpers/resortAllRecor
|
|
|
8
7
|
import DreamApp from './dream-app/index.js';
|
|
9
8
|
import DreamClassTransactionBuilder from './dream/DreamClassTransactionBuilder.js';
|
|
10
9
|
import DreamInstanceTransactionBuilder from './dream/DreamInstanceTransactionBuilder.js';
|
|
11
|
-
import DreamTransaction from './dream/DreamTransaction.js';
|
|
12
10
|
import associationQuery from './dream/internal/associations/associationQuery.js';
|
|
13
11
|
import associationUpdateQuery from './dream/internal/associations/associationUpdateQuery.js';
|
|
14
12
|
import createAssociation from './dream/internal/associations/createAssociation.js';
|
|
@@ -106,8 +104,14 @@ export default class Dream {
|
|
|
106
104
|
get schema() {
|
|
107
105
|
throw new DreamMissingRequiredOverride(this.constructor, 'schema');
|
|
108
106
|
}
|
|
109
|
-
get
|
|
110
|
-
|
|
107
|
+
get connectionName() {
|
|
108
|
+
return 'default';
|
|
109
|
+
}
|
|
110
|
+
get connectionTypeConfig() {
|
|
111
|
+
throw new DreamMissingRequiredOverride(this.constructor, 'connectionTypeConfig');
|
|
112
|
+
}
|
|
113
|
+
get globalTypeConfig() {
|
|
114
|
+
throw new DreamMissingRequiredOverride(this.constructor, 'globalTypeConfig');
|
|
111
115
|
}
|
|
112
116
|
/**
|
|
113
117
|
* Determines if the provided Dream class is the same as or a subclass of this Dream class.
|
|
@@ -1731,16 +1735,8 @@ export default class Dream {
|
|
|
1731
1735
|
* @returns void
|
|
1732
1736
|
*/
|
|
1733
1737
|
static async transaction(callback) {
|
|
1734
|
-
const
|
|
1735
|
-
|
|
1736
|
-
await db('primary')
|
|
1737
|
-
.transaction()
|
|
1738
|
-
.execute(async (kyselyTransaction) => {
|
|
1739
|
-
dreamTransaction.kyselyTransaction = kyselyTransaction;
|
|
1740
|
-
callbackResponse = (await callback(dreamTransaction));
|
|
1741
|
-
});
|
|
1742
|
-
await dreamTransaction.runAfterCommitHooks(dreamTransaction);
|
|
1743
|
-
return callbackResponse;
|
|
1738
|
+
const dbDriverClass = Query.dbDriverClass(this.prototype.connectionName);
|
|
1739
|
+
return (await dbDriverClass.transaction(this.prototype, callback));
|
|
1744
1740
|
}
|
|
1745
1741
|
/**
|
|
1746
1742
|
* Sends data through for use as passthrough data
|
|
@@ -1,22 +1,38 @@
|
|
|
1
1
|
import DreamCLI from '../cli/index.js';
|
|
2
|
+
import DreamApp from '../dream-app/index.js';
|
|
2
3
|
import Query from '../dream/Query.js';
|
|
3
4
|
import generateDream from '../helpers/cli/generateDream.js';
|
|
4
5
|
import sspawn from '../helpers/sspawn.js';
|
|
5
6
|
export default class DreamBin {
|
|
6
7
|
static async sync(onSync, options) {
|
|
7
|
-
|
|
8
|
+
const dreamApp = DreamApp.getOrFail();
|
|
9
|
+
for (const connectionName of Object.keys(dreamApp.dbCredentials)) {
|
|
10
|
+
await Query.dbDriverClass(connectionName).sync(connectionName, onSync, options);
|
|
11
|
+
}
|
|
8
12
|
}
|
|
9
13
|
static async dbCreate() {
|
|
10
|
-
|
|
14
|
+
const dreamApp = DreamApp.getOrFail();
|
|
15
|
+
for (const connectionName of Object.keys(dreamApp.dbCredentials)) {
|
|
16
|
+
await Query.dbDriverClass(connectionName).dbCreate(connectionName);
|
|
17
|
+
}
|
|
11
18
|
}
|
|
12
19
|
static async dbDrop() {
|
|
13
|
-
|
|
20
|
+
const dreamApp = DreamApp.getOrFail();
|
|
21
|
+
for (const connectionName of Object.keys(dreamApp.dbCredentials)) {
|
|
22
|
+
await Query.dbDriverClass(connectionName).dbDrop(connectionName);
|
|
23
|
+
}
|
|
14
24
|
}
|
|
15
25
|
static async dbMigrate() {
|
|
16
|
-
|
|
26
|
+
const dreamApp = DreamApp.getOrFail();
|
|
27
|
+
for (const connectionName of Object.keys(dreamApp.dbCredentials)) {
|
|
28
|
+
await Query.dbDriverClass(connectionName).migrate(connectionName);
|
|
29
|
+
}
|
|
17
30
|
}
|
|
18
31
|
static async dbRollback(opts) {
|
|
19
|
-
|
|
32
|
+
const dreamApp = DreamApp.getOrFail();
|
|
33
|
+
for (const connectionName of Object.keys(dreamApp.dbCredentials)) {
|
|
34
|
+
await Query.dbDriverClass(connectionName).rollback({ ...opts, connectionName });
|
|
35
|
+
}
|
|
20
36
|
}
|
|
21
37
|
static async generateDream(fullyQualifiedModelName, columnsWithTypes, options) {
|
|
22
38
|
await generateDream({
|
|
@@ -33,8 +49,8 @@ export default class DreamBin {
|
|
|
33
49
|
fullyQualifiedParentName,
|
|
34
50
|
});
|
|
35
51
|
}
|
|
36
|
-
static async generateMigration(migrationName, columnsWithTypes) {
|
|
37
|
-
await Query.dbDriverClass().generateMigration(migrationName, columnsWithTypes);
|
|
52
|
+
static async generateMigration(migrationName, columnsWithTypes, connectionName) {
|
|
53
|
+
await Query.dbDriverClass(connectionName).generateMigration(connectionName, migrationName, columnsWithTypes);
|
|
38
54
|
}
|
|
39
55
|
// though this is a private method, it is still used internally.
|
|
40
56
|
// It is only made private so that people don't mistakenly try
|
|
@@ -13,25 +13,37 @@ ${INDENT} subtitle:string:optional
|
|
|
13
13
|
${INDENT}
|
|
14
14
|
${INDENT}supported types:
|
|
15
15
|
${INDENT} - citext:
|
|
16
|
+
${INDENT} - citext[]:
|
|
16
17
|
${INDENT} case insensitive text (indexes and queries are automatically case insensitive)
|
|
17
18
|
${INDENT}
|
|
18
19
|
${INDENT} - string:
|
|
20
|
+
${INDENT} - string[]:
|
|
19
21
|
${INDENT} varchar; allowed length defaults to 255, but may be customized, e.g.: subtitle:string:128 or subtitle:string:128:optional
|
|
20
22
|
${INDENT}
|
|
21
23
|
${INDENT} - text
|
|
24
|
+
${INDENT} - text[]
|
|
22
25
|
${INDENT} - date
|
|
26
|
+
${INDENT} - date[]
|
|
23
27
|
${INDENT} - datetime
|
|
28
|
+
${INDENT} - datetime[]
|
|
24
29
|
${INDENT} - integer
|
|
30
|
+
${INDENT} - integer[]
|
|
25
31
|
${INDENT}
|
|
26
32
|
${INDENT} - decimal:
|
|
33
|
+
${INDENT} - decimal[]:
|
|
27
34
|
${INDENT} scale,precision is required, e.g.: volume:decimal:3,2 or volume:decimal:3,2:optional
|
|
35
|
+
${INDENT}
|
|
36
|
+
${INDENT} leveraging arrays, add the "[]" suffix, e.g.: volume:decimal[]:3,2
|
|
28
37
|
${INDENT}
|
|
29
38
|
${INDENT} - enum:
|
|
39
|
+
${INDENT} - enum[]:
|
|
30
40
|
${INDENT} include the enum name to automatically create the enum:
|
|
31
41
|
${INDENT} type:enum:room_types:bathroom,kitchen,bedroom or type:enum:room_types:bathroom,kitchen,bedroom:optional
|
|
32
42
|
${INDENT}
|
|
33
43
|
${INDENT} omit the enum values to leverage an existing enum (omits the enum type creation):
|
|
34
|
-
${INDENT} type:enum:room_types or type:enum:room_types:optional
|
|
44
|
+
${INDENT} type:enum:room_types or type:enum:room_types:optional
|
|
45
|
+
${INDENT}
|
|
46
|
+
${INDENT} leveraging arrays, add the "[]" suffix, e.g.: type:enum[]:room_types:bathroom,kitchen,bedroom`;
|
|
35
47
|
const columnsWithTypesDescription = baseColumnsWithTypesDescription +
|
|
36
48
|
`
|
|
37
49
|
${INDENT}
|
|
@@ -78,10 +90,11 @@ export default class DreamCLI {
|
|
|
78
90
|
.alias('g:migration')
|
|
79
91
|
.description('create a new migration')
|
|
80
92
|
.argument('<migrationName>', 'end with -to-table-name to prepopulate with an alterTable command')
|
|
93
|
+
.option('--connection-name <connectionName>', 'the connection name you wish to use for your migration')
|
|
81
94
|
.argument('[columnsWithTypes...]', columnsWithTypesDescriptionForMigration)
|
|
82
|
-
.action(async (migrationName, columnsWithTypes) => {
|
|
95
|
+
.action(async (migrationName, columnsWithTypes, options) => {
|
|
83
96
|
await initializeDreamApp();
|
|
84
|
-
await DreamBin.generateMigration(migrationName, columnsWithTypes);
|
|
97
|
+
await DreamBin.generateMigration(migrationName, columnsWithTypes, options.connectionName || 'default');
|
|
85
98
|
process.exit();
|
|
86
99
|
});
|
|
87
100
|
program
|
|
@@ -90,6 +103,7 @@ export default class DreamCLI {
|
|
|
90
103
|
.alias('generate:dream')
|
|
91
104
|
.alias('g:dream')
|
|
92
105
|
.option('--no-serializer')
|
|
106
|
+
.option('--connection-name <connectionName>', 'the db connection you want this attached to (defaults to the default db connection)', 'default')
|
|
93
107
|
.option('--sti-base-serializer')
|
|
94
108
|
.description('create a new Dream model')
|
|
95
109
|
.argument('<modelName>', 'the name of the model to create, e.g. Post or Settings/CommunicationPreferences')
|
|
@@ -104,6 +118,7 @@ export default class DreamCLI {
|
|
|
104
118
|
.alias('g:sti-child')
|
|
105
119
|
.description('create a new Dream model that extends another Dream model, leveraging STI (single table inheritance)')
|
|
106
120
|
.option('--no-serializer')
|
|
121
|
+
.option('--connection-name', 'the db connection you want this model attached to (defaults to the default connection)', 'default')
|
|
107
122
|
.argument('<childModelName>', 'the name of the model to create, e.g. Post or Settings/CommunicationPreferences')
|
|
108
123
|
.argument('<extends>', 'just the word "extends"')
|
|
109
124
|
.argument('<parentModelName>', `fully qualified name of the parent model, e.g.:
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import _db from '../db/index.js';
|
|
2
1
|
export default class ConnectedToDB {
|
|
3
2
|
dreamInstance;
|
|
4
3
|
dreamClass;
|
|
@@ -27,13 +26,6 @@ export default class ConnectedToDB {
|
|
|
27
26
|
return 'primary';
|
|
28
27
|
}
|
|
29
28
|
}
|
|
30
|
-
// ATTENTION FRED
|
|
31
|
-
// stop trying to make this async. You never learn...
|
|
32
|
-
dbFor(sqlCommandType) {
|
|
33
|
-
if (this.dreamTransaction?.kyselyTransaction)
|
|
34
|
-
return this.dreamTransaction?.kyselyTransaction;
|
|
35
|
-
return _db(this.dbConnectionType(sqlCommandType));
|
|
36
|
-
}
|
|
37
29
|
isReplicaSafe() {
|
|
38
30
|
return this.innerJoinDreamClasses.reduce((accumulator, dreamClass) => accumulator && dreamClass['replicaSafe'], this.dreamClass['replicaSafe']);
|
|
39
31
|
}
|
|
@@ -6,19 +6,19 @@
|
|
|
6
6
|
// when being imported from our esm build.
|
|
7
7
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
8
8
|
// @ts-ignore
|
|
9
|
-
import
|
|
10
|
-
import { CamelCasePlugin, Kysely, PostgresDialect } from 'kysely';
|
|
9
|
+
import { CamelCasePlugin, Kysely } from 'kysely';
|
|
11
10
|
import DreamApp from '../dream-app/index.js';
|
|
11
|
+
import protectAgainstPollutingAssignment from '../helpers/protectAgainstPollutingAssignment.js';
|
|
12
12
|
let connections = {};
|
|
13
13
|
export default class DreamDbConnection {
|
|
14
|
-
static getConnection(connectionType) {
|
|
14
|
+
static getConnection(connectionName, connectionType, dialectProvider) {
|
|
15
15
|
const dreamApp = DreamApp.getOrFail();
|
|
16
|
-
const
|
|
17
|
-
const connection = connections[connectionName];
|
|
16
|
+
const connectionTypeName = this.getConnectionTypeName(connectionType);
|
|
17
|
+
const connection = connections[connectionName]?.[connectionTypeName];
|
|
18
18
|
if (connection) {
|
|
19
19
|
return connection;
|
|
20
20
|
}
|
|
21
|
-
const connectionConf = dreamApp.dbConnectionConfig(connectionType);
|
|
21
|
+
const connectionConf = dreamApp.dbConnectionConfig(connectionName, connectionType);
|
|
22
22
|
const dbConn = new Kysely({
|
|
23
23
|
log(event) {
|
|
24
24
|
const dreamApp = DreamApp.getOrFail();
|
|
@@ -26,47 +26,35 @@ export default class DreamDbConnection {
|
|
|
26
26
|
fn(event);
|
|
27
27
|
});
|
|
28
28
|
},
|
|
29
|
-
dialect:
|
|
30
|
-
pool: new pg.Pool({
|
|
31
|
-
user: connectionConf.user || '',
|
|
32
|
-
password: connectionConf.password || '',
|
|
33
|
-
database: dreamApp.dbName(connectionType),
|
|
34
|
-
host: connectionConf.host || 'localhost',
|
|
35
|
-
port: connectionConf.port || 5432,
|
|
36
|
-
ssl: connectionConf.useSsl ? sslConfig(connectionConf) : false,
|
|
37
|
-
}),
|
|
38
|
-
}),
|
|
29
|
+
dialect: dialectProvider(connectionConf),
|
|
39
30
|
plugins: [new CamelCasePlugin({ underscoreBetweenUppercaseLetters: true })],
|
|
40
31
|
});
|
|
41
|
-
|
|
32
|
+
const protectedName = protectAgainstPollutingAssignment(connectionName);
|
|
33
|
+
connections[protectedName] ||= {};
|
|
34
|
+
connections[protectedName][this.getConnectionTypeName(connectionType)] = dbConn;
|
|
42
35
|
return dbConn;
|
|
43
36
|
}
|
|
44
|
-
static async dropAllConnections() {
|
|
45
|
-
for (const key of Object.keys(connections)) {
|
|
46
|
-
await connections[key]?.destroy();
|
|
47
|
-
delete connections[key];
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
37
|
static getConnectionTypeName(connectionType) {
|
|
51
38
|
return DreamApp.getOrFail().parallelDatabasesEnabled
|
|
52
39
|
? `${connectionType}_${process.env.VITEST_POOL_ID}`
|
|
53
40
|
: connectionType;
|
|
54
41
|
}
|
|
55
42
|
}
|
|
56
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
57
|
-
function sslConfig(connectionConf) {
|
|
58
|
-
// TODO: properly configure (https://rvohealth.atlassian.net/browse/PDTC-2914)
|
|
59
|
-
return {
|
|
60
|
-
rejectUnauthorized: false,
|
|
61
|
-
// ca: fs.readFileSync('/path/to/server-certificates/root.crt').toString(),
|
|
62
|
-
// key: fs.readFileSync('/path/to/client-key/postgresql.key').toString(),
|
|
63
|
-
// cert: fs.readFileSync('/path/to/client-certificates/postgresql.crt').toString(),
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
43
|
export function dreamDbConnections() {
|
|
67
44
|
return connections;
|
|
68
45
|
}
|
|
69
46
|
export async function closeAllDbConnections() {
|
|
70
|
-
|
|
47
|
+
const connectionNames = Object.keys(connections);
|
|
48
|
+
for (const connectionName of connectionNames) {
|
|
49
|
+
await closeAllConnectionsForConnectionName(connectionName);
|
|
50
|
+
}
|
|
71
51
|
connections = {};
|
|
72
52
|
}
|
|
53
|
+
export async function closeAllConnectionsForConnectionName(connectionName) {
|
|
54
|
+
const protectedName = protectAgainstPollutingAssignment(connectionName);
|
|
55
|
+
return await Promise.allSettled(Object.keys(connections[protectedName]).map(async (key) => {
|
|
56
|
+
const conn = connections[protectedName][key];
|
|
57
|
+
await conn.destroy();
|
|
58
|
+
delete connections[protectedName][key];
|
|
59
|
+
}));
|
|
60
|
+
}
|