@rvoh/dream 2.36.0 → 2.37.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 (242) hide show
  1. package/dist/cjs/src/Dream.js +3 -1
  2. package/dist/cjs/src/db/dataTypes.js +0 -8
  3. package/dist/cjs/src/decorators/field/Encrypted.js +8 -3
  4. package/dist/cjs/src/dream/QueryDriver/Base.js +17 -24
  5. package/dist/cjs/src/dream/QueryDriver/Kysely.js +22 -23
  6. package/dist/cjs/src/dream/QueryDriver/Postgres.js +113 -31
  7. package/dist/cjs/src/encrypt/InternalEncrypt.js +28 -0
  8. package/dist/cjs/src/helpers/cli/ASTBuilder.js +11 -202
  9. package/dist/cjs/src/helpers/cli/ASTConnectionBuilder.js +97 -75
  10. package/dist/cjs/src/helpers/cli/ASTDbTypesBuilder.js +132 -0
  11. package/dist/cjs/src/helpers/cli/ASTSchemaBuilder.js +37 -81
  12. package/dist/cjs/src/helpers/cli/dbTypesNaming.js +90 -0
  13. package/dist/cjs/src/helpers/cli/tableMatchesPattern.js +21 -0
  14. package/dist/esm/src/Dream.js +3 -1
  15. package/dist/esm/src/db/dataTypes.js +0 -8
  16. package/dist/esm/src/decorators/field/Encrypted.js +8 -3
  17. package/dist/esm/src/dream/QueryDriver/Base.js +17 -24
  18. package/dist/esm/src/dream/QueryDriver/Kysely.js +22 -23
  19. package/dist/esm/src/dream/QueryDriver/Postgres.js +113 -31
  20. package/dist/esm/src/encrypt/InternalEncrypt.js +28 -0
  21. package/dist/esm/src/helpers/cli/ASTBuilder.js +11 -202
  22. package/dist/esm/src/helpers/cli/ASTConnectionBuilder.js +97 -75
  23. package/dist/esm/src/helpers/cli/ASTDbTypesBuilder.js +132 -0
  24. package/dist/esm/src/helpers/cli/ASTSchemaBuilder.js +37 -81
  25. package/dist/esm/src/helpers/cli/dbTypesNaming.js +90 -0
  26. package/dist/esm/src/helpers/cli/tableMatchesPattern.js +21 -0
  27. package/dist/types/src/Dream.d.ts +3 -1
  28. package/dist/types/src/db/dataTypes.d.ts +0 -2
  29. package/dist/types/src/dream/QueryDriver/Base.d.ts +16 -18
  30. package/dist/types/src/dream/QueryDriver/Kysely.d.ts +0 -9
  31. package/dist/types/src/dream/QueryDriver/Postgres.d.ts +9 -9
  32. package/dist/types/src/dream-app/index.d.ts +12 -12
  33. package/dist/types/src/encrypt/InternalEncrypt.d.ts +9 -0
  34. package/dist/types/src/helpers/cli/ASTBuilder.d.ts +11 -101
  35. package/dist/types/src/helpers/cli/ASTConnectionBuilder.d.ts +40 -37
  36. package/dist/types/src/helpers/cli/ASTDbTypesBuilder.d.ts +26 -0
  37. package/dist/types/src/helpers/cli/ASTSchemaBuilder.d.ts +5 -15
  38. package/dist/types/src/helpers/cli/dbTypesNaming.d.ts +37 -0
  39. package/dist/types/src/helpers/cli/tableMatchesPattern.d.ts +15 -0
  40. package/dist/types/src/package-exports/types.d.ts +1 -1
  41. package/dist/types/src/types/db.d.ts +71 -0
  42. package/dist/types/src/types/types/db.ts +91 -0
  43. package/docs/assets/hierarchy.js +1 -1
  44. package/docs/assets/navigation.js +1 -1
  45. package/docs/assets/search.js +1 -1
  46. package/docs/classes/db.DreamMigrationHelpers.html +11 -11
  47. package/docs/classes/db.KyselyQueryDriver.html +55 -54
  48. package/docs/classes/db.PostgresQueryDriver.html +56 -56
  49. package/docs/classes/db.QueryDriverBase.html +60 -59
  50. package/docs/classes/errors.CannotSaveMissingDream.html +2 -2
  51. package/docs/classes/errors.CheckConstraintViolation.html +3 -3
  52. package/docs/classes/errors.ColumnOverflow.html +3 -3
  53. package/docs/classes/errors.CreateOrFindByFailedToCreateAndFind.html +3 -3
  54. package/docs/classes/errors.DataIncompatibleWithDatabaseField.html +3 -3
  55. package/docs/classes/errors.DataTypeColumnTypeMismatch.html +3 -3
  56. package/docs/classes/errors.DecryptionError.html +2 -2
  57. package/docs/classes/errors.DecryptionParseError.html +2 -2
  58. package/docs/classes/errors.DecryptionRotationError.html +3 -3
  59. package/docs/classes/errors.GlobalNameNotSet.html +3 -3
  60. package/docs/classes/errors.InvalidCalendarDate.html +2 -2
  61. package/docs/classes/errors.InvalidClockTime.html +2 -2
  62. package/docs/classes/errors.InvalidClockTimeTz.html +2 -2
  63. package/docs/classes/errors.InvalidDateTime.html +2 -2
  64. package/docs/classes/errors.MissingSerializersDefinition.html +3 -3
  65. package/docs/classes/errors.NonLoadedAssociation.html +3 -3
  66. package/docs/classes/errors.NotNullViolation.html +3 -3
  67. package/docs/classes/errors.RecordNotFound.html +3 -3
  68. package/docs/classes/errors.SortableScopeDidNotStabilize.html +3 -3
  69. package/docs/classes/errors.SortableScopeLockWaitTimedOut.html +3 -3
  70. package/docs/classes/errors.ValidationError.html +3 -3
  71. package/docs/classes/index.CalendarDate.html +33 -33
  72. package/docs/classes/index.ClockTime.html +32 -32
  73. package/docs/classes/index.ClockTimeTz.html +35 -35
  74. package/docs/classes/index.DateTime.html +86 -86
  75. package/docs/classes/index.Decorators.html +19 -19
  76. package/docs/classes/index.Dream.html +125 -125
  77. package/docs/classes/index.DreamApp.html +11 -11
  78. package/docs/classes/index.DreamTransaction.html +3 -3
  79. package/docs/classes/index.Env.html +5 -5
  80. package/docs/classes/index.Query.html +65 -65
  81. package/docs/classes/system.CliFileWriter.html +4 -4
  82. package/docs/classes/system.DreamBin.html +3 -3
  83. package/docs/classes/system.DreamCLI.html +7 -7
  84. package/docs/classes/system.DreamImporter.html +2 -2
  85. package/docs/classes/system.DreamLogos.html +2 -2
  86. package/docs/classes/system.DreamSerializerBuilder.html +11 -11
  87. package/docs/classes/system.ObjectSerializerBuilder.html +8 -8
  88. package/docs/classes/system.PathHelpers.html +3 -3
  89. package/docs/classes/utils.Encrypt.html +4 -4
  90. package/docs/classes/utils.Range.html +2 -2
  91. package/docs/functions/db.closeAllDbConnections.html +1 -1
  92. package/docs/functions/db.dreamDbConnections.html +1 -1
  93. package/docs/functions/db.untypedDb.html +1 -1
  94. package/docs/functions/db.validateColumn.html +1 -1
  95. package/docs/functions/db.validateTable.html +1 -1
  96. package/docs/functions/errors.pgErrorType.html +1 -1
  97. package/docs/functions/index.DreamSerializer.html +1 -1
  98. package/docs/functions/index.ObjectSerializer.html +1 -1
  99. package/docs/functions/index.ReplicaSafe.html +1 -1
  100. package/docs/functions/index.STI.html +1 -1
  101. package/docs/functions/index.SoftDelete.html +1 -1
  102. package/docs/functions/utils.camelize.html +1 -1
  103. package/docs/functions/utils.capitalize.html +1 -1
  104. package/docs/functions/utils.cloneDeepSafe.html +1 -1
  105. package/docs/functions/utils.compact.html +1 -1
  106. package/docs/functions/utils.groupBy.html +1 -1
  107. package/docs/functions/utils.hyphenize.html +1 -1
  108. package/docs/functions/utils.intersection.html +1 -1
  109. package/docs/functions/utils.isEmpty.html +1 -1
  110. package/docs/functions/utils.normalizeUnicode.html +1 -1
  111. package/docs/functions/utils.pascalize.html +1 -1
  112. package/docs/functions/utils.percent.html +1 -1
  113. package/docs/functions/utils.range.html +1 -1
  114. package/docs/functions/utils.round.html +1 -1
  115. package/docs/functions/utils.sanitizeString.html +1 -1
  116. package/docs/functions/utils.snakeify.html +1 -1
  117. package/docs/functions/utils.sort.html +1 -1
  118. package/docs/functions/utils.sortBy.html +1 -1
  119. package/docs/functions/utils.sortObjectByKey.html +1 -1
  120. package/docs/functions/utils.sortObjectByValue.html +1 -1
  121. package/docs/functions/utils.uncapitalize.html +1 -1
  122. package/docs/functions/utils.uniq.html +1 -1
  123. package/docs/hierarchy.html +1 -1
  124. package/docs/interfaces/openapi.OpenapiDescription.html +2 -2
  125. package/docs/interfaces/openapi.OpenapiSchemaProperties.html +1 -1
  126. package/docs/interfaces/openapi.OpenapiSchemaPropertiesShorthand.html +1 -1
  127. package/docs/interfaces/openapi.OpenapiTypeFieldObject.html +1 -1
  128. package/docs/interfaces/types.BelongsToStatement.html +2 -2
  129. package/docs/interfaces/types.DecoratorContext.html +2 -2
  130. package/docs/interfaces/types.DecryptCallbacks.html +2 -2
  131. package/docs/interfaces/types.DreamAppInitOptions.html +2 -2
  132. package/docs/interfaces/types.DreamAppOpts.html +2 -2
  133. package/docs/interfaces/types.DreamDbConfig.html +6 -6
  134. package/docs/interfaces/types.DurationObject.html +2 -2
  135. package/docs/interfaces/types.EncryptOptions.html +2 -2
  136. package/docs/interfaces/types.EncryptionLegacyKeyUsedEvent.html +4 -4
  137. package/docs/interfaces/types.InternalAnyTypedSerializerRendersMany.html +2 -2
  138. package/docs/interfaces/types.InternalAnyTypedSerializerRendersOne.html +2 -2
  139. package/docs/interfaces/types.IntrospectedColumn.html +17 -0
  140. package/docs/interfaces/types.IntrospectedDatabase.html +7 -0
  141. package/docs/interfaces/types.IntrospectedEnum.html +5 -0
  142. package/docs/interfaces/types.IntrospectedTable.html +11 -0
  143. package/docs/interfaces/types.SerializerRendererOpts.html +2 -2
  144. package/docs/modules/types.html +1 -1
  145. package/docs/types/openapi.CommonOpenapiSchemaObjectFields.html +1 -1
  146. package/docs/types/openapi.OpenapiAllTypes.html +1 -1
  147. package/docs/types/openapi.OpenapiFormats.html +1 -1
  148. package/docs/types/openapi.OpenapiNumberFormats.html +1 -1
  149. package/docs/types/openapi.OpenapiPrimitiveBaseTypes.html +1 -1
  150. package/docs/types/openapi.OpenapiPrimitiveTypes.html +1 -1
  151. package/docs/types/openapi.OpenapiSchemaArray.html +1 -1
  152. package/docs/types/openapi.OpenapiSchemaArrayShorthand.html +1 -1
  153. package/docs/types/openapi.OpenapiSchemaBase.html +1 -1
  154. package/docs/types/openapi.OpenapiSchemaBody.html +1 -1
  155. package/docs/types/openapi.OpenapiSchemaBodyShorthand.html +1 -1
  156. package/docs/types/openapi.OpenapiSchemaCombinatorFields.html +1 -1
  157. package/docs/types/openapi.OpenapiSchemaCommonFields.html +1 -1
  158. package/docs/types/openapi.OpenapiSchemaExpressionAllOf.html +1 -1
  159. package/docs/types/openapi.OpenapiSchemaExpressionAnyOf.html +1 -1
  160. package/docs/types/openapi.OpenapiSchemaExpressionOneOf.html +1 -1
  161. package/docs/types/openapi.OpenapiSchemaExpressionRef.html +2 -2
  162. package/docs/types/openapi.OpenapiSchemaExpressionRefSchemaShorthand.html +2 -2
  163. package/docs/types/openapi.OpenapiSchemaInteger.html +1 -1
  164. package/docs/types/openapi.OpenapiSchemaNull.html +2 -2
  165. package/docs/types/openapi.OpenapiSchemaNumber.html +1 -1
  166. package/docs/types/openapi.OpenapiSchemaObject.html +1 -1
  167. package/docs/types/openapi.OpenapiSchemaObjectAllOf.html +1 -1
  168. package/docs/types/openapi.OpenapiSchemaObjectAllOfShorthand.html +1 -1
  169. package/docs/types/openapi.OpenapiSchemaObjectBase.html +1 -1
  170. package/docs/types/openapi.OpenapiSchemaObjectBaseShorthand.html +1 -1
  171. package/docs/types/openapi.OpenapiSchemaObjectShorthand.html +1 -1
  172. package/docs/types/openapi.OpenapiSchemaPrimitiveGeneric.html +1 -1
  173. package/docs/types/openapi.OpenapiSchemaShorthandExpressionAllOf.html +1 -1
  174. package/docs/types/openapi.OpenapiSchemaShorthandExpressionAnyOf.html +1 -1
  175. package/docs/types/openapi.OpenapiSchemaShorthandExpressionOneOf.html +1 -1
  176. package/docs/types/openapi.OpenapiSchemaShorthandExpressionSerializableRef.html +2 -2
  177. package/docs/types/openapi.OpenapiSchemaShorthandExpressionSerializerRef.html +2 -2
  178. package/docs/types/openapi.OpenapiSchemaShorthandPrimitiveGeneric.html +1 -1
  179. package/docs/types/openapi.OpenapiSchemaString.html +1 -1
  180. package/docs/types/openapi.OpenapiShorthandAllTypes.html +1 -1
  181. package/docs/types/openapi.OpenapiShorthandPrimitiveBaseTypes.html +1 -1
  182. package/docs/types/openapi.OpenapiShorthandPrimitiveTypes.html +1 -1
  183. package/docs/types/openapi.OpenapiTypeField.html +1 -1
  184. package/docs/types/system.DreamAppAllowedPackageManagersEnum.html +1 -1
  185. package/docs/types/types.CalendarDateDurationUnit.html +1 -1
  186. package/docs/types/types.CalendarDateObject.html +1 -1
  187. package/docs/types/types.Camelized.html +1 -1
  188. package/docs/types/types.ClockTimeObject.html +1 -1
  189. package/docs/types/types.DbConnectionType.html +1 -1
  190. package/docs/types/types.DbTypes.html +1 -1
  191. package/docs/types/types.DreamAssociationMetadata.html +1 -1
  192. package/docs/types/types.DreamAttributes.html +1 -1
  193. package/docs/types/types.DreamClassAssociationAndStatement.html +1 -1
  194. package/docs/types/types.DreamClassColumn.html +1 -1
  195. package/docs/types/types.DreamColumn.html +1 -1
  196. package/docs/types/types.DreamColumnNames.html +1 -1
  197. package/docs/types/types.DreamLogLevel.html +1 -1
  198. package/docs/types/types.DreamLogger.html +2 -2
  199. package/docs/types/types.DreamModelSerializerType.html +1 -1
  200. package/docs/types/types.DreamOrViewModelClassSerializerKey.html +1 -1
  201. package/docs/types/types.DreamOrViewModelSerializerKey.html +1 -1
  202. package/docs/types/types.DreamParamSafeAttributes.html +1 -1
  203. package/docs/types/types.DreamParamSafeColumnNames.html +1 -1
  204. package/docs/types/types.DreamSerializable.html +1 -1
  205. package/docs/types/types.DreamSerializableArray.html +1 -1
  206. package/docs/types/types.DreamSerializerKey.html +1 -1
  207. package/docs/types/types.DreamSerializers.html +1 -1
  208. package/docs/types/types.DreamVirtualColumns.html +1 -1
  209. package/docs/types/types.DurationUnit.html +1 -1
  210. package/docs/types/types.EncryptAlgorithm.html +1 -1
  211. package/docs/types/types.HasManyStatement.html +1 -1
  212. package/docs/types/types.HasOneStatement.html +1 -1
  213. package/docs/types/types.Hyphenized.html +1 -1
  214. package/docs/types/types.IntrospectedValueType.html +5 -0
  215. package/docs/types/types.Pascalized.html +1 -1
  216. package/docs/types/types.PrimaryKeyType.html +1 -1
  217. package/docs/types/types.RoundingPrecision.html +1 -1
  218. package/docs/types/types.SerializerCasing.html +1 -1
  219. package/docs/types/types.SimpleObjectSerializerType.html +1 -1
  220. package/docs/types/types.Snakeified.html +1 -1
  221. package/docs/types/types.StrictInterface.html +1 -1
  222. package/docs/types/types.UpdateableAssociationProperties.html +1 -1
  223. package/docs/types/types.UpdateableProperties.html +1 -1
  224. package/docs/types/types.ValidationType.html +1 -1
  225. package/docs/types/types.ViewModel.html +2 -2
  226. package/docs/types/types.ViewModelClass.html +1 -1
  227. package/docs/types/types.WeekdayName.html +1 -1
  228. package/docs/types/types.WhereStatementForDream.html +1 -1
  229. package/docs/types/types.WhereStatementForDreamClass.html +1 -1
  230. package/docs/variables/index.DreamConst.html +1 -1
  231. package/docs/variables/index.ops.html +1 -1
  232. package/docs/variables/openapi.openapiPrimitiveTypes.html +1 -1
  233. package/docs/variables/openapi.openapiShorthandPrimitiveTypes.html +1 -1
  234. package/docs/variables/system.DreamAppAllowedPackageManagersEnumValues.html +1 -1
  235. package/docs/variables/system.primaryKeyTypes.html +1 -1
  236. package/package.json +6 -3
  237. package/dist/cjs/src/db/helpers/syncDbTypesFiles.js +0 -41
  238. package/dist/cjs/src/helpers/cli/ASTKyselyCodegenEnhancer.js +0 -380
  239. package/dist/esm/src/db/helpers/syncDbTypesFiles.js +0 -41
  240. package/dist/esm/src/helpers/cli/ASTKyselyCodegenEnhancer.js +0 -380
  241. package/dist/types/src/db/helpers/syncDbTypesFiles.d.ts +0 -1
  242. package/dist/types/src/helpers/cli/ASTKyselyCodegenEnhancer.d.ts +0 -94
@@ -1,9 +1,7 @@
1
1
  import { CompiledQuery, DeleteQueryBuilder, SelectQueryBuilder, UpdateQueryBuilder } from 'kysely';
2
- import type { DreamDbConfig } from '../../dream-app/index.js';
3
2
  import Dream from '../../Dream.js';
4
- import { SchemaBuilderAssociationData, SchemaBuilderColumnData } from '../../helpers/cli/ASTBuilder.js';
5
3
  import { AssociationStatement, OrderQueryStatement } from '../../types/associations/shared.js';
6
- import { DbConnectionType, LegacyCompatiblePrimaryKeyType } from '../../types/db.js';
4
+ import { DbConnectionType, IntrospectedDatabase, LegacyCompatiblePrimaryKeyType } from '../../types/db.js';
7
5
  import { DreamColumnNames, DreamConstructorType, DreamTableSchema } from '../../types/dream.js';
8
6
  import { PreloadedDreamsAndWhatTheyPointTo, QueryToKyselyDBType, QueryToKyselyTableNamesType } from '../../types/query.js';
9
7
  import DreamTransaction from '../DreamTransaction.js';
@@ -63,14 +61,12 @@ export default class QueryDriverBase<DreamInstance extends Dream> {
63
61
  * comlpex to override. You will need to do the following
64
62
  * when overriding this method:
65
63
  *
66
- * 1. introspect the db and use it to generate a db.ts file in the
67
- * same shape as the existing one. Currently, the process for generating
68
- * this file is extremely complex and messy, and will be difficult
69
- * to achieve.
64
+ * 1. generate a types/db.ts file in the same shape as the existing one.
65
+ * KyselyQueryDriver does this with the ASTDbTypesBuilder, which writes
66
+ * it from {@link introspectDatabase}.
70
67
  * 2. generate a types/dream.ts file in the same shape as the existing
71
- * one. This is normally done using the ASTSchemaBuilder
72
- * but this will likely need to be overridden to tailor to your custom
73
- * database engine.
68
+ * one. KyselyQueryDriver does this with the ASTSchemaBuilder, which
69
+ * also reads the connection through {@link introspectDatabase}.
74
70
  */
75
71
  static sync(connectionName: string, _: () => Promise<void> | void, options?: {
76
72
  schemaOnly?: boolean;
@@ -228,11 +224,16 @@ export default class QueryDriverBase<DreamInstance extends Dream> {
228
224
  * @param keys - the 64-bit signed lock keys, in acquisition order
229
225
  */
230
226
  static acquireAdvisoryTransactionLocks(txn: DreamTransaction<Dream>, keys: bigint[]): Promise<void>;
231
- static getColumnData(connectionName: string, tableName: string, allTableAssociationData: {
232
- [key: string]: SchemaBuilderAssociationData;
233
- }): Promise<{
234
- [key: string]: SchemaBuilderColumnData;
235
- }>;
227
+ /**
228
+ * Reads the tables, columns and enums of the connection's database, from
229
+ * which `sync` writes the connection's `types/db.ts` and `types/dream.ts`.
230
+ * It returns every table the connection can read, sorted by schema and
231
+ * name; `sync` applies `tableIncludePattern` and `tableExcludePattern`
232
+ * itself.
233
+ *
234
+ * @param connectionName - the connection to read
235
+ */
236
+ static introspectDatabase(connectionName: string): Promise<IntrospectedDatabase>;
236
237
  /**
237
238
  * @internal
238
239
  *
@@ -542,9 +543,6 @@ export default class QueryDriverBase<DreamInstance extends Dream> {
542
543
  * decide whether there is anything for the after-destroy hooks to act on.
543
544
  */
544
545
  static destroyDream(dream: Dream, txn: DreamTransaction<Dream>, reallyDestroy: boolean): Promise<number>;
545
- static get syncDialect(): string;
546
- /** @internal Resolve the credential used by the database type generator. */
547
- static codegenPassword(password: DreamDbConfig['password']): Promise<string>;
548
546
  /**
549
547
  * Returns the sql that would be executed by this Query
550
548
  *
@@ -3,7 +3,6 @@ import { DeleteQueryBuilder, Kysely, Transaction as KyselyTransaction, OrderByIt
3
3
  import { DialectProviderCb } from '../../db/DreamDbConnection.js';
4
4
  import { DreamDbConfig } from '../../dream-app/index.js';
5
5
  import Dream from '../../Dream.js';
6
- import { SchemaBuilderInformationSchemaRow } from '../../helpers/cli/ASTBuilder.js';
7
6
  import { AssociationStatement, OrderQueryStatement } from '../../types/associations/shared.js';
8
7
  import { DbConnectionType, LegacyCompatiblePrimaryKeyType } from '../../types/db.js';
9
8
  import { DreamColumnNames, DreamTableSchema, OrderDir, SqlCommandType } from '../../types/dream.js';
@@ -58,8 +57,6 @@ export default class KyselyQueryDriver<DreamInstance extends Dream> extends Quer
58
57
  static sync(connectionName: string, onSync: () => Promise<void> | void, options?: {
59
58
  schemaOnly?: boolean;
60
59
  }): Promise<void>;
61
- static get syncDialect(): string;
62
- static codegenPassword(password: DreamDbConfig['password']): Promise<string>;
63
60
  /**
64
61
  * @internal
65
62
  *
@@ -114,12 +111,6 @@ export default class KyselyQueryDriver<DreamInstance extends Dream> extends Quer
114
111
  * @returns void
115
112
  */
116
113
  static transaction<DreamInstance extends Dream, CB extends (txn: DreamTransaction<DreamInstance>) => unknown, RetType extends ReturnType<CB>>(dreamInstance: DreamInstance, callback: CB): Promise<RetType>;
117
- /**
118
- * @internal
119
- *
120
- * this is used by getColumnData to serialize enums
121
- */
122
- static enumType(row: SchemaBuilderInformationSchemaRow): Capitalize<string>;
123
114
  /**
124
115
  * @internal
125
116
  *
@@ -1,6 +1,6 @@
1
1
  import { type SelectQueryBuilder } from 'kysely';
2
2
  import Dream from '../../Dream.js';
3
- import { SchemaBuilderAssociationData, SchemaBuilderColumnData } from '../../helpers/cli/ASTBuilder.js';
3
+ import { IntrospectedDatabase } from '../../types/db.js';
4
4
  import DreamTransaction from '../DreamTransaction.js';
5
5
  import KyselyQueryDriver from './Kysely.js';
6
6
  import type { TestDatabaseLockSession } from './Base.js';
@@ -25,16 +25,16 @@ export default class PostgresQueryDriver<DreamInstance extends Dream> extends Ky
25
25
  */
26
26
  static setDatabaseTypeParsers(connectionName: string): Promise<void>;
27
27
  /**
28
- * @internal
28
+ * Reads the tables, views, materialized views, and partitioned and foreign
29
+ * tables of every schema the connection's user can use, except Postgres's
30
+ * own schemas, tables that inherit from another (partitions included) and
31
+ * Kysely's migration tables. A domain column reads as the type at the
32
+ * bottom of its domains. An enum outside the `public` schema is named
33
+ * `<schema>.<name>`.
29
34
  *
30
- * this is used by the SchemaBuilder to store column data permanently
31
- * within the types/dream.ts file.
35
+ * @param connectionName - the connection to read
32
36
  */
33
- static getColumnData(connectionName: string, tableName: string, allTableAssociationData: {
34
- [key: string]: SchemaBuilderAssociationData;
35
- }): Promise<{
36
- [key: string]: SchemaBuilderColumnData;
37
- }>;
37
+ static introspectDatabase(connectionName: string): Promise<IntrospectedDatabase>;
38
38
  static duplicateDatabase(connectionName: string): Promise<void>;
39
39
  /**
40
40
  * @internal
@@ -299,23 +299,23 @@ export interface DreamDbCredentialOptions {
299
299
  replica?: DreamDbConfig | undefined;
300
300
  queryDriverClass?: typeof QueryDriverBase;
301
301
  /**
302
- * a string which informs the kysely-codegen tool,
303
- * used to introspect tables under the hood, which tables to use.
304
- * Can be an actual table name, or a glob pattern, i.e.
302
+ * The tables `sync` writes types for, when it should not be every table
303
+ * the connection can read. Every table it writes types for needs a Dream
304
+ * model. A table name, or a glob in Node's `path.matchesGlob` syntax,
305
+ * compared without regard to case, e.g.
306
+ * "users"
307
+ * "chalupas*"
305
308
  * "public.*"
306
- * "public.+(user|post)"
309
+ * "public.+(users|posts)"
307
310
  *
308
- * see https://github.com/RobinBlomberg/kysely-codegen for more info
311
+ * A pattern that contains a `.` is matched against `<schema>.<table>`, and
312
+ * any other against the table's name alone.
309
313
  */
310
314
  tableIncludePattern?: string;
311
315
  /**
312
- * a string which informs the kysely-codegen tool,
313
- * used to introspect tables under the hood, which tables to use.
314
- * Can be an actual table name, or a glob pattern, i.e.
315
- * "public.*"
316
- * "public.+(user|post)"
317
- *
318
- * see https://github.com/RobinBlomberg/kysely-codegen for more info
316
+ * The tables `sync` leaves out of the types it writes, e.g. tables that no
317
+ * Dream model reads. Written like `tableIncludePattern`, and applied after
318
+ * it.
319
319
  */
320
320
  tableExcludePattern?: string;
321
321
  }
@@ -1,6 +1,15 @@
1
1
  import { DecryptCallbacks } from './index.js';
2
2
  export default class InternalEncrypt {
3
3
  static encryptColumn(data: any): string | null;
4
+ /**
5
+ * Returns `existingCiphertext` when the current column key opens it to a
6
+ * value whose JSON serialization equals `data`'s, the plaintext
7
+ * `encryptColumn(data)` would encrypt, and otherwise `encryptColumn(data)`.
8
+ * A value only the legacy key opens, ciphertext no key opens, and a
9
+ * missing `existingCiphertext` all get fresh ciphertext. It never calls
10
+ * `onLegacyKeyUsed`.
11
+ */
12
+ static reuseOrEncryptColumn(data: any, existingCiphertext: unknown): string | null;
4
13
  /**
5
14
  * Decrypts `data` with the column keys, current then legacy.
6
15
  * `onLegacyKeyUsed` is passed to `Encrypt.decrypt`, so it is called only
@@ -1,10 +1,11 @@
1
1
  import ts from 'typescript';
2
2
  import { DreamConst } from '../../dream/constants.js';
3
+ import { IntrospectedValueType } from '../../types/db.js';
3
4
  /**
4
5
  * @internal
5
6
  *
6
7
  * This is a base class, which is inherited by the ASTSchemaBuilder,
7
- * the ASTKyselyCodegenEnhancer, and the ASTGlobalSchemaBuilder,
8
+ * the ASTDbTypesBuilder, and the ASTGlobalSchemaBuilder,
8
9
  * each of which is responsible for building up the output of the various
9
10
  * type files consumed by dream internally.
10
11
  *
@@ -18,61 +19,6 @@ export default class ASTBuilder {
18
19
  * builds a new line, useful for injecting new lines into AST statements
19
20
  */
20
21
  protected newLine(): ts.Identifier;
21
- /**
22
- * @internal
23
- *
24
- * given an interface declaration, it will extrace the relevant property statement
25
- * by the given property name.
26
- */
27
- protected getPropertyFromInterface(interfaceNode: ts.InterfaceDeclaration, propertyName: string): ts.PropertySignature | null;
28
- /**
29
- * @internal
30
- *
31
- * returns an array of string type literals which were extracted from
32
- * either a type or type union, depending on what is provided
33
- * for the typeAlias. this allows you to safely and easily collect
34
- * an array of types given an alias
35
- */
36
- protected extractStringLiteralTypeNodesFromTypeOrUnion(typeAlias: ts.TypeAliasDeclaration): (ts.LiteralTypeNode & {
37
- literal: {
38
- text: string;
39
- };
40
- })[];
41
- /**
42
- * @internal
43
- *
44
- * returns an array of type literals which were extracted from
45
- * either a type or type union, depending on what is provided
46
- * for the typeAlias. this allows you to safely and easily collect
47
- * an array of types given an alias
48
- */
49
- protected extractTypeNodesFromTypeOrUnion(typeAlias: ts.TypeAliasDeclaration | ts.PropertySignature): ts.TypeNode[];
50
- /**
51
- * @internal
52
- *
53
- * returns the provided node iff
54
- * a.) the node is an exported type alias
55
- * b.) the exported name matches the provided name (or else there was no name provided)
56
- *
57
- * otherwise, returns null
58
- */
59
- protected exportedTypeAliasOrNull(node: ts.Node, exportName?: string): ts.TypeAliasDeclaration | null;
60
- /**
61
- * @internal
62
- *
63
- * returns the provided node iff
64
- * a.) the node is an exported interface
65
- * b.) the exported name matches the provided name (or else there was no name provided)
66
- *
67
- * otherwise, returns null
68
- */
69
- protected exportedInterfaceOrNull(node: ts.Node, exportName?: string): ts.InterfaceDeclaration | null;
70
- /**
71
- * @internal
72
- *
73
- * extracts the exportName from the provided dbSourceFile
74
- */
75
- protected findDbExport(dbSourceFile: ts.SourceFile, exportName: string): ts.Node | undefined;
76
22
  /**
77
23
  * @internal
78
24
  *
@@ -86,13 +32,6 @@ export default class ASTBuilder {
86
32
  * is not installed, then the original output is returned
87
33
  */
88
34
  protected prettier(output: string): Promise<string>;
89
- /**
90
- * @internal
91
- *
92
- * given a type node, it will send back the first found generic
93
- * provided to that type.
94
- */
95
- protected getFirstGenericType(node: ts.Node): ts.TypeNode | null;
96
35
  /**
97
36
  * @internal
98
37
  *
@@ -103,13 +42,6 @@ export default class ASTBuilder {
103
42
  * import them both from @rvoh/dream.
104
43
  */
105
44
  protected dateAndDateTimeImports(): ts.ImportDeclaration[];
106
- /**
107
- * @internal
108
- *
109
- * for a given table name (i.e. balloon_lines), it will return the exported
110
- * `BalloonLines` interface within the dbSourceFile
111
- */
112
- protected getTableInterfaceDeclaration(dbSourceFile: ts.SourceFile, tableName: string): ts.InterfaceDeclaration;
113
45
  /**
114
46
  * @internal
115
47
  *
@@ -122,32 +54,15 @@ export default class ASTBuilder {
122
54
  * checks if a database type is a date type (with optional array suffix)
123
55
  */
124
56
  protected isDateDbType(dbType: string): boolean;
125
- /**
126
- * @internal
127
- *
128
- * checks if a database type is a time without time zone type (with optional array suffix)
129
- */
130
- protected isTimeWithoutTimeZoneDbType(dbType: string): boolean;
131
- /**
132
- * @internal
133
- *
134
- * checks if a database type is a time with time zone type (with optional array suffix)
135
- */
136
- protected isTimeWithTimeZoneDbType(dbType: string): boolean;
137
- /**
138
- * @internal
139
- *
140
- * checks if a database type is any time type (with or without time zone, with optional array suffix)
141
- */
142
- protected isTimeDbType(dbType: string): boolean;
143
- /**
144
- * @internal
145
- *
146
- * checks if a TypeScript type node represents a string-like type
147
- * (string keyword, string type reference, or string literal)
148
- */
149
- protected isStringLikeType(node: ts.TypeNode, sourceFile: ts.SourceFile): boolean;
150
57
  }
58
+ /**
59
+ * @internal
60
+ *
61
+ * A property name for an object literal, interface or class: the name itself
62
+ * when it is an identifier, and a string literal otherwise, such as a table
63
+ * outside the default schema (`inventory.parts`).
64
+ */
65
+ export declare function propertyName(name: string): ts.Identifier | ts.StringLiteral;
151
66
  export interface SchemaData {
152
67
  [key: string]: TableData;
153
68
  }
@@ -176,15 +91,10 @@ export interface SchemaBuilderAssociationData {
176
91
  }
177
92
  export interface SchemaBuilderColumnData {
178
93
  dbType: string;
94
+ valueType: IntrospectedValueType;
179
95
  allowNull: boolean;
180
96
  enumType: string | null;
181
97
  enumValues: string | null;
182
98
  foreignKey: string | null;
183
99
  isArray: boolean;
184
100
  }
185
- export interface SchemaBuilderInformationSchemaRow {
186
- columnName: string;
187
- udtName: string;
188
- dataType: string;
189
- isNullable: 'YES' | 'NO';
190
- }
@@ -1,11 +1,11 @@
1
- import ts from 'typescript';
1
+ import { IntrospectedDatabase, IntrospectedTable } from '../../types/db.js';
2
2
  import ASTBuilder, { SchemaData } from './ASTBuilder.js';
3
3
  /**
4
4
  * @internal
5
5
  *
6
6
  * This is a base class, which is inherited by the ASTSchemaBuilder and
7
- * the ASTKyselyCodegenEnhancer, both of which is responsible for building
8
- * up the output of the various type files consumed by dream internally.
7
+ * the ASTDbTypesBuilder, each of which builds one of the type files a
8
+ * connection's sync writes, from the same introspection of the connection.
9
9
  *
10
10
  * This base class is just a container for common methods used by both
11
11
  * classes. It requires a connectionName to be provided, unlike the underlying
@@ -15,63 +15,66 @@ import ASTBuilder, { SchemaData } from './ASTBuilder.js';
15
15
  export default class ASTConnectionBuilder extends ASTBuilder {
16
16
  protected connectionName: string;
17
17
  hasForeignKeyError: boolean;
18
- constructor(connectionName: string);
18
+ private introspection;
19
+ private readonly introspectionFrom;
19
20
  /**
20
- * @internal
21
- *
22
- * returns the path from project root to the dream.ts file
23
- * for the particular connection. If the connectionName is anything
24
- * other than default, the path will represent that by injecting
25
- * the connectionName into the file name, i.e. dream.alternate.ts
21
+ * @param connectionName - the connection whose type file the builder writes
22
+ * @param opts.introspectionFrom - another builder of the same connection,
23
+ * whose reading of it this builder takes instead of reading the connection
24
+ * again, so that a sync reads each connection once for both type files
26
25
  */
27
- protected schemaPath(): string;
26
+ constructor(connectionName: string, opts?: {
27
+ introspectionFrom?: ASTConnectionBuilder | undefined;
28
+ });
28
29
  /**
29
30
  * @internal
30
31
  *
31
- * returns the path from project root to the db.ts file
32
- * for the particular connection. If the connectionName is anything
33
- * other than default, the path will represent that by injecting
34
- * the connectionName into the file name, i.e. db.alternate.ts
32
+ * the connection's tables, sorted by their key in the DB interface, and its
33
+ * enums, read through the query driver once, by this builder or by the one
34
+ * it takes its introspection from. The tables that `tableIncludePattern`
35
+ * leaves out, or `tableExcludePattern` names, are left out.
35
36
  */
36
- protected dbPath(): string;
37
+ protected introspectedDatabase(): Promise<IntrospectedDatabase>;
37
38
  /**
38
39
  * @internal
39
40
  *
40
- * returns the db source file for the given connectionName, injecting
41
- * the source file with the actual file contents, so that AST nodes
42
- * can be built through ingesting.
41
+ * the table's key in the DB interface and in Dream's schema: its name, or
42
+ * `<schema>.<name>` outside the connection's default schema
43
43
  */
44
- protected getDbSourceFile(): Promise<ts.SourceFile>;
44
+ protected tableKey(table: IntrospectedTable): string;
45
45
  /**
46
46
  * @internal
47
47
  *
48
- * reads the db source file for the given connection, returning the contents
49
- * as a raw string
48
+ * the TypeScript name of each enum the connection's tables use, keyed by
49
+ * the enum's database name (see dbTypesName), with a number added when it
50
+ * would repeat an earlier enum's name or a name types/db.ts reserves
50
51
  */
51
- protected loadDbSyncFile(): Promise<string>;
52
+ protected enumTypeNames(): Promise<Map<string, string>>;
52
53
  /**
53
54
  * @internal
54
55
  *
55
- * builds up the schema data for every table into an object, which
56
- * can be read and injected into AST nodes.
56
+ * returns the path from project root to the dream.ts file
57
+ * for the particular connection. If the connectionName is anything
58
+ * other than default, the path will represent that by injecting
59
+ * the connectionName into the file name, i.e. dream.alternate.ts
57
60
  */
58
- protected getSchemaData(): Promise<SchemaData>;
61
+ protected schemaPath(): string;
59
62
  /**
60
63
  * @internal
61
64
  *
62
- * used by getSchemaData to build up all table data
65
+ * returns the path from project root to the db.ts file
66
+ * for the particular connection. If the connectionName is anything
67
+ * other than default, the path will represent that by injecting
68
+ * the connectionName into the file name, i.e. db.alternate.ts
63
69
  */
64
- private getTables;
70
+ protected dbPath(): string;
65
71
  /**
66
72
  * @internal
67
73
  *
68
- * finds all enums used by the app, and returns information
69
- * about those enums that can be used for type generating purpposes
74
+ * builds up the schema data for every table into an object, which
75
+ * can be read and injected into AST nodes.
70
76
  */
71
- protected getAllEnumValueNames(): Promise<{
72
- enumValues: string;
73
- enumType: string;
74
- }[]>;
77
+ protected getSchemaData(): Promise<SchemaData>;
75
78
  /**
76
79
  * @internal
77
80
  *
@@ -117,8 +120,8 @@ export default class ASTConnectionBuilder extends ASTBuilder {
117
120
  /**
118
121
  * @internal
119
122
  *
120
- * retrieves the column data for an individual table and association.
121
- * Can be used to build up types
123
+ * the columns of an introspected table, as Dream's schema records them,
124
+ * keyed by camelized column name
122
125
  */
123
- private getColumnData;
126
+ private columnData;
124
127
  }
@@ -0,0 +1,26 @@
1
+ import ASTConnectionBuilder from './ASTConnectionBuilder.js';
2
+ /**
3
+ * Writes a connection's types/db.ts (types/db.<connectionName>.ts for any
4
+ * connection but default) from its introspection: the Kysely `DB`
5
+ * interface, an interface for each table, each enum the tables use as a
6
+ * union of its values with a `<Enum>Values` const of them, and the column
7
+ * types those declare. Columns that the table's models ignore are left out,
8
+ * so that code still referring to one fails to compile.
9
+ */
10
+ export default class ASTDbTypesBuilder extends ASTConnectionBuilder {
11
+ build(): Promise<void>;
12
+ /**
13
+ * @internal
14
+ *
15
+ * the table's columns, minus those its models ignore, sorted by name, each
16
+ * with its key (the camelized column name) and its type
17
+ */
18
+ private columns;
19
+ /**
20
+ * @internal
21
+ *
22
+ * the file's imports: Dream's date and time classes, Kysely's ColumnType,
23
+ * and postgres-interval's type when a column is an interval
24
+ */
25
+ private imports;
26
+ }
@@ -60,21 +60,11 @@ export default class ASTSchemaBuilder extends ASTConnectionBuilder {
60
60
  /**
61
61
  * @internal
62
62
  *
63
- * given a column and table, this method returns the coerced type representation
64
- * of whatever type was found in the db.ts file. In most cases, it will simply
65
- * return the same type as was found, but in some cases it will perform coercion
66
- * to provide consistent and useful types to the underlying application.
67
- *
68
- * Amongst other things, it will convert Numeric -> number, Int8 -> string, etc...
69
- * Timestamp will be carefully converted to either CalendarDate or DateTime, depending
70
- * on if the type set in the db is a date or datetime field.
71
- */
72
- private getCoercedTypeForTableColumn;
73
- /**
74
- * @internal
75
- *
76
- * used by the getCoercedTypeForTableColumn to get the specific type for a column
77
- * if no coercion is necessary, the original type node will be returned
63
+ * the type Dream gives the column's values: Int8 (bigint) values are
64
+ * strings, Numeric values numbers, and a Timestamp is a CalendarDate for a
65
+ * date column and a DateTime otherwise. Types declared in the db.ts file
66
+ * (Json and the like) are added to importedTypes.
78
67
  */
79
68
  private coercedType;
69
+ private coercedValueType;
80
70
  }
@@ -0,0 +1,37 @@
1
+ import { IntrospectedTable } from '../../types/db.js';
2
+ /**
3
+ * @internal
4
+ *
5
+ * The value types an array column lists in a plain array in types/db.ts. Any
6
+ * other is listed through `ArrayType`, which turns a `ColumnType` (such as
7
+ * `Int8`) into a `ColumnType` of arrays.
8
+ */
9
+ export declare const PLAIN_ARRAY_VALUE_TYPES: string[];
10
+ /**
11
+ * @internal
12
+ *
13
+ * The declaration of each type that types/db.ts declares for its columns.
14
+ */
15
+ export declare const HELPER_TYPE_DECLARATIONS: Record<string, string>;
16
+ /**
17
+ * @internal
18
+ *
19
+ * The types types/db.ts declares for the given tables' columns.
20
+ */
21
+ export declare function dbTypesHelperNames(tables: IntrospectedTable[]): string[];
22
+ /**
23
+ * @internal
24
+ *
25
+ * The names that types/db.ts declares, imports or uses from JavaScript for
26
+ * the given tables, which no enum or table interface may take.
27
+ */
28
+ export declare function dbTypesReservedNames(tables: IntrospectedTable[]): string[];
29
+ /**
30
+ * @internal
31
+ *
32
+ * The TypeScript name types/db.ts gives a table or enum: its name, with a
33
+ * schema's `.` as `_`, pascalized, and prefixed with `_` when it would start
34
+ * with a digit. A number is added when the name is in `takenNames`, which
35
+ * the chosen name joins.
36
+ */
37
+ export declare function dbTypesName(name: string, takenNames: Set<string>): string;
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @internal
3
+ *
4
+ * Whether a table matches a `tableIncludePattern` or `tableExcludePattern`:
5
+ * a glob in Node's `path.matchesGlob` syntax (e.g. `chalupas*`, `public.*`
6
+ * or `public.+(users|posts)`), compared without regard to case. A pattern
7
+ * that contains a `.` is matched against `<schema>.<name>`, and any other
8
+ * against the table's name alone. As with micromatch, a leading `!` (one that
9
+ * does not open an extglob `!(…)`) negates the pattern, and a backslash
10
+ * before a glob character such as `*` or `?` matches that character itself.
11
+ */
12
+ export default function tableMatchesPattern(table: {
13
+ schema: string | null;
14
+ name: string;
15
+ }, pattern: string): boolean;
@@ -6,7 +6,7 @@ export { type SerializerRendererOpts } from '../serializer/SerializerRenderer.js
6
6
  export { type BelongsToStatement } from '../types/associations/belongsTo.js';
7
7
  export { type HasManyStatement } from '../types/associations/hasMany.js';
8
8
  export { type HasOneStatement } from '../types/associations/hasOne.js';
9
- export { type DbConnectionType, type DbTypes, type PrimaryKeyType } from '../types/db.js';
9
+ export { type DbConnectionType, type DbTypes, type IntrospectedColumn, type IntrospectedDatabase, type IntrospectedEnum, type IntrospectedTable, type IntrospectedValueType, type PrimaryKeyType, } from '../types/db.js';
10
10
  export { type StrictInterface } from '../types/utils.js';
11
11
  export { type CalendarDateDurationUnit, type CalendarDateObject } from '../types/calendardate.js';
12
12
  export { type ClockTimeObject } from '../types/clocktime.js';