This repository has been archived by the owner on Oct 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor user manual to make it less confusing
- Loading branch information
Showing
4 changed files
with
109 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Data transformation | ||
|
||
```typescript | ||
const aggregateMuzzles = (pupper: DalmatianAttributes) => ({ | ||
...pupper, | ||
muzzleScore: pupper.muzzleLenght * pupper.age, | ||
muzzleCount: 1, | ||
}) | ||
|
||
const dalmatianService = createService<DalmatianAttributes>({ | ||
create: ctx => dalmatianRepository.create(aggregateMuzzles(ctx.data), ctx.options), | ||
update: ctx => dalmatianRepository.updateById(ctx.entity.id, aggregateMuzzles(ctx.data), ctx.options), | ||
}); | ||
``` | ||
|
||
But thats boring, repetitive and you might forget to do that on update etc. Use `processData` instead. | ||
|
||
```typescript | ||
const aggregateMuzzles = /*...*/ | ||
|
||
const dalmatianService = createService<DalmatianAttributes>({ | ||
create: ctx => dalmatianRepository.create(ctx.data, ctx.options), | ||
update: ctx => dalmatianRepository.updateById(ctx.entity.id, ctx.data, ctx.options), | ||
processData: ctx => aggregateMuzzles(ctx.data), | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Generating handlers | ||
|
||
Perhaps you don't want to use Express middleware but still want to use Crudella. | ||
Well luckily it provides handler creators you can use and bind them yourself or use any other framework except express. | ||
|
||
```typescript | ||
// dalmatianService.ts | ||
import { createService } from 'crudella'; | ||
|
||
const dalmatianService = createService<DalmatianAttributes>({ | ||
// each method receives relevant CRUD context | ||
detail: ctx => dalmatianRepository.find(ctx.id, ctx.options), | ||
create: ctx => dalmatianRepository.create(ctx.data, ctx.options), | ||
update: ctx => dalmatianRepository.updateById(ctx.entity.id, ctx.data, ctx.options), | ||
delete: ctx => dalmatianRepository.deleteById(ctx.entity.id), | ||
list: ctx => dalmatianRepository.list(ctx.filters, ctx.options), | ||
}); | ||
|
||
// dalmatianService.xxxHandler is a handler creator, called with optional options | ||
export const dalmatianDetail = dalmatianService.detailHandler({ withRelated: withRelated.detail }) | ||
export const dalmatianCreate = dalmatianService.createHandler({ withRelated: withRelated.detail }) | ||
export const dalmatianUpdate = dalmatianService.updateHandler({ withRelated: withRelated.detail }) | ||
export const dalmatianDelete = dalmatianService.deleteHandler() | ||
export const dalmatianList = dalmatianService.listHandler({ withRelated: withRelated.list }) | ||
``` | ||
|
||
No we have created a service exporting service-layer handlers for CRUD API on dalmatians. | ||
Handlers are async and always return a promise with the result you provide from your bindings, or an error. | ||
Here are are signatures of the service handlers (promise values depend on your implementation). | ||
```typescript | ||
dalmatianDetail: (id: number, context: C) => Promise<DalmatianAttributes>; | ||
dalmatianCreate: (data: any, context: C) => Promise<DalmatianAttributes>; | ||
dalmatianUpdate: (id: number, data: any, context: C) => Promise<DalmatianAttributes>; | ||
dalmatianDelete: (id: number, context: C) => Promise<bool>; | ||
dalmatianList: (filters: any, context: C) => Promise<DalmatianAttributes[]>; | ||
``` | ||
The `C` type is any HTTP context you prefer to use, it can contain client parameters, user session etc. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Repository | ||
|
||
If you are using an existing ORM or any data layer with consistent API you can take a shortcut when defining a Crudella service, using an option `repository`. | ||
Create a bridge function for you data abstraction layer. | ||
|
||
```typescript | ||
export const bridgeRepo = <T extends {id: any}>(repo: MyRepo<T>) => ({ | ||
create: repo.create.withDetailById, | ||
deleteById: repo.deleteById, | ||
detailById: repo.detailById, | ||
list: repo.list, | ||
updateById: repo.updateById.withDetail, | ||
}); | ||
``` | ||
Having this bridge function in your project, you can easily create CRUD services more consciously and focus on the hard stuff. | ||
|
||
```typescript | ||
const dalmatianService = createService<DalmatianAttributes>({ | ||
repository: bridgeRepo(dalmatianRepository), | ||
}); | ||
``` |