@rvoh/dream 1.0.4 → 1.1.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 +2 -3
- package/dist/cjs/src/bin/helpers/sync.js +3 -1
- package/dist/cjs/src/cli/CliFileWriter.js +39 -0
- package/dist/cjs/src/cli/logger/DreamCliLogger.js +5 -0
- package/dist/cjs/src/decorators/field/association/BelongsTo.js +3 -0
- package/dist/cjs/src/decorators/field/association/HasMany.js +1 -3
- package/dist/cjs/src/decorators/field/association/HasOne.js +1 -3
- package/dist/cjs/src/decorators/field/association/shared.js +2 -2
- package/dist/cjs/src/dream/QueryDriver/Kysely.js +385 -332
- package/dist/cjs/src/dream/internal/{extractAssociationMetadataFromAssociationName.js → associationStringToNameAndAlias.js} +3 -3
- package/dist/cjs/src/dream/internal/associations/throughAssociationHasOptionsBesidesThroughAndSource.js +14 -0
- package/dist/cjs/src/dream/internal/unaliasTableName.js +2 -2
- package/dist/cjs/src/errors/UnexpectedUndefined.js +1 -3
- package/dist/cjs/src/errors/associations/ArrayTargetIncompatibleWithThroughAssociation.js +19 -0
- package/dist/cjs/src/errors/associations/ArrayTargetOnlyOnPolymorphicBelongsTo.js +19 -0
- package/dist/cjs/src/errors/associations/JoinAttemptedOnMissingAssociation.js +1 -1
- package/dist/cjs/src/errors/associations/MissingThroughAssociationSource.js +1 -1
- package/dist/cjs/src/errors/associations/ThroughAssociationConditionsIncompatibleWithThroughAssociationSource.js +20 -0
- package/dist/cjs/src/helpers/cli/SchemaBuilder.js +2 -1
- package/dist/cjs/src/index.js +4 -2
- package/dist/esm/src/Dream.js +2 -3
- package/dist/esm/src/bin/helpers/sync.js +3 -1
- package/dist/esm/src/cli/CliFileWriter.js +35 -0
- package/dist/esm/src/cli/logger/DreamCliLogger.js +5 -0
- package/dist/esm/src/decorators/field/association/BelongsTo.js +3 -0
- package/dist/esm/src/decorators/field/association/HasMany.js +1 -3
- package/dist/esm/src/decorators/field/association/HasOne.js +1 -3
- package/dist/esm/src/decorators/field/association/shared.js +2 -2
- package/dist/esm/src/dream/QueryDriver/Kysely.js +385 -332
- package/dist/esm/src/dream/internal/{extractAssociationMetadataFromAssociationName.js → associationStringToNameAndAlias.js} +2 -2
- package/dist/esm/src/dream/internal/associations/throughAssociationHasOptionsBesidesThroughAndSource.js +11 -0
- package/dist/esm/src/dream/internal/unaliasTableName.js +2 -2
- package/dist/esm/src/errors/UnexpectedUndefined.js +1 -3
- package/dist/esm/src/errors/associations/ArrayTargetIncompatibleWithThroughAssociation.js +16 -0
- package/dist/esm/src/errors/associations/ArrayTargetOnlyOnPolymorphicBelongsTo.js +16 -0
- package/dist/esm/src/errors/associations/JoinAttemptedOnMissingAssociation.js +1 -1
- package/dist/esm/src/errors/associations/MissingThroughAssociationSource.js +1 -1
- package/dist/esm/src/errors/associations/ThroughAssociationConditionsIncompatibleWithThroughAssociationSource.js +17 -0
- package/dist/esm/src/helpers/cli/SchemaBuilder.js +2 -1
- package/dist/esm/src/index.js +1 -0
- package/dist/types/src/Dream.d.ts +0 -1
- package/dist/types/src/cli/CliFileWriter.d.ts +9 -0
- package/dist/types/src/cli/logger/DreamCliLogger.d.ts +1 -0
- package/dist/types/src/decorators/Decorators.d.ts +6 -6
- package/dist/types/src/decorators/field/association/HasMany.d.ts +3 -3
- package/dist/types/src/decorators/field/association/HasOne.d.ts +3 -3
- package/dist/types/src/dream/QueryDriver/Kysely.d.ts +55 -27
- package/dist/types/src/dream/internal/associationStringToNameAndAlias.d.ts +4 -0
- package/dist/types/src/dream/internal/associations/throughAssociationHasOptionsBesidesThroughAndSource.d.ts +11 -0
- package/dist/types/src/errors/associations/ArrayTargetIncompatibleWithThroughAssociation.d.ts +12 -0
- package/dist/types/src/errors/associations/ArrayTargetOnlyOnPolymorphicBelongsTo.d.ts +10 -0
- package/dist/types/src/errors/associations/ThroughAssociationConditionsIncompatibleWithThroughAssociationSource.d.ts +12 -0
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/types/associations/belongsTo.d.ts +3 -1
- package/dist/types/src/types/associations/belongsTo.ts +4 -1
- package/dist/types/src/types/associations/hasMany.d.ts +3 -3
- package/dist/types/src/types/associations/hasMany.ts +7 -3
- package/dist/types/src/types/associations/hasOne.d.ts +3 -3
- package/dist/types/src/types/associations/hasOne.ts +6 -3
- package/dist/types/src/types/associations/shared.d.ts +8 -10
- package/dist/types/src/types/associations/shared.ts +26 -18
- 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/CliFileWriter.html +8 -0
- package/docs/classes/CreateOrFindByFailedToCreateAndFind.html +3 -3
- package/docs/classes/Decorators.html +19 -19
- package/docs/classes/Dream.html +136 -136
- package/docs/classes/DreamApp.html +4 -4
- package/docs/classes/DreamBin.html +2 -2
- package/docs/classes/DreamCLI.html +4 -4
- package/docs/classes/DreamImporter.html +2 -2
- package/docs/classes/DreamLogos.html +2 -2
- package/docs/classes/DreamMigrationHelpers.html +7 -7
- package/docs/classes/DreamSerializerBuilder.html +8 -8
- package/docs/classes/DreamTransaction.html +2 -2
- package/docs/classes/Encrypt.html +2 -2
- package/docs/classes/Env.html +2 -2
- package/docs/classes/GlobalNameNotSet.html +3 -3
- package/docs/classes/NonLoadedAssociation.html +3 -3
- package/docs/classes/ObjectSerializerBuilder.html +8 -8
- package/docs/classes/Query.html +60 -60
- 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/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 +1 -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/DreamTableSchema.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/IdType.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/PrimaryKeyType.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/Timestamp.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 +2 -2
- package/CHANGELOG.md +0 -15
- package/dist/types/src/dream/internal/extractAssociationMetadataFromAssociationName.d.ts +0 -4
package/dist/cjs/src/Dream.js
CHANGED
|
@@ -15,10 +15,10 @@ const associationUpdateQuery_js_1 = require("./dream/internal/associations/assoc
|
|
|
15
15
|
const createAssociation_js_1 = require("./dream/internal/associations/createAssociation.js");
|
|
16
16
|
const destroyAssociation_js_1 = require("./dream/internal/associations/destroyAssociation.js");
|
|
17
17
|
const undestroyAssociation_js_1 = require("./dream/internal/associations/undestroyAssociation.js");
|
|
18
|
+
const associationStringToNameAndAlias_js_1 = require("./dream/internal/associationStringToNameAndAlias.js");
|
|
18
19
|
const destroyDream_js_1 = require("./dream/internal/destroyDream.js");
|
|
19
20
|
const destroyOptions_js_1 = require("./dream/internal/destroyOptions.js");
|
|
20
21
|
const ensureSTITypeFieldIsSet_js_1 = require("./dream/internal/ensureSTITypeFieldIsSet.js");
|
|
21
|
-
const extractAssociationMetadataFromAssociationName_js_1 = require("./dream/internal/extractAssociationMetadataFromAssociationName.js");
|
|
22
22
|
const findOrCreateBy_js_1 = require("./dream/internal/findOrCreateBy.js");
|
|
23
23
|
const reload_js_1 = require("./dream/internal/reload.js");
|
|
24
24
|
const runValidations_js_1 = require("./dream/internal/runValidations.js");
|
|
@@ -636,7 +636,7 @@ class Dream {
|
|
|
636
636
|
* @returns Association metadata for the requested association
|
|
637
637
|
*/
|
|
638
638
|
static getAssociationMetadata(associationName) {
|
|
639
|
-
return this.associationMetadataMap()[(0,
|
|
639
|
+
return this.associationMetadataMap()[(0, associationStringToNameAndAlias_js_1.default)(associationName).name];
|
|
640
640
|
}
|
|
641
641
|
/**
|
|
642
642
|
* @internal
|
|
@@ -1812,7 +1812,6 @@ class Dream {
|
|
|
1812
1812
|
* // as: 'ratings',
|
|
1813
1813
|
* // polymorphic: true,
|
|
1814
1814
|
* // source: 'ratings',
|
|
1815
|
-
* // preloadThroughColumns: undefined,
|
|
1816
1815
|
* // where: undefined,
|
|
1817
1816
|
* // whereNot: undefined,
|
|
1818
1817
|
* // selfWhere: undefined,
|
|
@@ -12,11 +12,13 @@ const EnvInternal_js_1 = require("../../helpers/EnvInternal.js");
|
|
|
12
12
|
const dreamPath_js_1 = require("../../helpers/path/dreamPath.js");
|
|
13
13
|
const snakeify_js_1 = require("../../helpers/snakeify.js");
|
|
14
14
|
const sspawn_js_1 = require("../../helpers/sspawn.js");
|
|
15
|
+
const CliFileWriter_js_1 = require("../../cli/CliFileWriter.js");
|
|
15
16
|
async function writeSyncFile() {
|
|
16
17
|
const dreamApp = index_js_2.default.getOrFail();
|
|
17
18
|
const dbConf = dreamApp.dbConnectionConfig('primary');
|
|
18
19
|
const dbSyncFilePath = path.join((0, dreamPath_js_1.default)('types'), 'db.ts');
|
|
19
20
|
const absoluteDbSyncPath = path.join(dreamApp.projectRoot, dbSyncFilePath);
|
|
21
|
+
await CliFileWriter_js_1.CliFileWriter.cache(absoluteDbSyncPath);
|
|
20
22
|
await (0, sspawn_js_1.default)(`kysely-codegen --dialect=postgres --url=postgres://${dbConf.user}:${dbConf.password}@${dbConf.host}:${dbConf.port}/${dbConf.name} --out-file=${absoluteDbSyncPath}`, {
|
|
21
23
|
onStdout: message => {
|
|
22
24
|
index_js_1.default.logger.logContinueProgress((0, colorize_js_1.default)(`[db]`, { color: 'cyan' }) + ' ' + message, {
|
|
@@ -28,7 +30,7 @@ async function writeSyncFile() {
|
|
|
28
30
|
// from the dist folder, whereas dirname here is pointing to true src folder.
|
|
29
31
|
const file = (await fs.readFile(absoluteDbSyncPath)).toString();
|
|
30
32
|
const enhancedSchema = enhanceSchema(file);
|
|
31
|
-
await
|
|
33
|
+
await CliFileWriter_js_1.CliFileWriter.write(absoluteDbSyncPath, enhancedSchema);
|
|
32
34
|
}
|
|
33
35
|
// begin: schema helpers
|
|
34
36
|
function enhanceSchema(file) {
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CliFileWriter = void 0;
|
|
4
|
+
const fs = require("node:fs/promises");
|
|
5
|
+
const index_js_1 = require("./index.js");
|
|
6
|
+
class CliFileWriter {
|
|
7
|
+
static async write(filepath, contents) {
|
|
8
|
+
await cliFileWriter.write(filepath, contents);
|
|
9
|
+
}
|
|
10
|
+
static async revert() {
|
|
11
|
+
await cliFileWriter.revert();
|
|
12
|
+
}
|
|
13
|
+
static async cache(filepath) {
|
|
14
|
+
await cliFileWriter.cache(filepath);
|
|
15
|
+
}
|
|
16
|
+
fileCache = {};
|
|
17
|
+
async write(filepath, contents) {
|
|
18
|
+
// if we have manually backed up this file, or else this file
|
|
19
|
+
// has been written to twice in one CLI session, we want
|
|
20
|
+
// to preserve the original backup, so we do not attempt
|
|
21
|
+
// to cache a second time
|
|
22
|
+
if (!this.fileCache[filepath])
|
|
23
|
+
await this.cache(filepath);
|
|
24
|
+
await fs.writeFile(filepath, contents);
|
|
25
|
+
}
|
|
26
|
+
async revert() {
|
|
27
|
+
const filepaths = Object.keys(this.fileCache);
|
|
28
|
+
for (const filepath of filepaths) {
|
|
29
|
+
index_js_1.default.logger.logContinueProgress(`reverting ${filepath}`);
|
|
30
|
+
await fs.writeFile(filepath, this.fileCache[filepath]);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
async cache(filepath) {
|
|
34
|
+
const originalContents = (await fs.readFile(filepath)).toString();
|
|
35
|
+
this.fileCache[filepath] = originalContents;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.CliFileWriter = CliFileWriter;
|
|
39
|
+
const cliFileWriter = new CliFileWriter();
|
|
@@ -10,6 +10,11 @@ class DreamCliLogger {
|
|
|
10
10
|
});
|
|
11
11
|
loggable.render();
|
|
12
12
|
}
|
|
13
|
+
async logProgress(text, cb, { logPrefix = '✺ ┌', logPrefixColor, logPrefixBgColor } = {}) {
|
|
14
|
+
this.log(text, { logPrefix, logPrefixColor, logPrefixBgColor });
|
|
15
|
+
await cb();
|
|
16
|
+
this.logEndProgress();
|
|
17
|
+
}
|
|
13
18
|
logStartProgress(text, { logPrefix = '✺ ┌', logPrefixColor, logPrefixBgColor } = {}) {
|
|
14
19
|
this.log(text, { logPrefix, logPrefixColor, logPrefixBgColor });
|
|
15
20
|
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.default = BelongsTo;
|
|
4
4
|
const lookupModelByGlobalNameOrNames_js_1 = require("../../../dream-app/helpers/lookupModelByGlobalNameOrNames.js");
|
|
5
|
+
const ArrayTargetOnlyOnPolymorphicBelongsTo_js_1 = require("../../../errors/associations/ArrayTargetOnlyOnPolymorphicBelongsTo.js");
|
|
5
6
|
const Validates_js_1 = require("../validation/Validates.js");
|
|
6
7
|
const shared_js_1 = require("./shared.js");
|
|
7
8
|
/**
|
|
@@ -47,6 +48,8 @@ function BelongsTo(globalAssociationNameOrNames, opts = {}) {
|
|
|
47
48
|
delete this[key];
|
|
48
49
|
return;
|
|
49
50
|
}
|
|
51
|
+
if (Array.isArray(globalAssociationNameOrNames) && !polymorphic)
|
|
52
|
+
throw new ArrayTargetOnlyOnPolymorphicBelongsTo_js_1.default({ dreamClass, associationName: key });
|
|
50
53
|
const partialAssociation = (0, shared_js_1.associationPrimaryKeyAccessors)({
|
|
51
54
|
modelCB: () => (0, lookupModelByGlobalNameOrNames_js_1.default)(globalAssociationNameOrNames),
|
|
52
55
|
globalAssociationNameOrNames,
|
|
@@ -29,7 +29,6 @@ const shared_js_1 = require("./shared.js");
|
|
|
29
29
|
* @param opts.andAny - An andAny clause to be applied when this association is loaded
|
|
30
30
|
* @param opts.order - A custom order statement to apply to this association.
|
|
31
31
|
* @param opts.polymorphic - If true, this association will be treated as a polymorphic association.
|
|
32
|
-
* @param opts.preloadThroughColumns - An array of columns to pluck off the through association attached to this association. Can only be set if `through` is also set.
|
|
33
32
|
* @param opts.primaryKeyOverride - A custom column name to use for the primary key.
|
|
34
33
|
* @param opts.selfAnd - Adds an and-clause to an association between a column on the associated model and a column on this model.
|
|
35
34
|
* @param opts.selfAndNot - Adds a not and-clause to an association between a column on the associated model and a column on this model.
|
|
@@ -38,7 +37,7 @@ const shared_js_1 = require("./shared.js");
|
|
|
38
37
|
* @param opts.withoutDefaultScopes - A list of default scopes to bypass when loading this association
|
|
39
38
|
*/
|
|
40
39
|
function HasMany(globalAssociationNameOrNames, opts = {}) {
|
|
41
|
-
const { dependent, distinct, foreignKey, and, andNot, andAny, order, polymorphic = false,
|
|
40
|
+
const { dependent, distinct, foreignKey, and, andNot, andAny, order, polymorphic = false, primaryKeyOverride = null, selfAnd, selfAndNot, source, through, withoutDefaultScopes, } = opts;
|
|
42
41
|
return function (_, context) {
|
|
43
42
|
const key = context.name;
|
|
44
43
|
context.addInitializer(function () {
|
|
@@ -71,7 +70,6 @@ function HasMany(globalAssociationNameOrNames, opts = {}) {
|
|
|
71
70
|
andNot,
|
|
72
71
|
andAny,
|
|
73
72
|
polymorphic,
|
|
74
|
-
preloadThroughColumns,
|
|
75
73
|
primaryKeyOverride,
|
|
76
74
|
selfAnd,
|
|
77
75
|
selfAndNot,
|
|
@@ -27,7 +27,6 @@ const shared_js_1 = require("./shared.js");
|
|
|
27
27
|
* @param opts.andNot - A not and-clause to be applied when this association is loaded
|
|
28
28
|
* @param opts.andAny - An andAny clause to be applied when this association is loaded
|
|
29
29
|
* @param opts.polymorphic - If true, this association will be treated as a polymorphic association.
|
|
30
|
-
* @param opts.preloadThroughColumns - An array of columns to pluck off the through association attached to this association. Can only be set if `through` is also set.
|
|
31
30
|
* @param opts.primaryKeyOverride - A custom column name to use for the primary key.
|
|
32
31
|
* @param opts.selfAnd - Adds an and-clause to an association between a column on the associated model and a column on this model.
|
|
33
32
|
* @param opts.selfAndNot - Adds a not and-clause to an association between a column on the associated model and a column on this model.
|
|
@@ -36,7 +35,7 @@ const shared_js_1 = require("./shared.js");
|
|
|
36
35
|
* @param opts.withoutDefaultScopes - A list of default scopes to bypass when loading this association
|
|
37
36
|
*/
|
|
38
37
|
function HasOne(globalAssociationNameOrNames, opts = {}) {
|
|
39
|
-
const { dependent, foreignKey, and, andNot, andAny, polymorphic = false,
|
|
38
|
+
const { dependent, foreignKey, and, andNot, andAny, polymorphic = false, primaryKeyOverride = null, selfAnd, selfAndNot, source, through, withoutDefaultScopes, } = opts;
|
|
40
39
|
return function (_, context) {
|
|
41
40
|
const key = context.name;
|
|
42
41
|
context.addInitializer(function () {
|
|
@@ -69,7 +68,6 @@ function HasOne(globalAssociationNameOrNames, opts = {}) {
|
|
|
69
68
|
andNot,
|
|
70
69
|
andAny,
|
|
71
70
|
polymorphic,
|
|
72
|
-
preloadThroughColumns,
|
|
73
71
|
primaryKeyOverride,
|
|
74
72
|
selfAnd,
|
|
75
73
|
selfAndNot,
|
|
@@ -98,12 +98,12 @@ function applyGetterAndSetter(target, partialAssociation, { foreignKeyBase, isBe
|
|
|
98
98
|
function associationPrimaryKeyAccessors(partialAssociation, dreamClass) {
|
|
99
99
|
return {
|
|
100
100
|
...partialAssociation,
|
|
101
|
-
primaryKey(associationInstance) {
|
|
101
|
+
primaryKey(associationInstance, { associatedClassOverride } = {}) {
|
|
102
102
|
if (this.primaryKeyOverride)
|
|
103
103
|
return this.primaryKeyOverride;
|
|
104
104
|
if (associationInstance)
|
|
105
105
|
return associationInstance.primaryKey;
|
|
106
|
-
const associationClass = this.modelCB();
|
|
106
|
+
const associationClass = associatedClassOverride ?? this.modelCB();
|
|
107
107
|
if (Array.isArray(associationClass)) {
|
|
108
108
|
throw new Error(`
|
|
109
109
|
Cannot lookup primaryKey on polymorphic association:
|