Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ilijanl/typed-ddd
Small typed ddd library which gives full type-safety
https://github.com/ilijanl/typed-ddd
ddd domain small typescript
Last synced: about 21 hours ago
JSON representation
Small typed ddd library which gives full type-safety
- Host: GitHub
- URL: https://github.com/ilijanl/typed-ddd
- Owner: ilijaNL
- License: mit
- Created: 2023-08-12T11:13:13.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-11-12T15:57:21.000Z (about 1 year ago)
- Last Synced: 2025-01-02T01:31:34.537Z (9 days ago)
- Topics: ddd, domain, small, typescript
- Language: TypeScript
- Homepage: https://github.com/ilijaNL/typed-ddd
- Size: 85 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# typed-ddd
Small typed ddd library which gives full type-safety
## Usage
```ts
// example of the usageimport { TypeBox, createAdapter, createDomain } from 'typed-ddd';
// Create domain with events and procedures (/commands)
const domain = createDomain({
// define events
eventsMap: {
account_created: TypeBox.Type.Object({
id: TypeBox.Type.String(),
}),
account_deleted: TypeBox.Type.Object({
deleted_id: TypeBox.Type.String(),
}),
},
// define procedures
procedures: {
// define procedures
// ctx can be used to have "DI"
async createAccount(factory, input: { works: string }, ctx: { id: string }) {
return {
events: [factory.account_created({ id: input.works })],
data: {
awda: 'works',
},
};
},
async deleteAccount(
factory,
input: { acc_id: string },
ctx: { getAccount: (acc_id: string) => Promise<{ id: string } | null> }
) {
if (await ctx.getAccount(input.acc_id)) {
return {
events: [factory.account_deleted({ deleted_id: input.acc_id })],
data: {
awda: 'works',
},
};
}return {
data: null,
events: [],
};
},
},
});type SqlCommand = { sql: string; paramaters?: unknown[] };
// Create SQL adapter
const sqlAdapter = createAdapter(domain)({
account_created: ({ data }) => [{ sql: 'INSERT INTO accounts VALUES($1)', paramaters: [data.id] }],
account_deleted: ({ data }) => [{ sql: 'DELETE FROM accounts WHERE $1', paramaters: [data.deleted_id] }],
});async function commit(_commands: ReadonlyArray) {
// run some db query
}async function deleteReqHandler() {
const result = await sqlAdapter.handlers.deleteAccount(
{ acc_id: '123' },
{
getAccount(acc_id) {
return Promise.resolve({ id: acc_id });
},
}
);await commit(result.effects);
return result.data;
}
```