@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.
- package/dist/ArrayReader.d.ts +12 -0
- package/dist/BatchCommandIBulkWriteStrategy.d.ts +33 -0
- package/dist/BuiltRepository.d.ts +16 -0
- package/dist/BulkCRUDRepository.d.ts +17 -0
- package/dist/BulkCommand.d.ts +5 -0
- package/dist/BulkWriteEnrichment.d.ts +4 -0
- package/dist/CRUDRepository.d.ts +40 -0
- package/dist/CRUDRepositoryBuilder.d.ts +19 -0
- package/dist/CRUDRepositoryOptions.d.ts +7 -0
- package/dist/CRUDRepositoryStatements.d.ts +17 -0
- package/dist/CascadeDeleteEnrichmentStrategy.d.ts +7 -0
- package/dist/CascadeDeleteUpdateEnrichmentStrategy.d.ts +8 -0
- package/dist/CommandMiddlewareHost.d.ts +22 -0
- package/dist/ConsoleCommandMiddleware.d.ts +14 -0
- package/dist/CreateEnrichmentStrategy.d.ts +14 -0
- package/dist/Datasource.d.ts +30 -0
- package/dist/DatasourceError.d.ts +3 -0
- package/dist/DatasourceFactory.d.ts +12 -0
- package/dist/DatasourceOptions.d.ts +1 -0
- package/dist/DatasourceUrl.d.ts +12 -0
- package/dist/DatasourceUrlParser.d.ts +4 -0
- package/dist/DefaultCreateEnrichmentStrategy.d.ts +7 -0
- package/dist/DefaultReadonlyEnrichmentStrategy.d.ts +19 -0
- package/dist/DefaultUpdateEnrichmentStrategy.d.ts +7 -0
- package/dist/DefaultWriteEnrichmentStrategy.d.ts +21 -0
- package/dist/DeleteEnrichmentStrategy.d.ts +11 -0
- package/dist/DriverLoader.d.ts +5 -0
- package/dist/Enrichment.d.ts +13 -0
- package/dist/EnrichmentPropertyType.d.ts +7 -0
- package/dist/ExpectConsoleMiddleware.d.ts +15 -0
- package/dist/FirstResultReader.d.ts +19 -0
- package/dist/HashsetOptimizedReadonlyEnrichmentStrategy.d.ts +9 -0
- package/dist/IBulkWriteStrategy.d.ts +12 -0
- package/dist/ICRUDRepository.d.ts +12 -0
- package/dist/ICRUDRepositoryFactory.d.ts +7 -0
- package/dist/ICommand.d.ts +20 -0
- package/dist/ICommandMiddleware.d.ts +12 -0
- package/dist/IConnection.d.ts +12 -0
- package/dist/IDatasource.d.ts +18 -0
- package/dist/IDatasourceFactory.d.ts +5 -0
- package/dist/IDriver.d.ts +19 -0
- package/dist/IEnriched.d.ts +6 -0
- package/dist/IReader.d.ts +8 -0
- package/dist/IReadonlyEnrichmentStrategy.d.ts +7 -0
- package/dist/IReadonlyRepository.d.ts +11 -0
- package/dist/IRepository.d.ts +4 -0
- package/dist/IRepositoryFactory.d.ts +7 -0
- package/dist/IRepositoryTree.d.ts +4 -0
- package/dist/IRepositoryTreeNode.d.ts +7 -0
- package/dist/ITransaction.d.ts +8 -0
- package/dist/IUnitOfWork.d.ts +5 -0
- package/dist/IWriteEnrichmentStrategy.d.ts +7 -0
- package/dist/Information.d.ts +5 -0
- package/dist/IsolationLevel.d.ts +7 -0
- package/dist/LazyWriteEnrichmentStrategy.d.ts +4 -0
- package/dist/NestedToObjectEnrichmentStrategy.d.ts +10 -0
- package/dist/Parameter.d.ts +7 -0
- package/dist/ParameterDirection.d.ts +7 -0
- package/dist/ProviderCatalog.d.ts +7 -0
- package/dist/ProviderNotImplementedError.d.ts +4 -0
- package/dist/ProviderNotInstalledError.d.ts +4 -0
- package/dist/ReadonlyRepository.d.ts +59 -0
- package/dist/ReadonlyRepositoryBuilder.d.ts +12 -0
- package/dist/ReadonlyRepositoryMappingOptions.d.ts +14 -0
- package/dist/ReadonlyRepositoryStatements.d.ts +8 -0
- package/dist/RepositoryAccess.d.ts +11 -0
- package/dist/RepositoryFactory.d.ts +23 -0
- package/dist/RepositoryNotRegisteredError.d.ts +4 -0
- package/dist/RepositoryTree.d.ts +6 -0
- package/dist/RepositoryTreeNode.d.ts +9 -0
- package/dist/Response.d.ts +7 -0
- package/dist/Row.d.ts +2 -0
- package/dist/SingleCommandIBulkWriteStrategy.d.ts +11 -0
- package/dist/StatementMappers.d.ts +16 -0
- package/dist/StoredEnrichment.d.ts +11 -0
- package/dist/StoredProcedureBulkWriteStrategy.d.ts +27 -0
- package/dist/StreamingReader.d.ts +16 -0
- package/dist/UnitOfWorkTransaction.d.ts +12 -0
- package/dist/UpdateEnrichmentStrategy.d.ts +18 -0
- package/dist/WriteMode.d.ts +1 -0
- package/dist/builders/Field.d.ts +5 -0
- package/dist/builders/Scalar.d.ts +1 -0
- package/dist/builders/index.d.ts +2 -0
- package/dist/collectAsync.d.ts +1 -0
- package/dist/converters/Cell.d.ts +2 -0
- package/dist/converters/ChangeCasingConverter.d.ts +7 -0
- package/dist/converters/Constructable.d.ts +1 -0
- package/dist/converters/MetaDataRowTypeConverter.d.ts +14 -0
- package/dist/converters/PluralizerConverter.d.ts +4 -0
- package/dist/converters/RegexConverter.d.ts +7 -0
- package/dist/converters/ReplaceConverter.d.ts +7 -0
- package/dist/converters/RowTypeConverter.d.ts +10 -0
- package/dist/converters/ToLowerCaseConverter.d.ts +4 -0
- package/dist/converters/ToUpperCaseConverter.d.ts +4 -0
- package/dist/converters/TypeConverter.d.ts +5 -0
- package/dist/createCRUDRepositoryOptions.d.ts +7 -0
- package/dist/createReadonlyRepositoryMappingOptions.d.ts +6 -0
- package/dist/dotEnv.d.ts +5 -0
- package/dist/index.d.ts +119 -0
- package/dist/mappers/ChangeCasingMapper.d.ts +9 -0
- package/dist/mappers/DictionaryMapper.d.ts +8 -0
- package/dist/mappers/FlatToNestedMapper.d.ts +20 -0
- package/dist/mappers/IFlatToNestedMapper.d.ts +6 -0
- package/dist/mappers/IMapper.d.ts +4 -0
- package/dist/mappers/IdentityMapper.d.ts +5 -0
- package/dist/mappers/PluralizerMapper.d.ts +6 -0
- package/dist/mappers/RowTypeMapper.d.ts +6 -0
- package/dist/mappers/TransformConverterMapper.d.ts +5 -0
- package/dist/mappers/TransformMapper.d.ts +6 -0
- package/dist/mappers/TypeMap.d.ts +8 -0
- package/dist/mappers/TypeMapGroup.d.ts +5 -0
- package/dist/mocks/index.d.ts +9 -0
- package/dist/mocks/mock-command.d.ts +28 -0
- package/dist/mocks/mock-connection.d.ts +22 -0
- package/dist/mocks/mock-datasource.d.ts +24 -0
- package/dist/mocks/mock-driver.d.ts +15 -0
- package/dist/mocks/mock-reader.d.ts +14 -0
- package/dist/mocks/mock-repository.d.ts +42 -0
- package/dist/mocks/mock-select-statement.d.ts +10 -0
- package/dist/mocks/mock-transaction.d.ts +16 -0
- package/dist/mocks/mock-where.d.ts +8 -0
- package/dist/parametersToRecord.d.ts +2 -0
- package/dist/readerToAsyncIterable.d.ts +2 -0
- package/dist/splitStatements.d.ts +1 -0
- package/dist/statementParameters.d.ts +1 -0
- package/dist/statementUsesParameter.d.ts +1 -0
- package/dist/statements/Aggregate.d.ts +6 -0
- package/dist/statements/ArrayExpression.d.ts +5 -0
- package/dist/statements/BulkDeleteStatement.d.ts +11 -0
- package/dist/statements/BulkDeleteStatementTextRenderer.d.ts +4 -0
- package/dist/statements/BulkInsertStatement.d.ts +12 -0
- package/dist/statements/BulkInsertStatementTextRenderer.d.ts +4 -0
- package/dist/statements/BulkUpdateStatement.d.ts +16 -0
- package/dist/statements/BulkUpdateStatementTextRenderer.d.ts +8 -0
- package/dist/statements/CheckConstraint.d.ts +7 -0
- package/dist/statements/Cmp.d.ts +9 -0
- package/dist/statements/Column.d.ts +9 -0
- package/dist/statements/ColumnDefault.d.ts +5 -0
- package/dist/statements/Condition.d.ts +3 -0
- package/dist/statements/ConditionRenderer.d.ts +12 -0
- package/dist/statements/CreateIndexStatement.d.ts +10 -0
- package/dist/statements/CreateIndexStatementTextRenderer.d.ts +5 -0
- package/dist/statements/CreateTableStatement.d.ts +11 -0
- package/dist/statements/CreateTableStatementTextRenderer.d.ts +18 -0
- package/dist/statements/DefaultStatementTextRenderer.d.ts +5 -0
- package/dist/statements/DeleteStatement.d.ts +9 -0
- package/dist/statements/DeleteStatementTextRenderer.d.ts +7 -0
- package/dist/statements/ExecuteStoredProcedureStatement.d.ts +9 -0
- package/dist/statements/ExecuteStoredProcedureStatementTextRenderer.d.ts +4 -0
- package/dist/statements/ExpressionEvaluator.d.ts +5 -0
- package/dist/statements/ExpressionRenderer.d.ts +8 -0
- package/dist/statements/ForeignKey.d.ts +19 -0
- package/dist/statements/IBulkDeleteStatement.d.ts +3 -0
- package/dist/statements/IBulkInsertStatement.d.ts +3 -0
- package/dist/statements/IBulkUpdateStatement.d.ts +3 -0
- package/dist/statements/ICondition.d.ts +4 -0
- package/dist/statements/IConditionVisitor.d.ts +6 -0
- package/dist/statements/ICreateIndexStatement.d.ts +3 -0
- package/dist/statements/ICreateTableStatement.d.ts +3 -0
- package/dist/statements/IDeleteStatement.d.ts +5 -0
- package/dist/statements/IExecuteStoredProcedureStatement.d.ts +3 -0
- package/dist/statements/IExpression.d.ts +2 -0
- package/dist/statements/IInsertStatement.d.ts +3 -0
- package/dist/statements/IReturnStatement.d.ts +3 -0
- package/dist/statements/ISelectStatement.d.ts +5 -0
- package/dist/statements/IStatement.d.ts +4 -0
- package/dist/statements/IStatementVisitor.d.ts +26 -0
- package/dist/statements/ITableConstraint.d.ts +5 -0
- package/dist/statements/IUpdateStatement.d.ts +5 -0
- package/dist/statements/IWhere.d.ts +6 -0
- package/dist/statements/Identifier.d.ts +5 -0
- package/dist/statements/InsertStatement.d.ts +9 -0
- package/dist/statements/InsertStatementTextRenderer.d.ts +4 -0
- package/dist/statements/Join.d.ts +14 -0
- package/dist/statements/Literal.d.ts +6 -0
- package/dist/statements/Native.d.ts +5 -0
- package/dist/statements/NotNull.d.ts +4 -0
- package/dist/statements/Operator.d.ts +107 -0
- package/dist/statements/OrderBy.d.ts +10 -0
- package/dist/statements/PrimaryKey.d.ts +7 -0
- package/dist/statements/ReturnStatement.d.ts +9 -0
- package/dist/statements/ReturnStatementTextRenderer.d.ts +4 -0
- package/dist/statements/SelectInsertedIdStatement.d.ts +8 -0
- package/dist/statements/SelectInsertedIdStatementTextRenderer.d.ts +4 -0
- package/dist/statements/SelectStatement.d.ts +20 -0
- package/dist/statements/SelectStatementOptions.d.ts +15 -0
- package/dist/statements/SelectStatementTextRenderer.d.ts +19 -0
- package/dist/statements/StatementEvaluator.d.ts +4 -0
- package/dist/statements/StatementParser.d.ts +4 -0
- package/dist/statements/StatementRenderer.d.ts +42 -0
- package/dist/statements/StatementTextRenderer.d.ts +14 -0
- package/dist/statements/Subquery.d.ts +6 -0
- package/dist/statements/UniqueConstraint.d.ts +7 -0
- package/dist/statements/UpdateStatement.d.ts +14 -0
- package/dist/statements/UpdateStatementTextRenderer.d.ts +14 -0
- package/dist/statements/Where.d.ts +16 -0
- package/dist/statements/WhereText.d.ts +8 -0
- package/dist/statements/index.d.ts +75 -0
- package/dist/stubs/StubCommand.d.ts +15 -0
- package/dist/stubs/StubConnection.d.ts +13 -0
- package/dist/stubs/StubDatasource.d.ts +20 -0
- package/dist/stubs/StubReader.d.ts +11 -0
- package/dist/stubs/StubRepository.d.ts +22 -0
- package/dist/stubs/StubTransaction.d.ts +10 -0
- package/dist/stubs/createStubDatasource.d.ts +2 -0
- package/dist/stubs/index.d.ts +7 -0
- package/dist/stubs/notImplemented.d.ts +1 -0
- package/package.json +14 -6
- package/src/ArrayReader.test.ts +23 -0
- package/src/BulkCRUDRepository.test.ts +183 -0
- package/src/BulkWriteStrategy.test.ts +293 -0
- package/src/CRUDRepository.test.ts +420 -0
- package/src/CRUDRepository.ts +1 -1
- package/src/CRUDRepositoryOptions.test.ts +76 -0
- package/src/Command.test.ts +105 -0
- package/src/CommandMiddlewareHost.test.ts +98 -0
- package/src/Connection.test.ts +34 -0
- package/src/ConsoleCommandMiddleware.test.ts +149 -0
- package/src/Datasource.test.ts +63 -0
- package/src/DatasourceError.test.ts +10 -0
- package/src/DatasourceFactory.test.ts +159 -0
- package/src/DatasourceUrlParser.test.ts +173 -0
- package/src/ExpectConsoleMiddleware.test.ts +79 -0
- package/src/FirstResultReader.test.ts +43 -0
- package/src/IsolationLevel.test.ts +13 -0
- package/src/ProviderCatalog.test.ts +110 -0
- package/src/ProviderNotImplementedError.test.ts +11 -0
- package/src/ProviderNotInstalledError.test.ts +11 -0
- package/src/ReadonlyRepository.test.ts +406 -0
- package/src/RepositoryFactory.test.ts +94 -0
- package/src/StatementMappers.test.ts +221 -0
- package/src/StreamingReader.test.ts +126 -0
- package/src/Transaction.test.ts +37 -0
- package/src/UnitOfWorkTransaction.test.ts +43 -0
- package/src/UserViewCRUDRepository.test.ts +323 -0
- package/src/builders/builders.test.ts +404 -0
- package/src/converters/ChangeCasingConverter.test.ts +34 -0
- package/src/converters/PluralizerConverter.test.ts +43 -0
- package/src/converters/RegexConverter.test.ts +11 -0
- package/src/converters/ReplaceConverter.test.ts +9 -0
- package/src/converters/RowTypeConverter.test.ts +79 -0
- package/src/converters/ToLowerCaseConverter.test.ts +9 -0
- package/src/converters/ToUpperCaseConverter.test.ts +9 -0
- package/src/dotEnv.test.ts +21 -0
- package/src/edgeCases.test.ts +485 -0
- package/src/mappers/ChangeCasingMapper.test.ts +32 -0
- package/src/mappers/DictionaryMapper.test.ts +69 -0
- package/src/mappers/FlatToNestedMapper.test.ts +269 -0
- package/src/mappers/IdentityMapper.test.ts +12 -0
- package/src/mappers/PluralizerMapper.test.ts +40 -0
- package/src/mappers/RowTypeMapper.test.ts +12 -0
- package/src/mappers/TransformConverterMapper.test.ts +181 -0
- package/src/mappers/transformMapper.test.ts +66 -0
- package/src/mocks/MockCommand.test.ts +75 -0
- package/src/mocks/MockConnection.test.ts +34 -0
- package/src/mocks/MockDatasource.test.ts +40 -0
- package/src/mocks/MockReader.test.ts +17 -0
- package/src/mocks/MockRepository.test.ts +32 -0
- package/src/mocks/MockSelectStatement.test.ts +11 -0
- package/src/mocks/MockTransaction.test.ts +17 -0
- package/src/mocks/MockWhere.test.ts +11 -0
- package/src/parametersToRecord.test.ts +19 -0
- package/src/public-surface.test.ts +23 -0
- package/src/readWithWhereClauseAndCustomFlatToNestedObjectMappingOptions.test.ts +126 -0
- package/src/readerToAsyncIterable.test.ts +39 -0
- package/src/repository.test.ts +410 -0
- package/src/repository.type.test.ts +29 -0
- package/src/statements/SelectInsertedIdStatement.test.ts +11 -0
- package/src/statements/StatementCompile.test.ts +231 -0
- package/src/statements/StatementParser.test.ts +304 -0
- package/src/statements/index.ts +1 -1
- package/src/stubs/StubDatasource.test.ts +99 -0
- package/src/tests/contact/Address.ts +14 -0
- package/src/tests/contact/AddressType.ts +6 -0
- package/src/tests/contact/Base.ts +7 -0
- package/src/tests/contact/Contact.ts +12 -0
- package/src/tests/contact/ContactBase.ts +8 -0
- package/src/tests/contact/ContactChangeLog.ts +8 -0
- package/src/tests/contact/ContactGroup.ts +6 -0
- package/src/tests/contact/ContactGroupBase.ts +6 -0
- package/src/tests/contact/ContactGroupMember.ts +6 -0
- package/src/tests/contact/ContactSource.ts +6 -0
- package/src/tests/contact/ContactType.ts +6 -0
- package/src/tests/contact/ContactsService.test.ts +118 -0
- package/src/tests/contact/ContactsService.ts +32 -0
- package/src/tests/contact/ImportContactRequest.ts +4 -0
- package/src/tests/contact/ImportContactResult.ts +5 -0
- package/src/tests/contact/LegacyContact.ts +7 -0
- package/src/tests/contact/LegacyMigrationResult.ts +5 -0
- package/src/tests/contact/Phone.ts +9 -0
- package/src/tests/contact/PhoneType.ts +6 -0
- package/src/tests/contact/asContactsView.ts +63 -0
- package/src/tests/contact/contactColumns.ts +40 -0
- package/src/tests/contact/contactLookupIds.ts +32 -0
- package/src/tests/contact/contactLookups.ts +6 -0
- package/src/tests/contact/contactsParity.ts +63 -0
- package/src/tests/contact/contactsServiceOf.ts +38 -0
- package/src/tests/contact/contactsView.ts +31 -0
- package/src/tests/contact/expectContactsView.ts +23 -0
- package/src/tests/contact/extraContact.ts +61 -0
- package/src/tests/contact/logMapped.ts +5 -0
- 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,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,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,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;
|