@rvoh/dream 2.19.0 → 2.20.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/dist/cjs/src/Dream.js +83 -0
- package/dist/cjs/src/dream/Query.js +11 -0
- package/dist/cjs/src/dream/QueryDriver/Kysely.js +62 -14
- package/dist/cjs/src/dream/internal/filterRowToKnownColumns.js +41 -0
- package/dist/cjs/src/dream/internal/saveDream.js +6 -1
- package/dist/cjs/src/dream/internal/sqlResultToDreamInstance.js +9 -2
- package/dist/cjs/src/errors/schema-builder/CannotIgnoreAssociationColumn.js +38 -0
- package/dist/cjs/src/errors/schema-builder/CannotIgnoreEncryptedColumn.js +20 -0
- package/dist/cjs/src/errors/schema-builder/CannotIgnorePrimaryKey.js +18 -0
- package/dist/cjs/src/errors/schema-builder/CannotIgnoreSoftDeleteColumn.js +21 -0
- package/dist/cjs/src/errors/schema-builder/CannotIgnoreSortablePositionColumn.js +20 -0
- package/dist/cjs/src/errors/schema-builder/CannotIgnoreSortableScopeColumn.js +24 -0
- package/dist/cjs/src/errors/schema-builder/CannotIgnoreStiTypeColumn.js +17 -0
- package/dist/cjs/src/errors/schema-builder/ConflictingIgnoredColumns.js +27 -0
- package/dist/cjs/src/errors/schema-builder/IgnoredColumnMustBeCamelCase.js +21 -0
- package/dist/cjs/src/helpers/cli/ASTConnectionBuilder.js +38 -1
- package/dist/cjs/src/helpers/cli/ASTKyselyCodegenEnhancer.js +60 -0
- package/dist/cjs/src/helpers/cli/resolveIgnoredColumns.js +198 -0
- package/dist/esm/src/Dream.js +83 -0
- package/dist/esm/src/dream/Query.js +11 -0
- package/dist/esm/src/dream/QueryDriver/Kysely.js +62 -14
- package/dist/esm/src/dream/internal/filterRowToKnownColumns.js +41 -0
- package/dist/esm/src/dream/internal/saveDream.js +6 -1
- package/dist/esm/src/dream/internal/sqlResultToDreamInstance.js +9 -2
- package/dist/esm/src/errors/schema-builder/CannotIgnoreAssociationColumn.js +38 -0
- package/dist/esm/src/errors/schema-builder/CannotIgnoreEncryptedColumn.js +20 -0
- package/dist/esm/src/errors/schema-builder/CannotIgnorePrimaryKey.js +18 -0
- package/dist/esm/src/errors/schema-builder/CannotIgnoreSoftDeleteColumn.js +21 -0
- package/dist/esm/src/errors/schema-builder/CannotIgnoreSortablePositionColumn.js +20 -0
- package/dist/esm/src/errors/schema-builder/CannotIgnoreSortableScopeColumn.js +24 -0
- package/dist/esm/src/errors/schema-builder/CannotIgnoreStiTypeColumn.js +17 -0
- package/dist/esm/src/errors/schema-builder/ConflictingIgnoredColumns.js +27 -0
- package/dist/esm/src/errors/schema-builder/IgnoredColumnMustBeCamelCase.js +21 -0
- package/dist/esm/src/helpers/cli/ASTConnectionBuilder.js +38 -1
- package/dist/esm/src/helpers/cli/ASTKyselyCodegenEnhancer.js +60 -0
- package/dist/esm/src/helpers/cli/resolveIgnoredColumns.js +198 -0
- package/dist/types/src/Dream.d.ts +81 -0
- package/dist/types/src/dream/Query.d.ts +11 -0
- package/dist/types/src/dream/QueryDriver/Kysely.d.ts +21 -0
- package/dist/types/src/dream/internal/filterRowToKnownColumns.d.ts +30 -0
- package/dist/types/src/errors/schema-builder/CannotIgnoreAssociationColumn.d.ts +12 -0
- package/dist/types/src/errors/schema-builder/CannotIgnoreEncryptedColumn.d.ts +7 -0
- package/dist/types/src/errors/schema-builder/CannotIgnorePrimaryKey.d.ts +6 -0
- package/dist/types/src/errors/schema-builder/CannotIgnoreSoftDeleteColumn.d.ts +7 -0
- package/dist/types/src/errors/schema-builder/CannotIgnoreSortablePositionColumn.d.ts +7 -0
- package/dist/types/src/errors/schema-builder/CannotIgnoreSortableScopeColumn.d.ts +8 -0
- package/dist/types/src/errors/schema-builder/CannotIgnoreStiTypeColumn.d.ts +6 -0
- package/dist/types/src/errors/schema-builder/ConflictingIgnoredColumns.d.ts +7 -0
- package/dist/types/src/errors/schema-builder/IgnoredColumnMustBeCamelCase.d.ts +7 -0
- package/dist/types/src/helpers/cli/ASTConnectionBuilder.d.ts +20 -0
- package/dist/types/src/helpers/cli/ASTKyselyCodegenEnhancer.d.ts +13 -0
- package/dist/types/src/helpers/cli/resolveIgnoredColumns.d.ts +41 -0
- package/docs/assets/search.js +1 -1
- package/docs/classes/db.DreamMigrationHelpers.html +11 -11
- package/docs/classes/db.KyselyQueryDriver.html +57 -39
- package/docs/classes/db.PostgresQueryDriver.html +58 -40
- package/docs/classes/db.QueryDriverBase.html +38 -38
- package/docs/classes/errors.CheckConstraintViolation.html +3 -3
- package/docs/classes/errors.ColumnOverflow.html +3 -3
- package/docs/classes/errors.CreateOrFindByFailedToCreateAndFind.html +3 -3
- package/docs/classes/errors.DataIncompatibleWithDatabaseField.html +3 -3
- package/docs/classes/errors.DataTypeColumnTypeMismatch.html +3 -3
- package/docs/classes/errors.DecryptionError.html +2 -2
- package/docs/classes/errors.DecryptionParseError.html +2 -2
- package/docs/classes/errors.DecryptionRotationError.html +3 -3
- package/docs/classes/errors.GlobalNameNotSet.html +3 -3
- package/docs/classes/errors.InvalidCalendarDate.html +2 -2
- package/docs/classes/errors.InvalidClockTime.html +2 -2
- package/docs/classes/errors.InvalidClockTimeTz.html +2 -2
- package/docs/classes/errors.InvalidDateTime.html +2 -2
- package/docs/classes/errors.MissingSerializersDefinition.html +3 -3
- package/docs/classes/errors.NonLoadedAssociation.html +3 -3
- package/docs/classes/errors.NotNullViolation.html +3 -3
- package/docs/classes/errors.RecordNotFound.html +3 -3
- package/docs/classes/errors.ValidationError.html +3 -3
- package/docs/classes/index.CalendarDate.html +33 -33
- package/docs/classes/index.ClockTime.html +32 -32
- package/docs/classes/index.ClockTimeTz.html +35 -35
- package/docs/classes/index.DateTime.html +86 -86
- package/docs/classes/index.Decorators.html +19 -19
- package/docs/classes/index.Dream.html +188 -123
- package/docs/classes/index.DreamApp.html +10 -10
- package/docs/classes/index.DreamTransaction.html +2 -2
- package/docs/classes/index.Env.html +2 -2
- package/docs/classes/index.Query.html +73 -62
- package/docs/classes/system.CliFileWriter.html +4 -4
- package/docs/classes/system.DreamBin.html +2 -2
- package/docs/classes/system.DreamCLI.html +7 -7
- package/docs/classes/system.DreamImporter.html +2 -2
- package/docs/classes/system.DreamLogos.html +2 -2
- package/docs/classes/system.DreamSerializerBuilder.html +11 -11
- package/docs/classes/system.ObjectSerializerBuilder.html +8 -8
- package/docs/classes/system.PathHelpers.html +3 -3
- package/docs/classes/utils.Encrypt.html +3 -3
- package/docs/classes/utils.Range.html +2 -2
- package/docs/functions/db.closeAllDbConnections.html +1 -1
- package/docs/functions/db.dreamDbConnections.html +1 -1
- package/docs/functions/db.untypedDb.html +1 -1
- package/docs/functions/db.validateColumn.html +1 -1
- package/docs/functions/db.validateTable.html +1 -1
- package/docs/functions/errors.pgErrorType.html +1 -1
- package/docs/functions/index.DreamSerializer.html +1 -1
- package/docs/functions/index.ObjectSerializer.html +1 -1
- package/docs/functions/index.ReplicaSafe.html +1 -1
- package/docs/functions/index.STI.html +1 -1
- package/docs/functions/index.SoftDelete.html +1 -1
- package/docs/functions/utils.camelize.html +1 -1
- package/docs/functions/utils.capitalize.html +1 -1
- package/docs/functions/utils.cloneDeepSafe.html +1 -1
- package/docs/functions/utils.compact.html +1 -1
- package/docs/functions/utils.groupBy.html +1 -1
- package/docs/functions/utils.hyphenize.html +1 -1
- package/docs/functions/utils.intersection.html +1 -1
- package/docs/functions/utils.isEmpty.html +1 -1
- package/docs/functions/utils.normalizeUnicode.html +1 -1
- package/docs/functions/utils.pascalize.html +1 -1
- package/docs/functions/utils.percent.html +1 -1
- package/docs/functions/utils.range.html +1 -1
- package/docs/functions/utils.round.html +1 -1
- package/docs/functions/utils.sanitizeString.html +1 -1
- package/docs/functions/utils.snakeify.html +1 -1
- package/docs/functions/utils.sort.html +1 -1
- package/docs/functions/utils.sortBy.html +1 -1
- package/docs/functions/utils.sortObjectByKey.html +1 -1
- package/docs/functions/utils.sortObjectByValue.html +1 -1
- package/docs/functions/utils.uncapitalize.html +1 -1
- package/docs/functions/utils.uniq.html +1 -1
- package/docs/interfaces/openapi.OpenapiDescription.html +2 -2
- package/docs/interfaces/openapi.OpenapiSchemaProperties.html +1 -1
- package/docs/interfaces/openapi.OpenapiSchemaPropertiesShorthand.html +1 -1
- package/docs/interfaces/openapi.OpenapiTypeFieldObject.html +1 -1
- package/docs/interfaces/types.BelongsToStatement.html +2 -2
- package/docs/interfaces/types.DecoratorContext.html +2 -2
- package/docs/interfaces/types.DreamAppInitOptions.html +2 -2
- package/docs/interfaces/types.DreamAppOpts.html +2 -2
- package/docs/interfaces/types.DreamDbConfig.html +5 -5
- package/docs/interfaces/types.DurationObject.html +2 -2
- package/docs/interfaces/types.EncryptOptions.html +2 -2
- package/docs/interfaces/types.InternalAnyTypedSerializerRendersMany.html +2 -2
- package/docs/interfaces/types.InternalAnyTypedSerializerRendersOne.html +2 -2
- package/docs/interfaces/types.SerializerRendererOpts.html +2 -2
- package/docs/types/openapi.CommonOpenapiSchemaObjectFields.html +1 -1
- package/docs/types/openapi.OpenapiAllTypes.html +1 -1
- package/docs/types/openapi.OpenapiFormats.html +1 -1
- package/docs/types/openapi.OpenapiNumberFormats.html +1 -1
- package/docs/types/openapi.OpenapiPrimitiveBaseTypes.html +1 -1
- package/docs/types/openapi.OpenapiPrimitiveTypes.html +1 -1
- package/docs/types/openapi.OpenapiSchemaArray.html +1 -1
- package/docs/types/openapi.OpenapiSchemaArrayShorthand.html +1 -1
- package/docs/types/openapi.OpenapiSchemaBase.html +1 -1
- package/docs/types/openapi.OpenapiSchemaBody.html +1 -1
- package/docs/types/openapi.OpenapiSchemaBodyShorthand.html +1 -1
- package/docs/types/openapi.OpenapiSchemaCommonFields.html +1 -1
- package/docs/types/openapi.OpenapiSchemaExpressionAllOf.html +2 -2
- package/docs/types/openapi.OpenapiSchemaExpressionAnyOf.html +2 -2
- package/docs/types/openapi.OpenapiSchemaExpressionOneOf.html +2 -2
- package/docs/types/openapi.OpenapiSchemaExpressionRef.html +2 -2
- package/docs/types/openapi.OpenapiSchemaExpressionRefSchemaShorthand.html +2 -2
- package/docs/types/openapi.OpenapiSchemaInteger.html +1 -1
- package/docs/types/openapi.OpenapiSchemaNull.html +2 -2
- package/docs/types/openapi.OpenapiSchemaNumber.html +1 -1
- package/docs/types/openapi.OpenapiSchemaObject.html +1 -1
- package/docs/types/openapi.OpenapiSchemaObjectAllOf.html +1 -1
- package/docs/types/openapi.OpenapiSchemaObjectAllOfShorthand.html +1 -1
- package/docs/types/openapi.OpenapiSchemaObjectAnyOf.html +1 -1
- package/docs/types/openapi.OpenapiSchemaObjectAnyOfShorthand.html +1 -1
- package/docs/types/openapi.OpenapiSchemaObjectBase.html +1 -1
- package/docs/types/openapi.OpenapiSchemaObjectBaseShorthand.html +1 -1
- package/docs/types/openapi.OpenapiSchemaObjectOneOf.html +1 -1
- package/docs/types/openapi.OpenapiSchemaObjectOneOfShorthand.html +1 -1
- package/docs/types/openapi.OpenapiSchemaObjectShorthand.html +1 -1
- package/docs/types/openapi.OpenapiSchemaPrimitiveGeneric.html +1 -1
- package/docs/types/openapi.OpenapiSchemaShorthandExpressionAllOf.html +2 -2
- package/docs/types/openapi.OpenapiSchemaShorthandExpressionAnyOf.html +2 -2
- package/docs/types/openapi.OpenapiSchemaShorthandExpressionOneOf.html +2 -2
- package/docs/types/openapi.OpenapiSchemaShorthandExpressionSerializableRef.html +2 -2
- package/docs/types/openapi.OpenapiSchemaShorthandExpressionSerializerRef.html +2 -2
- package/docs/types/openapi.OpenapiSchemaShorthandPrimitiveGeneric.html +1 -1
- package/docs/types/openapi.OpenapiSchemaString.html +1 -1
- package/docs/types/openapi.OpenapiShorthandAllTypes.html +1 -1
- package/docs/types/openapi.OpenapiShorthandPrimitiveBaseTypes.html +1 -1
- package/docs/types/openapi.OpenapiShorthandPrimitiveTypes.html +1 -1
- package/docs/types/openapi.OpenapiTypeField.html +1 -1
- package/docs/types/system.DreamAppAllowedPackageManagersEnum.html +1 -1
- package/docs/types/types.CalendarDateDurationUnit.html +1 -1
- package/docs/types/types.CalendarDateObject.html +1 -1
- package/docs/types/types.Camelized.html +1 -1
- package/docs/types/types.ClockTimeObject.html +1 -1
- package/docs/types/types.DbConnectionType.html +1 -1
- package/docs/types/types.DbTypes.html +1 -1
- package/docs/types/types.DreamAssociationMetadata.html +1 -1
- package/docs/types/types.DreamAttributes.html +1 -1
- package/docs/types/types.DreamClassAssociationAndStatement.html +1 -1
- package/docs/types/types.DreamClassColumn.html +1 -1
- package/docs/types/types.DreamColumn.html +1 -1
- package/docs/types/types.DreamColumnNames.html +1 -1
- package/docs/types/types.DreamLogLevel.html +1 -1
- package/docs/types/types.DreamLogger.html +2 -2
- package/docs/types/types.DreamModelSerializerType.html +1 -1
- package/docs/types/types.DreamOrViewModelClassSerializerKey.html +1 -1
- package/docs/types/types.DreamOrViewModelSerializerKey.html +1 -1
- package/docs/types/types.DreamParamSafeAttributes.html +1 -1
- package/docs/types/types.DreamParamSafeColumnNames.html +1 -1
- package/docs/types/types.DreamSerializable.html +1 -1
- package/docs/types/types.DreamSerializableArray.html +1 -1
- package/docs/types/types.DreamSerializerKey.html +1 -1
- package/docs/types/types.DreamSerializers.html +1 -1
- package/docs/types/types.DreamVirtualColumns.html +1 -1
- package/docs/types/types.DurationUnit.html +1 -1
- package/docs/types/types.EncryptAlgorithm.html +1 -1
- package/docs/types/types.HasManyStatement.html +1 -1
- package/docs/types/types.HasOneStatement.html +1 -1
- package/docs/types/types.Hyphenized.html +1 -1
- package/docs/types/types.Pascalized.html +1 -1
- package/docs/types/types.PrimaryKeyType.html +1 -1
- package/docs/types/types.RoundingPrecision.html +1 -1
- package/docs/types/types.SerializerCasing.html +1 -1
- package/docs/types/types.SimpleObjectSerializerType.html +1 -1
- package/docs/types/types.Snakeified.html +1 -1
- package/docs/types/types.StrictInterface.html +1 -1
- package/docs/types/types.UpdateableAssociationProperties.html +1 -1
- package/docs/types/types.UpdateableProperties.html +1 -1
- package/docs/types/types.ValidationType.html +1 -1
- package/docs/types/types.ViewModel.html +2 -2
- package/docs/types/types.ViewModelClass.html +1 -1
- package/docs/types/types.WeekdayName.html +1 -1
- package/docs/types/types.WhereStatementForDream.html +1 -1
- package/docs/types/types.WhereStatementForDreamClass.html +1 -1
- package/docs/variables/index.DreamConst.html +1 -1
- package/docs/variables/index.ops.html +1 -1
- package/docs/variables/openapi.openapiPrimitiveTypes.html +1 -1
- package/docs/variables/openapi.openapiShorthandPrimitiveTypes.html +1 -1
- package/docs/variables/system.DreamAppAllowedPackageManagersEnumValues.html +1 -1
- package/docs/variables/system.primaryKeyTypes.html +1 -1
- package/package.json +1 -1
|
@@ -25,6 +25,7 @@ export default class ASTKyselyCodegenEnhancer extends ASTConnectionBuilder {
|
|
|
25
25
|
async enhance() {
|
|
26
26
|
let dbSourceFile = await this.getDbSourceFile();
|
|
27
27
|
dbSourceFile = this.camelizeKeys(dbSourceFile);
|
|
28
|
+
dbSourceFile = this.removeIgnoredColumns(dbSourceFile);
|
|
28
29
|
dbSourceFile = this.replaceTimestampExport(dbSourceFile);
|
|
29
30
|
dbSourceFile = await this.replaceTimeFieldsInInterfaces(dbSourceFile);
|
|
30
31
|
dbSourceFile = this.addMissingImports(dbSourceFile);
|
|
@@ -283,6 +284,65 @@ ${output}`);
|
|
|
283
284
|
const result = ts.transform(dbSourceFile, [transformer]);
|
|
284
285
|
return result.transformed[0];
|
|
285
286
|
}
|
|
287
|
+
/**
|
|
288
|
+
* @internal
|
|
289
|
+
*
|
|
290
|
+
* removes columns declared in model `ignoredColumns` getters from the
|
|
291
|
+
* table interfaces generated by kysely-codegen. This is what removes
|
|
292
|
+
* ignored columns from the generated db types file: every column-name
|
|
293
|
+
* level type (`DreamColumnNames`, `UpdateableProperties`, where-clause
|
|
294
|
+
* statements) derives from the Kysely `DB` interface, so filtering here
|
|
295
|
+
* turns any remaining code reference to an ignored column into a type
|
|
296
|
+
* error. Runs after camelizeKeys so that member names align with the
|
|
297
|
+
* camelCase column names models declare in ignoredColumns.
|
|
298
|
+
*/
|
|
299
|
+
removeIgnoredColumns(dbSourceFile) {
|
|
300
|
+
const dbInterface = this.findDbExport(dbSourceFile, 'DB');
|
|
301
|
+
if (!dbInterface)
|
|
302
|
+
return dbSourceFile;
|
|
303
|
+
// the DB interface maps table names to table interfaces (e.g.
|
|
304
|
+
// `collars: Collars`), so it provides the table name each table
|
|
305
|
+
// interface corresponds to
|
|
306
|
+
const ignoredColumnsByInterfaceName = {};
|
|
307
|
+
dbInterface.members.forEach(member => {
|
|
308
|
+
if (!ts.isPropertySignature(member))
|
|
309
|
+
return;
|
|
310
|
+
if (!member.type || !ts.isTypeReferenceNode(member.type))
|
|
311
|
+
return;
|
|
312
|
+
if (!ts.isIdentifier(member.type.typeName))
|
|
313
|
+
return;
|
|
314
|
+
const tableName = ts.isIdentifier(member.name)
|
|
315
|
+
? member.name.text
|
|
316
|
+
: ts.isStringLiteral(member.name)
|
|
317
|
+
? member.name.text
|
|
318
|
+
: null;
|
|
319
|
+
if (!tableName)
|
|
320
|
+
return;
|
|
321
|
+
const ignoredColumns = this.ignoredColumnsForTable(tableName);
|
|
322
|
+
if (ignoredColumns.size)
|
|
323
|
+
ignoredColumnsByInterfaceName[member.type.typeName.text] = ignoredColumns;
|
|
324
|
+
});
|
|
325
|
+
if (!Object.keys(ignoredColumnsByInterfaceName).length)
|
|
326
|
+
return dbSourceFile;
|
|
327
|
+
// @ts-expect-error cannot lock this type down, though implementation is correct
|
|
328
|
+
const transformer = context => {
|
|
329
|
+
const visit = node => {
|
|
330
|
+
const interfaceNode = this.exportedInterfaceOrNull(node);
|
|
331
|
+
const ignoredColumns = interfaceNode
|
|
332
|
+
? ignoredColumnsByInterfaceName[interfaceNode.name.text]
|
|
333
|
+
: undefined;
|
|
334
|
+
if (interfaceNode && ignoredColumns) {
|
|
335
|
+
return f.updateInterfaceDeclaration(interfaceNode, interfaceNode.modifiers, interfaceNode.name, interfaceNode.typeParameters, interfaceNode.heritageClauses, interfaceNode.members.filter(member => !(ts.isPropertySignature(member) &&
|
|
336
|
+
(ts.isIdentifier(member.name) || ts.isStringLiteral(member.name)) &&
|
|
337
|
+
ignoredColumns.has(member.name.text))));
|
|
338
|
+
}
|
|
339
|
+
return ts.visitEachChild(node, visit, context);
|
|
340
|
+
};
|
|
341
|
+
return node => ts.visitNode(node, visit);
|
|
342
|
+
};
|
|
343
|
+
const result = ts.transform(dbSourceFile, [transformer]);
|
|
344
|
+
return result.transformed[0];
|
|
345
|
+
}
|
|
286
346
|
/**
|
|
287
347
|
* @internal
|
|
288
348
|
*
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import scopeArray from '../../decorators/field/sortable/helpers/scopeArray.js';
|
|
2
|
+
import CannotIgnoreAssociationColumn from '../../errors/schema-builder/CannotIgnoreAssociationColumn.js';
|
|
3
|
+
import CannotIgnoreEncryptedColumn from '../../errors/schema-builder/CannotIgnoreEncryptedColumn.js';
|
|
4
|
+
import CannotIgnorePrimaryKey from '../../errors/schema-builder/CannotIgnorePrimaryKey.js';
|
|
5
|
+
import CannotIgnoreSoftDeleteColumn from '../../errors/schema-builder/CannotIgnoreSoftDeleteColumn.js';
|
|
6
|
+
import CannotIgnoreSortablePositionColumn from '../../errors/schema-builder/CannotIgnoreSortablePositionColumn.js';
|
|
7
|
+
import CannotIgnoreSortableScopeColumn from '../../errors/schema-builder/CannotIgnoreSortableScopeColumn.js';
|
|
8
|
+
import CannotIgnoreStiTypeColumn from '../../errors/schema-builder/CannotIgnoreStiTypeColumn.js';
|
|
9
|
+
import ConflictingIgnoredColumns from '../../errors/schema-builder/ConflictingIgnoredColumns.js';
|
|
10
|
+
import IgnoredColumnMustBeCamelCase from '../../errors/schema-builder/IgnoredColumnMustBeCamelCase.js';
|
|
11
|
+
import camelize from '../camelize.js';
|
|
12
|
+
import uniq from '../uniq.js';
|
|
13
|
+
/**
|
|
14
|
+
* @internal
|
|
15
|
+
*
|
|
16
|
+
* Resolves the set of ignored columns for a table from the `ignoredColumns`
|
|
17
|
+
* declarations of every model backed by that table, validating the
|
|
18
|
+
* declarations along the way. Called while `sync` builds the generated types
|
|
19
|
+
* files (the declarations have no runtime behavior; see the `ignoredColumns`
|
|
20
|
+
* getter on Dream), so each of these guards fails the sync command loudly
|
|
21
|
+
* rather than surfacing as broken behavior at runtime:
|
|
22
|
+
*
|
|
23
|
+
* - ignored columns must be declared in camelCase, since generated column
|
|
24
|
+
* names are camelized, so any other shape can never match a generated
|
|
25
|
+
* column and would be silently inert
|
|
26
|
+
* - a model may never ignore its primary key
|
|
27
|
+
* - an STI model may never ignore the STI "type" column
|
|
28
|
+
* - a model may never ignore an @Sortable position field or a plain-column
|
|
29
|
+
* @Sortable scope, the backing column of an @Encrypted property, or (on a
|
|
30
|
+
* SoftDelete model) its deletedAtField — the framework reads and writes
|
|
31
|
+
* those columns by name
|
|
32
|
+
* - models sharing a table must agree on their ignored columns, since there
|
|
33
|
+
* is only one generated schema per table (STI children inherit the base
|
|
34
|
+
* model's getter, so agreement is automatic unless a child overrides it)
|
|
35
|
+
* - no association anywhere in the app may name an ignored column as the
|
|
36
|
+
* foreign key (or polymorphic type field) it reads and writes on this
|
|
37
|
+
* table — a BelongsTo on a model backed by this table names a foreign key
|
|
38
|
+
* on this table, and so does a HasMany/HasOne on any other model that
|
|
39
|
+
* points at a model backed by this table
|
|
40
|
+
* - no association anywhere in the app may name an ignored column as its
|
|
41
|
+
* primaryKeyOverride — the column the association's foreign key points
|
|
42
|
+
* at, which lives on the associated model's table for a BelongsTo and on
|
|
43
|
+
* the declaring model's own table for a HasMany/HasOne
|
|
44
|
+
*
|
|
45
|
+
* @param models - every model backed by the table
|
|
46
|
+
* @param tableName - the table whose ignored columns are being resolved
|
|
47
|
+
* @param allModels - every model in the app (on the table's connection);
|
|
48
|
+
* required because associations declared on other models can name foreign
|
|
49
|
+
* keys on this table
|
|
50
|
+
* @returns the set of column names to omit from the table's generated types
|
|
51
|
+
*/
|
|
52
|
+
export default function resolveIgnoredColumns(models, tableName, allModels) {
|
|
53
|
+
models.forEach(modelClass => {
|
|
54
|
+
const ignoredColumns = modelClass.prototype.ignoredColumns;
|
|
55
|
+
ignoredColumns.forEach(columnName => {
|
|
56
|
+
if (camelize(columnName) !== columnName)
|
|
57
|
+
throw new IgnoredColumnMustBeCamelCase(modelClass, columnName);
|
|
58
|
+
});
|
|
59
|
+
if (ignoredColumns.includes(modelClass.primaryKey))
|
|
60
|
+
throw new CannotIgnorePrimaryKey(modelClass);
|
|
61
|
+
if (ignoredColumns.includes('type') && (modelClass['isSTIBase'] || modelClass['isSTIChild']))
|
|
62
|
+
throw new CannotIgnoreStiTypeColumn(modelClass);
|
|
63
|
+
modelClass['sortableFields'].forEach(sortableFieldConfig => {
|
|
64
|
+
if (ignoredColumns.includes(sortableFieldConfig.positionField))
|
|
65
|
+
throw new CannotIgnoreSortablePositionColumn(modelClass, sortableFieldConfig.positionField);
|
|
66
|
+
// a Sortable scope entry names either a column or a BelongsTo
|
|
67
|
+
// association on the model (getColumnForSortableScope resolves columns
|
|
68
|
+
// first, then falls back to association metadata), so an ignored scope
|
|
69
|
+
// entry only breaks @Sortable when no association answers to the name
|
|
70
|
+
scopeArray(sortableFieldConfig.scope).forEach(scopeEntry => {
|
|
71
|
+
if (ignoredColumns.includes(scopeEntry) &&
|
|
72
|
+
modelClass['associationMetadataMap']()[scopeEntry] === undefined)
|
|
73
|
+
throw new CannotIgnoreSortableScopeColumn(modelClass, scopeEntry, sortableFieldConfig.positionField);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
// the Encrypted decorator records each backing column it manages in
|
|
77
|
+
// explicitUnsafeParamColumns (which nothing else populates)
|
|
78
|
+
modelClass['explicitUnsafeParamColumns'].forEach(columnName => {
|
|
79
|
+
if (ignoredColumns.includes(columnName))
|
|
80
|
+
throw new CannotIgnoreEncryptedColumn(modelClass, columnName);
|
|
81
|
+
});
|
|
82
|
+
if (modelClass['softDelete']) {
|
|
83
|
+
const deletedAtField = modelClass.prototype['_deletedAtField'];
|
|
84
|
+
if (ignoredColumns.includes(deletedAtField))
|
|
85
|
+
throw new CannotIgnoreSoftDeleteColumn(modelClass, deletedAtField);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
const distinctDeclarations = uniq(models.map(modelClass => JSON.stringify(uniq([...modelClass.prototype.ignoredColumns]).sort())));
|
|
89
|
+
if (distinctDeclarations.length > 1)
|
|
90
|
+
throw new ConflictingIgnoredColumns(tableName, models);
|
|
91
|
+
const ignoredColumns = new Set(models.flatMap(modelClass => [...modelClass.prototype.ignoredColumns]));
|
|
92
|
+
if (ignoredColumns.size)
|
|
93
|
+
guardAssociationColumns(ignoredColumns, tableName, allModels);
|
|
94
|
+
return ignoredColumns;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* @internal
|
|
98
|
+
*
|
|
99
|
+
* fails the sync when an ignored column of the table is the foreign key (or
|
|
100
|
+
* polymorphic type field) that any association in the app reads and writes
|
|
101
|
+
* on this table, or the primaryKeyOverride column that any association's
|
|
102
|
+
* foreign key points at on this table. A BelongsTo association names a
|
|
103
|
+
* foreign key on the declaring model's own table and a primaryKeyOverride
|
|
104
|
+
* on the associated model's table; a HasMany/HasOne association names a
|
|
105
|
+
* foreign key on the associated model's table and a primaryKeyOverride on
|
|
106
|
+
* the declaring model's own table — so the load-bearing association may be
|
|
107
|
+
* declared on any model in the app, not just the models backed by this
|
|
108
|
+
* table.
|
|
109
|
+
*/
|
|
110
|
+
function guardAssociationColumns(ignoredColumns, tableName, allModels) {
|
|
111
|
+
for (const modelClass of allModels) {
|
|
112
|
+
for (const associationName of modelClass.associationNames) {
|
|
113
|
+
const associationMetaData = modelClass['associationMetadataMap']()[associationName];
|
|
114
|
+
if (associationMetaData === undefined)
|
|
115
|
+
continue;
|
|
116
|
+
// a through association names no foreign key of its own; the source
|
|
117
|
+
// association it travels through is guarded directly
|
|
118
|
+
if (associationMetaData.through)
|
|
119
|
+
continue;
|
|
120
|
+
// unlike foreignKey(), primaryKeyOverride is a plain string on the
|
|
121
|
+
// association statement, so it can be guarded without consulting the
|
|
122
|
+
// generated schema — before the foreignKey-specific short-circuits below
|
|
123
|
+
const primaryKeyOverride = associationMetaData.primaryKeyOverride;
|
|
124
|
+
if (primaryKeyOverride &&
|
|
125
|
+
ignoredColumns.has(primaryKeyOverride) &&
|
|
126
|
+
primaryKeyOverrideTableForAssociationMatches(associationMetaData, modelClass, tableName))
|
|
127
|
+
throw new CannotIgnoreAssociationColumn(tableName, primaryKeyOverride, modelClass, associationMetaData, 'primary key override');
|
|
128
|
+
if (!foreignKeyTableForAssociationMatches(associationMetaData, modelClass, tableName))
|
|
129
|
+
continue;
|
|
130
|
+
// NOTE
|
|
131
|
+
// this try-catch mirrors getAssociationData in ASTConnectionBuilder:
|
|
132
|
+
// computing foreignKey() introspects columns via the generated schema
|
|
133
|
+
// file, so it may throw — on the first sync pass because the schema
|
|
134
|
+
// file has not been regenerated yet, and on the second pass because
|
|
135
|
+
// the regenerated schema omits the ignored column. So in orderings
|
|
136
|
+
// where foreignKey() throws on the first pass, it throws on the second
|
|
137
|
+
// pass too, and these guards never see the association's foreign key;
|
|
138
|
+
// the InvalidComputedForeignKey / ExplicitForeignKeyRequired errors
|
|
139
|
+
// swallowed here resurface at runtime as the backstop.
|
|
140
|
+
let foreignKey = null;
|
|
141
|
+
let foreignKeyTypeColumn = null;
|
|
142
|
+
try {
|
|
143
|
+
foreignKey = associationMetaData.foreignKey();
|
|
144
|
+
foreignKeyTypeColumn = associationMetaData.polymorphic
|
|
145
|
+
? associationMetaData.foreignKeyTypeField()
|
|
146
|
+
: null;
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
if (ignoredColumns.has(foreignKey))
|
|
152
|
+
throw new CannotIgnoreAssociationColumn(tableName, foreignKey, modelClass, associationMetaData, 'foreign key');
|
|
153
|
+
if (foreignKeyTypeColumn && ignoredColumns.has(foreignKeyTypeColumn))
|
|
154
|
+
throw new CannotIgnoreAssociationColumn(tableName, foreignKeyTypeColumn, modelClass, associationMetaData, 'polymorphic type field');
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* @internal
|
|
160
|
+
*
|
|
161
|
+
* returns whether the given association's foreign key physically lives on
|
|
162
|
+
* the given table: on the declaring model's own table for a BelongsTo, and
|
|
163
|
+
* on the associated model's table for a HasMany/HasOne
|
|
164
|
+
*/
|
|
165
|
+
function foreignKeyTableForAssociationMatches(associationMetaData, modelClass, tableName) {
|
|
166
|
+
if (associationMetaData.type === 'BelongsTo')
|
|
167
|
+
return modelClass.table === tableName;
|
|
168
|
+
const dreamClassOrClasses = associationMetaData.modelCB();
|
|
169
|
+
// a missing associated class raises FailedToIdentifyAssociation while the
|
|
170
|
+
// builder gathers association data; it is not this guard's concern
|
|
171
|
+
if (!dreamClassOrClasses)
|
|
172
|
+
return false;
|
|
173
|
+
return Array.isArray(dreamClassOrClasses)
|
|
174
|
+
? dreamClassOrClasses.some(dreamClass => dreamClass.table === tableName)
|
|
175
|
+
: dreamClassOrClasses.table === tableName;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* @internal
|
|
179
|
+
*
|
|
180
|
+
* returns whether the given association's primaryKeyOverride column
|
|
181
|
+
* physically lives on the given table — the opposite side from the foreign
|
|
182
|
+
* key (see associationPrimaryKeyAccessors in
|
|
183
|
+
* decorators/field/association/shared.ts): on the associated model's table
|
|
184
|
+
* for a BelongsTo, and on the declaring model's own table for a
|
|
185
|
+
* HasMany/HasOne
|
|
186
|
+
*/
|
|
187
|
+
function primaryKeyOverrideTableForAssociationMatches(associationMetaData, modelClass, tableName) {
|
|
188
|
+
if (associationMetaData.type !== 'BelongsTo')
|
|
189
|
+
return modelClass.table === tableName;
|
|
190
|
+
const dreamClassOrClasses = associationMetaData.modelCB();
|
|
191
|
+
// a missing associated class raises FailedToIdentifyAssociation while the
|
|
192
|
+
// builder gathers association data; it is not this guard's concern
|
|
193
|
+
if (!dreamClassOrClasses)
|
|
194
|
+
return false;
|
|
195
|
+
return Array.isArray(dreamClassOrClasses)
|
|
196
|
+
? dreamClassOrClasses.some(dreamClass => dreamClass.table === tableName)
|
|
197
|
+
: dreamClassOrClasses.table === tableName;
|
|
198
|
+
}
|
package/dist/esm/src/Dream.js
CHANGED
|
@@ -1461,6 +1461,12 @@ export default class Dream {
|
|
|
1461
1461
|
* 3. each nested association will result in an additional record which duplicates data from the outer record. E.g., given `.leftJoinPreload('a', 'b', 'c')`, if each `a` has 10 `b` and each `b` has 10 `c`, then for one `a`, 100 records will be returned, each of which has all of the columns of `a`. `.preload('a', 'b', 'c')` would perform three separate SQL queries, but the data for a single `a` would only be returned once.
|
|
1462
1462
|
* 4. the individual query becomes more complex the more associations are included
|
|
1463
1463
|
* 5. associations loading associations loading associations could result in exponential amounts of data; in those cases, `.preload(...).findEach(...)` avoids instantiating massive amounts of data at once
|
|
1464
|
+
* 6. leftJoinPreload must enumerate every compiled column of every joined
|
|
1465
|
+
* model, so unlike base-model reads, `preload`/`load`, and saves, it does
|
|
1466
|
+
* not tolerate schema/image skew from an unplanned column drop during a
|
|
1467
|
+
* rolling deploy; see {@link Query.leftJoinPreload} and the
|
|
1468
|
+
* `ignoredColumns` getter for the two-deploy process that makes a
|
|
1469
|
+
* planned drop safe.
|
|
1464
1470
|
*
|
|
1465
1471
|
* ```ts
|
|
1466
1472
|
* const user = await User.leftJoinPreload('posts', 'comments', { visibilty: 'public' }, 'replies').first()
|
|
@@ -2340,6 +2346,83 @@ export default class Dream {
|
|
|
2340
2346
|
get table() {
|
|
2341
2347
|
throw new DreamMissingRequiredOverride(this.constructor, 'table');
|
|
2342
2348
|
}
|
|
2349
|
+
/**
|
|
2350
|
+
* Columns that Dream should behave as though they do not exist.
|
|
2351
|
+
*
|
|
2352
|
+
* Declaring a column ignored removes it from the generated types the next
|
|
2353
|
+
* time `sync` runs: it is omitted from both the db types file (the Kysely
|
|
2354
|
+
* `DB` interface) and the dream schema file, so it disappears from
|
|
2355
|
+
* `columns()` and from every place that flows from `columns()` — select
|
|
2356
|
+
* lists built for `preload`/`load` and `leftJoinPreload`, save hydration,
|
|
2357
|
+
* attribute definition, and param safety. References to the column in
|
|
2358
|
+
* application code become type errors, which is the point: they must be
|
|
2359
|
+
* removed before the column can be dropped.
|
|
2360
|
+
*
|
|
2361
|
+
* This enables safely dropping a column under rolling deploys — the same
|
|
2362
|
+
* problem Rails solves with `ignored_columns`. The safety requirement is
|
|
2363
|
+
* that no image that can run against the post-drop schema names the
|
|
2364
|
+
* column in any SQL it generates. To satisfy it: remove all application
|
|
2365
|
+
* code that uses the column, declare it here, and run `sync`; then let
|
|
2366
|
+
* the drop migration run only once no image lacking the declaration can
|
|
2367
|
+
* run against the database — whether the migration ships in a later
|
|
2368
|
+
* deploy of its own, or together with the declaration in a pipeline that
|
|
2369
|
+
* runs migrations only after the new images have rolled out. Once the
|
|
2370
|
+
* column is dropped, remove this declaration and resync.
|
|
2371
|
+
*
|
|
2372
|
+
* Precondition: as soon as an image built with this declaration runs,
|
|
2373
|
+
* the column is never placed in INSERT column lists, so before that
|
|
2374
|
+
* image can run, the column must be nullable or carry a database
|
|
2375
|
+
* default — a live `NOT NULL` column without a default fails every
|
|
2376
|
+
* create against the table.
|
|
2377
|
+
*
|
|
2378
|
+
* Dropping the column while an image that names it can still run leaves
|
|
2379
|
+
* a window during which those containers (including a rolled-back image)
|
|
2380
|
+
* fail with `42703 column does not exist` — `leftJoinPreload` in
|
|
2381
|
+
* particular has no runtime tolerance for this, since it must enumerate
|
|
2382
|
+
* aliased columns.
|
|
2383
|
+
*
|
|
2384
|
+
* This is a mechanism for the drop window, not for permanently hiding
|
|
2385
|
+
* wide columns: the ignored column is still transferred from the
|
|
2386
|
+
* database on every `RETURNING *` / `select *` until it is actually
|
|
2387
|
+
* dropped.
|
|
2388
|
+
*
|
|
2389
|
+
* The declaration is read only while `sync` generates the types files; it
|
|
2390
|
+
* has no runtime behavior of its own. A declared-but-not-synced model is
|
|
2391
|
+
* therefore not yet protected — CI should verify that `sync` produces no
|
|
2392
|
+
* diff. `sync` will fail loudly if a declared name is not camelCase
|
|
2393
|
+
* (generated column names are camelized, so any other shape could never
|
|
2394
|
+
* match and would be silently inert), if models sharing a table declare
|
|
2395
|
+
* different ignored columns, or if a model attempts to ignore a column
|
|
2396
|
+
* the framework itself reads and writes by name: its primary key, an STI
|
|
2397
|
+
* model's `type` column, any association's foreign key, polymorphic type
|
|
2398
|
+
* field, or `primaryKeyOverride` column, an `@Sortable` position field or
|
|
2399
|
+
* plain-column `@Sortable` scope, an `@Encrypted` backing column, or a
|
|
2400
|
+
* SoftDelete model's `deletedAt` column.
|
|
2401
|
+
*
|
|
2402
|
+
* Because an ignored column vanishes from `columns()`, runtime access via
|
|
2403
|
+
* type escape hatches behaves exactly like any unknown attribute: reads
|
|
2404
|
+
* return `undefined`, and writes assign a plain instance property that is
|
|
2405
|
+
* never persisted.
|
|
2406
|
+
*
|
|
2407
|
+
* ```ts
|
|
2408
|
+
* class User extends ApplicationModel {
|
|
2409
|
+
* public override get ignoredColumns() {
|
|
2410
|
+
* return ['legacyEmail'] as const
|
|
2411
|
+
* }
|
|
2412
|
+
* }
|
|
2413
|
+
* ```
|
|
2414
|
+
*
|
|
2415
|
+
* NOTE: this getter is intentionally typed as `readonly string[]` rather
|
|
2416
|
+
* than as a union of known column names: during the deploy that declares
|
|
2417
|
+
* a column ignored, the regenerated types no longer contain the column,
|
|
2418
|
+
* so a column-name-derived type would reject the very declaration that
|
|
2419
|
+
* removed it.
|
|
2420
|
+
*
|
|
2421
|
+
* @returns The list of column names this model should ignore
|
|
2422
|
+
*/
|
|
2423
|
+
get ignoredColumns() {
|
|
2424
|
+
return [];
|
|
2425
|
+
}
|
|
2343
2426
|
/**
|
|
2344
2427
|
* @internal
|
|
2345
2428
|
*
|
|
@@ -442,6 +442,17 @@ export default class Query {
|
|
|
442
442
|
* 3. each nested association will result in an additional record which duplicates data from the outer record. E.g., given `.leftJoinPreload('a', 'b', 'c')`, if each `a` has 10 `b` and each `b` has 10 `c`, then for one `a`, 100 records will be returned, each of which has all of the columns of `a`. `.preload('a', 'b', 'c')` would perform three separate SQL queries, but the data for a single `a` would only be returned once.
|
|
443
443
|
* 4. the individual query becomes more complex the more associations are included
|
|
444
444
|
* 5. associations loading associations loading associations could result in exponential amounts of data; in those cases, `.preload(...).findEach(...)` avoids instantiating massive amounts of data at once
|
|
445
|
+
* 6. unlike base-model reads, `preload`/`load`, and saves — which tolerate
|
|
446
|
+
* schema/image skew (e.g. a rolling deploy dropping a column while
|
|
447
|
+
* containers compiled against the previous schema are still draining) —
|
|
448
|
+
* leftJoinPreload must enumerate every compiled column of every joined
|
|
449
|
+
* model under per-alias names (per-alias `*` is not expressible in a
|
|
450
|
+
* single flat row), so an **unplanned** column drop breaks
|
|
451
|
+
* leftJoinPreload queries for the duration of the rollout window. A
|
|
452
|
+
* **planned** drop is safe when performed via the two-deploy process
|
|
453
|
+
* documented on the `ignoredColumns` getter of Dream: declaring the
|
|
454
|
+
* column ignored removes it from the generated schema, so leftJoinPreload
|
|
455
|
+
* stops naming it a full deploy before the column is actually dropped.
|
|
445
456
|
*
|
|
446
457
|
*
|
|
447
458
|
* ```ts
|
|
@@ -734,6 +734,27 @@ export default class KyselyQueryDriver extends QueryDriverBase {
|
|
|
734
734
|
* is provided as a second argument, it will use that transaction
|
|
735
735
|
* to encapsulate the persisting of the dream, as well as any
|
|
736
736
|
* subsequent model hooks that are fired.
|
|
737
|
+
*
|
|
738
|
+
* `RETURNING *` (rather than enumerating the compiled column list) keeps
|
|
739
|
+
* writes working under schema/image skew: during a rolling deploy, a
|
|
740
|
+
* container built before a drop-column migration would otherwise name the
|
|
741
|
+
* dropped column in `RETURNING` and fail with `42703 column does not
|
|
742
|
+
* exist` on every write, even writes that never touch that column. The
|
|
743
|
+
* `SET`/`VALUES` half still names only dirty attributes, so a write that
|
|
744
|
+
* actually sets a dropped column still fails loudly. The returned row is
|
|
745
|
+
* filtered to the compiled column list before hydration (see
|
|
746
|
+
* internal/saveDream.ts), so a column the image doesn't know about never
|
|
747
|
+
* reaches `setAttributes`.
|
|
748
|
+
*
|
|
749
|
+
* KNOWN CONSTRAINT: star-selects are only safe because nothing in this
|
|
750
|
+
* stack uses named prepared statements — node-postgres prepares a
|
|
751
|
+
* statement only when given an explicit `name`, and Kysely never names
|
|
752
|
+
* them, so every query is re-planned. With named prepared statements, a
|
|
753
|
+
* concurrent `ADD COLUMN` changes a cached plan's result shape and
|
|
754
|
+
* Postgres raises `cached plan must not change result type` (the reason
|
|
755
|
+
* Rails added `enumerate_columns_in_select_statements`). If a future
|
|
756
|
+
* driver or pooling layer enables named prepared statements, revisit
|
|
757
|
+
* every `RETURNING *` / `select *` in this driver.
|
|
737
758
|
*/
|
|
738
759
|
static async saveDream(dream, txn = null) {
|
|
739
760
|
const connectionName = dream.connectionName || 'default';
|
|
@@ -745,13 +766,13 @@ export default class KyselyQueryDriver extends QueryDriverBase {
|
|
|
745
766
|
.updateTable(dream.table)
|
|
746
767
|
.set(sqlifiedAttributes)
|
|
747
768
|
.where(namespaceColumn(dream['_primaryKey'], dream.table), '=', dream.primaryKeyValue());
|
|
748
|
-
return await executeDatabaseQuery(query.
|
|
769
|
+
return await executeDatabaseQuery(query.returningAll(), 'executeTakeFirstOrThrow');
|
|
749
770
|
}
|
|
750
771
|
else {
|
|
751
772
|
const query = db
|
|
752
773
|
.insertInto(dream.table)
|
|
753
774
|
.values(sqlifiedAttributes)
|
|
754
|
-
.
|
|
775
|
+
.returningAll();
|
|
755
776
|
return await executeDatabaseQuery(query, 'executeTakeFirstOrThrow');
|
|
756
777
|
}
|
|
757
778
|
}
|
|
@@ -2401,15 +2422,29 @@ export default class KyselyQueryDriver extends QueryDriverBase {
|
|
|
2401
2422
|
const preloadedPolymorphicBelongsTos = await this.preloadPolymorphicBelongsTo(association, dreamClassToHydrate, dreams);
|
|
2402
2423
|
return preloadedPolymorphicBelongsTos;
|
|
2403
2424
|
}
|
|
2404
|
-
const dreamClassToHydrateColumns =
|
|
2405
|
-
|
|
2406
|
-
|
|
2425
|
+
const dreamClassToHydrateColumns = dreamClassToHydrate.columns();
|
|
2426
|
+
// The base-model primary key rides along under a short alias so each
|
|
2427
|
+
// associated row can be matched back to the dream it belongs to. The
|
|
2428
|
+
// alias is lowercase without underscores so Kysely's CamelCasePlugin
|
|
2429
|
+
// passes it through untouched (the same strategy as `pluck0` /
|
|
2430
|
+
// `groupvalue`), and is extended until it cannot collide with a real
|
|
2431
|
+
// column of the associated table. That collision loop can only consult
|
|
2432
|
+
// the compiled column set, so under add-column skew a live column
|
|
2433
|
+
// literally named like the alias would produce two identically-named
|
|
2434
|
+
// result fields. The aliased base PK is deliberately selected last,
|
|
2435
|
+
// and node-postgres resolves duplicate field names last-wins, so the
|
|
2436
|
+
// base PK still wins even in that pathological case — and the
|
|
2437
|
+
// hydration filter strips the colliding key from the instance's
|
|
2438
|
+
// attributes.
|
|
2439
|
+
let basePrimaryKeyAlias = 'preloadbasepk';
|
|
2440
|
+
while (dreamClassToHydrateColumns.has(basePrimaryKeyAlias))
|
|
2441
|
+
basePrimaryKeyAlias += '0';
|
|
2407
2442
|
const baseClass = dreamClass['stiBaseClassOrOwnClass']['getAssociationMetadata'](associationName)
|
|
2408
2443
|
? dreamClass['stiBaseClassOrOwnClass']
|
|
2409
2444
|
: dreamClass;
|
|
2410
2445
|
const associationDataScope = this.dreamClassQueryWithScopeBypasses(baseClass, {
|
|
2411
2446
|
// In order to stay DRY, preloading leverages the association logic built into
|
|
2412
|
-
// `joins
|
|
2447
|
+
// `joins`. However, baseClass may have
|
|
2413
2448
|
// default scopes that would preclude finding that instance. We remove all
|
|
2414
2449
|
// default scopes on baseClass, but not subsequent associations, so that the
|
|
2415
2450
|
// single query will be able to find each row corresponding to a Dream in `dreams`,
|
|
@@ -2418,16 +2453,29 @@ export default class KyselyQueryDriver extends QueryDriverBase {
|
|
|
2418
2453
|
}).where({
|
|
2419
2454
|
[dreamClass.primaryKey]: dreams.map(obj => obj.primaryKeyValue()),
|
|
2420
2455
|
});
|
|
2421
|
-
const
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2456
|
+
const joinedQuery = associationDataScope['_connection'](this.connectionOverride).innerJoin(associationName, (onStatement || {}));
|
|
2457
|
+
// Select the association's row wholesale (`"alias".*`) rather than
|
|
2458
|
+
// enumerating the compiled column list. Enumerating made every preload
|
|
2459
|
+
// assert the compiled schema against the live database: under
|
|
2460
|
+
// schema/image skew (rolling deploy or rollback around a drop-column
|
|
2461
|
+
// migration), naming a dropped column fails the whole preload with
|
|
2462
|
+
// `42703 column does not exist`. The raw row is passed straight to
|
|
2463
|
+
// hydration, where sqlResultToDreamInstance — the single filtering
|
|
2464
|
+
// point — intersects it with the compiled column list (also stripping
|
|
2465
|
+
// the base-PK alias key), so a column the image doesn't know about
|
|
2466
|
+
// (the add-column direction of skew) never reaches the instance. See
|
|
2467
|
+
// the prepared-statements note on `saveDream` before changing this
|
|
2468
|
+
// back to an enumerated select.
|
|
2469
|
+
const kyselyQuery = new this.constructor(joinedQuery)
|
|
2470
|
+
.buildSelect({ bypassSelectAll: true })
|
|
2471
|
+
.selectAll(alias)
|
|
2472
|
+
.select(`${this.namespaceColumn(dreamClass.primaryKey, dreamClass.table)} as ${basePrimaryKeyAlias}`);
|
|
2473
|
+
const rows = await executeDatabaseQuery(kyselyQuery, 'execute');
|
|
2474
|
+
const preloadedDreamsAndWhatTheyPointTo = rows.map((row) => {
|
|
2475
|
+
const hydratedDream = this.dbResultToDreamInstance(row, dreamClassToHydrate);
|
|
2428
2476
|
return {
|
|
2429
2477
|
dream: hydratedDream,
|
|
2430
|
-
pointsToPrimaryKey:
|
|
2478
|
+
pointsToPrimaryKey: row[basePrimaryKeyAlias],
|
|
2431
2479
|
};
|
|
2432
2480
|
});
|
|
2433
2481
|
this.hydrateAssociation(dreams, association, preloadedDreamsAndWhatTheyPointTo);
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import protectAgainstPollutingAssignment from '../../helpers/protectAgainstPollutingAssignment.js';
|
|
2
|
+
/**
|
|
3
|
+
* @internal
|
|
4
|
+
*
|
|
5
|
+
* Returns an object containing only the keys of `row` that are columns
|
|
6
|
+
* the compiled schema knows about (per the provided column set).
|
|
7
|
+
*
|
|
8
|
+
* Under schema/image skew (e.g. a rolling deploy in which a migration adds
|
|
9
|
+
* a column while containers built against the previous schema are still
|
|
10
|
+
* draining, or application code is rolled back after an add-column
|
|
11
|
+
* migration), a `RETURNING *` / `select *` row can include columns this
|
|
12
|
+
* build has never heard of. Passing such keys to `setAttributes` would
|
|
13
|
+
* assign them as plain properties — invoking a same-named user-defined
|
|
14
|
+
* setter, or throwing on a getter-only property — so they must be dropped
|
|
15
|
+
* before hydration.
|
|
16
|
+
*
|
|
17
|
+
* Keys are intersected rather than enumerated from the column set so that
|
|
18
|
+
* a column missing from the row (the dropped-column direction of skew)
|
|
19
|
+
* simply does not appear, rather than appearing with an `undefined` value.
|
|
20
|
+
*
|
|
21
|
+
* Identity fast path: when every key of `row` is a known column (the
|
|
22
|
+
* no-skew steady state), `row` itself is returned unchanged, without
|
|
23
|
+
* allocating a copy; a filtered copy is built only when at least one
|
|
24
|
+
* unknown key is present.
|
|
25
|
+
*
|
|
26
|
+
* @param row - a raw database row
|
|
27
|
+
* @param columns - the compiled column set for the Dream class being hydrated
|
|
28
|
+
* @returns `row` itself when every key is a known column; otherwise a new
|
|
29
|
+
* object containing only the known-column entries of `row`
|
|
30
|
+
*/
|
|
31
|
+
export default function filterRowToKnownColumns(row, columns) {
|
|
32
|
+
const keys = Object.keys(row);
|
|
33
|
+
if (keys.every(key => columns.has(key)))
|
|
34
|
+
return row;
|
|
35
|
+
const filtered = {};
|
|
36
|
+
keys.forEach(key => {
|
|
37
|
+
if (columns.has(key))
|
|
38
|
+
filtered[protectAgainstPollutingAssignment(key)] = row[key];
|
|
39
|
+
});
|
|
40
|
+
return filtered;
|
|
41
|
+
}
|
|
@@ -2,6 +2,7 @@ import ValidationError from '../../errors/ValidationError.js';
|
|
|
2
2
|
import sqlAttributes from '../../helpers/sqlAttributes.js';
|
|
3
3
|
import { DateTime } from '../../utils/datetime/DateTime.js';
|
|
4
4
|
import Query from '../Query.js';
|
|
5
|
+
import filterRowToKnownColumns from './filterRowToKnownColumns.js';
|
|
5
6
|
import runHooksFor from './runHooksFor.js';
|
|
6
7
|
export default async function saveDream(dream, txn = null, { skipHooks = false } = {}) {
|
|
7
8
|
const alreadyPersisted = dream.isPersisted;
|
|
@@ -30,7 +31,11 @@ export default async function saveDream(dream, txn = null, { skipHooks = false }
|
|
|
30
31
|
if (!alreadyPersisted || hasUnsavedData) {
|
|
31
32
|
const data = await Query.dbDriverClass(dream.connectionName || 'default').saveDream(dream, txn);
|
|
32
33
|
dream['isPersisted'] = true;
|
|
33
|
-
|
|
34
|
+
// the row returned by the driver comes from `RETURNING *` (or a
|
|
35
|
+
// `select *` reload on drivers without RETURNING support), so under
|
|
36
|
+
// schema/image skew it can contain columns this build's compiled
|
|
37
|
+
// schema doesn't know about; those must never reach setAttributes
|
|
38
|
+
dream.setAttributes(filterRowToKnownColumns(data, dream.columns()));
|
|
34
39
|
}
|
|
35
40
|
// set frozen attributes to what has already been saved
|
|
36
41
|
dream['freezeAttributes']();
|
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
import STIChildMissing from '../../errors/sti/STIChildMissing.js';
|
|
2
|
+
import filterRowToKnownColumns from './filterRowToKnownColumns.js';
|
|
2
3
|
export default function sqlResultToDreamInstance(dreamClass, sqlResult) {
|
|
4
|
+
// base-model reads select `*`, so under schema/image skew (or with a
|
|
5
|
+
// column declared in ignoredColumns but not yet dropped) the row can
|
|
6
|
+
// contain columns the compiled schema doesn't know about. Passing such
|
|
7
|
+
// keys into the constructor would assign them as plain properties —
|
|
8
|
+
// invoking a same-named user-defined setter, or throwing on a
|
|
9
|
+
// getter-only property — so they are dropped before hydration.
|
|
3
10
|
if (dreamClass['isSTIBase']) {
|
|
4
11
|
const extendingDreamClass = findExtendingDreamClass(dreamClass, sqlResult.type);
|
|
5
12
|
if (!extendingDreamClass)
|
|
6
13
|
throw new STIChildMissing(dreamClass, sqlResult.type, sqlResult[dreamClass.primaryKey]);
|
|
7
|
-
const dreamModel = new extendingDreamClass(sqlResult, {
|
|
14
|
+
const dreamModel = new extendingDreamClass(filterRowToKnownColumns(sqlResult, extendingDreamClass.columns()), {
|
|
8
15
|
bypassUserDefinedSetters: true,
|
|
9
16
|
isPersisted: true,
|
|
10
17
|
_internalUseOnly: true,
|
|
@@ -13,7 +20,7 @@ export default function sqlResultToDreamInstance(dreamClass, sqlResult) {
|
|
|
13
20
|
return dreamModel;
|
|
14
21
|
}
|
|
15
22
|
else {
|
|
16
|
-
const dreamModel = new dreamClass(sqlResult, {
|
|
23
|
+
const dreamModel = new dreamClass(filterRowToKnownColumns(sqlResult, dreamClass.columns()), {
|
|
17
24
|
bypassUserDefinedSetters: true,
|
|
18
25
|
isPersisted: true,
|
|
19
26
|
_internalUseOnly: true,
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export default class CannotIgnoreAssociationColumn extends Error {
|
|
2
|
+
tableName;
|
|
3
|
+
columnName;
|
|
4
|
+
modelClass;
|
|
5
|
+
association;
|
|
6
|
+
columnRole;
|
|
7
|
+
constructor(tableName, columnName, modelClass, association, columnRole) {
|
|
8
|
+
super();
|
|
9
|
+
this.tableName = tableName;
|
|
10
|
+
this.columnName = columnName;
|
|
11
|
+
this.modelClass = modelClass;
|
|
12
|
+
this.association = association;
|
|
13
|
+
this.columnRole = columnRole;
|
|
14
|
+
}
|
|
15
|
+
get message() {
|
|
16
|
+
return `
|
|
17
|
+
The models backed by the "${this.tableName}" table declare
|
|
18
|
+
"${this.columnName}" in ignoredColumns, but "${this.columnName}" is the
|
|
19
|
+
${this.columnRole} of the ${this.association.type} association
|
|
20
|
+
"${this.association.as}" on ${this.modelClass.sanitizedName}.
|
|
21
|
+
|
|
22
|
+
Associations read and write their columns by name, so ignoring
|
|
23
|
+
"${this.columnName}" would silently break "${this.association.as}" at
|
|
24
|
+
runtime: ${this.consequence}. Remove the
|
|
25
|
+
"${this.association.as}" association from ${this.modelClass.sanitizedName}
|
|
26
|
+
(or point it at a different column) before ignoring "${this.columnName}".
|
|
27
|
+
`;
|
|
28
|
+
}
|
|
29
|
+
get consequence() {
|
|
30
|
+
if (this.columnRole === 'primary key override')
|
|
31
|
+
return `associating a record would read "${this.columnName}" from an
|
|
32
|
+
instance that no longer hydrates it, silently persisting an empty foreign
|
|
33
|
+
key, and association loads would join on a column missing from the
|
|
34
|
+
generated schema`;
|
|
35
|
+
return `writes to the column would stop persisting, and association loads
|
|
36
|
+
would reference a column missing from the generated schema`;
|
|
37
|
+
}
|
|
38
|
+
}
|