@rvoh/dream 1.4.1 → 1.5.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 (283) hide show
  1. package/CHANGELOG.md +166 -0
  2. package/dist/cjs/src/Dream.js +12 -16
  3. package/dist/cjs/src/bin/index.js +23 -7
  4. package/dist/cjs/src/cli/index.js +18 -3
  5. package/dist/cjs/src/db/ConnectedToDB.js +0 -8
  6. package/dist/cjs/src/db/DreamDbConnection.js +25 -36
  7. package/dist/cjs/src/db/helpers/dbTypesFilenameForConnection.js +6 -0
  8. package/dist/cjs/src/{bin/helpers/sync.js → db/helpers/syncDbTypesFiles.js} +8 -5
  9. package/dist/cjs/src/db/index.js +3 -2
  10. package/dist/cjs/src/decorators/field/sortable/helpers/setPosition.js +17 -2
  11. package/dist/cjs/src/dream/Query.js +9 -6
  12. package/dist/cjs/src/dream/QueryDriver/Base.js +111 -7
  13. package/dist/cjs/src/dream/QueryDriver/Kysely.js +213 -86
  14. package/dist/cjs/src/dream/QueryDriver/Postgres.js +162 -0
  15. package/dist/cjs/src/{helpers/db → dream/QueryDriver/helpers/kysely}/runMigration.js +20 -16
  16. package/dist/cjs/src/{helpers/db → dream/QueryDriver/helpers/pg}/createDb.js +5 -5
  17. package/dist/cjs/src/{helpers/db → dream/QueryDriver/helpers/pg}/dropDb.js +6 -6
  18. package/dist/cjs/src/{helpers/db → dream/QueryDriver/helpers/pg}/loadPgClient.js +5 -3
  19. package/dist/cjs/src/dream/internal/destroyDream.js +3 -10
  20. package/dist/cjs/src/dream/internal/saveDream.js +1 -1
  21. package/dist/cjs/src/dream/internal/similarity/SimilarityBuilder.js +9 -3
  22. package/dist/cjs/src/dream-app/index.js +59 -65
  23. package/dist/cjs/src/helpers/cli/SchemaBuilder.js +30 -42
  24. package/dist/cjs/src/helpers/cli/addImportSuffix.js +17 -0
  25. package/dist/cjs/src/helpers/cli/generateDream.js +2 -0
  26. package/dist/cjs/src/helpers/cli/generateDreamContent.js +5 -3
  27. package/dist/cjs/src/helpers/cli/generateFactoryContent.js +29 -0
  28. package/dist/cjs/src/helpers/cli/generateMigration.js +7 -5
  29. package/dist/cjs/src/helpers/cli/generateMigrationContent.js +48 -15
  30. package/dist/cjs/src/helpers/db/primaryKeyType.js +4 -22
  31. package/dist/cjs/src/helpers/path/relativeDreamPath.js +4 -3
  32. package/dist/cjs/src/helpers/sqlAttributes.js +4 -1
  33. package/dist/cjs/src/index.js +8 -2
  34. package/dist/esm/src/Dream.js +10 -14
  35. package/dist/esm/src/bin/index.js +23 -7
  36. package/dist/esm/src/cli/index.js +18 -3
  37. package/dist/esm/src/db/ConnectedToDB.js +0 -8
  38. package/dist/esm/src/db/DreamDbConnection.js +22 -34
  39. package/dist/esm/src/db/helpers/dbTypesFilenameForConnection.js +3 -0
  40. package/dist/esm/src/{bin/helpers/sync.js → db/helpers/syncDbTypesFiles.js} +7 -4
  41. package/dist/esm/src/db/index.js +3 -2
  42. package/dist/esm/src/decorators/field/sortable/helpers/setPosition.js +16 -2
  43. package/dist/esm/src/dream/Query.js +9 -6
  44. package/dist/esm/src/dream/QueryDriver/Base.js +111 -7
  45. package/dist/esm/src/dream/QueryDriver/Kysely.js +215 -88
  46. package/dist/esm/src/dream/QueryDriver/Postgres.js +162 -0
  47. package/dist/esm/src/{helpers/db → dream/QueryDriver/helpers/kysely}/runMigration.js +14 -10
  48. package/dist/esm/src/{helpers/db → dream/QueryDriver/helpers/pg}/createDb.js +5 -5
  49. package/dist/esm/src/{helpers/db → dream/QueryDriver/helpers/pg}/dropDb.js +6 -6
  50. package/dist/esm/src/{helpers/db → dream/QueryDriver/helpers/pg}/loadPgClient.js +5 -3
  51. package/dist/esm/src/dream/internal/destroyDream.js +3 -10
  52. package/dist/esm/src/dream/internal/saveDream.js +1 -1
  53. package/dist/esm/src/dream/internal/similarity/SimilarityBuilder.js +9 -3
  54. package/dist/esm/src/dream-app/index.js +55 -61
  55. package/dist/esm/src/helpers/cli/SchemaBuilder.js +24 -36
  56. package/dist/esm/src/helpers/cli/addImportSuffix.js +14 -0
  57. package/dist/esm/src/helpers/cli/generateDream.js +2 -0
  58. package/dist/esm/src/helpers/cli/generateDreamContent.js +5 -3
  59. package/dist/esm/src/helpers/cli/generateFactoryContent.js +29 -0
  60. package/dist/esm/src/helpers/cli/generateMigration.js +7 -5
  61. package/dist/esm/src/helpers/cli/generateMigrationContent.js +48 -15
  62. package/dist/esm/src/helpers/db/primaryKeyType.js +4 -22
  63. package/dist/esm/src/helpers/path/relativeDreamPath.js +4 -3
  64. package/dist/esm/src/helpers/sqlAttributes.js +4 -1
  65. package/dist/esm/src/index.js +3 -0
  66. package/dist/types/src/Dream.d.ts +5 -3
  67. package/dist/types/src/bin/index.d.ts +3 -1
  68. package/dist/types/src/db/ConnectedToDB.d.ts +0 -2
  69. package/dist/types/src/db/DreamDbConnection.d.ts +7 -3
  70. package/dist/types/src/db/helpers/dbTypesFilenameForConnection.d.ts +1 -0
  71. package/dist/types/src/db/helpers/syncDbTypesFiles.d.ts +1 -0
  72. package/dist/types/src/db/index.d.ts +2 -1
  73. package/dist/types/src/decorators/field/sortable/helpers/setPosition.d.ts +2 -0
  74. package/dist/types/src/dream/Query.d.ts +5 -20
  75. package/dist/types/src/dream/QueryDriver/Base.d.ts +74 -13
  76. package/dist/types/src/dream/QueryDriver/Kysely.d.ts +75 -9
  77. package/dist/types/src/dream/QueryDriver/Postgres.d.ts +32 -0
  78. package/dist/types/src/dream/QueryDriver/helpers/kysely/foreignKeyTypeFromPrimaryKey.d.ts +2 -0
  79. package/dist/types/src/dream/QueryDriver/helpers/kysely/runMigration.d.ts +9 -0
  80. package/dist/types/src/dream/QueryDriver/helpers/pg/createDb.d.ts +2 -0
  81. package/dist/types/src/dream/QueryDriver/helpers/pg/dropDb.d.ts +2 -0
  82. package/dist/types/src/dream/QueryDriver/helpers/pg/loadPgClient.d.ts +5 -0
  83. package/dist/types/src/dream/internal/destroyOptions.d.ts +3 -3
  84. package/dist/types/src/dream-app/index.d.ts +16 -7
  85. package/dist/types/src/helpers/cli/SchemaBuilder.d.ts +26 -0
  86. package/dist/types/src/helpers/cli/addImportSuffix.d.ts +1 -0
  87. package/dist/types/src/helpers/cli/generateDream.d.ts +1 -0
  88. package/dist/types/src/helpers/cli/generateDreamContent.d.ts +2 -1
  89. package/dist/types/src/helpers/cli/generateMigration.d.ts +2 -1
  90. package/dist/types/src/helpers/cli/generateMigrationContent.d.ts +2 -1
  91. package/dist/types/src/helpers/db/primaryKeyType.d.ts +1 -1
  92. package/dist/types/src/index.d.ts +3 -0
  93. package/dist/types/src/types/dream.d.ts +4 -4
  94. package/dist/types/src/types/dream.ts +8 -9
  95. package/docs/assets/navigation.js +1 -1
  96. package/docs/assets/search.js +1 -1
  97. package/docs/classes/Benchmark.html +2 -2
  98. package/docs/classes/CalendarDate.html +2 -2
  99. package/docs/classes/CheckConstraintViolation.html +3 -3
  100. package/docs/classes/CliFileWriter.html +2 -2
  101. package/docs/classes/CreateOrFindByFailedToCreateAndFind.html +3 -3
  102. package/docs/classes/DataTypeColumnTypeMismatch.html +3 -3
  103. package/docs/classes/Decorators.html +19 -19
  104. package/docs/classes/Dream.html +117 -115
  105. package/docs/classes/DreamApp.html +9 -5
  106. package/docs/classes/DreamBin.html +2 -2
  107. package/docs/classes/DreamCLI.html +4 -4
  108. package/docs/classes/DreamImporter.html +2 -2
  109. package/docs/classes/DreamLogos.html +2 -2
  110. package/docs/classes/DreamMigrationHelpers.html +7 -7
  111. package/docs/classes/DreamSerializerBuilder.html +8 -8
  112. package/docs/classes/DreamTransaction.html +2 -2
  113. package/docs/classes/Encrypt.html +2 -2
  114. package/docs/classes/Env.html +2 -2
  115. package/docs/classes/GlobalNameNotSet.html +3 -3
  116. package/docs/classes/KyselyQueryDriver.html +163 -0
  117. package/docs/classes/NonLoadedAssociation.html +3 -3
  118. package/docs/classes/NotNullViolation.html +3 -3
  119. package/docs/classes/ObjectSerializerBuilder.html +8 -8
  120. package/docs/classes/PostgresQueryDriver.html +165 -0
  121. package/docs/classes/Query.html +59 -87
  122. package/docs/classes/QueryDriverBase.html +156 -0
  123. package/docs/classes/Range.html +2 -2
  124. package/docs/classes/RecordNotFound.html +3 -3
  125. package/docs/classes/ValidationError.html +3 -3
  126. package/docs/functions/DreamSerializer.html +1 -1
  127. package/docs/functions/ObjectSerializer.html +1 -1
  128. package/docs/functions/ReplicaSafe.html +1 -1
  129. package/docs/functions/STI.html +1 -1
  130. package/docs/functions/SoftDelete.html +1 -1
  131. package/docs/functions/camelize.html +1 -1
  132. package/docs/functions/capitalize.html +1 -1
  133. package/docs/functions/cloneDeepSafe.html +1 -1
  134. package/docs/functions/closeAllDbConnections.html +1 -1
  135. package/docs/functions/compact.html +1 -1
  136. package/docs/functions/dreamDbConnections.html +1 -1
  137. package/docs/functions/dreamPath.html +1 -1
  138. package/docs/functions/expandStiClasses.html +1 -1
  139. package/docs/functions/generateDream.html +1 -1
  140. package/docs/functions/globalClassNameFromFullyQualifiedModelName.html +1 -1
  141. package/docs/functions/groupBy.html +1 -1
  142. package/docs/functions/hyphenize.html +1 -1
  143. package/docs/functions/inferSerializerFromDreamOrViewModel.html +1 -1
  144. package/docs/functions/inferSerializersFromDreamClassOrViewModelClass.html +1 -1
  145. package/docs/functions/intersection.html +1 -1
  146. package/docs/functions/isDreamSerializer.html +1 -1
  147. package/docs/functions/isEmpty.html +1 -1
  148. package/docs/functions/loadRepl.html +1 -1
  149. package/docs/functions/lookupClassByGlobalName.html +1 -1
  150. package/docs/functions/normalizeUnicode.html +1 -1
  151. package/docs/functions/pascalize.html +1 -1
  152. package/docs/functions/pgErrorType.html +1 -1
  153. package/docs/functions/range-1.html +1 -1
  154. package/docs/functions/relativeDreamPath.html +1 -1
  155. package/docs/functions/round.html +1 -1
  156. package/docs/functions/serializerNameFromFullyQualifiedModelName.html +1 -1
  157. package/docs/functions/sharedPathPrefix.html +1 -1
  158. package/docs/functions/snakeify.html +1 -1
  159. package/docs/functions/sort.html +1 -1
  160. package/docs/functions/sortBy.html +1 -1
  161. package/docs/functions/sortObjectByKey.html +1 -1
  162. package/docs/functions/sortObjectByValue.html +1 -1
  163. package/docs/functions/standardizeFullyQualifiedModelName.html +1 -1
  164. package/docs/functions/uncapitalize.html +1 -1
  165. package/docs/functions/uniq.html +1 -1
  166. package/docs/functions/untypedDb.html +1 -1
  167. package/docs/functions/validateColumn.html +1 -1
  168. package/docs/functions/validateTable.html +1 -1
  169. package/docs/hierarchy.html +1 -0
  170. package/docs/interfaces/BelongsToStatement.html +2 -2
  171. package/docs/interfaces/DecoratorContext.html +2 -2
  172. package/docs/interfaces/DreamAppInitOptions.html +2 -2
  173. package/docs/interfaces/DreamAppOpts.html +2 -2
  174. package/docs/interfaces/EncryptOptions.html +2 -2
  175. package/docs/interfaces/InternalAnyTypedSerializerRendersMany.html +2 -2
  176. package/docs/interfaces/InternalAnyTypedSerializerRendersOne.html +2 -2
  177. package/docs/interfaces/OpenapiDescription.html +2 -2
  178. package/docs/interfaces/OpenapiSchemaProperties.html +1 -1
  179. package/docs/interfaces/OpenapiSchemaPropertiesShorthand.html +1 -1
  180. package/docs/interfaces/OpenapiTypeFieldObject.html +1 -1
  181. package/docs/interfaces/SerializerRendererOpts.html +2 -2
  182. package/docs/modules.html +3 -0
  183. package/docs/types/Camelized.html +1 -1
  184. package/docs/types/CommonOpenapiSchemaObjectFields.html +1 -1
  185. package/docs/types/DateTime.html +1 -1
  186. package/docs/types/DbConnectionType.html +1 -1
  187. package/docs/types/DbTypes.html +1 -1
  188. package/docs/types/DreamAppAllowedPackageManagersEnum.html +1 -1
  189. package/docs/types/DreamAssociationMetadata.html +1 -1
  190. package/docs/types/DreamAttributes.html +1 -1
  191. package/docs/types/DreamClassAssociationAndStatement.html +1 -1
  192. package/docs/types/DreamClassColumn.html +1 -1
  193. package/docs/types/DreamColumn.html +1 -1
  194. package/docs/types/DreamColumnNames.html +1 -1
  195. package/docs/types/DreamLogLevel.html +1 -1
  196. package/docs/types/DreamLogger.html +1 -1
  197. package/docs/types/DreamModelSerializerType.html +1 -1
  198. package/docs/types/DreamOrViewModelClassSerializerKey.html +1 -1
  199. package/docs/types/DreamOrViewModelSerializerKey.html +1 -1
  200. package/docs/types/DreamParamSafeAttributes.html +1 -1
  201. package/docs/types/DreamParamSafeColumnNames.html +1 -1
  202. package/docs/types/DreamSerializable.html +1 -1
  203. package/docs/types/DreamSerializableArray.html +1 -1
  204. package/docs/types/DreamSerializerKey.html +1 -1
  205. package/docs/types/DreamSerializers.html +1 -1
  206. package/docs/types/DreamVirtualColumns.html +1 -1
  207. package/docs/types/EncryptAlgorithm.html +1 -1
  208. package/docs/types/HasManyStatement.html +1 -1
  209. package/docs/types/HasOneStatement.html +1 -1
  210. package/docs/types/Hyphenized.html +1 -1
  211. package/docs/types/OpenapiAllTypes.html +1 -1
  212. package/docs/types/OpenapiFormats.html +1 -1
  213. package/docs/types/OpenapiNumberFormats.html +1 -1
  214. package/docs/types/OpenapiPrimitiveBaseTypes.html +1 -1
  215. package/docs/types/OpenapiPrimitiveTypes.html +1 -1
  216. package/docs/types/OpenapiSchemaArray.html +1 -1
  217. package/docs/types/OpenapiSchemaArrayShorthand.html +1 -1
  218. package/docs/types/OpenapiSchemaBase.html +1 -1
  219. package/docs/types/OpenapiSchemaBody.html +1 -1
  220. package/docs/types/OpenapiSchemaBodyShorthand.html +1 -1
  221. package/docs/types/OpenapiSchemaCommonFields.html +1 -1
  222. package/docs/types/OpenapiSchemaExpressionAllOf.html +1 -1
  223. package/docs/types/OpenapiSchemaExpressionAnyOf.html +1 -1
  224. package/docs/types/OpenapiSchemaExpressionOneOf.html +1 -1
  225. package/docs/types/OpenapiSchemaExpressionRef.html +1 -1
  226. package/docs/types/OpenapiSchemaExpressionRefSchemaShorthand.html +1 -1
  227. package/docs/types/OpenapiSchemaInteger.html +1 -1
  228. package/docs/types/OpenapiSchemaNull.html +1 -1
  229. package/docs/types/OpenapiSchemaNumber.html +1 -1
  230. package/docs/types/OpenapiSchemaObject.html +1 -1
  231. package/docs/types/OpenapiSchemaObjectAllOf.html +1 -1
  232. package/docs/types/OpenapiSchemaObjectAllOfShorthand.html +1 -1
  233. package/docs/types/OpenapiSchemaObjectAnyOf.html +1 -1
  234. package/docs/types/OpenapiSchemaObjectAnyOfShorthand.html +1 -1
  235. package/docs/types/OpenapiSchemaObjectBase.html +1 -1
  236. package/docs/types/OpenapiSchemaObjectBaseShorthand.html +1 -1
  237. package/docs/types/OpenapiSchemaObjectOneOf.html +1 -1
  238. package/docs/types/OpenapiSchemaObjectOneOfShorthand.html +1 -1
  239. package/docs/types/OpenapiSchemaObjectShorthand.html +1 -1
  240. package/docs/types/OpenapiSchemaPrimitiveGeneric.html +1 -1
  241. package/docs/types/OpenapiSchemaShorthandExpressionAllOf.html +1 -1
  242. package/docs/types/OpenapiSchemaShorthandExpressionAnyOf.html +1 -1
  243. package/docs/types/OpenapiSchemaShorthandExpressionOneOf.html +1 -1
  244. package/docs/types/OpenapiSchemaShorthandExpressionSerializableRef.html +1 -1
  245. package/docs/types/OpenapiSchemaShorthandExpressionSerializerRef.html +1 -1
  246. package/docs/types/OpenapiSchemaShorthandPrimitiveGeneric.html +1 -1
  247. package/docs/types/OpenapiSchemaString.html +1 -1
  248. package/docs/types/OpenapiShorthandAllTypes.html +1 -1
  249. package/docs/types/OpenapiShorthandPrimitiveBaseTypes.html +1 -1
  250. package/docs/types/OpenapiShorthandPrimitiveTypes.html +1 -1
  251. package/docs/types/OpenapiTypeField.html +1 -1
  252. package/docs/types/Pascalized.html +1 -1
  253. package/docs/types/RoundingPrecision.html +1 -1
  254. package/docs/types/SerializerCasing.html +1 -1
  255. package/docs/types/SimpleObjectSerializerType.html +1 -1
  256. package/docs/types/Snakeified.html +1 -1
  257. package/docs/types/UpdateableAssociationProperties.html +1 -1
  258. package/docs/types/UpdateableProperties.html +1 -1
  259. package/docs/types/ValidationType.html +1 -1
  260. package/docs/types/ViewModel.html +1 -1
  261. package/docs/types/ViewModelClass.html +1 -1
  262. package/docs/types/WhereStatementForDream.html +1 -1
  263. package/docs/types/WhereStatementForDreamClass.html +1 -1
  264. package/docs/variables/DateTime-1.html +1 -1
  265. package/docs/variables/DreamAppAllowedPackageManagersEnumValues.html +1 -1
  266. package/docs/variables/DreamConst.html +1 -1
  267. package/docs/variables/TRIGRAM_OPERATORS.html +1 -1
  268. package/docs/variables/openapiPrimitiveTypes-1.html +1 -1
  269. package/docs/variables/openapiShorthandPrimitiveTypes-1.html +1 -1
  270. package/docs/variables/ops.html +1 -1
  271. package/docs/variables/primaryKeyTypes.html +1 -1
  272. package/package.json +4 -3
  273. package/dist/cjs/src/helpers/db/truncateDb.js +0 -27
  274. package/dist/esm/src/helpers/db/truncateDb.js +0 -24
  275. package/dist/types/src/bin/helpers/sync.d.ts +0 -1
  276. package/dist/types/src/helpers/db/createDb.d.ts +0 -2
  277. package/dist/types/src/helpers/db/dropDb.d.ts +0 -2
  278. package/dist/types/src/helpers/db/foreignKeyTypeFromPrimaryKey.d.ts +0 -2
  279. package/dist/types/src/helpers/db/loadPgClient.d.ts +0 -4
  280. package/dist/types/src/helpers/db/runMigration.d.ts +0 -6
  281. package/dist/types/src/helpers/db/truncateDb.d.ts +0 -1
  282. /package/dist/cjs/src/{helpers/db → dream/QueryDriver/helpers/kysely}/foreignKeyTypeFromPrimaryKey.js +0 -0
  283. /package/dist/esm/src/{helpers/db → dream/QueryDriver/helpers/kysely}/foreignKeyTypeFromPrimaryKey.js +0 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,166 @@
1
+ ## 1.5.0
2
+
3
+ - add support for multiple database connections in a single dream application. To take advantage of this new feature, you can do the following:
4
+
5
+ 1. Add a new connection configuration to your conf/dream.ts file, providing it an explicit alternate connection name as the second argument, like so:
6
+
7
+ ```ts
8
+ dreamApp.set('db', 'myAlternateConnection', {
9
+ primary: {
10
+ user: AppEnv.string('DB_USER'),
11
+ password: AppEnv.string('DB_PASSWORD', { optional: !AppEnv.isProduction }),
12
+ host: AppEnv.string('DB_HOST', { optional: true }),
13
+ name: AppEnv.string('DB_NAME_2', { optional: true }),
14
+ port: AppEnv.integer('DB_PORT_2', { optional: true }),
15
+ useSsl: false,
16
+ },
17
+ })
18
+ ```
19
+
20
+ Be sure to add any new environment variables to your .env and .env.test files.
21
+
22
+ 2. Run sync
23
+
24
+ ```sh
25
+ yarn psy sync
26
+ ```
27
+
28
+ 3. add a new application model for your new connection, naming it the name of your connection, pascalized, with the string `ApplicationModel` at the end, like so:
29
+
30
+ ```ts
31
+ // app/models/MyAlternateConnectionApplicationModel.ts
32
+
33
+ import Dream from '../../../src/Dream.js'
34
+ import { DBClass } from '../../types/db.alternateConnection.js'
35
+ import { connectionTypeConfig, schema } from '../../types/dream.alternateConnection.js'
36
+ import { globalTypeConfig } from '../../types/dream.globals.js'
37
+
38
+ export default class MyAlternateConnectionApplicationModel extends Dream {
39
+ declare public DB: DBClass
40
+
41
+ public override get connectionName() {
42
+ return 'alternateConnection' as const
43
+ }
44
+
45
+ public override get schema(): any {
46
+ return schema
47
+ }
48
+
49
+ public override get connectionTypeConfig() {
50
+ return connectionTypeConfig
51
+ }
52
+
53
+ public override get globalTypeConfig() {
54
+ return globalTypeConfig
55
+ }
56
+ }
57
+ ```
58
+
59
+ 4. Now you can proceed to generate a model for your new connection, like so:
60
+
61
+ ```sh
62
+ yarn psy g:model MyNewModel someField:text --connection-name=myAlternateConnection
63
+ ```
64
+
65
+ Dream will automatically read the connectionName and use it to derive the `MyAlternateConnectionApplicationModel` automatically, though if this isn't correct, you will need to manually adjust it.
66
+
67
+ ## 1.4.2
68
+
69
+ - add ability to set custom import extension, which will be used when generating new files for your application
70
+
71
+ ## 1.4.1
72
+
73
+ - cache Dream app earlier in the initialization sequence
74
+
75
+ ## 1.4.0
76
+
77
+ - fix `preloadFor` infinite loop when serializers have circular references
78
+
79
+ - generated STI base serializer updated to only include the single type of the particular STI child in the OpenAPI shape for that child
80
+
81
+ - change `primaryKeyValue` from a getter to a method so it can return the correct type even when `primaryKey` has been overridden on a particular Dream model
82
+
83
+ - remove `IdType`, an unnecessary abstraction that caused type inconsistencies
84
+
85
+ - explicitly handle bigint from the database as string
86
+
87
+ ## 1.3.3
88
+
89
+ - make `referenceTypeString` public
90
+
91
+ ## 1.3.2
92
+
93
+ - restore aliasing in preload/load queries
94
+
95
+ ## 1.3.1
96
+
97
+ - throw NotNullViolation when Postgres throws a not null violation
98
+
99
+ - throw CheckViolation when Postgres throws a check violation
100
+
101
+ ## 1.3.0
102
+
103
+ - sti-child generator includes check constraint instead of not-null since the column should only be not-null for that STI child (or modified by hand to accommodate more than one STI child)
104
+
105
+ - add `Dream#hasAssociation`
106
+
107
+ - fix preloading associations on other side of a polyorphic belongs-to association so that we don't set an association on a dream model that doesn't define that association
108
+
109
+ - fix preloading association on the other side of a polymorphic belongs-to since the same association name may be defined differently on different models. For example, taskable may be a Chore or a Workout, both of which have an `images` association, but `images` goes through ChoreImage on Chore and through WorkoutImage on Workout
110
+
111
+ - fix preloadFor when a rendersOne/Many renders a polymorphic belongs-to (was only preloading serializer associations for one of the polymorphically associated models)
112
+
113
+ - fix preloadFor when an explicit serializer option is provided to a rendersOne/Many
114
+
115
+ ## 1.2.1
116
+
117
+ - Throw DataTypeColumnTypeMismatch when Postgres throws an error attempting to save to a column with a value that can't be cast to the column type.
118
+
119
+ ## 1.2.0
120
+
121
+ - Add Dream.lookup, enabling devs to tap into the IOC provided by dream to dodge circular import issues
122
+
123
+ ## 1.1.2
124
+
125
+ - CliFileWriter does not raise error if the file we are writing is not in the file system yet.
126
+
127
+ ## 1.1.1
128
+
129
+ - Add fs.writeFile options as third argument to CliFileWriter.write, enabling psychic to provide custom flags when writing openapi.json files.
130
+
131
+ ## 1.1.0
132
+
133
+ - Remove support for preloadThroughColumns. They were broken, fixing them would be overly complex, and the same effect can be obtained using flatten on a serializer rendersOne
134
+
135
+ ## 1.0.6
136
+
137
+ - Fix joining after a through a polymorphic BelongsTo
138
+ association.
139
+
140
+ - Improve join implementation
141
+
142
+ - Disable leftJoinPreload preloadThroughColumns since it doesn't actually work on through associations that resolve to a source that doesn't match the association name (and we were unnecessarily including columns in the leftJoinPreload statement even when there was no `preloadThroughColumns`, thereby bloating the queries unnecessarily)
143
+
144
+ ## 1.0.5
145
+
146
+ - Support HasOne/Many through polymorphic BelongsTo (trick is that it uses the associated model from the HasOne/Many to limit the BelongsTo to a single associated class / table)
147
+
148
+ - Fix HasOne/Many through source type
149
+
150
+ - sync process is now fail-safe, leveraging a utility which caches old copies of files before writing to them. If an exception is thrown at any point during the process, dream will revert all files written using the new `CliFileWriter` class
151
+
152
+ ## 1.0.4
153
+
154
+ - properly exclude type from `DreamParamSafeColumnNames`
155
+
156
+ ## 1.0.3
157
+
158
+ - exclude type from `DreamParamSafeColumnNames`
159
+
160
+ ## 1.0.2
161
+
162
+ - stop computing foreign keys in schema builder when building schema for through associations
163
+
164
+ ## 1.0.1
165
+
166
+ - [bug] fix preloading STI model via polymporhic association (polymorphic type was being altered to the STI child rather than left as the STI base)
@@ -2,15 +2,13 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const yoctocolors_1 = require("yoctocolors");
4
4
  const errors_js_1 = require("./db/errors.js");
5
- const index_js_1 = require("./db/index.js");
6
5
  const associationToGetterSetterProp_js_1 = require("./decorators/field/association/associationToGetterSetterProp.js");
7
6
  const shared_js_1 = require("./decorators/field/association/shared.js");
8
7
  const shared_js_2 = require("./decorators/field/lifecycle/shared.js");
9
8
  const resortAllRecords_js_1 = require("./decorators/field/sortable/helpers/resortAllRecords.js");
10
- const index_js_2 = require("./dream-app/index.js");
9
+ const index_js_1 = require("./dream-app/index.js");
11
10
  const DreamClassTransactionBuilder_js_1 = require("./dream/DreamClassTransactionBuilder.js");
12
11
  const DreamInstanceTransactionBuilder_js_1 = require("./dream/DreamInstanceTransactionBuilder.js");
13
- const DreamTransaction_js_1 = require("./dream/DreamTransaction.js");
14
12
  const associationQuery_js_1 = require("./dream/internal/associations/associationQuery.js");
15
13
  const associationUpdateQuery_js_1 = require("./dream/internal/associations/associationUpdateQuery.js");
16
14
  const createAssociation_js_1 = require("./dream/internal/associations/createAssociation.js");
@@ -108,8 +106,14 @@ class Dream {
108
106
  get schema() {
109
107
  throw new DreamMissingRequiredOverride_js_1.default(this.constructor, 'schema');
110
108
  }
111
- get globalSchema() {
112
- throw new DreamMissingRequiredOverride_js_1.default(this.constructor, 'globalSchema');
109
+ get connectionName() {
110
+ return 'default';
111
+ }
112
+ get connectionTypeConfig() {
113
+ throw new DreamMissingRequiredOverride_js_1.default(this.constructor, 'connectionTypeConfig');
114
+ }
115
+ get globalTypeConfig() {
116
+ throw new DreamMissingRequiredOverride_js_1.default(this.constructor, 'globalTypeConfig');
113
117
  }
114
118
  /**
115
119
  * Determines if the provided Dream class is the same as or a subclass of this Dream class.
@@ -842,7 +846,7 @@ class Dream {
842
846
  * @returns the found model or serializer, depending on what you are looking for
843
847
  */
844
848
  static lookup(lookupName) {
845
- const dreamApp = index_js_2.default.getOrFail();
849
+ const dreamApp = index_js_1.default.getOrFail();
846
850
  const models = dreamApp.models;
847
851
  const serializers = dreamApp.serializers;
848
852
  return models[lookupName] || serializers[lookupName];
@@ -1733,16 +1737,8 @@ class Dream {
1733
1737
  * @returns void
1734
1738
  */
1735
1739
  static async transaction(callback) {
1736
- const dreamTransaction = new DreamTransaction_js_1.default();
1737
- let callbackResponse = undefined;
1738
- await (0, index_js_1.default)('primary')
1739
- .transaction()
1740
- .execute(async (kyselyTransaction) => {
1741
- dreamTransaction.kyselyTransaction = kyselyTransaction;
1742
- callbackResponse = (await callback(dreamTransaction));
1743
- });
1744
- await dreamTransaction.runAfterCommitHooks(dreamTransaction);
1745
- return callbackResponse;
1740
+ const dbDriverClass = Query_js_1.default.dbDriverClass(this.prototype.connectionName);
1741
+ return (await dbDriverClass.transaction(this.prototype, callback));
1746
1742
  }
1747
1743
  /**
1748
1744
  * Sends data through for use as passthrough data
@@ -1,24 +1,40 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const index_js_1 = require("../cli/index.js");
4
+ const index_js_2 = require("../dream-app/index.js");
4
5
  const Query_js_1 = require("../dream/Query.js");
5
6
  const generateDream_js_1 = require("../helpers/cli/generateDream.js");
6
7
  const sspawn_js_1 = require("../helpers/sspawn.js");
7
8
  class DreamBin {
8
9
  static async sync(onSync, options) {
9
- await Query_js_1.default.dbDriverClass().sync(onSync, options);
10
+ const dreamApp = index_js_2.default.getOrFail();
11
+ for (const connectionName of Object.keys(dreamApp.dbCredentials)) {
12
+ await Query_js_1.default.dbDriverClass(connectionName).sync(connectionName, onSync, options);
13
+ }
10
14
  }
11
15
  static async dbCreate() {
12
- await Query_js_1.default.dbDriverClass().dbCreate();
16
+ const dreamApp = index_js_2.default.getOrFail();
17
+ for (const connectionName of Object.keys(dreamApp.dbCredentials)) {
18
+ await Query_js_1.default.dbDriverClass(connectionName).dbCreate(connectionName);
19
+ }
13
20
  }
14
21
  static async dbDrop() {
15
- await Query_js_1.default.dbDriverClass().dbDrop();
22
+ const dreamApp = index_js_2.default.getOrFail();
23
+ for (const connectionName of Object.keys(dreamApp.dbCredentials)) {
24
+ await Query_js_1.default.dbDriverClass(connectionName).dbDrop(connectionName);
25
+ }
16
26
  }
17
27
  static async dbMigrate() {
18
- await Query_js_1.default.dbDriverClass().migrate();
28
+ const dreamApp = index_js_2.default.getOrFail();
29
+ for (const connectionName of Object.keys(dreamApp.dbCredentials)) {
30
+ await Query_js_1.default.dbDriverClass(connectionName).migrate(connectionName);
31
+ }
19
32
  }
20
33
  static async dbRollback(opts) {
21
- await Query_js_1.default.dbDriverClass().rollback(opts);
34
+ const dreamApp = index_js_2.default.getOrFail();
35
+ for (const connectionName of Object.keys(dreamApp.dbCredentials)) {
36
+ await Query_js_1.default.dbDriverClass(connectionName).rollback({ ...opts, connectionName });
37
+ }
22
38
  }
23
39
  static async generateDream(fullyQualifiedModelName, columnsWithTypes, options) {
24
40
  await (0, generateDream_js_1.default)({
@@ -35,8 +51,8 @@ class DreamBin {
35
51
  fullyQualifiedParentName,
36
52
  });
37
53
  }
38
- static async generateMigration(migrationName, columnsWithTypes) {
39
- await Query_js_1.default.dbDriverClass().generateMigration(migrationName, columnsWithTypes);
54
+ static async generateMigration(migrationName, columnsWithTypes, connectionName) {
55
+ await Query_js_1.default.dbDriverClass(connectionName).generateMigration(connectionName, migrationName, columnsWithTypes);
40
56
  }
41
57
  // though this is a private method, it is still used internally.
42
58
  // It is only made private so that people don't mistakenly try
@@ -15,25 +15,37 @@ ${INDENT} subtitle:string:optional
15
15
  ${INDENT}
16
16
  ${INDENT}supported types:
17
17
  ${INDENT} - citext:
18
+ ${INDENT} - citext[]:
18
19
  ${INDENT} case insensitive text (indexes and queries are automatically case insensitive)
19
20
  ${INDENT}
20
21
  ${INDENT} - string:
22
+ ${INDENT} - string[]:
21
23
  ${INDENT} varchar; allowed length defaults to 255, but may be customized, e.g.: subtitle:string:128 or subtitle:string:128:optional
22
24
  ${INDENT}
23
25
  ${INDENT} - text
26
+ ${INDENT} - text[]
24
27
  ${INDENT} - date
28
+ ${INDENT} - date[]
25
29
  ${INDENT} - datetime
30
+ ${INDENT} - datetime[]
26
31
  ${INDENT} - integer
32
+ ${INDENT} - integer[]
27
33
  ${INDENT}
28
34
  ${INDENT} - decimal:
35
+ ${INDENT} - decimal[]:
29
36
  ${INDENT} scale,precision is required, e.g.: volume:decimal:3,2 or volume:decimal:3,2:optional
37
+ ${INDENT}
38
+ ${INDENT} leveraging arrays, add the "[]" suffix, e.g.: volume:decimal[]:3,2
30
39
  ${INDENT}
31
40
  ${INDENT} - enum:
41
+ ${INDENT} - enum[]:
32
42
  ${INDENT} include the enum name to automatically create the enum:
33
43
  ${INDENT} type:enum:room_types:bathroom,kitchen,bedroom or type:enum:room_types:bathroom,kitchen,bedroom:optional
34
44
  ${INDENT}
35
45
  ${INDENT} omit the enum values to leverage an existing enum (omits the enum type creation):
36
- ${INDENT} type:enum:room_types or type:enum:room_types:optional`;
46
+ ${INDENT} type:enum:room_types or type:enum:room_types:optional
47
+ ${INDENT}
48
+ ${INDENT} leveraging arrays, add the "[]" suffix, e.g.: type:enum[]:room_types:bathroom,kitchen,bedroom`;
37
49
  const columnsWithTypesDescription = baseColumnsWithTypesDescription +
38
50
  `
39
51
  ${INDENT}
@@ -80,10 +92,11 @@ class DreamCLI {
80
92
  .alias('g:migration')
81
93
  .description('create a new migration')
82
94
  .argument('<migrationName>', 'end with -to-table-name to prepopulate with an alterTable command')
95
+ .option('--connection-name <connectionName>', 'the connection name you wish to use for your migration')
83
96
  .argument('[columnsWithTypes...]', columnsWithTypesDescriptionForMigration)
84
- .action(async (migrationName, columnsWithTypes) => {
97
+ .action(async (migrationName, columnsWithTypes, options) => {
85
98
  await initializeDreamApp();
86
- await index_js_1.default.generateMigration(migrationName, columnsWithTypes);
99
+ await index_js_1.default.generateMigration(migrationName, columnsWithTypes, options.connectionName || 'default');
87
100
  process.exit();
88
101
  });
89
102
  program
@@ -92,6 +105,7 @@ class DreamCLI {
92
105
  .alias('generate:dream')
93
106
  .alias('g:dream')
94
107
  .option('--no-serializer')
108
+ .option('--connection-name <connectionName>', 'the db connection you want this attached to (defaults to the default db connection)', 'default')
95
109
  .option('--sti-base-serializer')
96
110
  .description('create a new Dream model')
97
111
  .argument('<modelName>', 'the name of the model to create, e.g. Post or Settings/CommunicationPreferences')
@@ -106,6 +120,7 @@ class DreamCLI {
106
120
  .alias('g:sti-child')
107
121
  .description('create a new Dream model that extends another Dream model, leveraging STI (single table inheritance)')
108
122
  .option('--no-serializer')
123
+ .option('--connection-name', 'the db connection you want this model attached to (defaults to the default connection)', 'default')
109
124
  .argument('<childModelName>', 'the name of the model to create, e.g. Post or Settings/CommunicationPreferences')
110
125
  .argument('<extends>', 'just the word "extends"')
111
126
  .argument('<parentModelName>', `fully qualified name of the parent model, e.g.:
@@ -1,6 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const index_js_1 = require("../db/index.js");
4
3
  class ConnectedToDB {
5
4
  dreamInstance;
6
5
  dreamClass;
@@ -29,13 +28,6 @@ class ConnectedToDB {
29
28
  return 'primary';
30
29
  }
31
30
  }
32
- // ATTENTION FRED
33
- // stop trying to make this async. You never learn...
34
- dbFor(sqlCommandType) {
35
- if (this.dreamTransaction?.kyselyTransaction)
36
- return this.dreamTransaction?.kyselyTransaction;
37
- return (0, index_js_1.default)(this.dbConnectionType(sqlCommandType));
38
- }
39
31
  isReplicaSafe() {
40
32
  return this.innerJoinDreamClasses.reduce((accumulator, dreamClass) => accumulator && dreamClass['replicaSafe'], this.dreamClass['replicaSafe']);
41
33
  }
@@ -1,7 +1,4 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.dreamDbConnections = dreamDbConnections;
4
- exports.closeAllDbConnections = closeAllDbConnections;
5
2
  // after building for esm, importing pg using the following:
6
3
  //
7
4
  // import * as pg from 'pg'
@@ -10,19 +7,23 @@ exports.closeAllDbConnections = closeAllDbConnections;
10
7
  // when being imported from our esm build.
11
8
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
12
9
  // @ts-ignore
13
- const pg_1 = require("pg");
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.dreamDbConnections = dreamDbConnections;
12
+ exports.closeAllDbConnections = closeAllDbConnections;
13
+ exports.closeAllConnectionsForConnectionName = closeAllConnectionsForConnectionName;
14
14
  const kysely_1 = require("kysely");
15
15
  const index_js_1 = require("../dream-app/index.js");
16
+ const protectAgainstPollutingAssignment_js_1 = require("../helpers/protectAgainstPollutingAssignment.js");
16
17
  let connections = {};
17
18
  class DreamDbConnection {
18
- static getConnection(connectionType) {
19
+ static getConnection(connectionName, connectionType, dialectProvider) {
19
20
  const dreamApp = index_js_1.default.getOrFail();
20
- const connectionName = this.getConnectionTypeName(connectionType);
21
- const connection = connections[connectionName];
21
+ const connectionTypeName = this.getConnectionTypeName(connectionType);
22
+ const connection = connections[connectionName]?.[connectionTypeName];
22
23
  if (connection) {
23
24
  return connection;
24
25
  }
25
- const connectionConf = dreamApp.dbConnectionConfig(connectionType);
26
+ const connectionConf = dreamApp.dbConnectionConfig(connectionName, connectionType);
26
27
  const dbConn = new kysely_1.Kysely({
27
28
  log(event) {
28
29
  const dreamApp = index_js_1.default.getOrFail();
@@ -30,27 +31,14 @@ class DreamDbConnection {
30
31
  fn(event);
31
32
  });
32
33
  },
33
- dialect: new kysely_1.PostgresDialect({
34
- pool: new pg_1.default.Pool({
35
- user: connectionConf.user || '',
36
- password: connectionConf.password || '',
37
- database: dreamApp.dbName(connectionType),
38
- host: connectionConf.host || 'localhost',
39
- port: connectionConf.port || 5432,
40
- ssl: connectionConf.useSsl ? sslConfig(connectionConf) : false,
41
- }),
42
- }),
34
+ dialect: dialectProvider(connectionConf),
43
35
  plugins: [new kysely_1.CamelCasePlugin({ underscoreBetweenUppercaseLetters: true })],
44
36
  });
45
- connections[this.getConnectionTypeName(connectionType)] = dbConn;
37
+ const protectedName = (0, protectAgainstPollutingAssignment_js_1.default)(connectionName);
38
+ connections[protectedName] ||= {};
39
+ connections[protectedName][this.getConnectionTypeName(connectionType)] = dbConn;
46
40
  return dbConn;
47
41
  }
48
- static async dropAllConnections() {
49
- for (const key of Object.keys(connections)) {
50
- await connections[key]?.destroy();
51
- delete connections[key];
52
- }
53
- }
54
42
  static getConnectionTypeName(connectionType) {
55
43
  return index_js_1.default.getOrFail().parallelDatabasesEnabled
56
44
  ? `${connectionType}_${process.env.VITEST_POOL_ID}`
@@ -58,20 +46,21 @@ class DreamDbConnection {
58
46
  }
59
47
  }
60
48
  exports.default = DreamDbConnection;
61
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
62
- function sslConfig(connectionConf) {
63
- // TODO: properly configure (https://rvohealth.atlassian.net/browse/PDTC-2914)
64
- return {
65
- rejectUnauthorized: false,
66
- // ca: fs.readFileSync('/path/to/server-certificates/root.crt').toString(),
67
- // key: fs.readFileSync('/path/to/client-key/postgresql.key').toString(),
68
- // cert: fs.readFileSync('/path/to/client-certificates/postgresql.crt').toString(),
69
- };
70
- }
71
49
  function dreamDbConnections() {
72
50
  return connections;
73
51
  }
74
52
  async function closeAllDbConnections() {
75
- await Promise.all(Object.values(connections).map(conn => conn.destroy()));
53
+ const connectionNames = Object.keys(connections);
54
+ for (const connectionName of connectionNames) {
55
+ await closeAllConnectionsForConnectionName(connectionName);
56
+ }
76
57
  connections = {};
77
58
  }
59
+ async function closeAllConnectionsForConnectionName(connectionName) {
60
+ const protectedName = (0, protectAgainstPollutingAssignment_js_1.default)(connectionName);
61
+ return await Promise.allSettled(Object.keys(connections[protectedName]).map(async (key) => {
62
+ const conn = connections[protectedName][key];
63
+ await conn.destroy();
64
+ delete connections[protectedName][key];
65
+ }));
66
+ }
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = dbTypesFilenameForConnection;
4
+ function dbTypesFilenameForConnection(connectionName) {
5
+ return connectionName === 'default' ? 'db.ts' : `db.${connectionName}.ts`;
6
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = writeSyncFile;
3
+ exports.default = syncDbTypesFiles;
4
4
  const fs = require("node:fs/promises");
5
5
  const path = require("node:path");
6
6
  const CliFileWriter_js_1 = require("../../cli/CliFileWriter.js");
@@ -13,13 +13,16 @@ const EnvInternal_js_1 = require("../../helpers/EnvInternal.js");
13
13
  const dreamPath_js_1 = require("../../helpers/path/dreamPath.js");
14
14
  const snakeify_js_1 = require("../../helpers/snakeify.js");
15
15
  const sspawn_js_1 = require("../../helpers/sspawn.js");
16
- async function writeSyncFile() {
16
+ const dbTypesFilenameForConnection_js_1 = require("./dbTypesFilenameForConnection.js");
17
+ async function syncDbTypesFiles(connectionName) {
17
18
  const dreamApp = index_js_2.default.getOrFail();
18
- const dbConf = dreamApp.dbConnectionConfig('primary');
19
- const dbSyncFilePath = path.join((0, dreamPath_js_1.default)('types'), 'db.ts');
19
+ const dbConf = dreamApp.dbConnectionConfig(connectionName, 'primary');
20
+ const driverClass = dreamApp.dbConnectionQueryDriverClass(connectionName);
21
+ const dbFilename = (0, dbTypesFilenameForConnection_js_1.default)(connectionName);
22
+ const dbSyncFilePath = path.join((0, dreamPath_js_1.default)('types'), dbFilename);
20
23
  const absoluteDbSyncPath = path.join(dreamApp.projectRoot, dbSyncFilePath);
21
24
  await CliFileWriter_js_1.CliFileWriter.cache(absoluteDbSyncPath);
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}`, {
25
+ await (0, sspawn_js_1.default)(`kysely-codegen --dialect=${driverClass.syncDialect} --url=${driverClass.syncDialect}://${dbConf.user}:${dbConf.password}@${dbConf.host}:${dbConf.port}/${dbConf.name} --out-file=${absoluteDbSyncPath}`, {
23
26
  onStdout: message => {
24
27
  index_js_1.default.logger.logContinueProgress((0, colorize_js_1.default)(`[db]`, { color: 'cyan' }) + ' ' + message, {
25
28
  logPrefixColor: 'cyan',
@@ -4,8 +4,9 @@ exports.default = db;
4
4
  const DateTime_js_1 = require("../helpers/DateTime.js");
5
5
  const EnvInternal_js_1 = require("../helpers/EnvInternal.js");
6
6
  const DreamDbConnection_js_1 = require("./DreamDbConnection.js");
7
+ const Postgres_js_1 = require("../dream/QueryDriver/Postgres.js");
7
8
  if (EnvInternal_js_1.default.string('TZ', { optional: true }))
8
9
  DateTime_js_1.Settings.defaultZone = EnvInternal_js_1.default.string('TZ');
9
- function db(connectionType = 'primary') {
10
- return DreamDbConnection_js_1.default.getConnection(connectionType);
10
+ function db(connectionName = 'default', connectionType = 'primary', dialectProvider = Postgres_js_1.default.dialectProvider(connectionName, connectionType)) {
11
+ return DreamDbConnection_js_1.default.getConnection(connectionName, connectionType, dialectProvider);
11
12
  }
@@ -2,7 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = setPosition;
4
4
  exports.applySortableScopesToQuery = applySortableScopesToQuery;
5
- const index_js_1 = require("../../../../db/index.js");
5
+ exports.getPostgresQueryDriver = getPostgresQueryDriver;
6
+ const index_js_1 = require("../../../../dream-app/index.js");
7
+ const Postgres_js_1 = require("../../../../dream/QueryDriver/Postgres.js");
6
8
  const range_js_1 = require("../../../../helpers/range.js");
7
9
  const getColumnForSortableScope_js_1 = require("./getColumnForSortableScope.js");
8
10
  const scopeArray_js_1 = require("./scopeArray.js");
@@ -79,7 +81,10 @@ async function applyUpdates({ position, previousPosition, dream, positionField,
79
81
  }
80
82
  async function setNewPosition({ dream, positionField, query, scope, previousPosition, txn, }) {
81
83
  const newPosition = (await (0, sortableQueryExcludingDream_js_1.default)(dream, query, scope).max(positionField)) + 1;
82
- const dbOrTxn = txn ? txn.kyselyTransaction : (0, index_js_1.default)('primary');
84
+ const queryDriverClass = getPostgresQueryDriver(dream.connectionName || 'default');
85
+ const dbOrTxn = txn
86
+ ? txn.kyselyTransaction
87
+ : queryDriverClass.dbFor(dream.connectionName || 'default', 'primary');
83
88
  await dbOrTxn
84
89
  .updateTable(dream.table)
85
90
  .where(dream['_primaryKey'], '=', dream.primaryKeyValue())
@@ -156,3 +161,13 @@ async function updatePositionForRecord(txn, dream, positionField, position) {
156
161
  })
157
162
  .execute();
158
163
  }
164
+ function getPostgresQueryDriver(connectionName) {
165
+ const queryDriverClass = index_js_1.default.getOrFail().dbConnectionQueryDriverClass(connectionName);
166
+ if (!(queryDriverClass === Postgres_js_1.default)) {
167
+ throw new Error(`
168
+ ${queryDriverClass.name} is not an instance of PostgresQueryDriver.
169
+ You must be using the PostgresQueryDriver when leveraging the Sortable decorator
170
+ `);
171
+ }
172
+ return queryDriverClass;
173
+ }
@@ -20,7 +20,6 @@ const index_js_2 = require("../ops/index.js");
20
20
  const computedPaginatePage_js_1 = require("./internal/computedPaginatePage.js");
21
21
  const convertDreamClassAndAssociationNameTupleArrayToPreloadArgs_js_1 = require("./internal/convertDreamClassAndAssociationNameTupleArrayToPreloadArgs.js");
22
22
  const extractNestedPaths_js_1 = require("./internal/extractNestedPaths.js");
23
- const Postgres_js_1 = require("./QueryDriver/Postgres.js");
24
23
  class Query {
25
24
  /**
26
25
  * @internal
@@ -1170,13 +1169,17 @@ class Query {
1170
1169
  async all(options = {}) {
1171
1170
  return await this.dbDriverInstance().takeAll(options);
1172
1171
  }
1173
- static dbDriverClass() {
1174
- return Postgres_js_1.default;
1172
+ static dbDriverClass(connectionName) {
1173
+ const dreamApp = index_js_1.default.getOrFail();
1174
+ return dreamApp.dbConnectionQueryDriverClass(connectionName);
1175
1175
  }
1176
1176
  dbDriverInstance(query = this) {
1177
- const driverClass = Query.dbDriverClass();
1177
+ const driverClass = Query.dbDriverClass(this.connectionName);
1178
1178
  return new driverClass(query);
1179
1179
  }
1180
+ get connectionName() {
1181
+ return this.dreamInstance.connectionName || 'default';
1182
+ }
1180
1183
  /**
1181
1184
  * Paginates the results of your query, accepting a pageSize and page argument,
1182
1185
  * which it uses to segment your query into pages, leveraging limit and offset
@@ -1281,7 +1284,7 @@ class Query {
1281
1284
  const query = this.orderStatements.length
1282
1285
  ? this
1283
1286
  : this.order({ [this.namespacedPrimaryKey]: 'asc' });
1284
- const dbDriverClass = Query.dbDriverClass();
1287
+ const dbDriverClass = Query.dbDriverClass(this.connectionName);
1285
1288
  return await new dbDriverClass(query).takeOne();
1286
1289
  }
1287
1290
  /**
@@ -1321,7 +1324,7 @@ class Query {
1321
1324
  const query = this.orderStatements.length
1322
1325
  ? this.invertOrder()
1323
1326
  : this.order({ [this.namespacedPrimaryKey]: 'desc' });
1324
- const dbDriverClass = Query.dbDriverClass();
1327
+ const dbDriverClass = Query.dbDriverClass(this.connectionName);
1325
1328
  return await new dbDriverClass(query).takeOne();
1326
1329
  }
1327
1330
  /**