@rvoh/dream 1.2.1 → 1.3.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.
Files changed (198) hide show
  1. package/dist/cjs/src/Dream.js +99 -31
  2. package/dist/cjs/src/dream/QueryDriver/Kysely.js +20 -19
  3. package/dist/cjs/src/dream/internal/printSerializerHierarchyLevel.js +17 -0
  4. package/dist/cjs/src/dream-app/cache.js +12 -2
  5. package/dist/cjs/src/helpers/cli/generateMigration.js +1 -0
  6. package/dist/cjs/src/helpers/cli/generateMigrationContent.js +20 -5
  7. package/dist/cjs/src/helpers/cli/generateStiMigrationContent.js +8 -2
  8. package/dist/cjs/src/serializer/helpers/serializerForAssociatedClass.js +3 -2
  9. package/dist/esm/src/Dream.js +99 -31
  10. package/dist/esm/src/dream/QueryDriver/Kysely.js +20 -19
  11. package/dist/esm/src/dream/internal/printSerializerHierarchyLevel.js +14 -0
  12. package/dist/esm/src/dream-app/cache.js +12 -2
  13. package/dist/esm/src/helpers/cli/generateMigration.js +1 -0
  14. package/dist/esm/src/helpers/cli/generateMigrationContent.js +20 -5
  15. package/dist/esm/src/helpers/cli/generateStiMigrationContent.js +8 -2
  16. package/dist/esm/src/serializer/helpers/serializerForAssociatedClass.js +3 -2
  17. package/dist/types/src/Dream.d.ts +77 -52
  18. package/dist/types/src/dream/QueryDriver/Kysely.d.ts +1 -0
  19. package/dist/types/src/dream/internal/printSerializerHierarchyLevel.d.ts +7 -0
  20. package/dist/types/src/helpers/cli/generateMigrationContent.d.ts +2 -1
  21. package/dist/types/src/helpers/cli/generateStiMigrationContent.d.ts +2 -1
  22. package/dist/types/src/serializer/builders/DreamSerializerBuilder.d.ts +1 -1
  23. package/dist/types/src/types/dream.d.ts +1 -1
  24. package/dist/types/src/types/dream.ts +2 -2
  25. package/docs/assets/search.js +1 -1
  26. package/docs/classes/Benchmark.html +2 -2
  27. package/docs/classes/CalendarDate.html +2 -2
  28. package/docs/classes/CliFileWriter.html +2 -2
  29. package/docs/classes/CreateOrFindByFailedToCreateAndFind.html +3 -3
  30. package/docs/classes/DataTypeColumnTypeMismatch.html +3 -3
  31. package/docs/classes/Decorators.html +19 -19
  32. package/docs/classes/Dream.html +209 -308
  33. package/docs/classes/DreamApp.html +4 -4
  34. package/docs/classes/DreamBin.html +2 -2
  35. package/docs/classes/DreamCLI.html +4 -4
  36. package/docs/classes/DreamImporter.html +2 -2
  37. package/docs/classes/DreamLogos.html +2 -2
  38. package/docs/classes/DreamMigrationHelpers.html +7 -7
  39. package/docs/classes/DreamSerializerBuilder.html +8 -8
  40. package/docs/classes/DreamTransaction.html +2 -2
  41. package/docs/classes/Encrypt.html +2 -2
  42. package/docs/classes/Env.html +2 -2
  43. package/docs/classes/GlobalNameNotSet.html +3 -3
  44. package/docs/classes/NonLoadedAssociation.html +3 -3
  45. package/docs/classes/ObjectSerializerBuilder.html +8 -8
  46. package/docs/classes/Query.html +60 -60
  47. package/docs/classes/Range.html +2 -2
  48. package/docs/classes/RecordNotFound.html +3 -3
  49. package/docs/classes/ValidationError.html +3 -3
  50. package/docs/functions/DreamSerializer.html +1 -1
  51. package/docs/functions/ObjectSerializer.html +1 -1
  52. package/docs/functions/ReplicaSafe.html +1 -1
  53. package/docs/functions/STI.html +1 -1
  54. package/docs/functions/SoftDelete.html +1 -1
  55. package/docs/functions/camelize.html +1 -1
  56. package/docs/functions/capitalize.html +1 -1
  57. package/docs/functions/cloneDeepSafe.html +1 -1
  58. package/docs/functions/closeAllDbConnections.html +1 -1
  59. package/docs/functions/compact.html +1 -1
  60. package/docs/functions/dreamDbConnections.html +1 -1
  61. package/docs/functions/dreamPath.html +1 -1
  62. package/docs/functions/expandStiClasses.html +1 -1
  63. package/docs/functions/generateDream.html +1 -1
  64. package/docs/functions/globalClassNameFromFullyQualifiedModelName.html +1 -1
  65. package/docs/functions/groupBy.html +1 -1
  66. package/docs/functions/hyphenize.html +1 -1
  67. package/docs/functions/inferSerializerFromDreamOrViewModel.html +1 -1
  68. package/docs/functions/inferSerializersFromDreamClassOrViewModelClass.html +1 -1
  69. package/docs/functions/intersection.html +1 -1
  70. package/docs/functions/isDreamSerializer.html +1 -1
  71. package/docs/functions/isEmpty.html +1 -1
  72. package/docs/functions/loadRepl.html +1 -1
  73. package/docs/functions/lookupClassByGlobalName.html +1 -1
  74. package/docs/functions/normalizeUnicode.html +1 -1
  75. package/docs/functions/pascalize.html +1 -1
  76. package/docs/functions/pgErrorType.html +1 -1
  77. package/docs/functions/range-1.html +1 -1
  78. package/docs/functions/relativeDreamPath.html +1 -1
  79. package/docs/functions/round.html +1 -1
  80. package/docs/functions/serializerNameFromFullyQualifiedModelName.html +1 -1
  81. package/docs/functions/sharedPathPrefix.html +1 -1
  82. package/docs/functions/snakeify.html +1 -1
  83. package/docs/functions/sort.html +1 -1
  84. package/docs/functions/sortBy.html +1 -1
  85. package/docs/functions/sortObjectByKey.html +1 -1
  86. package/docs/functions/sortObjectByValue.html +1 -1
  87. package/docs/functions/standardizeFullyQualifiedModelName.html +1 -1
  88. package/docs/functions/uncapitalize.html +1 -1
  89. package/docs/functions/uniq.html +1 -1
  90. package/docs/functions/untypedDb.html +1 -1
  91. package/docs/functions/validateColumn.html +1 -1
  92. package/docs/functions/validateTable.html +1 -1
  93. package/docs/interfaces/BelongsToStatement.html +2 -2
  94. package/docs/interfaces/DecoratorContext.html +2 -2
  95. package/docs/interfaces/DreamAppInitOptions.html +2 -2
  96. package/docs/interfaces/DreamAppOpts.html +2 -2
  97. package/docs/interfaces/EncryptOptions.html +2 -2
  98. package/docs/interfaces/InternalAnyTypedSerializerRendersMany.html +2 -2
  99. package/docs/interfaces/InternalAnyTypedSerializerRendersOne.html +2 -2
  100. package/docs/interfaces/OpenapiDescription.html +2 -2
  101. package/docs/interfaces/OpenapiSchemaProperties.html +1 -1
  102. package/docs/interfaces/OpenapiSchemaPropertiesShorthand.html +1 -1
  103. package/docs/interfaces/OpenapiTypeFieldObject.html +1 -1
  104. package/docs/interfaces/SerializerRendererOpts.html +2 -2
  105. package/docs/types/Camelized.html +1 -1
  106. package/docs/types/CommonOpenapiSchemaObjectFields.html +1 -1
  107. package/docs/types/DateTime.html +1 -1
  108. package/docs/types/DbConnectionType.html +1 -1
  109. package/docs/types/DbTypes.html +1 -1
  110. package/docs/types/DreamAppAllowedPackageManagersEnum.html +1 -1
  111. package/docs/types/DreamAssociationMetadata.html +1 -1
  112. package/docs/types/DreamAttributes.html +1 -1
  113. package/docs/types/DreamClassAssociationAndStatement.html +1 -1
  114. package/docs/types/DreamClassColumn.html +1 -1
  115. package/docs/types/DreamColumn.html +1 -1
  116. package/docs/types/DreamColumnNames.html +1 -1
  117. package/docs/types/DreamLogLevel.html +1 -1
  118. package/docs/types/DreamLogger.html +1 -1
  119. package/docs/types/DreamModelSerializerType.html +1 -1
  120. package/docs/types/DreamOrViewModelClassSerializerKey.html +1 -1
  121. package/docs/types/DreamOrViewModelSerializerKey.html +1 -1
  122. package/docs/types/DreamParamSafeAttributes.html +1 -1
  123. package/docs/types/DreamParamSafeColumnNames.html +1 -1
  124. package/docs/types/DreamSerializable.html +1 -1
  125. package/docs/types/DreamSerializableArray.html +1 -1
  126. package/docs/types/DreamSerializerKey.html +1 -1
  127. package/docs/types/DreamSerializers.html +1 -1
  128. package/docs/types/DreamTableSchema.html +1 -1
  129. package/docs/types/DreamVirtualColumns.html +1 -1
  130. package/docs/types/EncryptAlgorithm.html +1 -1
  131. package/docs/types/HasManyStatement.html +1 -1
  132. package/docs/types/HasOneStatement.html +1 -1
  133. package/docs/types/Hyphenized.html +1 -1
  134. package/docs/types/IdType.html +1 -1
  135. package/docs/types/OpenapiAllTypes.html +1 -1
  136. package/docs/types/OpenapiFormats.html +1 -1
  137. package/docs/types/OpenapiNumberFormats.html +1 -1
  138. package/docs/types/OpenapiPrimitiveBaseTypes.html +1 -1
  139. package/docs/types/OpenapiPrimitiveTypes.html +1 -1
  140. package/docs/types/OpenapiSchemaArray.html +1 -1
  141. package/docs/types/OpenapiSchemaArrayShorthand.html +1 -1
  142. package/docs/types/OpenapiSchemaBase.html +1 -1
  143. package/docs/types/OpenapiSchemaBody.html +1 -1
  144. package/docs/types/OpenapiSchemaBodyShorthand.html +1 -1
  145. package/docs/types/OpenapiSchemaCommonFields.html +1 -1
  146. package/docs/types/OpenapiSchemaExpressionAllOf.html +1 -1
  147. package/docs/types/OpenapiSchemaExpressionAnyOf.html +1 -1
  148. package/docs/types/OpenapiSchemaExpressionOneOf.html +1 -1
  149. package/docs/types/OpenapiSchemaExpressionRef.html +1 -1
  150. package/docs/types/OpenapiSchemaExpressionRefSchemaShorthand.html +1 -1
  151. package/docs/types/OpenapiSchemaInteger.html +1 -1
  152. package/docs/types/OpenapiSchemaNull.html +1 -1
  153. package/docs/types/OpenapiSchemaNumber.html +1 -1
  154. package/docs/types/OpenapiSchemaObject.html +1 -1
  155. package/docs/types/OpenapiSchemaObjectAllOf.html +1 -1
  156. package/docs/types/OpenapiSchemaObjectAllOfShorthand.html +1 -1
  157. package/docs/types/OpenapiSchemaObjectAnyOf.html +1 -1
  158. package/docs/types/OpenapiSchemaObjectAnyOfShorthand.html +1 -1
  159. package/docs/types/OpenapiSchemaObjectBase.html +1 -1
  160. package/docs/types/OpenapiSchemaObjectBaseShorthand.html +1 -1
  161. package/docs/types/OpenapiSchemaObjectOneOf.html +1 -1
  162. package/docs/types/OpenapiSchemaObjectOneOfShorthand.html +1 -1
  163. package/docs/types/OpenapiSchemaObjectShorthand.html +1 -1
  164. package/docs/types/OpenapiSchemaPrimitiveGeneric.html +1 -1
  165. package/docs/types/OpenapiSchemaShorthandExpressionAllOf.html +1 -1
  166. package/docs/types/OpenapiSchemaShorthandExpressionAnyOf.html +1 -1
  167. package/docs/types/OpenapiSchemaShorthandExpressionOneOf.html +1 -1
  168. package/docs/types/OpenapiSchemaShorthandExpressionSerializableRef.html +1 -1
  169. package/docs/types/OpenapiSchemaShorthandExpressionSerializerRef.html +1 -1
  170. package/docs/types/OpenapiSchemaShorthandPrimitiveGeneric.html +1 -1
  171. package/docs/types/OpenapiSchemaString.html +1 -1
  172. package/docs/types/OpenapiShorthandAllTypes.html +1 -1
  173. package/docs/types/OpenapiShorthandPrimitiveBaseTypes.html +1 -1
  174. package/docs/types/OpenapiShorthandPrimitiveTypes.html +1 -1
  175. package/docs/types/OpenapiTypeField.html +1 -1
  176. package/docs/types/Pascalized.html +1 -1
  177. package/docs/types/PrimaryKeyType.html +1 -1
  178. package/docs/types/RoundingPrecision.html +1 -1
  179. package/docs/types/SerializerCasing.html +1 -1
  180. package/docs/types/SimpleObjectSerializerType.html +1 -1
  181. package/docs/types/Snakeified.html +1 -1
  182. package/docs/types/Timestamp.html +1 -1
  183. package/docs/types/UpdateableAssociationProperties.html +1 -1
  184. package/docs/types/UpdateableProperties.html +1 -1
  185. package/docs/types/ValidationType.html +1 -1
  186. package/docs/types/ViewModel.html +1 -1
  187. package/docs/types/ViewModelClass.html +1 -1
  188. package/docs/types/WhereStatementForDream.html +1 -1
  189. package/docs/types/WhereStatementForDreamClass.html +1 -1
  190. package/docs/variables/DateTime-1.html +1 -1
  191. package/docs/variables/DreamAppAllowedPackageManagersEnumValues.html +1 -1
  192. package/docs/variables/DreamConst.html +1 -1
  193. package/docs/variables/TRIGRAM_OPERATORS.html +1 -1
  194. package/docs/variables/openapiPrimitiveTypes-1.html +1 -1
  195. package/docs/variables/openapiShorthandPrimitiveTypes-1.html +1 -1
  196. package/docs/variables/ops.html +1 -1
  197. package/docs/variables/primaryKeyTypes.html +1 -1
  198. package/package.json +1 -1
@@ -21,6 +21,7 @@ const destroyDream_js_1 = require("./dream/internal/destroyDream.js");
21
21
  const destroyOptions_js_1 = require("./dream/internal/destroyOptions.js");
22
22
  const ensureSTITypeFieldIsSet_js_1 = require("./dream/internal/ensureSTITypeFieldIsSet.js");
23
23
  const findOrCreateBy_js_1 = require("./dream/internal/findOrCreateBy.js");
24
+ const printSerializerHierarchyLevel_js_1 = require("./dream/internal/printSerializerHierarchyLevel.js");
24
25
  const reload_js_1 = require("./dream/internal/reload.js");
25
26
  const runValidations_js_1 = require("./dream/internal/runValidations.js");
26
27
  const saveDream_js_1 = require("./dream/internal/saveDream.js");
@@ -49,10 +50,8 @@ const cloneDeepSafe_js_1 = require("./helpers/cloneDeepSafe.js");
49
50
  const DateTime_js_1 = require("./helpers/DateTime.js");
50
51
  const cachedTypeForAttribute_js_1 = require("./helpers/db/cachedTypeForAttribute.js");
51
52
  const isJsonColumn_js_1 = require("./helpers/db/types/isJsonColumn.js");
52
- const indent_js_1 = require("./helpers/indent.js");
53
53
  const notEqual_js_1 = require("./helpers/notEqual.js");
54
54
  const inferSerializerFromDreamOrViewModel_js_1 = require("./serializer/helpers/inferSerializerFromDreamOrViewModel.js");
55
- const serializerForAssociatedClass_js_1 = require("./serializer/helpers/serializerForAssociatedClass.js");
56
55
  class Dream {
57
56
  DB;
58
57
  /**
@@ -489,42 +488,65 @@ class Dream {
489
488
  static recursiveSerializationMap(serializer, { forDisplay = false, forDisplayDepth = 0, } = {}) {
490
489
  const serializerBuilder = serializer(undefined, undefined);
491
490
  const serializerAssociations = serializerBuilder['attributes'].filter(attribute => ['rendersOne', 'rendersMany', 'delegatedAttribute'].includes(attribute.type));
492
- return serializerAssociations.reduce((acc, serializerAssociation) => {
491
+ return serializerAssociations.reduce((accumulator, serializerAssociation) => {
493
492
  const serializerAssociationName = serializerAssociation.targetName ??
494
493
  serializerAssociation.name;
495
494
  const serializerAssociationType = serializerAssociation.type;
496
495
  const association = this['getAssociationMetadata'](serializerAssociationName);
497
496
  if (!association)
498
- return acc;
499
- const associatedClasses = association.modelCB();
500
- const associatedClass = Array.isArray(associatedClasses) ? associatedClasses[0] : associatedClasses;
501
- if (!associatedClass)
497
+ return accumulator;
498
+ const maybeAssociatedClasses = association.modelCB();
499
+ if (!maybeAssociatedClasses)
502
500
  throw new Error(`No class defined on ${serializerAssociationName} association on ${this.sanitizedName}`);
503
- const associationSerializer = (0, serializerForAssociatedClass_js_1.serializerForAssociatedClass)(this.prototype, serializerAssociationName, serializerAssociation.options);
504
- if (!associationSerializer)
505
- throw new Error(`No serializer found to render ${serializerAssociationName} on ${this.sanitizedName}`);
506
- if (forDisplay && serializerAssociationType !== 'delegatedAttribute') {
507
- const hierarchyLine = '└───';
508
- const indentation = (0, indent_js_1.indent)((hierarchyLine.length + 1) * forDisplayDepth, {
509
- tabWidth: 1,
510
- });
511
- const prefix = `${hierarchyLine} `;
512
- const nestedAssociationDisplay = indentation + `${prefix}${serializerAssociationType} ${yoctocolors_1.default.cyan(serializerAssociationName)}`;
513
- console.log(nestedAssociationDisplay);
514
- console.log(yoctocolors_1.default.gray(indentation +
515
- (0, indent_js_1.indent)(prefix.length, { tabWidth: 1 }) +
516
- associationSerializer.globalName));
517
- }
518
- acc[association.as] = {
519
- parentDreamClass: this,
520
- nestedSerializerInfo: serializerAssociation.type === 'delegatedAttribute'
521
- ? {}
522
- : associatedClass['recursiveSerializationMap'](associationSerializer, {
501
+ const associatedClasses = Array.isArray(maybeAssociatedClasses)
502
+ ? maybeAssociatedClasses
503
+ : [maybeAssociatedClasses];
504
+ /////////////////////////////////////////////////
505
+ // map associated classes to their serializers //
506
+ /////////////////////////////////////////////////
507
+ const associatedClassSerializerTuples = associatedClasses.flatMap(associatedClass => {
508
+ /**
509
+ * `serializers` is an array because `associatedClass` may be an STI
510
+ * base, with each of its STI children having its own serializer
511
+ */
512
+ const serializers = serializerAssociation.options.serializer
513
+ ? [serializerAssociation.options.serializer]
514
+ : (0, inferSerializerFromDreamOrViewModel_js_1.inferSerializersFromDreamClassOrViewModelClass)(associatedClass, serializerAssociation.options.serializerKey);
515
+ if (!serializers.length)
516
+ throw new Error(`No serializer found to render ${serializerAssociationName} on ${this.sanitizedName}`);
517
+ return serializers.map(serializer => [associatedClass, serializer]);
518
+ });
519
+ /////////////////////////////////////////////////////
520
+ // end:map associated classes to their serializers //
521
+ /////////////////////////////////////////////////////
522
+ ///////////////////////////////////////////////////////////////////////////////////////////////////
523
+ // reduce over all associated serializers, recursively building out their associated serializers //
524
+ ///////////////////////////////////////////////////////////////////////////////////////////////////
525
+ const innerAssociationSerializerInfo = associatedClassSerializerTuples.reduce((innerAccumulator, [associatedClass, associatedSerializer]) => {
526
+ if (forDisplay && serializerAssociationType !== 'delegatedAttribute') {
527
+ (0, printSerializerHierarchyLevel_js_1.default)({
528
+ serializerAssociationType,
529
+ serializerAssociationName,
530
+ associationSerializer: associatedSerializer,
531
+ forDisplayDepth,
532
+ });
533
+ }
534
+ return {
535
+ ...innerAccumulator,
536
+ ...associatedClass['recursiveSerializationMap'](associatedSerializer, {
523
537
  forDisplay,
524
538
  forDisplayDepth: forDisplayDepth + 1,
525
539
  }),
540
+ };
541
+ }, {});
542
+ ///////////////////////////////////////////////////////////////////////////////////////////////////////
543
+ // end:reduce over all associated serializers, recursively building out their associated serializers //
544
+ ///////////////////////////////////////////////////////////////////////////////////////////////////////
545
+ accumulator[association.as] = {
546
+ parentDreamClass: this,
547
+ nestedSerializerInfo: serializerAssociation.type === 'delegatedAttribute' ? {} : innerAssociationSerializerInfo,
526
548
  };
527
- return acc;
549
+ return accumulator;
528
550
  }, {});
529
551
  }
530
552
  /**
@@ -659,11 +681,57 @@ class Dream {
659
681
  return map;
660
682
  }
661
683
  /**
662
- * @internal
684
+ * Checks whether the specified association has been defined on this Dream model.
685
+ *
686
+ * ```ts
687
+ * const user = User.new()
688
+ *
689
+ * user.hasAssociation('posts')
690
+ * // true (if User has a posts association defined)
691
+ *
692
+ * user.hasAssociation('nonExistentAssociation')
693
+ * // false
694
+ *
695
+ * // Useful for conditional association handling
696
+ * if (user.hasAssociation('posts')) {
697
+ * user = await user.load('posts').execute()
698
+ * }
699
+ * ```
700
+ *
701
+ * @param associationName - The name of the association to check for
702
+ * @returns `true` if the association exists on this model, `false` otherwise
703
+ */
704
+ hasAssociation(associationName) {
705
+ return !!(this.associationMetadataByType.belongsTo.find(association => association.as === associationName) ||
706
+ this.associationMetadataByType.hasOne.find(association => association.as === associationName) ||
707
+ this.associationMetadataByType.hasMany.find(association => association.as === associationName));
708
+ }
709
+ /**
710
+ * Returns all of the association names for this Dream class.
711
+ * This includes all BelongsTo, HasOne, and HasMany associations
712
+ * defined on the model.
663
713
  *
664
- * Returns all of the association names for this dream class
714
+ * This is useful for introspection, debugging, or dynamically working
715
+ * with all associations on a model.
716
+ *
717
+ * ```ts
718
+ * class User extends ApplicationModel {
719
+ * @deco.HasMany('Post')
720
+ * public posts: Post[]
721
+ *
722
+ * @deco.HasOne('Profile')
723
+ * public profile: Profile
724
+ *
725
+ * @deco.BelongsTo('Company')
726
+ * public company: Company
727
+ * }
728
+ *
729
+ * User.associationNames
730
+ * // ['posts', 'profile', 'company']
731
+ * })
732
+ * ```
665
733
  *
666
- * @returns All of the association names for this dream class
734
+ * @returns An array containing all association names defined on this Dream class
667
735
  */
668
736
  static get associationNames() {
669
737
  const allAssociations = [
@@ -33,6 +33,7 @@ const dropDb_js_1 = require("../../helpers/db/dropDb.js");
33
33
  const loadPgClient_js_1 = require("../../helpers/db/loadPgClient.js");
34
34
  const runMigration_js_1 = require("../../helpers/db/runMigration.js");
35
35
  const EnvInternal_js_1 = require("../../helpers/EnvInternal.js");
36
+ const groupBy_js_1 = require("../../helpers/groupBy.js");
36
37
  const isEmpty_js_1 = require("../../helpers/isEmpty.js");
37
38
  const namespaceColumn_js_1 = require("../../helpers/namespaceColumn.js");
38
39
  const normalizeUnicode_js_1 = require("../../helpers/normalizeUnicode.js");
@@ -1204,7 +1205,7 @@ class KyselyQueryDriver extends Base_js_1.default {
1204
1205
  const keys = Object.keys(preloadStatement);
1205
1206
  for (const key of keys) {
1206
1207
  const nestedDreams = await this.applyOnePreload(key, dream, this.applyablePreloadOnStatements(preloadOnStatements[key]));
1207
- if (nestedDreams) {
1208
+ if (nestedDreams.length) {
1208
1209
  await this.applyPreload(preloadStatement[key], preloadOnStatements[key], nestedDreams);
1209
1210
  }
1210
1211
  }
@@ -1699,18 +1700,10 @@ class KyselyQueryDriver extends Base_js_1.default {
1699
1700
  *
1700
1701
  * Used to preload polymorphic belongs to associations
1701
1702
  */
1702
- async preloadPolymorphicBelongsTo(association, dreams) {
1703
- if (!association.polymorphic)
1704
- throw new Error(`Association ${association.as} points to an array of models but is not designated polymorphic`);
1705
- if (association.type !== 'BelongsTo')
1706
- throw new Error(`Polymorphic association ${association.as} points to an array of models but is ${association.type}. Only BelongsTo associations may point to an array of models.`);
1707
- const nestedDreamsForNextRoundOfPreloading = [];
1708
- for (const associatedModel of association.modelCB()) {
1709
- await this.preloadPolymorphicAssociationModel(dreams, association, associatedModel, nestedDreamsForNextRoundOfPreloading);
1710
- }
1711
- return nestedDreamsForNextRoundOfPreloading;
1703
+ async preloadPolymorphicBelongsTo(association, associatedModels, dreams) {
1704
+ return (0, compact_js_1.default)(await Promise.all(associatedModels.map(associatedModel => this.preloadPolymorphicAssociationModel(dreams, association, associatedModel)))).flat();
1712
1705
  }
1713
- async preloadPolymorphicAssociationModel(dreams, association, associatedDreamClass, nestedDreamsForNextRoundOfPreloading) {
1706
+ async preloadPolymorphicAssociationModel(dreams, association, associatedDreamClass) {
1714
1707
  const relevantAssociatedModels = dreams.filter((dream) => {
1715
1708
  const field = association.foreignKeyTypeField();
1716
1709
  return dream[field] === associatedDreamClass['stiBaseClassOrOwnClassName'] || dream[field] === null;
@@ -1729,7 +1722,6 @@ class KyselyQueryDriver extends Base_js_1.default {
1729
1722
  [associatedDreamClass.primaryKey]: relevantAssociatedModels.map((dream) => dream[association.foreignKey()]),
1730
1723
  })
1731
1724
  .all();
1732
- loadedAssociations.forEach((loadedAssociation) => nestedDreamsForNextRoundOfPreloading.push(loadedAssociation));
1733
1725
  //////////////////////////////////////////////////////////////////////////////////////////////
1734
1726
  // Associate each loaded association with each dream based on primary key and foreign key type
1735
1727
  //////////////////////////////////////////////////////////////////////////////////////////////
@@ -1744,6 +1736,7 @@ class KyselyQueryDriver extends Base_js_1.default {
1744
1736
  ///////////////////////////////////////////////////////////////////////////////////////////////////
1745
1737
  // end: Associate each loaded association with each dream based on primary key and foreign key type
1746
1738
  ///////////////////////////////////////////////////////////////////////////////////////////////////
1739
+ return loadedAssociations;
1747
1740
  }
1748
1741
  }
1749
1742
  /**
@@ -1782,12 +1775,18 @@ class KyselyQueryDriver extends Base_js_1.default {
1782
1775
  *
1783
1776
  * Applies a preload statement
1784
1777
  */
1785
- async applyOnePreload(associationName, dreams, onStatement = {}) {
1778
+ async applyOnePreload(associationNameAndMaybeAlias, dreams, onStatement = {}) {
1786
1779
  if (!Array.isArray(dreams))
1787
1780
  dreams = [dreams];
1788
- const dream = dreams.find(dream => dream['getAssociationMetadata'](associationName));
1789
- if (!dream)
1790
- return;
1781
+ const { name: associationName } = (0, associationStringToNameAndAlias_js_1.default)(associationNameAndMaybeAlias);
1782
+ dreams = dreams.filter(dream => dream.hasAssociation(associationName));
1783
+ const groupedDreams = (0, groupBy_js_1.default)(dreams, dream => dream.sanitizedConstructorName);
1784
+ return (await Promise.all(Object.keys(groupedDreams).map(key => this._applyOnePreload(associationName, groupedDreams[key], onStatement)))).flat();
1785
+ }
1786
+ async _applyOnePreload(associationName, dreams, onStatement = {}) {
1787
+ if (!dreams.length)
1788
+ return [];
1789
+ const dream = dreams[0];
1791
1790
  const { name, alias: _alias } = (0, associationStringToNameAndAlias_js_1.default)(associationName);
1792
1791
  const alias = _alias || name;
1793
1792
  const association = dream['getAssociationMetadata'](name);
@@ -1795,8 +1794,10 @@ class KyselyQueryDriver extends Base_js_1.default {
1795
1794
  throw new UnexpectedUndefined_js_1.default();
1796
1795
  const dreamClass = dream.constructor;
1797
1796
  const dreamClassToHydrate = association.modelCB();
1798
- if ((association.polymorphic && association.type === 'BelongsTo') || Array.isArray(dreamClassToHydrate))
1799
- return this.preloadPolymorphicBelongsTo(association, dreams);
1797
+ if (Array.isArray(dreamClassToHydrate)) {
1798
+ const preloadedPolymorphicBelongsTos = await this.preloadPolymorphicBelongsTo(association, dreamClassToHydrate, dreams);
1799
+ return preloadedPolymorphicBelongsTos;
1800
+ }
1800
1801
  const dreamClassToHydrateColumns = [...dreamClassToHydrate.columns()];
1801
1802
  const columnsToPluck = dreamClassToHydrateColumns.map(column => this.namespaceColumn(column.toString(), alias));
1802
1803
  columnsToPluck.push(this.namespaceColumn(dreamClass.primaryKey, dreamClass.table));
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = printSerializerHierarchyLevel;
4
+ const yoctocolors_1 = require("yoctocolors");
5
+ const indent_js_1 = require("../../helpers/indent.js");
6
+ function printSerializerHierarchyLevel({ serializerAssociationType, serializerAssociationName, associationSerializer, forDisplayDepth, }) {
7
+ const hierarchyLine = '└───';
8
+ const indentation = (0, indent_js_1.indent)((hierarchyLine.length + 1) * forDisplayDepth, {
9
+ tabWidth: 1,
10
+ });
11
+ const prefix = `${hierarchyLine} `;
12
+ const nestedAssociationDisplay = indentation + `${prefix}${serializerAssociationType} ${yoctocolors_1.default.cyan(serializerAssociationName)}`;
13
+ console.log(nestedAssociationDisplay);
14
+ console.log(yoctocolors_1.default.gray(indentation +
15
+ (0, indent_js_1.indent)(prefix.length, { tabWidth: 1 }) +
16
+ associationSerializer.globalName));
17
+ }
@@ -2,12 +2,22 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.cacheDreamApp = cacheDreamApp;
4
4
  exports.getCachedDreamAppOrFail = getCachedDreamAppOrFail;
5
+ const EnvInternal_js_1 = require("../helpers/EnvInternal.js");
5
6
  let _dreamApp = undefined;
6
7
  function cacheDreamApp(dreamconf) {
7
8
  _dreamApp = dreamconf;
8
9
  }
9
10
  function getCachedDreamAppOrFail() {
10
- if (!_dreamApp)
11
- throw new Error('Must call `initializeDreamApp` or `initializePsychicApp` before loading cached DreamApp.getOrFail()');
11
+ if (!_dreamApp) {
12
+ const baseErrorMessage = 'Must call `initializeDreamApp` or `initializePsychicApp` before loading cached DreamApp.getOrFail().';
13
+ if (EnvInternal_js_1.default.isTest) {
14
+ throw new Error(`${baseErrorMessage}
15
+
16
+ Check for specs running directly in a \`describe\` or \`context\` block rather than nested within an \`it\` block.`);
17
+ }
18
+ else {
19
+ throw new Error(baseErrorMessage);
20
+ }
21
+ }
12
22
  return _dreamApp;
13
23
  }
@@ -22,6 +22,7 @@ async function generateMigration({ migrationName, columnsWithTypes, fullyQualifi
22
22
  table: (0, snakeify_js_1.default)((0, pluralize_esm_1.default)((0, pascalizePath_js_1.default)(fullyQualifiedParentName))),
23
23
  columnsWithTypes,
24
24
  primaryKeyType: (0, primaryKeyType_js_1.default)(),
25
+ stiChildClassName: (0, pascalizePath_js_1.default)(fullyQualifiedModelName),
25
26
  });
26
27
  }
27
28
  else if (fullyQualifiedModelName) {
@@ -9,13 +9,17 @@ const foreignKeyTypeFromPrimaryKey_js_1 = require("../db/foreignKeyTypeFromPrima
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', } = {}) {
12
+ function generateMigrationContent({ table, columnsWithTypes = [], primaryKeyType = 'bigserial', createOrAlter = 'create', stiChildClassName, } = {}) {
13
13
  const altering = createOrAlter === 'alter';
14
14
  let requireCitextExtension = false;
15
- const { columnDefs, columnDrops, indexDefs, indexDrops } = columnsWithTypes.reduce((acc, attribute) => {
15
+ const checkConstraints = [];
16
+ const { columnDefs, columnDrops, indexDefs, indexDrops } = columnsWithTypes.reduce((acc, attributeDeclaration) => {
16
17
  const { columnDefs, columnDrops, indexDefs, indexDrops } = acc;
17
- const [nonStandardAttributeName, attributeType, ...descriptors] = attribute.split(':');
18
- const optional = optionalFromDescriptors(descriptors);
18
+ const [nonStandardAttributeName, attributeType, ...descriptors] = attributeDeclaration.split(':');
19
+ const userWantsThisOptional = optionalFromDescriptors(descriptors);
20
+ // when creating a migration for an STI child, we don't want to include notNull;
21
+ // instead, we'll add a check constraint that uses the STI child class name
22
+ const optional = userWantsThisOptional || !!stiChildClassName;
19
23
  const sqlAttributeType = getAttributeType(attributeType, descriptors);
20
24
  let attributeName = (0, snakeify_js_1.default)(nonStandardAttributeName);
21
25
  if (attributeName === undefined)
@@ -24,6 +28,17 @@ function generateMigrationContent({ table, columnsWithTypes = [], primaryKeyType
24
28
  return acc;
25
29
  if (attributeType === 'citext')
26
30
  requireCitextExtension = true;
31
+ if (stiChildClassName && !userWantsThisOptional) {
32
+ checkConstraints.push(`
33
+
34
+ await db.schema
35
+ .alterTable('${table}')
36
+ .addCheckConstraint(
37
+ '${table}_not_null_${attributeName}',
38
+ sql\`type != '${stiChildClassName}' OR ${attributeName} IS NOT NULL\`,
39
+ )
40
+ .execute()`);
41
+ }
27
42
  switch (attributeType) {
28
43
  case 'belongs_to':
29
44
  columnDefs.push(generateBelongsToStr(attributeName, {
@@ -100,7 +115,7 @@ ${citextExtension}${generateEnumStatements(columnsWithTypes)} await db.schema
100
115
  ".addColumn('created_at', 'timestamp', col => col.notNull())" +
101
116
  newlineDoubleIndent +
102
117
  ".addColumn('updated_at', 'timestamp', col => col.notNull())"}
103
- .execute()${indexDefs.length ? `\n${newlineIndent}` : ''}${indexDefs.join(newlineDoubleIndent)}
118
+ .execute()${indexDefs.length ? `\n${newlineIndent}` : ''}${indexDefs.join(newlineDoubleIndent)}${checkConstraints.join('')}
104
119
  }
105
120
 
106
121
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -2,6 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = generateStiMigrationContent;
4
4
  const generateMigrationContent_js_1 = require("./generateMigrationContent.js");
5
- function generateStiMigrationContent({ table, columnsWithTypes = [], primaryKeyType = 'bigserial', } = {}) {
6
- return (0, generateMigrationContent_js_1.default)({ table, columnsWithTypes, primaryKeyType, createOrAlter: 'alter' });
5
+ function generateStiMigrationContent({ table, columnsWithTypes = [], primaryKeyType = 'bigserial', stiChildClassName, }) {
6
+ return (0, generateMigrationContent_js_1.default)({
7
+ table,
8
+ columnsWithTypes,
9
+ primaryKeyType,
10
+ createOrAlter: 'alter',
11
+ stiChildClassName,
12
+ });
7
13
  }
@@ -14,7 +14,8 @@ function serializerForAssociatedClass(object, associationName, options) {
14
14
  return null;
15
15
  const dream = object;
16
16
  const association = dream['getAssociationMetadata'](associationName);
17
- const associatedClasses = association.modelCB();
18
- const associatedClass = Array.isArray(associatedClasses) ? associatedClasses[0] : associatedClasses;
17
+ const associatedClass = association.modelCB();
18
+ if (Array.isArray(associatedClass))
19
+ throw new Error('rendersOne flatten is incompatible with a polymorphic belongs-to association');
19
20
  return (0, inferSerializerFromDreamOrViewModel_js_1.inferSerializersFromDreamClassOrViewModelClass)(associatedClass, options.serializerKey)[0] ?? null;
20
21
  }
@@ -19,6 +19,7 @@ import destroyDream from './dream/internal/destroyDream.js';
19
19
  import { destroyOptions, reallyDestroyOptions, undestroyOptions, } from './dream/internal/destroyOptions.js';
20
20
  import ensureSTITypeFieldIsSet from './dream/internal/ensureSTITypeFieldIsSet.js';
21
21
  import findOrCreateBy from './dream/internal/findOrCreateBy.js';
22
+ import printSerializerHierarchyLevel from './dream/internal/printSerializerHierarchyLevel.js';
22
23
  import reload from './dream/internal/reload.js';
23
24
  import runValidations from './dream/internal/runValidations.js';
24
25
  import saveDream from './dream/internal/saveDream.js';
@@ -47,10 +48,8 @@ import cloneDeepSafe from './helpers/cloneDeepSafe.js';
47
48
  import { DateTime } from './helpers/DateTime.js';
48
49
  import cachedTypeForAttribute from './helpers/db/cachedTypeForAttribute.js';
49
50
  import isJsonColumn from './helpers/db/types/isJsonColumn.js';
50
- import { indent } from './helpers/indent.js';
51
51
  import notEqual from './helpers/notEqual.js';
52
52
  import { inferSerializersFromDreamClassOrViewModelClass } from './serializer/helpers/inferSerializerFromDreamOrViewModel.js';
53
- import { serializerForAssociatedClass } from './serializer/helpers/serializerForAssociatedClass.js';
54
53
  export default class Dream {
55
54
  DB;
56
55
  /**
@@ -487,42 +486,65 @@ export default class Dream {
487
486
  static recursiveSerializationMap(serializer, { forDisplay = false, forDisplayDepth = 0, } = {}) {
488
487
  const serializerBuilder = serializer(undefined, undefined);
489
488
  const serializerAssociations = serializerBuilder['attributes'].filter(attribute => ['rendersOne', 'rendersMany', 'delegatedAttribute'].includes(attribute.type));
490
- return serializerAssociations.reduce((acc, serializerAssociation) => {
489
+ return serializerAssociations.reduce((accumulator, serializerAssociation) => {
491
490
  const serializerAssociationName = serializerAssociation.targetName ??
492
491
  serializerAssociation.name;
493
492
  const serializerAssociationType = serializerAssociation.type;
494
493
  const association = this['getAssociationMetadata'](serializerAssociationName);
495
494
  if (!association)
496
- return acc;
497
- const associatedClasses = association.modelCB();
498
- const associatedClass = Array.isArray(associatedClasses) ? associatedClasses[0] : associatedClasses;
499
- if (!associatedClass)
495
+ return accumulator;
496
+ const maybeAssociatedClasses = association.modelCB();
497
+ if (!maybeAssociatedClasses)
500
498
  throw new Error(`No class defined on ${serializerAssociationName} association on ${this.sanitizedName}`);
501
- const associationSerializer = serializerForAssociatedClass(this.prototype, serializerAssociationName, serializerAssociation.options);
502
- if (!associationSerializer)
503
- throw new Error(`No serializer found to render ${serializerAssociationName} on ${this.sanitizedName}`);
504
- if (forDisplay && serializerAssociationType !== 'delegatedAttribute') {
505
- const hierarchyLine = '└───';
506
- const indentation = indent((hierarchyLine.length + 1) * forDisplayDepth, {
507
- tabWidth: 1,
508
- });
509
- const prefix = `${hierarchyLine} `;
510
- const nestedAssociationDisplay = indentation + `${prefix}${serializerAssociationType} ${yoctocolors.cyan(serializerAssociationName)}`;
511
- console.log(nestedAssociationDisplay);
512
- console.log(yoctocolors.gray(indentation +
513
- indent(prefix.length, { tabWidth: 1 }) +
514
- associationSerializer.globalName));
515
- }
516
- acc[association.as] = {
517
- parentDreamClass: this,
518
- nestedSerializerInfo: serializerAssociation.type === 'delegatedAttribute'
519
- ? {}
520
- : associatedClass['recursiveSerializationMap'](associationSerializer, {
499
+ const associatedClasses = Array.isArray(maybeAssociatedClasses)
500
+ ? maybeAssociatedClasses
501
+ : [maybeAssociatedClasses];
502
+ /////////////////////////////////////////////////
503
+ // map associated classes to their serializers //
504
+ /////////////////////////////////////////////////
505
+ const associatedClassSerializerTuples = associatedClasses.flatMap(associatedClass => {
506
+ /**
507
+ * `serializers` is an array because `associatedClass` may be an STI
508
+ * base, with each of its STI children having its own serializer
509
+ */
510
+ const serializers = serializerAssociation.options.serializer
511
+ ? [serializerAssociation.options.serializer]
512
+ : inferSerializersFromDreamClassOrViewModelClass(associatedClass, serializerAssociation.options.serializerKey);
513
+ if (!serializers.length)
514
+ throw new Error(`No serializer found to render ${serializerAssociationName} on ${this.sanitizedName}`);
515
+ return serializers.map(serializer => [associatedClass, serializer]);
516
+ });
517
+ /////////////////////////////////////////////////////
518
+ // end:map associated classes to their serializers //
519
+ /////////////////////////////////////////////////////
520
+ ///////////////////////////////////////////////////////////////////////////////////////////////////
521
+ // reduce over all associated serializers, recursively building out their associated serializers //
522
+ ///////////////////////////////////////////////////////////////////////////////////////////////////
523
+ const innerAssociationSerializerInfo = associatedClassSerializerTuples.reduce((innerAccumulator, [associatedClass, associatedSerializer]) => {
524
+ if (forDisplay && serializerAssociationType !== 'delegatedAttribute') {
525
+ printSerializerHierarchyLevel({
526
+ serializerAssociationType,
527
+ serializerAssociationName,
528
+ associationSerializer: associatedSerializer,
529
+ forDisplayDepth,
530
+ });
531
+ }
532
+ return {
533
+ ...innerAccumulator,
534
+ ...associatedClass['recursiveSerializationMap'](associatedSerializer, {
521
535
  forDisplay,
522
536
  forDisplayDepth: forDisplayDepth + 1,
523
537
  }),
538
+ };
539
+ }, {});
540
+ ///////////////////////////////////////////////////////////////////////////////////////////////////////
541
+ // end:reduce over all associated serializers, recursively building out their associated serializers //
542
+ ///////////////////////////////////////////////////////////////////////////////////////////////////////
543
+ accumulator[association.as] = {
544
+ parentDreamClass: this,
545
+ nestedSerializerInfo: serializerAssociation.type === 'delegatedAttribute' ? {} : innerAssociationSerializerInfo,
524
546
  };
525
- return acc;
547
+ return accumulator;
526
548
  }, {});
527
549
  }
528
550
  /**
@@ -657,11 +679,57 @@ export default class Dream {
657
679
  return map;
658
680
  }
659
681
  /**
660
- * @internal
682
+ * Checks whether the specified association has been defined on this Dream model.
683
+ *
684
+ * ```ts
685
+ * const user = User.new()
686
+ *
687
+ * user.hasAssociation('posts')
688
+ * // true (if User has a posts association defined)
689
+ *
690
+ * user.hasAssociation('nonExistentAssociation')
691
+ * // false
692
+ *
693
+ * // Useful for conditional association handling
694
+ * if (user.hasAssociation('posts')) {
695
+ * user = await user.load('posts').execute()
696
+ * }
697
+ * ```
698
+ *
699
+ * @param associationName - The name of the association to check for
700
+ * @returns `true` if the association exists on this model, `false` otherwise
701
+ */
702
+ hasAssociation(associationName) {
703
+ return !!(this.associationMetadataByType.belongsTo.find(association => association.as === associationName) ||
704
+ this.associationMetadataByType.hasOne.find(association => association.as === associationName) ||
705
+ this.associationMetadataByType.hasMany.find(association => association.as === associationName));
706
+ }
707
+ /**
708
+ * Returns all of the association names for this Dream class.
709
+ * This includes all BelongsTo, HasOne, and HasMany associations
710
+ * defined on the model.
661
711
  *
662
- * Returns all of the association names for this dream class
712
+ * This is useful for introspection, debugging, or dynamically working
713
+ * with all associations on a model.
714
+ *
715
+ * ```ts
716
+ * class User extends ApplicationModel {
717
+ * @deco.HasMany('Post')
718
+ * public posts: Post[]
719
+ *
720
+ * @deco.HasOne('Profile')
721
+ * public profile: Profile
722
+ *
723
+ * @deco.BelongsTo('Company')
724
+ * public company: Company
725
+ * }
726
+ *
727
+ * User.associationNames
728
+ * // ['posts', 'profile', 'company']
729
+ * })
730
+ * ```
663
731
  *
664
- * @returns All of the association names for this dream class
732
+ * @returns An array containing all association names defined on this Dream class
665
733
  */
666
734
  static get associationNames() {
667
735
  const allAssociations = [