@deterministic-code/datasources 0.1.0 → 0.1.1

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 (302) hide show
  1. package/dist/ArrayReader.d.ts +12 -0
  2. package/dist/BatchCommandIBulkWriteStrategy.d.ts +33 -0
  3. package/dist/BuiltRepository.d.ts +16 -0
  4. package/dist/BulkCRUDRepository.d.ts +17 -0
  5. package/dist/BulkCommand.d.ts +5 -0
  6. package/dist/BulkWriteEnrichment.d.ts +4 -0
  7. package/dist/CRUDRepository.d.ts +40 -0
  8. package/dist/CRUDRepositoryBuilder.d.ts +19 -0
  9. package/dist/CRUDRepositoryOptions.d.ts +7 -0
  10. package/dist/CRUDRepositoryStatements.d.ts +17 -0
  11. package/dist/CascadeDeleteEnrichmentStrategy.d.ts +7 -0
  12. package/dist/CascadeDeleteUpdateEnrichmentStrategy.d.ts +8 -0
  13. package/dist/CommandMiddlewareHost.d.ts +22 -0
  14. package/dist/ConsoleCommandMiddleware.d.ts +14 -0
  15. package/dist/CreateEnrichmentStrategy.d.ts +14 -0
  16. package/dist/Datasource.d.ts +30 -0
  17. package/dist/DatasourceError.d.ts +3 -0
  18. package/dist/DatasourceFactory.d.ts +12 -0
  19. package/dist/DatasourceOptions.d.ts +1 -0
  20. package/dist/DatasourceUrl.d.ts +12 -0
  21. package/dist/DatasourceUrlParser.d.ts +4 -0
  22. package/dist/DefaultCreateEnrichmentStrategy.d.ts +7 -0
  23. package/dist/DefaultReadonlyEnrichmentStrategy.d.ts +19 -0
  24. package/dist/DefaultUpdateEnrichmentStrategy.d.ts +7 -0
  25. package/dist/DefaultWriteEnrichmentStrategy.d.ts +21 -0
  26. package/dist/DeleteEnrichmentStrategy.d.ts +11 -0
  27. package/dist/DriverLoader.d.ts +5 -0
  28. package/dist/Enrichment.d.ts +13 -0
  29. package/dist/EnrichmentPropertyType.d.ts +7 -0
  30. package/dist/ExpectConsoleMiddleware.d.ts +15 -0
  31. package/dist/FirstResultReader.d.ts +19 -0
  32. package/dist/HashsetOptimizedReadonlyEnrichmentStrategy.d.ts +9 -0
  33. package/dist/IBulkWriteStrategy.d.ts +12 -0
  34. package/dist/ICRUDRepository.d.ts +12 -0
  35. package/dist/ICRUDRepositoryFactory.d.ts +7 -0
  36. package/dist/ICommand.d.ts +20 -0
  37. package/dist/ICommandMiddleware.d.ts +12 -0
  38. package/dist/IConnection.d.ts +12 -0
  39. package/dist/IDatasource.d.ts +18 -0
  40. package/dist/IDatasourceFactory.d.ts +5 -0
  41. package/dist/IDriver.d.ts +19 -0
  42. package/dist/IEnriched.d.ts +6 -0
  43. package/dist/IReader.d.ts +8 -0
  44. package/dist/IReadonlyEnrichmentStrategy.d.ts +7 -0
  45. package/dist/IReadonlyRepository.d.ts +11 -0
  46. package/dist/IRepository.d.ts +4 -0
  47. package/dist/IRepositoryFactory.d.ts +7 -0
  48. package/dist/IRepositoryTree.d.ts +4 -0
  49. package/dist/IRepositoryTreeNode.d.ts +7 -0
  50. package/dist/ITransaction.d.ts +8 -0
  51. package/dist/IUnitOfWork.d.ts +5 -0
  52. package/dist/IWriteEnrichmentStrategy.d.ts +7 -0
  53. package/dist/Information.d.ts +5 -0
  54. package/dist/IsolationLevel.d.ts +7 -0
  55. package/dist/LazyWriteEnrichmentStrategy.d.ts +4 -0
  56. package/dist/NestedToObjectEnrichmentStrategy.d.ts +10 -0
  57. package/dist/Parameter.d.ts +7 -0
  58. package/dist/ParameterDirection.d.ts +7 -0
  59. package/dist/ProviderCatalog.d.ts +7 -0
  60. package/dist/ProviderNotImplementedError.d.ts +4 -0
  61. package/dist/ProviderNotInstalledError.d.ts +4 -0
  62. package/dist/ReadonlyRepository.d.ts +59 -0
  63. package/dist/ReadonlyRepositoryBuilder.d.ts +12 -0
  64. package/dist/ReadonlyRepositoryMappingOptions.d.ts +14 -0
  65. package/dist/ReadonlyRepositoryStatements.d.ts +8 -0
  66. package/dist/RepositoryAccess.d.ts +11 -0
  67. package/dist/RepositoryFactory.d.ts +23 -0
  68. package/dist/RepositoryNotRegisteredError.d.ts +4 -0
  69. package/dist/RepositoryTree.d.ts +6 -0
  70. package/dist/RepositoryTreeNode.d.ts +9 -0
  71. package/dist/Response.d.ts +7 -0
  72. package/dist/Row.d.ts +2 -0
  73. package/dist/SingleCommandIBulkWriteStrategy.d.ts +11 -0
  74. package/dist/StatementMappers.d.ts +16 -0
  75. package/dist/StoredEnrichment.d.ts +11 -0
  76. package/dist/StoredProcedureBulkWriteStrategy.d.ts +27 -0
  77. package/dist/StreamingReader.d.ts +16 -0
  78. package/dist/UnitOfWorkTransaction.d.ts +12 -0
  79. package/dist/UpdateEnrichmentStrategy.d.ts +18 -0
  80. package/dist/WriteMode.d.ts +1 -0
  81. package/dist/builders/Field.d.ts +5 -0
  82. package/dist/builders/Scalar.d.ts +1 -0
  83. package/dist/builders/index.d.ts +2 -0
  84. package/dist/collectAsync.d.ts +1 -0
  85. package/dist/converters/Cell.d.ts +2 -0
  86. package/dist/converters/ChangeCasingConverter.d.ts +7 -0
  87. package/dist/converters/Constructable.d.ts +1 -0
  88. package/dist/converters/MetaDataRowTypeConverter.d.ts +14 -0
  89. package/dist/converters/PluralizerConverter.d.ts +4 -0
  90. package/dist/converters/RegexConverter.d.ts +7 -0
  91. package/dist/converters/ReplaceConverter.d.ts +7 -0
  92. package/dist/converters/RowTypeConverter.d.ts +10 -0
  93. package/dist/converters/ToLowerCaseConverter.d.ts +4 -0
  94. package/dist/converters/ToUpperCaseConverter.d.ts +4 -0
  95. package/dist/converters/TypeConverter.d.ts +5 -0
  96. package/dist/createCRUDRepositoryOptions.d.ts +7 -0
  97. package/dist/createReadonlyRepositoryMappingOptions.d.ts +6 -0
  98. package/dist/dotEnv.d.ts +5 -0
  99. package/dist/index.d.ts +119 -0
  100. package/dist/mappers/ChangeCasingMapper.d.ts +9 -0
  101. package/dist/mappers/DictionaryMapper.d.ts +8 -0
  102. package/dist/mappers/FlatToNestedMapper.d.ts +20 -0
  103. package/dist/mappers/IFlatToNestedMapper.d.ts +6 -0
  104. package/dist/mappers/IMapper.d.ts +4 -0
  105. package/dist/mappers/IdentityMapper.d.ts +5 -0
  106. package/dist/mappers/PluralizerMapper.d.ts +6 -0
  107. package/dist/mappers/RowTypeMapper.d.ts +6 -0
  108. package/dist/mappers/TransformConverterMapper.d.ts +5 -0
  109. package/dist/mappers/TransformMapper.d.ts +6 -0
  110. package/dist/mappers/TypeMap.d.ts +8 -0
  111. package/dist/mappers/TypeMapGroup.d.ts +5 -0
  112. package/dist/mocks/index.d.ts +9 -0
  113. package/dist/mocks/mock-command.d.ts +28 -0
  114. package/dist/mocks/mock-connection.d.ts +22 -0
  115. package/dist/mocks/mock-datasource.d.ts +24 -0
  116. package/dist/mocks/mock-driver.d.ts +15 -0
  117. package/dist/mocks/mock-reader.d.ts +14 -0
  118. package/dist/mocks/mock-repository.d.ts +42 -0
  119. package/dist/mocks/mock-select-statement.d.ts +10 -0
  120. package/dist/mocks/mock-transaction.d.ts +16 -0
  121. package/dist/mocks/mock-where.d.ts +8 -0
  122. package/dist/parametersToRecord.d.ts +2 -0
  123. package/dist/readerToAsyncIterable.d.ts +2 -0
  124. package/dist/splitStatements.d.ts +1 -0
  125. package/dist/statementParameters.d.ts +1 -0
  126. package/dist/statementUsesParameter.d.ts +1 -0
  127. package/dist/statements/Aggregate.d.ts +6 -0
  128. package/dist/statements/ArrayExpression.d.ts +5 -0
  129. package/dist/statements/BulkDeleteStatement.d.ts +11 -0
  130. package/dist/statements/BulkDeleteStatementTextRenderer.d.ts +4 -0
  131. package/dist/statements/BulkInsertStatement.d.ts +12 -0
  132. package/dist/statements/BulkInsertStatementTextRenderer.d.ts +4 -0
  133. package/dist/statements/BulkUpdateStatement.d.ts +16 -0
  134. package/dist/statements/BulkUpdateStatementTextRenderer.d.ts +8 -0
  135. package/dist/statements/CheckConstraint.d.ts +7 -0
  136. package/dist/statements/Cmp.d.ts +9 -0
  137. package/dist/statements/Column.d.ts +9 -0
  138. package/dist/statements/ColumnDefault.d.ts +5 -0
  139. package/dist/statements/Condition.d.ts +3 -0
  140. package/dist/statements/ConditionRenderer.d.ts +12 -0
  141. package/dist/statements/CreateIndexStatement.d.ts +10 -0
  142. package/dist/statements/CreateIndexStatementTextRenderer.d.ts +5 -0
  143. package/dist/statements/CreateTableStatement.d.ts +11 -0
  144. package/dist/statements/CreateTableStatementTextRenderer.d.ts +18 -0
  145. package/dist/statements/DefaultStatementTextRenderer.d.ts +5 -0
  146. package/dist/statements/DeleteStatement.d.ts +9 -0
  147. package/dist/statements/DeleteStatementTextRenderer.d.ts +7 -0
  148. package/dist/statements/ExecuteStoredProcedureStatement.d.ts +9 -0
  149. package/dist/statements/ExecuteStoredProcedureStatementTextRenderer.d.ts +4 -0
  150. package/dist/statements/ExpressionEvaluator.d.ts +5 -0
  151. package/dist/statements/ExpressionRenderer.d.ts +8 -0
  152. package/dist/statements/ForeignKey.d.ts +19 -0
  153. package/dist/statements/IBulkDeleteStatement.d.ts +3 -0
  154. package/dist/statements/IBulkInsertStatement.d.ts +3 -0
  155. package/dist/statements/IBulkUpdateStatement.d.ts +3 -0
  156. package/dist/statements/ICondition.d.ts +4 -0
  157. package/dist/statements/IConditionVisitor.d.ts +6 -0
  158. package/dist/statements/ICreateIndexStatement.d.ts +3 -0
  159. package/dist/statements/ICreateTableStatement.d.ts +3 -0
  160. package/dist/statements/IDeleteStatement.d.ts +5 -0
  161. package/dist/statements/IExecuteStoredProcedureStatement.d.ts +3 -0
  162. package/dist/statements/IExpression.d.ts +2 -0
  163. package/dist/statements/IInsertStatement.d.ts +3 -0
  164. package/dist/statements/IReturnStatement.d.ts +3 -0
  165. package/dist/statements/ISelectStatement.d.ts +5 -0
  166. package/dist/statements/IStatement.d.ts +4 -0
  167. package/dist/statements/IStatementVisitor.d.ts +26 -0
  168. package/dist/statements/ITableConstraint.d.ts +5 -0
  169. package/dist/statements/IUpdateStatement.d.ts +5 -0
  170. package/dist/statements/IWhere.d.ts +6 -0
  171. package/dist/statements/Identifier.d.ts +5 -0
  172. package/dist/statements/InsertStatement.d.ts +9 -0
  173. package/dist/statements/InsertStatementTextRenderer.d.ts +4 -0
  174. package/dist/statements/Join.d.ts +14 -0
  175. package/dist/statements/Literal.d.ts +6 -0
  176. package/dist/statements/Native.d.ts +5 -0
  177. package/dist/statements/NotNull.d.ts +4 -0
  178. package/dist/statements/Operator.d.ts +107 -0
  179. package/dist/statements/OrderBy.d.ts +10 -0
  180. package/dist/statements/PrimaryKey.d.ts +7 -0
  181. package/dist/statements/ReturnStatement.d.ts +9 -0
  182. package/dist/statements/ReturnStatementTextRenderer.d.ts +4 -0
  183. package/dist/statements/SelectInsertedIdStatement.d.ts +8 -0
  184. package/dist/statements/SelectInsertedIdStatementTextRenderer.d.ts +4 -0
  185. package/dist/statements/SelectStatement.d.ts +20 -0
  186. package/dist/statements/SelectStatementOptions.d.ts +15 -0
  187. package/dist/statements/SelectStatementTextRenderer.d.ts +19 -0
  188. package/dist/statements/StatementEvaluator.d.ts +4 -0
  189. package/dist/statements/StatementParser.d.ts +4 -0
  190. package/dist/statements/StatementRenderer.d.ts +42 -0
  191. package/dist/statements/StatementTextRenderer.d.ts +14 -0
  192. package/dist/statements/Subquery.d.ts +6 -0
  193. package/dist/statements/UniqueConstraint.d.ts +7 -0
  194. package/dist/statements/UpdateStatement.d.ts +14 -0
  195. package/dist/statements/UpdateStatementTextRenderer.d.ts +14 -0
  196. package/dist/statements/Where.d.ts +16 -0
  197. package/dist/statements/WhereText.d.ts +8 -0
  198. package/dist/statements/index.d.ts +75 -0
  199. package/dist/stubs/StubCommand.d.ts +15 -0
  200. package/dist/stubs/StubConnection.d.ts +13 -0
  201. package/dist/stubs/StubDatasource.d.ts +20 -0
  202. package/dist/stubs/StubReader.d.ts +11 -0
  203. package/dist/stubs/StubRepository.d.ts +22 -0
  204. package/dist/stubs/StubTransaction.d.ts +10 -0
  205. package/dist/stubs/createStubDatasource.d.ts +2 -0
  206. package/dist/stubs/index.d.ts +7 -0
  207. package/dist/stubs/notImplemented.d.ts +1 -0
  208. package/package.json +14 -6
  209. package/src/ArrayReader.test.ts +23 -0
  210. package/src/BulkCRUDRepository.test.ts +183 -0
  211. package/src/BulkWriteStrategy.test.ts +293 -0
  212. package/src/CRUDRepository.test.ts +420 -0
  213. package/src/CRUDRepository.ts +1 -1
  214. package/src/CRUDRepositoryOptions.test.ts +76 -0
  215. package/src/Command.test.ts +105 -0
  216. package/src/CommandMiddlewareHost.test.ts +98 -0
  217. package/src/Connection.test.ts +34 -0
  218. package/src/ConsoleCommandMiddleware.test.ts +149 -0
  219. package/src/Datasource.test.ts +63 -0
  220. package/src/DatasourceError.test.ts +10 -0
  221. package/src/DatasourceFactory.test.ts +159 -0
  222. package/src/DatasourceUrlParser.test.ts +173 -0
  223. package/src/ExpectConsoleMiddleware.test.ts +79 -0
  224. package/src/FirstResultReader.test.ts +43 -0
  225. package/src/IsolationLevel.test.ts +13 -0
  226. package/src/ProviderCatalog.test.ts +110 -0
  227. package/src/ProviderNotImplementedError.test.ts +11 -0
  228. package/src/ProviderNotInstalledError.test.ts +11 -0
  229. package/src/ReadonlyRepository.test.ts +406 -0
  230. package/src/RepositoryFactory.test.ts +94 -0
  231. package/src/StatementMappers.test.ts +221 -0
  232. package/src/StreamingReader.test.ts +126 -0
  233. package/src/Transaction.test.ts +37 -0
  234. package/src/UnitOfWorkTransaction.test.ts +43 -0
  235. package/src/UserViewCRUDRepository.test.ts +323 -0
  236. package/src/builders/builders.test.ts +404 -0
  237. package/src/converters/ChangeCasingConverter.test.ts +34 -0
  238. package/src/converters/PluralizerConverter.test.ts +43 -0
  239. package/src/converters/RegexConverter.test.ts +11 -0
  240. package/src/converters/ReplaceConverter.test.ts +9 -0
  241. package/src/converters/RowTypeConverter.test.ts +79 -0
  242. package/src/converters/ToLowerCaseConverter.test.ts +9 -0
  243. package/src/converters/ToUpperCaseConverter.test.ts +9 -0
  244. package/src/dotEnv.test.ts +21 -0
  245. package/src/edgeCases.test.ts +485 -0
  246. package/src/mappers/ChangeCasingMapper.test.ts +32 -0
  247. package/src/mappers/DictionaryMapper.test.ts +69 -0
  248. package/src/mappers/FlatToNestedMapper.test.ts +269 -0
  249. package/src/mappers/IdentityMapper.test.ts +12 -0
  250. package/src/mappers/PluralizerMapper.test.ts +40 -0
  251. package/src/mappers/RowTypeMapper.test.ts +12 -0
  252. package/src/mappers/TransformConverterMapper.test.ts +181 -0
  253. package/src/mappers/transformMapper.test.ts +66 -0
  254. package/src/mocks/MockCommand.test.ts +75 -0
  255. package/src/mocks/MockConnection.test.ts +34 -0
  256. package/src/mocks/MockDatasource.test.ts +40 -0
  257. package/src/mocks/MockReader.test.ts +17 -0
  258. package/src/mocks/MockRepository.test.ts +32 -0
  259. package/src/mocks/MockSelectStatement.test.ts +11 -0
  260. package/src/mocks/MockTransaction.test.ts +17 -0
  261. package/src/mocks/MockWhere.test.ts +11 -0
  262. package/src/parametersToRecord.test.ts +19 -0
  263. package/src/public-surface.test.ts +23 -0
  264. package/src/readWithWhereClauseAndCustomFlatToNestedObjectMappingOptions.test.ts +126 -0
  265. package/src/readerToAsyncIterable.test.ts +39 -0
  266. package/src/repository.test.ts +410 -0
  267. package/src/repository.type.test.ts +29 -0
  268. package/src/statements/SelectInsertedIdStatement.test.ts +11 -0
  269. package/src/statements/StatementCompile.test.ts +231 -0
  270. package/src/statements/StatementParser.test.ts +304 -0
  271. package/src/statements/index.ts +1 -1
  272. package/src/stubs/StubDatasource.test.ts +99 -0
  273. package/src/tests/contact/Address.ts +14 -0
  274. package/src/tests/contact/AddressType.ts +6 -0
  275. package/src/tests/contact/Base.ts +7 -0
  276. package/src/tests/contact/Contact.ts +12 -0
  277. package/src/tests/contact/ContactBase.ts +8 -0
  278. package/src/tests/contact/ContactChangeLog.ts +8 -0
  279. package/src/tests/contact/ContactGroup.ts +6 -0
  280. package/src/tests/contact/ContactGroupBase.ts +6 -0
  281. package/src/tests/contact/ContactGroupMember.ts +6 -0
  282. package/src/tests/contact/ContactSource.ts +6 -0
  283. package/src/tests/contact/ContactType.ts +6 -0
  284. package/src/tests/contact/ContactsService.test.ts +118 -0
  285. package/src/tests/contact/ContactsService.ts +32 -0
  286. package/src/tests/contact/ImportContactRequest.ts +4 -0
  287. package/src/tests/contact/ImportContactResult.ts +5 -0
  288. package/src/tests/contact/LegacyContact.ts +7 -0
  289. package/src/tests/contact/LegacyMigrationResult.ts +5 -0
  290. package/src/tests/contact/Phone.ts +9 -0
  291. package/src/tests/contact/PhoneType.ts +6 -0
  292. package/src/tests/contact/asContactsView.ts +63 -0
  293. package/src/tests/contact/contactColumns.ts +40 -0
  294. package/src/tests/contact/contactLookupIds.ts +32 -0
  295. package/src/tests/contact/contactLookups.ts +6 -0
  296. package/src/tests/contact/contactsParity.ts +63 -0
  297. package/src/tests/contact/contactsServiceOf.ts +38 -0
  298. package/src/tests/contact/contactsView.ts +31 -0
  299. package/src/tests/contact/expectContactsView.ts +23 -0
  300. package/src/tests/contact/extraContact.ts +61 -0
  301. package/src/tests/contact/logMapped.ts +5 -0
  302. package/src/tests/contact/seedContacts.ts +31 -0
@@ -0,0 +1,32 @@
1
+ import type { ICRUDRepository } from "../../ICRUDRepository.ts";
2
+ import type { IEnriched } from "../../IEnriched.ts";
3
+ import type { IReadonlyRepository } from "../../IReadonlyRepository.ts";
4
+ import type { Address } from "./Address.ts";
5
+ import type { AddressType } from "./AddressType.ts";
6
+ import type { Contact } from "./Contact.ts";
7
+ import type { ContactSource } from "./ContactSource.ts";
8
+ import type { ContactType } from "./ContactType.ts";
9
+ import type { Phone } from "./Phone.ts";
10
+ import type { PhoneType } from "./PhoneType.ts";
11
+
12
+ type EnrichedCRUD<T> = ICRUDRepository<T> & IEnriched<T>;
13
+
14
+ export const ContactsService = {
15
+ open(
16
+ contacts: EnrichedCRUD<Contact>,
17
+ types: IReadonlyRepository<ContactType>,
18
+ sources: IReadonlyRepository<ContactSource>,
19
+ addressTypes: IReadonlyRepository<AddressType>,
20
+ phoneTypes: IReadonlyRepository<PhoneType>,
21
+ addresses: EnrichedCRUD<Address>,
22
+ phones: EnrichedCRUD<Phone>,
23
+ ): EnrichedCRUD<Contact> {
24
+ addresses.addEnriched({ destinationProperty: "AddressType", sourceProperty: "AddressType", relatedKey: "Id" }, addressTypes);
25
+ phones.addEnriched({ destinationProperty: "PhoneType", sourceProperty: "PhoneType", relatedKey: "Id" }, phoneTypes);
26
+ contacts.addEnriched({ destinationProperty: "ContactType", sourceProperty: "ContactType", relatedKey: "Id" }, types);
27
+ contacts.addEnriched({ destinationProperty: "ContactSource", sourceProperty: "ContactSource", relatedKey: "Id" }, sources);
28
+ contacts.addEnriched({ destinationProperty: "Addresses", sourceProperty: "Id", relatedKey: "Contact", type: "list" }, addresses);
29
+ contacts.addEnriched({ destinationProperty: "Phones", sourceProperty: "Id", relatedKey: "Contact", type: "list" }, phones);
30
+ return contacts;
31
+ },
32
+ };
@@ -0,0 +1,4 @@
1
+ export class ImportContactRequest {
2
+ source = "";
3
+ payload = "";
4
+ }
@@ -0,0 +1,5 @@
1
+ export class ImportContactResult {
2
+ imported_count = 0;
3
+ skipped_count = 0;
4
+ failed_count = 0;
5
+ }
@@ -0,0 +1,7 @@
1
+ export class LegacyContact {
2
+ key = "";
3
+ first_name = "";
4
+ last_name = "";
5
+ email: string | null = null;
6
+ imported_at: Date | null = null;
7
+ }
@@ -0,0 +1,5 @@
1
+ export class LegacyMigrationResult {
2
+ migrated_count = 0;
3
+ skipped_count = 0;
4
+ completed_at: Date | null = null;
5
+ }
@@ -0,0 +1,9 @@
1
+ import { Base } from "./Base.ts";
2
+ import { Contact } from "./Contact.ts";
3
+ import { PhoneType } from "./PhoneType.ts";
4
+
5
+ export class Phone extends Base {
6
+ Contact = new Contact();
7
+ PhoneType = new PhoneType();
8
+ Number = "";
9
+ }
@@ -0,0 +1,6 @@
1
+ import { Base } from "./Base.ts";
2
+
3
+ export class PhoneType extends Base {
4
+ Name = "";
5
+ Description: string | null = null;
6
+ }
@@ -0,0 +1,63 @@
1
+ import type { Contact } from "./Contact.ts";
2
+
3
+ type AddressView = { id: number; line1: string; city: string; address_type_name: string };
4
+ type PhoneView = { id: number; number: string; phone_type_name: string };
5
+
6
+ export type ContactsView = {
7
+ id: number;
8
+ first_name: string;
9
+ last_name: string;
10
+ email: string | null;
11
+ contact_source_name: string;
12
+ contact_type_name: string;
13
+ addresses: AddressView[];
14
+ phones: PhoneView[];
15
+ };
16
+
17
+ const byId = (left: { id: number }, right: { id: number }) => left.id - right.id;
18
+
19
+ export const fromContacts = (contacts: Contact[]): ContactsView[] =>
20
+ contacts
21
+ .map((contact) => ({
22
+ id: contact.Id,
23
+ first_name: contact.FirstName,
24
+ last_name: contact.LastName,
25
+ email: contact.Email,
26
+ contact_source_name: contact.ContactSource.Name,
27
+ contact_type_name: contact.ContactType.Name,
28
+ addresses: contact.Addresses.map((address) => ({
29
+ id: address.Id,
30
+ line1: address.Line1,
31
+ city: address.City,
32
+ address_type_name: address.AddressType.Name,
33
+ })).sort(byId),
34
+ phones: contact.Phones.map((phone) => ({
35
+ id: phone.Id,
36
+ number: phone.Number,
37
+ phone_type_name: phone.PhoneType.Name,
38
+ })).sort(byId),
39
+ }))
40
+ .sort(byId);
41
+
42
+ export const asContactsView = (contacts: ContactsView[]): ContactsView[] =>
43
+ [...contacts]
44
+ .sort(byId)
45
+ .map((contact) => ({
46
+ id: contact.id,
47
+ first_name: contact.first_name,
48
+ last_name: contact.last_name,
49
+ email: contact.email,
50
+ contact_source_name: contact.contact_source_name,
51
+ contact_type_name: contact.contact_type_name,
52
+ addresses: [...contact.addresses]
53
+ .map(({ id, line1, city, address_type_name }) => ({
54
+ id,
55
+ line1,
56
+ city,
57
+ address_type_name,
58
+ }))
59
+ .sort(byId),
60
+ phones: [...contact.phones]
61
+ .map(({ id, number, phone_type_name }) => ({ id, number, phone_type_name }))
62
+ .sort(byId),
63
+ }));
@@ -0,0 +1,40 @@
1
+ import { ChangeCasingMapper } from "../../mappers/ChangeCasingMapper.ts";
2
+ import { DictionaryMapper } from "../../mappers/DictionaryMapper.ts";
3
+ import type { IMapper } from "../../mappers/IMapper.ts";
4
+ import { TransformMapper } from "../../mappers/TransformMapper.ts";
5
+
6
+ export const snake = new ChangeCasingMapper("pascalCase", "snakeCase");
7
+
8
+ const references = new DictionaryMapper<string>([
9
+ ["Contact", "contact_id"],
10
+ ["AddressType", "address_type_id"],
11
+ ["ContactType", "contact_type_id"],
12
+ ["ContactSource", "contact_source_id"],
13
+ ["PhoneType", "phone_type_id"],
14
+ ["Line1", "line1"],
15
+ ["Line2", "line2"],
16
+ ]);
17
+
18
+ export const columns: IMapper<string, string> = new TransformMapper(
19
+ (name) => {
20
+ const mapped = references.map(name);
21
+ return mapped !== name ? mapped : snake.map(name);
22
+ },
23
+ (name) => {
24
+ const from = references.reverseMap(name);
25
+ return from !== name ? from : snake.reverseMap(name);
26
+ },
27
+ );
28
+
29
+ export const contactEntity = new DictionaryMapper<string>([["Contact", "contacts_base"]]);
30
+
31
+ export const joinEntity: IMapper<string, string> = new TransformMapper(
32
+ (name) => {
33
+ const mapped = contactEntity.map(name);
34
+ return mapped !== name ? mapped : snake.map(name);
35
+ },
36
+ (name) => {
37
+ const from = contactEntity.reverseMap(name);
38
+ return from !== name ? from : snake.reverseMap(name);
39
+ },
40
+ );
@@ -0,0 +1,32 @@
1
+ import { Operator, Where } from "../../statements/index.ts";
2
+ import type { IConnection } from "../../IConnection.ts";
3
+ import { CRUDRepository } from "../../CRUDRepository.ts";
4
+ import type { Constructable } from "../../converters/Constructable.ts";
5
+ import { AddressType } from "./AddressType.ts";
6
+ import { columns, snake } from "./contactColumns.ts";
7
+ import type { ContactLookups } from "./contactLookups.ts";
8
+ import { ContactSource } from "./ContactSource.ts";
9
+ import { ContactType } from "./ContactType.ts";
10
+ import { PhoneType } from "./PhoneType.ts";
11
+
12
+ const of = async <T extends { Id: number }>(
13
+ connection: IConnection,
14
+ type: Constructable<T>,
15
+ name: string,
16
+ ): Promise<number> => {
17
+ const row = await new CRUDRepository(connection, {
18
+ type,
19
+ entityMapper: snake,
20
+ propertyMappings: columns,
21
+ ids: ["Id"],
22
+ }).readSingle(new Where(new Operator.Equals("Name", name)));
23
+ if (!row) throw new Error(`${type.name} '${name}' missing`);
24
+ return row.Id;
25
+ };
26
+
27
+ export const contactLookupIds = async (connection: IConnection): Promise<ContactLookups> => ({
28
+ ContactSourceId: await of(connection, ContactSource, "Web"),
29
+ ContactTypeId: await of(connection, ContactType, "Person"),
30
+ AddressTypeId: await of(connection, AddressType, "Home"),
31
+ PhoneTypeId: await of(connection, PhoneType, "Mobile"),
32
+ });
@@ -0,0 +1,6 @@
1
+ export type ContactLookups = {
2
+ ContactSourceId: number;
3
+ ContactTypeId: number;
4
+ AddressTypeId: number;
5
+ PhoneTypeId: number;
6
+ };
@@ -0,0 +1,63 @@
1
+ import { collectAsync } from "../../collectAsync.ts";
2
+ import { ConsoleCommandMiddleware } from "../../ConsoleCommandMiddleware.ts";
3
+ import type { Cells } from "../../converters/Cell.ts";
4
+ import { FlatToNestedMapper } from "../../mappers/FlatToNestedMapper.ts";
5
+ import type { IConnection } from "../../IConnection.ts";
6
+ import { CRUDRepository } from "../../CRUDRepository.ts";
7
+ import { Join, SelectStatement, type ISelectStatement, type IWhere } from "../../index.ts";
8
+ import { fromContacts, type ContactsView } from "./asContactsView.ts";
9
+ import { columns, joinEntity } from "./contactColumns.ts";
10
+ import { contactsServiceOf } from "./contactsServiceOf.ts";
11
+ import { contactsView } from "./contactsView.ts";
12
+
13
+ export const contactsJoinSelect = (where?: IWhere): ISelectStatement =>
14
+ new SelectStatement({
15
+ table: "Contact",
16
+ fields: [
17
+ "Contact.Id AS Id",
18
+ "Contact.FirstName AS FirstName",
19
+ "Contact.LastName AS LastName",
20
+ "Contact.Email AS Email",
21
+ "ContactSource.Name AS ContactSourceName",
22
+ "ContactType.Name AS ContactTypeName",
23
+ "Address.Id AS AddressId",
24
+ "Address.Line1 AS Line1",
25
+ "Address.City AS City",
26
+ "AddressType.Name AS AddressTypeName",
27
+ "Phone.Id AS PhoneId",
28
+ "Phone.Number AS Number",
29
+ "PhoneType.Name AS PhoneTypeName",
30
+ ],
31
+ joins: [
32
+ Join.inner("ContactSource", "Contact.ContactSourceId", "ContactSource.Id"),
33
+ Join.inner("ContactType", "Contact.ContactTypeId", "ContactType.Id"),
34
+ Join.left("Address", "Contact.Id", "Address.ContactId"),
35
+ Join.left("AddressType", "Address.AddressTypeId", "AddressType.Id"),
36
+ Join.left("Phone", "Contact.Id", "Phone.ContactId"),
37
+ Join.left("PhoneType", "Phone.PhoneTypeId", "PhoneType.Id"),
38
+ ],
39
+ where,
40
+ });
41
+
42
+ export const contactsParity = async (
43
+ connection: IConnection,
44
+ verbose: boolean,
45
+ ): Promise<{ eager: ContactsView[]; enriched: ContactsView[] }> => {
46
+ const middleware = new ConsoleCommandMiddleware(undefined, undefined, {
47
+ isVerbose: verbose ? "true" : "false",
48
+ });
49
+ const eager = new FlatToNestedMapper(contactsView).map(
50
+ await collectAsync(
51
+ new CRUDRepository<Cells>(connection, {
52
+ entityMapper: joinEntity,
53
+ propertyMappings: columns,
54
+ })
55
+ .addMiddleware(middleware)
56
+ .read(contactsJoinSelect()),
57
+ ),
58
+ );
59
+ const enriched = fromContacts(
60
+ await collectAsync(contactsServiceOf(connection, verbose ? middleware : undefined).read()),
61
+ );
62
+ return { eager, enriched };
63
+ };
@@ -0,0 +1,38 @@
1
+ import type { ICommandMiddleware } from "../../ICommandMiddleware.ts";
2
+ import type { IConnection } from "../../IConnection.ts";
3
+ import { CRUDRepository } from "../../CRUDRepository.ts";
4
+ import type { Constructable } from "../../converters/Constructable.ts";
5
+ import type { IMapper } from "../../mappers/IMapper.ts";
6
+ import { Address } from "./Address.ts";
7
+ import { AddressType } from "./AddressType.ts";
8
+ import { columns, contactEntity, snake } from "./contactColumns.ts";
9
+ import { Contact } from "./Contact.ts";
10
+ import { ContactSource } from "./ContactSource.ts";
11
+ import { ContactType } from "./ContactType.ts";
12
+ import { ContactsService } from "./ContactsService.ts";
13
+ import { Phone } from "./Phone.ts";
14
+ import { PhoneType } from "./PhoneType.ts";
15
+
16
+ export const contactsServiceOf = (
17
+ connection: IConnection,
18
+ middleware?: ICommandMiddleware,
19
+ ) => {
20
+ const of = <T>(type: Constructable<T>, entity?: IMapper<string, string>) => {
21
+ const repo = new CRUDRepository(connection, {
22
+ type,
23
+ entityMapper: entity ?? snake,
24
+ propertyMappings: columns,
25
+ ids: ["Id"],
26
+ });
27
+ return middleware ? repo.addMiddleware(middleware) : repo;
28
+ };
29
+ return ContactsService.open(
30
+ of(Contact, contactEntity),
31
+ of(ContactType),
32
+ of(ContactSource),
33
+ of(AddressType),
34
+ of(PhoneType),
35
+ of(Address),
36
+ of(Phone),
37
+ );
38
+ };
@@ -0,0 +1,31 @@
1
+ import type { TypeMap } from "../../mappers/TypeMap.ts";
2
+ import type { Contact } from "./Contact.ts";
3
+
4
+ export const contactsView: TypeMap<Contact> = {
5
+ key: "id",
6
+ fields: {
7
+ id: "Id",
8
+ first_name: "FirstName",
9
+ last_name: "LastName",
10
+ email: "Email",
11
+ contact_source_name: "ContactSourceName",
12
+ contact_type_name: "ContactTypeName",
13
+ addresses: {
14
+ key: "AddressId",
15
+ fields: {
16
+ id: "AddressId",
17
+ line1: "Line1",
18
+ city: "City",
19
+ address_type_name: "AddressTypeName",
20
+ },
21
+ },
22
+ phones: {
23
+ key: "PhoneId",
24
+ fields: {
25
+ id: "PhoneId",
26
+ number: "Number",
27
+ phone_type_name: "PhoneTypeName",
28
+ },
29
+ },
30
+ },
31
+ };
@@ -0,0 +1,23 @@
1
+ import { expect } from "vitest";
2
+
3
+ type View = { id: number; addresses: unknown[]; phones: unknown[] };
4
+
5
+ export const contactWith = <T extends View>(
6
+ contacts: T[],
7
+ addresses: number,
8
+ phones: number,
9
+ ): T => {
10
+ const contact = contacts.find(
11
+ (item) => item.addresses.length === addresses && item.phones.length === phones,
12
+ );
13
+ if (!contact) throw new Error(`no contact with ${addresses} addresses and ${phones} phones`);
14
+ return contact;
15
+ };
16
+
17
+ export const expectContactsView = (contacts: View[], count: number) => {
18
+ expect(contacts).toHaveLength(count);
19
+ contactWith(contacts, 2, 1);
20
+ contactWith(contacts, 0, 1);
21
+ contactWith(contacts, 2, 0);
22
+ contactWith(contacts, 0, 0);
23
+ };
@@ -0,0 +1,61 @@
1
+ import { Operator, Where } from "../../statements/index.ts";
2
+ import { collectAsync } from "../../collectAsync.ts";
3
+ import type { ICRUDRepository } from "../../ICRUDRepository.ts";
4
+ import { Address } from "./Address.ts";
5
+ import { AddressType } from "./AddressType.ts";
6
+ import { Contact } from "./Contact.ts";
7
+ import { ContactSource } from "./ContactSource.ts";
8
+ import { ContactType } from "./ContactType.ts";
9
+ import type { ContactLookups } from "./contactLookups.ts";
10
+ import { Phone } from "./Phone.ts";
11
+ import { PhoneType } from "./PhoneType.ts";
12
+
13
+ const stamp = new Date("2024-01-01T00:00:00Z");
14
+ const version = new Uint8Array();
15
+
16
+ const base = <T extends { Uuid: string; Created: Date; Updated: Date; Version: Uint8Array }>(
17
+ type: { new (): T },
18
+ extra: Partial<T>,
19
+ ): T =>
20
+ Object.assign(new type(), {
21
+ Uuid: crypto.randomUUID(),
22
+ Created: stamp,
23
+ Updated: stamp,
24
+ Version: version,
25
+ ...extra,
26
+ });
27
+
28
+ export const extraContact = (ids: ContactLookups): Contact =>
29
+ base(Contact, {
30
+ ContactSource: Object.assign(new ContactSource(), { Id: ids.ContactSourceId }),
31
+ ContactType: Object.assign(new ContactType(), { Id: ids.ContactTypeId }),
32
+ FirstName: "Extra",
33
+ LastName: "Contact",
34
+ Addresses: [
35
+ base(Address, {
36
+ AddressType: Object.assign(new AddressType(), { Id: ids.AddressTypeId }),
37
+ Line1: "9 Extra",
38
+ City: "Oslo",
39
+ }),
40
+ ],
41
+ Phones: [
42
+ base(Phone, {
43
+ PhoneType: Object.assign(new PhoneType(), { Id: ids.PhoneTypeId }),
44
+ Number: "555-extra",
45
+ }),
46
+ ],
47
+ });
48
+
49
+ export const addExtraContact = async (
50
+ service: ICRUDRepository<Contact>,
51
+ ids: ContactLookups,
52
+ ): Promise<Contact> => {
53
+ const contact = extraContact(ids);
54
+ const created = await service.create(contact);
55
+ if (!(created.Id > 0)) throw new Error("create returned no id");
56
+ const loaded = (
57
+ await collectAsync(service.read(new Where(new Operator.Equals("Id", contact.Id))))
58
+ )[0];
59
+ if (!loaded) throw new Error("extra contact missing after create");
60
+ return loaded;
61
+ };
@@ -0,0 +1,5 @@
1
+ export const logMapped = (value: object): void => {
2
+ console.log(
3
+ `[${new Date().toISOString()}]-[datasource][Mapped]-[${JSON.stringify(value)}]`,
4
+ );
5
+ };
@@ -0,0 +1,31 @@
1
+ const stamp = "2024-01-01T00:00:00Z";
2
+ const uuid = (n: number) =>
3
+ `00000000-0000-4000-8000-${n.toString().padStart(12, "0")}`;
4
+
5
+ export const seedContactsSql = (count = 250, version = "X''"): string[] => {
6
+ const statements: string[] = [];
7
+ let addressId = 1;
8
+ let phoneId = 1;
9
+ for (let id = 1; id <= count; id++) {
10
+ const source = (id % 3) + 1;
11
+ const type = (id % 2) + 1;
12
+ statements.push(
13
+ `INSERT INTO "contacts_base" ("id", "uuid", "created", "updated", "version", "contact_source_id", "contact_type_id", "first_name", "last_name", "email", "notes") VALUES (${id}, '${uuid(id)}', '${stamp}', '${stamp}', ${version}, ${source}, ${type}, 'First${id}', 'Last${id}', 'c${id}@ex.com', NULL)`,
14
+ );
15
+ for (let a = 0; a < id % 4; a++) {
16
+ statements.push(
17
+ `INSERT INTO "address" ("id", "uuid", "created", "updated", "version", "contact_id", "address_type_id", "line1", "line2", "city", "region", "postal_code", "country") VALUES (${addressId}, '${uuid(1000 + addressId)}', '${stamp}', '${stamp}', ${version}, ${id}, ${a === 0 ? 1 : 2}, '${id} Line ${a + 1}', NULL, 'City${id}', NULL, NULL, NULL)`,
18
+ );
19
+ addressId += 1;
20
+ }
21
+ for (let p = 0; p < id % 3; p++) {
22
+ statements.push(
23
+ `INSERT INTO "phone" ("id", "uuid", "created", "updated", "version", "contact_id", "phone_type_id", "number") VALUES (${phoneId}, '${uuid(2000 + phoneId)}', '${stamp}', '${stamp}', ${version}, ${id}, ${p === 0 ? 1 : 2}, '555-000-${id.toString().padStart(4, "0")}-${p}')`,
24
+ );
25
+ phoneId += 1;
26
+ }
27
+ }
28
+ return statements;
29
+ };
30
+
31
+ export const seedContactCount = 250;