@rvoh/dream 0.40.3 → 0.40.5

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 (229) hide show
  1. package/dist/cjs/src/Dream.js +64 -12
  2. package/dist/cjs/src/dream/DreamClassTransactionBuilder.js +46 -6
  3. package/dist/cjs/src/dream/DreamInstanceTransactionBuilder.js +2 -2
  4. package/dist/cjs/src/dream/LeftJoinLoadBuilder.js +3 -3
  5. package/dist/cjs/src/dream/LoadBuilder.js +3 -3
  6. package/dist/cjs/src/dream/internal/associations/createAssociation.js +1 -6
  7. package/dist/cjs/src/dream/internal/findOrCreateBy.js +16 -0
  8. package/dist/cjs/src/dream/internal/updateOrCreateBy.js +18 -0
  9. package/dist/cjs/src/errors/CreateOrUpdateByFailedToCreateAndUpdate.js +18 -0
  10. package/dist/cjs/src/types/associations/belongsTo.js +2 -0
  11. package/dist/cjs/src/types/associations/hasMany.js +2 -0
  12. package/dist/cjs/src/types/associations/hasOne.js +2 -0
  13. package/dist/cjs/src/types/associations/shared.js +2 -0
  14. package/dist/cjs/src/types/db.js +2 -0
  15. package/dist/cjs/src/types/dream.js +2 -0
  16. package/dist/cjs/src/types/lifecycle.js +2 -0
  17. package/dist/cjs/src/types/logger.js +2 -0
  18. package/dist/cjs/src/types/openapi.js +2 -0
  19. package/dist/cjs/src/types/query.js +2 -0
  20. package/dist/cjs/src/types/utils.js +2 -0
  21. package/dist/cjs/src/types/validation.js +2 -0
  22. package/dist/cjs/src/types/variadic.js +2 -0
  23. package/dist/esm/src/Dream.js +64 -12
  24. package/dist/esm/src/dream/DreamClassTransactionBuilder.js +46 -6
  25. package/dist/esm/src/dream/DreamInstanceTransactionBuilder.js +2 -2
  26. package/dist/esm/src/dream/LeftJoinLoadBuilder.js +3 -3
  27. package/dist/esm/src/dream/LoadBuilder.js +3 -3
  28. package/dist/esm/src/dream/internal/associations/createAssociation.js +1 -6
  29. package/dist/esm/src/dream/internal/findOrCreateBy.js +13 -0
  30. package/dist/esm/src/dream/internal/updateOrCreateBy.js +15 -0
  31. package/dist/esm/src/errors/CreateOrUpdateByFailedToCreateAndUpdate.js +15 -0
  32. package/dist/esm/src/types/associations/belongsTo.js +1 -0
  33. package/dist/esm/src/types/associations/hasMany.js +1 -0
  34. package/dist/esm/src/types/associations/hasOne.js +1 -0
  35. package/dist/esm/src/types/associations/shared.js +1 -0
  36. package/dist/esm/src/types/db.js +1 -0
  37. package/dist/esm/src/types/dream.js +1 -0
  38. package/dist/esm/src/types/lifecycle.js +1 -0
  39. package/dist/esm/src/types/logger.js +1 -0
  40. package/dist/esm/src/types/openapi.js +1 -0
  41. package/dist/esm/src/types/query.js +1 -0
  42. package/dist/esm/src/types/utils.js +1 -0
  43. package/dist/esm/src/types/validation.js +1 -0
  44. package/dist/esm/src/types/variadic.js +1 -0
  45. package/dist/types/src/Dream.d.ts +42 -9
  46. package/dist/types/src/dream/DreamClassTransactionBuilder.d.ts +72 -37
  47. package/dist/types/src/dream/DreamInstanceTransactionBuilder.d.ts +3 -3
  48. package/dist/types/src/dream/LeftJoinLoadBuilder.d.ts +2 -2
  49. package/dist/types/src/dream/LoadBuilder.d.ts +2 -2
  50. package/dist/types/src/dream/Query.d.ts +1 -1
  51. package/dist/types/src/dream/internal/findOrCreateBy.d.ts +4 -0
  52. package/dist/types/src/dream/internal/updateOrCreateBy.d.ts +4 -0
  53. package/dist/types/src/errors/CreateOrUpdateByFailedToCreateAndUpdate.d.ts +6 -0
  54. package/dist/types/src/types/associations/belongsTo.d.ts +28 -73
  55. package/dist/types/src/types/associations/belongsTo.ts +76 -0
  56. package/dist/types/src/types/associations/hasMany.d.ts +15 -79
  57. package/dist/types/src/types/associations/hasMany.ts +80 -0
  58. package/dist/types/src/types/associations/hasOne.d.ts +8 -26
  59. package/dist/types/src/types/associations/hasOne.ts +26 -0
  60. package/dist/types/src/types/associations/shared.d.ts +98 -444
  61. package/dist/types/src/types/associations/shared.ts +450 -0
  62. package/dist/types/src/types/db.d.ts +9 -14
  63. package/dist/types/src/types/db.ts +14 -0
  64. package/dist/types/src/types/dream.d.ts +130 -509
  65. package/dist/types/src/types/dream.ts +530 -0
  66. package/dist/types/src/types/lifecycle.d.ts +23 -42
  67. package/dist/types/src/types/lifecycle.ts +50 -0
  68. package/dist/types/src/types/logger.d.ts +6 -40
  69. package/dist/types/src/types/logger.ts +42 -0
  70. package/dist/types/src/types/openapi.d.ts +105 -222
  71. package/dist/types/src/types/openapi.ts +255 -0
  72. package/dist/types/src/types/query.d.ts +53 -161
  73. package/dist/types/src/types/query.ts +165 -0
  74. package/dist/types/src/types/utils.d.ts +83 -141
  75. package/dist/types/src/types/utils.ts +173 -0
  76. package/dist/types/src/types/validation.d.ts +17 -18
  77. package/dist/types/src/types/validation.ts +20 -0
  78. package/dist/types/src/types/variadic.d.ts +42 -444
  79. package/dist/types/src/types/variadic.ts +447 -0
  80. package/docs/assets/search.js +1 -1
  81. package/docs/classes/Benchmark.html +2 -2
  82. package/docs/classes/CalendarDate.html +2 -2
  83. package/docs/classes/CreateOrFindByFailedToCreateAndFind.html +3 -3
  84. package/docs/classes/Decorators.html +19 -19
  85. package/docs/classes/Dream.html +205 -185
  86. package/docs/classes/DreamApp.html +4 -4
  87. package/docs/classes/DreamBin.html +2 -2
  88. package/docs/classes/DreamCLI.html +4 -4
  89. package/docs/classes/DreamImporter.html +2 -2
  90. package/docs/classes/DreamLogos.html +2 -2
  91. package/docs/classes/DreamMigrationHelpers.html +7 -7
  92. package/docs/classes/DreamSerializer.html +2 -2
  93. package/docs/classes/DreamTransaction.html +2 -2
  94. package/docs/classes/Encrypt.html +2 -2
  95. package/docs/classes/Env.html +2 -2
  96. package/docs/classes/GlobalNameNotSet.html +3 -3
  97. package/docs/classes/NonLoadedAssociation.html +3 -3
  98. package/docs/classes/Query.html +53 -53
  99. package/docs/classes/Range.html +2 -2
  100. package/docs/classes/RecordNotFound.html +3 -3
  101. package/docs/classes/ValidationError.html +3 -3
  102. package/docs/functions/Attribute.html +1 -1
  103. package/docs/functions/RendersMany.html +1 -1
  104. package/docs/functions/RendersOne.html +1 -1
  105. package/docs/functions/ReplicaSafe.html +1 -1
  106. package/docs/functions/STI.html +1 -1
  107. package/docs/functions/SoftDelete.html +1 -1
  108. package/docs/functions/camelize.html +1 -1
  109. package/docs/functions/capitalize.html +1 -1
  110. package/docs/functions/closeAllDbConnections.html +1 -1
  111. package/docs/functions/compact.html +1 -1
  112. package/docs/functions/dreamDbConnections.html +1 -1
  113. package/docs/functions/dreamPath.html +1 -1
  114. package/docs/functions/generateDream.html +1 -1
  115. package/docs/functions/globalClassNameFromFullyQualifiedModelName.html +1 -1
  116. package/docs/functions/hyphenize.html +1 -1
  117. package/docs/functions/inferSerializerFromDreamClassOrViewModelClass.html +1 -1
  118. package/docs/functions/inferSerializerFromDreamOrViewModel.html +1 -1
  119. package/docs/functions/isEmpty.html +1 -1
  120. package/docs/functions/loadRepl.html +1 -1
  121. package/docs/functions/lookupClassByGlobalName.html +1 -1
  122. package/docs/functions/pascalize.html +1 -1
  123. package/docs/functions/pgErrorType.html +1 -1
  124. package/docs/functions/range-1.html +1 -1
  125. package/docs/functions/relativeDreamPath.html +1 -1
  126. package/docs/functions/round.html +1 -1
  127. package/docs/functions/serializerNameFromFullyQualifiedModelName.html +1 -1
  128. package/docs/functions/sharedPathPrefix.html +1 -1
  129. package/docs/functions/snakeify.html +1 -1
  130. package/docs/functions/sort.html +1 -1
  131. package/docs/functions/sortBy.html +1 -1
  132. package/docs/functions/standardizeFullyQualifiedModelName.html +1 -1
  133. package/docs/functions/uncapitalize.html +1 -1
  134. package/docs/functions/uniq.html +1 -1
  135. package/docs/functions/untypedDb.html +1 -1
  136. package/docs/functions/validateColumn.html +1 -1
  137. package/docs/functions/validateTable.html +1 -1
  138. package/docs/interfaces/AttributeStatement.html +2 -2
  139. package/docs/interfaces/DecoratorContext.html +2 -2
  140. package/docs/interfaces/DreamAppInitOptions.html +2 -2
  141. package/docs/interfaces/DreamAppOpts.html +2 -2
  142. package/docs/interfaces/DreamSerializerAssociationStatement.html +2 -2
  143. package/docs/interfaces/EncryptOptions.html +2 -2
  144. package/docs/interfaces/OpenapiSchemaProperties.html +1 -1
  145. package/docs/interfaces/OpenapiSchemaPropertiesShorthand.html +1 -1
  146. package/docs/interfaces/OpenapiTypeFieldObject.html +1 -1
  147. package/docs/types/Camelized.html +1 -1
  148. package/docs/types/CommonOpenapiSchemaObjectFields.html +1 -1
  149. package/docs/types/DateTime.html +1 -1
  150. package/docs/types/DbConnectionType.html +1 -1
  151. package/docs/types/DreamAssociationMetadata.html +1 -1
  152. package/docs/types/DreamAttributes.html +1 -1
  153. package/docs/types/DreamClassColumn.html +1 -1
  154. package/docs/types/DreamColumn.html +1 -1
  155. package/docs/types/DreamColumnNames.html +1 -1
  156. package/docs/types/DreamLogLevel.html +1 -1
  157. package/docs/types/DreamLogger.html +1 -1
  158. package/docs/types/DreamOrViewModelSerializerKey.html +1 -1
  159. package/docs/types/DreamParamSafeAttributes.html +1 -1
  160. package/docs/types/DreamParamSafeColumnNames.html +1 -1
  161. package/docs/types/DreamSerializerKey.html +1 -1
  162. package/docs/types/DreamSerializers.html +1 -1
  163. package/docs/types/DreamTableSchema.html +1 -1
  164. package/docs/types/DreamVirtualColumns.html +1 -1
  165. package/docs/types/EncryptAlgorithm.html +1 -1
  166. package/docs/types/Hyphenized.html +1 -1
  167. package/docs/types/IdType.html +1 -1
  168. package/docs/types/OpenapiAllTypes.html +1 -1
  169. package/docs/types/OpenapiFormats.html +1 -1
  170. package/docs/types/OpenapiNumberFormats.html +1 -1
  171. package/docs/types/OpenapiPrimitiveTypes.html +1 -1
  172. package/docs/types/OpenapiSchemaArray.html +1 -1
  173. package/docs/types/OpenapiSchemaArrayShorthand.html +1 -1
  174. package/docs/types/OpenapiSchemaBase.html +1 -1
  175. package/docs/types/OpenapiSchemaBody.html +1 -1
  176. package/docs/types/OpenapiSchemaBodyShorthand.html +1 -1
  177. package/docs/types/OpenapiSchemaCommonFields.html +1 -1
  178. package/docs/types/OpenapiSchemaExpressionAllOf.html +1 -1
  179. package/docs/types/OpenapiSchemaExpressionAnyOf.html +1 -1
  180. package/docs/types/OpenapiSchemaExpressionOneOf.html +1 -1
  181. package/docs/types/OpenapiSchemaExpressionRef.html +1 -1
  182. package/docs/types/OpenapiSchemaExpressionRefSchemaShorthand.html +1 -1
  183. package/docs/types/OpenapiSchemaInteger.html +1 -1
  184. package/docs/types/OpenapiSchemaNull.html +1 -1
  185. package/docs/types/OpenapiSchemaNumber.html +1 -1
  186. package/docs/types/OpenapiSchemaObject.html +1 -1
  187. package/docs/types/OpenapiSchemaObjectAllOf.html +1 -1
  188. package/docs/types/OpenapiSchemaObjectAllOfShorthand.html +1 -1
  189. package/docs/types/OpenapiSchemaObjectAnyOf.html +1 -1
  190. package/docs/types/OpenapiSchemaObjectAnyOfShorthand.html +1 -1
  191. package/docs/types/OpenapiSchemaObjectBase.html +1 -1
  192. package/docs/types/OpenapiSchemaObjectBaseShorthand.html +1 -1
  193. package/docs/types/OpenapiSchemaObjectOneOf.html +1 -1
  194. package/docs/types/OpenapiSchemaObjectOneOfShorthand.html +1 -1
  195. package/docs/types/OpenapiSchemaObjectShorthand.html +1 -1
  196. package/docs/types/OpenapiSchemaPrimitiveGeneric.html +1 -1
  197. package/docs/types/OpenapiSchemaShorthandExpressionAllOf.html +1 -1
  198. package/docs/types/OpenapiSchemaShorthandExpressionAnyOf.html +1 -1
  199. package/docs/types/OpenapiSchemaShorthandExpressionOneOf.html +1 -1
  200. package/docs/types/OpenapiSchemaShorthandExpressionSerializableRef.html +1 -1
  201. package/docs/types/OpenapiSchemaShorthandExpressionSerializerRef.html +1 -1
  202. package/docs/types/OpenapiSchemaShorthandPrimitiveGeneric.html +1 -1
  203. package/docs/types/OpenapiSchemaString.html +1 -1
  204. package/docs/types/OpenapiShorthandAllTypes.html +1 -1
  205. package/docs/types/OpenapiShorthandPrimitiveTypes.html +1 -1
  206. package/docs/types/OpenapiTypeField.html +1 -1
  207. package/docs/types/Pascalized.html +1 -1
  208. package/docs/types/PrimaryKeyType.html +1 -1
  209. package/docs/types/RoundingPrecision.html +1 -1
  210. package/docs/types/SerializableClassOrSerializerCallback.html +1 -1
  211. package/docs/types/SerializableDreamClassOrViewModelClass.html +1 -1
  212. package/docs/types/SerializableDreamOrViewModel.html +1 -1
  213. package/docs/types/SerializableTypes.html +1 -1
  214. package/docs/types/Snakeified.html +1 -1
  215. package/docs/types/Timestamp.html +1 -1
  216. package/docs/types/UpdateableAssociationProperties.html +1 -1
  217. package/docs/types/UpdateableProperties.html +1 -1
  218. package/docs/types/ValidationType.html +1 -1
  219. package/docs/types/ViewModelSerializerKey.html +1 -1
  220. package/docs/types/WhereStatementForDream.html +1 -1
  221. package/docs/types/WhereStatementForDreamClass.html +1 -1
  222. package/docs/variables/DateTime-1.html +1 -1
  223. package/docs/variables/DreamConst.html +1 -1
  224. package/docs/variables/TRIGRAM_OPERATORS.html +1 -1
  225. package/docs/variables/openapiPrimitiveTypes-1.html +1 -1
  226. package/docs/variables/openapiShorthandPrimitiveTypes-1.html +1 -1
  227. package/docs/variables/ops.html +1 -1
  228. package/docs/variables/primaryKeyTypes.html +1 -1
  229. package/package.json +1 -1
@@ -0,0 +1,530 @@
1
+ import { ColumnType, Updateable } from 'kysely'
2
+ import { STI_SCOPE_NAME } from '../decorators/class/STI.js'
3
+ import Dream from '../Dream.js'
4
+ import { primaryKeyTypes, TRIGRAM_OPERATORS } from '../dream/constants.js'
5
+ import CalendarDate from '../helpers/CalendarDate.js'
6
+ import { DateTime } from '../helpers/DateTime.js'
7
+ import OpsStatement from '../ops/ops-statement.js'
8
+ import DreamSerializer from '../serializer/index.js'
9
+ import {
10
+ AssociatedModelParam,
11
+ AssociationStatement,
12
+ OnStatementForAssociation,
13
+ WhereStatement,
14
+ } from './associations/shared.js'
15
+ import { AssociationTableNames } from './db.js'
16
+ import { FindEachOpts } from './query.js'
17
+ import { FilterInterface, Inc } from './utils.js'
18
+ import { AliasedSchemaAssociation } from './variadic.js'
19
+
20
+ export type PrimaryKeyType = (typeof primaryKeyTypes)[number]
21
+
22
+ export type IdType = string | number | bigint
23
+ export type Timestamp = ColumnType<DateTime | CalendarDate>
24
+
25
+ export type MAX_VARIADIC_DEPTH = 25
26
+ export type TrigramOperator = (typeof TRIGRAM_OPERATORS)[number]
27
+ export type OrderDir = 'asc' | 'desc'
28
+
29
+ export interface SortableOptions<T extends Dream> {
30
+ scope?:
31
+ | keyof DreamBelongsToAssociationMetadata<T>
32
+ | DreamColumnNames<T>
33
+ | (keyof DreamBelongsToAssociationMetadata<T> | DreamColumnNames<T>)[]
34
+ }
35
+
36
+ export type PrimaryKeyForFind<
37
+ I extends Dream,
38
+ Schema extends I['schema'] = I['schema'],
39
+ TableName extends keyof Schema = I['table'] & keyof Schema,
40
+ > =
41
+ | Schema[TableName]['columns'][I['primaryKey'] & keyof Schema[TableName]['columns']]['coercedType']
42
+ | string
43
+ | null
44
+ | undefined
45
+
46
+ export type DreamColumnNames<
47
+ DreamInstance extends Dream,
48
+ DB = DreamInstance['DB'],
49
+ TableName extends keyof DB = DreamInstance['table'] & keyof DB,
50
+ Table extends DB[keyof DB] = DB[TableName],
51
+ > = keyof Table & string
52
+
53
+ export type DreamParamSafeColumnNames<
54
+ DreamInstance extends Dream,
55
+ BelongsToForeignKeys = DreamBelongsToForeignKeys<DreamInstance>,
56
+ Schema = DreamInstance['schema'],
57
+ TableSchema = Schema[DreamInstance['table'] & keyof Schema],
58
+ > = Exclude<
59
+ DreamColumnNames<DreamInstance>,
60
+ | BelongsToForeignKeys
61
+ | DreamVirtualColumns<DreamInstance>
62
+ | TableSchema['primaryKey' & keyof TableSchema]
63
+ | TableSchema['createdAtField' & keyof TableSchema]
64
+ | TableSchema['updatedAtField' & keyof TableSchema]
65
+ | TableSchema['deletedAtField' & keyof TableSchema]
66
+ >
67
+
68
+ export type DreamBelongsToForeignKeys<
69
+ DreamInstance extends Dream,
70
+ Schema = DreamInstance['schema'],
71
+ TableSchema = Schema[DreamInstance['table'] & keyof Schema],
72
+ AssociationSchema = TableSchema['associations' & keyof TableSchema],
73
+ BelongsToAssociationSchema = AssociationSchema[keyof DreamBelongsToAssociationMetadata<DreamInstance> &
74
+ keyof AssociationSchema],
75
+ BelongsToForeignKeys = Exclude<
76
+ BelongsToAssociationSchema['foreignKey' & keyof BelongsToAssociationSchema],
77
+ null
78
+ >,
79
+ > = BelongsToForeignKeys
80
+
81
+ export type DreamClassColumnNames<
82
+ DreamClass extends typeof Dream,
83
+ DreamInstance extends InstanceType<DreamClass> = InstanceType<DreamClass>,
84
+ DB = DreamInstance['DB'],
85
+ TableName extends keyof DB = DreamInstance['table'] & keyof DB,
86
+ Table extends DB[keyof DB] = DB[TableName],
87
+ > = keyof Table & string
88
+
89
+ export type TableColumnNames<
90
+ DB,
91
+ TableName extends keyof DB,
92
+ Table extends DB[keyof DB] = DB[TableName],
93
+ > = keyof Table & string
94
+
95
+ export type DreamColumn<
96
+ DreamInstance extends Dream,
97
+ Column extends keyof Attrs,
98
+ Attrs = DreamAttributes<DreamInstance>,
99
+ > = Attrs[Column & keyof Attrs]
100
+
101
+ export type DreamClassColumn<
102
+ DreamClass extends typeof Dream,
103
+ Column extends keyof DreamAttributes<DreamInstance>,
104
+ DreamInstance extends InstanceType<DreamClass> = InstanceType<DreamClass>,
105
+ > = DreamColumn<DreamInstance, Column>
106
+
107
+ export type AssociationNameToDream<
108
+ DreamInstance extends Dream,
109
+ AssociationName extends DreamAssociationNames<DreamInstance>,
110
+ PossibleArrayAssociationType = DreamInstance[AssociationName & keyof DreamInstance],
111
+ AssociationType extends Dream = PossibleArrayAssociationType extends (infer ElementType)[]
112
+ ? ElementType extends Dream
113
+ ? ElementType
114
+ : never
115
+ : PossibleArrayAssociationType extends Dream
116
+ ? PossibleArrayAssociationType
117
+ : never,
118
+ > = AssociationType
119
+
120
+ export type DreamAssociationMetadata<
121
+ DreamInstance extends Dream,
122
+ Schema = DreamInstance['schema'],
123
+ AssociationMetadata = Schema[DreamInstance['table'] & keyof Schema]['associations' &
124
+ keyof Schema[DreamInstance['table'] & keyof Schema]],
125
+ > = AssociationMetadata
126
+
127
+ export type DreamAssociationNamesWithoutRequiredOnClauses<
128
+ DreamInstance extends Dream,
129
+ SchemaAssociations = DreamAssociationMetadata<DreamInstance>,
130
+ SchemaTypeInterface = {
131
+ [K in keyof SchemaAssociations]: SchemaAssociations[K]['requiredOnClauses' & keyof SchemaAssociations[K]]
132
+ },
133
+ RequiredOnClauses = keyof FilterInterface<SchemaTypeInterface, null> & string,
134
+ > = RequiredOnClauses
135
+
136
+ export type DreamAssociationNames<
137
+ DreamInstance extends Dream,
138
+ SchemaAssociations = DreamAssociationMetadata<DreamInstance>,
139
+ > = keyof SchemaAssociations
140
+
141
+ type VirtualColumnsForTable<
142
+ Schema,
143
+ TableName extends keyof Schema,
144
+ TableSchema = Schema[TableName],
145
+ > = TableSchema['virtualColumns' & keyof TableSchema]
146
+
147
+ export type DreamVirtualColumns<
148
+ DreamInstance extends Dream,
149
+ Schema = DreamInstance['schema'],
150
+ > = VirtualColumnsForTable<Schema, DreamInstance['table'] & keyof Schema>
151
+
152
+ export type DreamBelongsToAssociationMetadata<
153
+ DreamInstance extends Dream,
154
+ SchemaAssociations = DreamAssociationMetadata<DreamInstance>,
155
+ SchemaTypeInterface = {
156
+ [K in keyof SchemaAssociations]: SchemaAssociations[K]['type' & keyof SchemaAssociations[K]]
157
+ },
158
+ BelongsToKeys = keyof FilterInterface<SchemaTypeInterface, 'BelongsTo'> & string,
159
+ TypeRecord = { [K in BelongsToKeys & string]: SchemaAssociations[K & keyof SchemaAssociations] },
160
+ > = TypeRecord
161
+
162
+ export type DreamAttributes<
163
+ DreamInstance extends Dream,
164
+ Schema = DreamInstance['schema'],
165
+ SchemaColumns = Schema[DreamInstance['table'] & keyof Schema]['columns' &
166
+ keyof Schema[DreamInstance['table'] & keyof Schema]],
167
+ > = {
168
+ -readonly [K in keyof SchemaColumns]: SchemaColumns[K]['coercedType' & keyof SchemaColumns[K]]
169
+ }
170
+
171
+ export type DreamAttributeDbTypes<
172
+ DreamInstance extends Dream,
173
+ Schema = DreamInstance['schema'],
174
+ SchemaColumns = Schema[DreamInstance['table'] & keyof Schema]['columns' &
175
+ keyof Schema[DreamInstance['table'] & keyof Schema]],
176
+ > = {
177
+ -readonly [K in keyof SchemaColumns]: SchemaColumns[K]['dbType' & keyof SchemaColumns[K]]
178
+ }
179
+
180
+ export type DreamParamSafeAttributes<DreamInstance extends Dream> = {
181
+ [K in keyof DreamAttributes<DreamInstance> &
182
+ DreamParamSafeColumnNames<DreamInstance>]: DreamAttributes<DreamInstance>[K]
183
+ }
184
+
185
+ export type DreamTableSchema<DreamInstance extends Dream> = Updateable<
186
+ DreamInstance['DB'][DreamInstance['table']]
187
+ >
188
+
189
+ export type TableColumnType<
190
+ Schema,
191
+ TableName,
192
+ Column,
193
+ TableSchema extends Schema[TableName & keyof Schema] = Schema[TableName & keyof Schema],
194
+ TableColumns extends TableSchema['columns' & keyof TableSchema] = TableSchema['columns' &
195
+ keyof TableSchema],
196
+ TableColumnMetadata extends TableColumns[Column & keyof TableColumns] = TableColumns[Column &
197
+ keyof TableColumns],
198
+ ColumnType extends TableColumnMetadata['coercedType' &
199
+ keyof TableColumnMetadata] = TableColumnMetadata['coercedType' & keyof TableColumnMetadata],
200
+ > = ColumnType
201
+
202
+ export type TableColumnEnumTypeArray<
203
+ Schema,
204
+ TableName,
205
+ Column,
206
+ TableSchema extends Schema[TableName & keyof Schema] = Schema[TableName & keyof Schema],
207
+ TableColumns extends TableSchema['columns' & keyof TableSchema] = TableSchema['columns' &
208
+ keyof TableSchema],
209
+ TableColumnMetadata extends TableColumns[Column & keyof TableColumns] = TableColumns[Column &
210
+ keyof TableColumns],
211
+ EnumTypeCandidate = TableColumnMetadata['enumArrayType' & keyof TableColumnMetadata],
212
+ EnumTypeArray extends string[] | null = EnumTypeCandidate extends null
213
+ ? null
214
+ : EnumTypeCandidate extends undefined
215
+ ? null
216
+ : EnumTypeCandidate extends string[]
217
+ ? EnumTypeCandidate
218
+ : never,
219
+ > = EnumTypeArray
220
+
221
+ ///////////////////////////
222
+ // Association type helpers
223
+ ///////////////////////////
224
+ type AssociationMetadataForTable<Schema, TableName extends keyof Schema> = Schema[TableName]['associations' &
225
+ keyof Schema[TableName]]
226
+
227
+ export type AssociationNamesForTable<Schema, TableName extends keyof Schema> =
228
+ | keyof AssociationMetadataForTable<Schema, TableName>
229
+ | AliasedSchemaAssociation<Schema, TableName>
230
+
231
+ type MetadataForAssociation<
232
+ Schema,
233
+ TableName extends keyof Schema,
234
+ AssociationName,
235
+ AssociationMetadata = AssociationMetadataForTable<Schema, TableName>,
236
+ > = AssociationMetadata[AssociationName & keyof AssociationMetadata]
237
+
238
+ export type AssociationTableName<
239
+ Schema,
240
+ TableName extends keyof Schema,
241
+ AssociationName,
242
+ AssociationData = MetadataForAssociation<Schema, TableName, AssociationName>,
243
+ > = (AssociationData['tables' & keyof AssociationData] & any[])[0] & keyof Schema
244
+
245
+ export type JoinAndStatements<
246
+ DB,
247
+ Schema,
248
+ TableName extends keyof Schema & AssociationTableNames<DB, Schema> & keyof DB,
249
+ RequiredOnClauseKeysForThisAssociation,
250
+ > = RequiredOnClauseKeysForThisAssociation extends null
251
+ ? {
252
+ and?: WhereStatement<DB, Schema, TableName>
253
+ andNot?: WhereStatement<DB, Schema, TableName>
254
+ // andNot?: WhereStatementWithoutSimilarityClauses<DB, Schema, TableName>
255
+ andAny?: WhereStatement<DB, Schema, TableName>[]
256
+ // andAny?: WhereStatementWithoutSimilarityClauses<DB, Schema, TableName>[]
257
+ }
258
+ : RequiredOnClauseKeysForThisAssociation extends string[]
259
+ ? {
260
+ and: OnStatementForAssociation<DB, Schema, TableName, RequiredOnClauseKeysForThisAssociation>
261
+ andNot?: WhereStatement<DB, Schema, TableName>
262
+ // andNot?: WhereStatementWithoutSimilarityClauses<DB, Schema, TableName>
263
+ andAny?: WhereStatement<DB, Schema, TableName>[]
264
+ // andAny?: WhereStatementWithoutSimilarityClauses<DB, Schema, TableName>[]
265
+ }
266
+ : never
267
+ ////////////////////////////////
268
+ // end: Association type helpers
269
+ ////////////////////////////////
270
+
271
+ export type UpdateablePropertiesForClass<
272
+ DreamClass extends typeof Dream,
273
+ TableName extends AssociationTableNames<
274
+ InstanceType<DreamClass>['DB'],
275
+ InstanceType<DreamClass>['schema']
276
+ > &
277
+ InstanceType<DreamClass>['table'] = InstanceType<DreamClass>['table'],
278
+ VirtualColumns = DreamVirtualColumns<InstanceType<DreamClass>>,
279
+ > = Partial<
280
+ Updateable<InstanceType<DreamClass>['DB'][TableName]> &
281
+ (VirtualColumns extends readonly any[] ? Record<VirtualColumns[number], any> : object) &
282
+ (AssociatedModelParam<InstanceType<DreamClass>> extends never
283
+ ? object
284
+ : AssociatedModelParam<InstanceType<DreamClass>>)
285
+ >
286
+
287
+ export type UpdateableAssociationProperties<
288
+ DreamInstance extends Dream,
289
+ AssociationClass extends Dream,
290
+ Schema = DreamInstance['schema'],
291
+ AssociationTableName extends AssociationTableNames<DreamInstance['DB'], DreamInstance['schema']> &
292
+ keyof DreamInstance['DB'] = AssociationClass['table'],
293
+ VirtualColumns = VirtualColumnsForTable<Schema, AssociationTableName>,
294
+ > = Partial<
295
+ Updateable<DreamInstance['DB'][AssociationTableName]> &
296
+ (VirtualColumns extends readonly any[] ? Record<VirtualColumns[number], any> : object) &
297
+ (AssociatedModelParam<AssociationClass> extends never ? object : AssociatedModelParam<AssociationClass>)
298
+ >
299
+
300
+ export type AttributeKeys<
301
+ I extends Dream,
302
+ TableName extends AssociationTableNames<I['DB'], I['schema']> & I['table'] = I['table'],
303
+ VirtualColumns = DreamVirtualColumns<I>,
304
+ > = keyof (Updateable<I['DB'][TableName]> &
305
+ (VirtualColumns extends readonly any[] ? Record<VirtualColumns[number], any> : object))
306
+
307
+ export type UpdateableProperties<
308
+ I extends Dream,
309
+ TableName extends AssociationTableNames<I['DB'], I['schema']> & I['table'] = I['table'],
310
+ VirtualColumns = DreamVirtualColumns<I>,
311
+ > = Partial<
312
+ Updateable<I['DB'][TableName]> &
313
+ (VirtualColumns extends readonly any[] ? Record<VirtualColumns[number], any> : object) &
314
+ (AssociatedModelParam<I> extends never ? object : AssociatedModelParam<I>)
315
+ >
316
+
317
+ export interface CreateOrFindByExtraOpts<T extends typeof Dream> {
318
+ createWith?: UpdateablePropertiesForClass<T>
319
+ }
320
+
321
+ export interface UpdateOrCreateByExtraOpts<T extends typeof Dream> {
322
+ with?: UpdateablePropertiesForClass<T>
323
+ skipHooks?: boolean
324
+ }
325
+
326
+ // Model global names and tables
327
+ export type TableNameForGlobalModelName<
328
+ I extends Dream,
329
+ GMN extends keyof GlobalModelNameTableMap<I>,
330
+ > = GlobalModelNameTableMap<I>[GMN]
331
+
332
+ export type GlobalModelNames<I extends Dream> = keyof GlobalModelNameTableMap<I>
333
+
334
+ export type GlobalModelNameTableMap<
335
+ I extends Dream,
336
+ GlobalSchema = I['globalSchema'],
337
+ GlobalNames = GlobalSchema['globalNames' & keyof GlobalSchema],
338
+ > = GlobalNames['models' & keyof GlobalNames]
339
+ // end:Model global names and tables
340
+
341
+ // Serializer global names
342
+ export type GlobalSerializerName<I extends Dream> = GlobalSerializerNames<I>[number]
343
+
344
+ type GlobalSerializerNames<
345
+ I extends Dream,
346
+ GlobalSchema = I['globalSchema'],
347
+ GlobalNames = GlobalSchema['globalNames' & keyof GlobalSchema],
348
+ > = GlobalNames['serializers' & keyof GlobalNames]
349
+ // end:Serializer global names
350
+
351
+ export type DreamSerializers<I extends Dream> = Record<'default', GlobalSerializerName<I>> &
352
+ Record<string, GlobalSerializerName<I>>
353
+
354
+ export type DreamConstructorType<T extends Dream> = (new (...arguments_: any[]) => T) & typeof Dream
355
+
356
+ export type ViewModel = { serializers: Record<string, string> }
357
+ export type ViewModelClass = abstract new (...args: any) => ViewModel
358
+
359
+ export type SerializableDreamOrViewModel = ViewModel
360
+ export type SerializableDreamClassOrViewModelClass = abstract new (
361
+ ...args: any
362
+ ) => SerializableDreamOrViewModel
363
+
364
+ export type DreamSerializerCallback = () => typeof DreamSerializer
365
+ export type SerializableClassOrSerializerCallback =
366
+ | SerializableDreamClassOrViewModelClass
367
+ | DreamSerializerCallback
368
+
369
+ export type SerializableClassOrClasses =
370
+ | DreamSerializerCallback
371
+ | SerializableDreamClassOrViewModelClass
372
+ | SerializableDreamClassOrViewModelClass[]
373
+
374
+ // preload
375
+ export type NextPreloadArgumentType<
376
+ Schema,
377
+ PreviousTableNames,
378
+ PreviousSchemaAssociations = PreviousTableNames extends undefined
379
+ ? undefined
380
+ : Schema[PreviousTableNames & keyof Schema]['associations' &
381
+ keyof Schema[PreviousTableNames & keyof Schema]],
382
+ > = PreviousTableNames extends undefined
383
+ ? undefined
384
+ : (keyof PreviousSchemaAssociations & string) | (keyof PreviousSchemaAssociations & string)[]
385
+
386
+ export type PreloadArgumentTypeAssociatedTableNames<
387
+ Schema,
388
+ PreviousTableNames,
389
+ ArgumentType,
390
+ PreviousSchemaAssociations = PreviousTableNames extends undefined
391
+ ? undefined
392
+ : Schema[PreviousTableNames & keyof Schema],
393
+ > = ArgumentType extends string[]
394
+ ? undefined
395
+ : (PreviousSchemaAssociations[ArgumentType & (keyof PreviousSchemaAssociations & string)] &
396
+ string[])[number]
397
+ // end:preload
398
+
399
+ export type AssociationNameToDreamClassMap = Record<string, typeof Dream>
400
+ export type AssociationNameToAssociationMap = Record<string, AssociationStatement>
401
+ export type AssociationNameToAssociationDataAndDreamClassMap = Record<
402
+ string,
403
+ {
404
+ dreamClass: typeof Dream
405
+ association: AssociationStatement
406
+ }
407
+ >
408
+
409
+ type IdToDreamMap = Map<string | number, Dream>
410
+ export type AliasToDreamIdMap = Record<string, IdToDreamMap>
411
+
412
+ export type RelaxedPreloadStatement<Depth extends number = 0> = RelaxedJoinStatement<Depth>
413
+
414
+ export type RelaxedJoinStatement<Depth extends number = 0> = Depth extends 7
415
+ ? object
416
+ : Record<string, RelaxedJoinStatement<Inc<Depth>>>
417
+
418
+ export type RelaxedPreloadOnStatement<DB, Schema, Depth extends number = 0> = Depth extends 7
419
+ ? object
420
+ : {
421
+ [key: string]:
422
+ | RelaxedPreloadOnStatement<DB, Schema, Inc<Depth>>
423
+ | JoinAndStatements<any, any, any, any>
424
+ | FakeOnClauseValue
425
+ | object
426
+ }
427
+
428
+ export type RelaxedJoinAndStatement<DB, Schema, Depth extends number = 0> = Depth extends 7
429
+ ? object
430
+ : {
431
+ [key: string]:
432
+ | RelaxedJoinAndStatement<DB, Schema, Inc<Depth>>
433
+ | JoinAndStatements<any, any, any, any>
434
+ | FakeOnClauseValue
435
+ | object
436
+ }
437
+
438
+ // Just need something that is not an object, but that could be an array and also null
439
+ type FakeOnClauseValue = string | string[] | number | number[] | null
440
+
441
+ export type TableOrAssociationName<Schema> = (keyof Schema & string) | (keyof Schema[keyof Schema] & string)
442
+
443
+ export type SqlCommandType = 'select' | 'update' | 'delete' | 'insert'
444
+
445
+ export type DreamSerializeOptions<T> = {
446
+ serializerKey?: DreamSerializerKey<T>
447
+ casing?: 'camel' | 'snake' | null
448
+ }
449
+
450
+ export type DreamOrViewModelSerializerKey<T> = T extends Dream
451
+ ? DreamSerializerKey<T>
452
+ : ViewModelSerializerKey<T>
453
+
454
+ export type DreamSerializerKey<
455
+ T,
456
+ U = T extends (infer R)[] ? R : T,
457
+ Table = U['table' & keyof U] extends string ? U['table' & keyof U] : never,
458
+ Schema = U['schema' & keyof U] extends object ? U['schema' & keyof U] : never,
459
+ TableSchema = Table extends never ? never : Schema extends never ? never : Schema[Table & keyof Schema],
460
+ SerializerKeys = TableSchema extends never
461
+ ? never
462
+ : TableSchema['serializerKeys' & keyof TableSchema] & (string[] | Readonly<string[]>),
463
+ SerializerKey = SerializerKeys extends string[] | Readonly<string[]> ? SerializerKeys[number] : never,
464
+ > = SerializerKey
465
+
466
+ export type ViewModelSerializerKey<
467
+ T,
468
+ U = T extends (infer R)[] ? R : T,
469
+ SerializerType = U extends null
470
+ ? never
471
+ : U['serializers' & keyof U] extends object
472
+ ? keyof U['serializers' & keyof U]
473
+ : never,
474
+ > = SerializerType
475
+
476
+ export interface SimilarityStatement {
477
+ tableName: string
478
+ tableAlias: string
479
+ columnName: string
480
+ opsStatement: OpsStatement<any, any>
481
+ }
482
+
483
+ export type PassthroughColumnNames<
484
+ DreamConf,
485
+ GlobalSchema = DreamConf['globalSchema' & keyof DreamConf],
486
+ PassthroughColumns = GlobalSchema['passthroughColumns' & keyof GlobalSchema],
487
+ > = PassthroughColumns
488
+
489
+ export type DefaultScopeName<
490
+ DreamInstance extends Dream,
491
+ Schema = DreamInstance['schema'],
492
+ TableName extends keyof Schema = DreamInstance['table'] & keyof Schema,
493
+ > = DefaultScopeNameForTable<Schema, TableName>
494
+
495
+ export type DefaultScopeNameForTable<
496
+ Schema,
497
+ TableName extends keyof Schema,
498
+ SchemaTable = Schema[TableName],
499
+ SchemaDefaultScopes extends string[] = SchemaTable['scopes' & keyof SchemaTable]['default' &
500
+ keyof SchemaTable['scopes' & keyof SchemaTable]] &
501
+ string[],
502
+ > =
503
+ // it is not valid to remove the STI scope
504
+ Exclude<SchemaDefaultScopes[number], typeof STI_SCOPE_NAME>
505
+
506
+ export type AllDefaultScopeNames<
507
+ DreamConf,
508
+ GlobalSchema = DreamConf['globalSchema' & keyof DreamConf],
509
+ AllNames = GlobalSchema['allDefaultScopeNames' & keyof GlobalSchema],
510
+ > =
511
+ // it is not valid to remove the STI scope
512
+ Exclude<AllNames[number & keyof AllNames], typeof STI_SCOPE_NAME>
513
+
514
+ export type NamedScopeName<
515
+ DreamInstance extends Dream,
516
+ Schema = DreamInstance['schema'],
517
+ SchemaTable = Schema[DreamInstance['table'] & keyof Schema],
518
+ SchemaNamedScopes extends string[] = SchemaTable['scopes' & keyof SchemaTable]['named' &
519
+ keyof SchemaTable['scopes' & keyof SchemaTable]] &
520
+ string[],
521
+ > = SchemaNamedScopes[number]
522
+
523
+ export type DefaultOrNamedScopeName<DreamInstance extends Dream> =
524
+ | DefaultScopeName<DreamInstance>
525
+ | NamedScopeName<DreamInstance>
526
+
527
+ export type PluckEachArgs<ColumnNames extends unknown[], CbArgTypes extends unknown[]> =
528
+ | [...fields: ColumnNames]
529
+ | [...fields: ColumnNames, callback: (...values: CbArgTypes) => void | Promise<void>]
530
+ | [...fields: ColumnNames, callback: (...values: CbArgTypes) => void | Promise<void>, opts: FindEachOpts]
@@ -1,50 +1,31 @@
1
- import Dream from '../Dream.ts'
2
- import { DreamColumnNames } from './dream.js'
3
-
4
- export type HookType =
5
- | 'beforeCreate'
6
- | 'beforeSave'
7
- | 'beforeUpdate'
8
- | 'beforeDestroy'
9
- | 'afterCreate'
10
- | 'afterSave'
11
- | 'afterUpdate'
12
- | 'afterDestroy'
13
- | CommitHookType
14
-
15
- export type CommitHookType =
16
- | 'afterCreateCommit'
17
- | 'afterSaveCommit'
18
- | 'afterUpdateCommit'
19
- | 'afterDestroyCommit'
20
-
1
+ import Dream from '../Dream.js';
2
+ import { DreamColumnNames } from './dream.js';
3
+ export type HookType = 'beforeCreate' | 'beforeSave' | 'beforeUpdate' | 'beforeDestroy' | 'afterCreate' | 'afterSave' | 'afterUpdate' | 'afterDestroy' | CommitHookType;
4
+ export type CommitHookType = 'afterCreateCommit' | 'afterSaveCommit' | 'afterUpdateCommit' | 'afterDestroyCommit';
21
5
  export interface HookStatement {
22
- type: HookType
23
- className: string
24
- method: string
25
- ifChanging?: string[] | undefined
26
- ifChanged?: string[] | undefined
6
+ type: HookType;
7
+ className: string;
8
+ method: string;
9
+ ifChanging?: string[] | undefined;
10
+ ifChanged?: string[] | undefined;
27
11
  }
28
-
29
12
  export interface BeforeHookOpts<T extends Dream | null = null> {
30
- ifChanging?: T extends Dream ? DreamColumnNames<T>[] : string[]
13
+ ifChanging?: T extends Dream ? DreamColumnNames<T>[] : string[];
31
14
  }
32
-
33
15
  export interface AfterHookOpts<T extends Dream | null = null> {
34
- ifChanged?: T extends Dream ? DreamColumnNames<T>[] : string[]
16
+ ifChanged?: T extends Dream ? DreamColumnNames<T>[] : string[];
35
17
  }
36
-
37
18
  export interface HookStatementMap {
38
- beforeCreate: readonly HookStatement[] | HookStatement[]
39
- beforeUpdate: readonly HookStatement[] | HookStatement[]
40
- beforeSave: readonly HookStatement[] | HookStatement[]
41
- beforeDestroy: readonly HookStatement[] | HookStatement[]
42
- afterCreate: readonly HookStatement[] | HookStatement[]
43
- afterCreateCommit: readonly HookStatement[] | HookStatement[]
44
- afterUpdate: readonly HookStatement[] | HookStatement[]
45
- afterUpdateCommit: readonly HookStatement[] | HookStatement[]
46
- afterSave: readonly HookStatement[] | HookStatement[]
47
- afterSaveCommit: readonly HookStatement[] | HookStatement[]
48
- afterDestroy: readonly HookStatement[] | HookStatement[]
49
- afterDestroyCommit: readonly HookStatement[] | HookStatement[]
19
+ beforeCreate: readonly HookStatement[] | HookStatement[];
20
+ beforeUpdate: readonly HookStatement[] | HookStatement[];
21
+ beforeSave: readonly HookStatement[] | HookStatement[];
22
+ beforeDestroy: readonly HookStatement[] | HookStatement[];
23
+ afterCreate: readonly HookStatement[] | HookStatement[];
24
+ afterCreateCommit: readonly HookStatement[] | HookStatement[];
25
+ afterUpdate: readonly HookStatement[] | HookStatement[];
26
+ afterUpdateCommit: readonly HookStatement[] | HookStatement[];
27
+ afterSave: readonly HookStatement[] | HookStatement[];
28
+ afterSaveCommit: readonly HookStatement[] | HookStatement[];
29
+ afterDestroy: readonly HookStatement[] | HookStatement[];
30
+ afterDestroyCommit: readonly HookStatement[] | HookStatement[];
50
31
  }
@@ -0,0 +1,50 @@
1
+ import Dream from '../Dream.js'
2
+ import { DreamColumnNames } from './dream.js'
3
+
4
+ export type HookType =
5
+ | 'beforeCreate'
6
+ | 'beforeSave'
7
+ | 'beforeUpdate'
8
+ | 'beforeDestroy'
9
+ | 'afterCreate'
10
+ | 'afterSave'
11
+ | 'afterUpdate'
12
+ | 'afterDestroy'
13
+ | CommitHookType
14
+
15
+ export type CommitHookType =
16
+ | 'afterCreateCommit'
17
+ | 'afterSaveCommit'
18
+ | 'afterUpdateCommit'
19
+ | 'afterDestroyCommit'
20
+
21
+ export interface HookStatement {
22
+ type: HookType
23
+ className: string
24
+ method: string
25
+ ifChanging?: string[] | undefined
26
+ ifChanged?: string[] | undefined
27
+ }
28
+
29
+ export interface BeforeHookOpts<T extends Dream | null = null> {
30
+ ifChanging?: T extends Dream ? DreamColumnNames<T>[] : string[]
31
+ }
32
+
33
+ export interface AfterHookOpts<T extends Dream | null = null> {
34
+ ifChanged?: T extends Dream ? DreamColumnNames<T>[] : string[]
35
+ }
36
+
37
+ export interface HookStatementMap {
38
+ beforeCreate: readonly HookStatement[] | HookStatement[]
39
+ beforeUpdate: readonly HookStatement[] | HookStatement[]
40
+ beforeSave: readonly HookStatement[] | HookStatement[]
41
+ beforeDestroy: readonly HookStatement[] | HookStatement[]
42
+ afterCreate: readonly HookStatement[] | HookStatement[]
43
+ afterCreateCommit: readonly HookStatement[] | HookStatement[]
44
+ afterUpdate: readonly HookStatement[] | HookStatement[]
45
+ afterUpdateCommit: readonly HookStatement[] | HookStatement[]
46
+ afterSave: readonly HookStatement[] | HookStatement[]
47
+ afterSaveCommit: readonly HookStatement[] | HookStatement[]
48
+ afterDestroy: readonly HookStatement[] | HookStatement[]
49
+ afterDestroyCommit: readonly HookStatement[] | HookStatement[]
50
+ }