Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/master-co/business
A business data model for quick verification, access and output of specific data formats.
https://github.com/master-co/business
angular api bo data-format data-model dto express input inputs koa nestjs nodejs output react request response typescript verification vue
Last synced: 2 days ago
JSON representation
A business data model for quick verification, access and output of specific data formats.
- Host: GitHub
- URL: https://github.com/master-co/business
- Owner: master-co
- Created: 2021-02-09T07:49:06.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-02-09T11:04:12.000Z (about 3 years ago)
- Last Synced: 2024-04-14T00:59:16.838Z (10 months ago)
- Topics: angular, api, bo, data-format, data-model, dto, express, input, inputs, koa, nestjs, nodejs, output, react, request, response, typescript, verification, vue
- Language: TypeScript
- Homepage:
- Size: 332 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Master Business
### A business data model for quick verification, access and output of specific data formats.
```shell
npm install @master/business
```### `tsconfig.json`
```tsx
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}
}
```## Usage
```tsx
import { Business, BusinessModel, Input, Output } from '@master/business';@Business()
export class MyBusiness extends BusinessModel {
@Input()
prop1: string;@Output()
prop2: number;@Input()
@Output()
prop3: OtherBusinessModel;...
}```
### `@Input(options?)`
Decorate the property that need to be validated| options | type | description |
| ----------- | ------------------- | ---------------------------------------------------------------------------------- |
| `disabled` | boolean | Used to disable the @Input() decoration behavior of extended objects |
| `required` | boolean | Is the property required |
| `arrayType` | any | Assuming the type is YourType[], the target type must be additionally defined here |
| `enum` | Record | Assuming the type is enum, the target type must be additionally defined here |### `@Output(options?)`
Decorate the property that need to be outputed| options | type | description |
| ---------- | ------- | -------------------------------------------------------------------- |
| `disabled` | boolean | Used to disable the @Input() decoration behavior of extended objects |## Example
The front-end inputs the registration data to the server through the sign-up API, and then outputs the registration result back to the front-end.### File Structure
```tree
├── businesses
│ └── member
│ ├── member.controller.ts
│ ├── member.service.ts
│ ├── member.ts // DAO
│ └── signing-up.ts
```### Define the business model
```tsx
// signing-up.tsimport { Business, BusinessModel, Input } from '@master/business';
@Business()
export class SigningUp extends BusinessModel {@Output()
@Input({ required: true })
name: string;@Output()
@Input()
address: SigningUpAddress;@Output()
type = 'general';// other fields for quick access
a = 1;
b = 2;
c = 3;
d = 4;
}@Business()
class SigningUpAddress extends BusinessModel {
@Output()
@Input()
city: string;@Input()
district: string;
@Input()
street: string;
}
```### Process business logic ( nestjs for example )
```tsx
// member.controller.tsimport { Business, BusinessModel, Input, validate } from '@master/business';
import { MemberService } from './member.service.ts';
import { SigningUp } from './signing-up.ts';@Controller('member')
export class MemberController {
constructor(
private memberService: MemberService
) {}@Post()
async SignUp(
@Body() data: any,
@Res() res: Response
): Promise {const signingUp = new SigningUp(data);
const errors = signingUp.validate();// validate
if(errors.length) {
// property error
res.status(400).send(errors);
} else {
// correct
// business logic process here ...
this.memberService.signUp(signingUp);
res.status(200).send(signingUp);
}
}
}
```### *Input*:request data
```tsx
{
name: "joy",
address: {
city: "taipei",
district: "zhongshan",
street: "my home"
}
}
```### *Processing*:business data
```tsx
{
name: "joy",
address: {
city: "taipei",
district: "zhongshan",
street: "my home"
},
type: 'general',
a: 1,
b: 2,
c: 3,
d: 4
}
```### *Output*:response data
```tsx
{
name: "joy",
address: {
city: "taipei"
},
type: 'general'
}
```## @Input definitions
```tsx
@Business()
class MyBusiness extends BusinessModel {
@Input()
str: string;@Input()
num: number;@Input({ enum: MyEnum })
enum: MyEnum;@Input({ arrayType: MyArrayType })
arrayType: MyArrayType[];
}
```## Solutions
- ### Provide a rich access interface for developers
- ### Follow the DRY principle (Don't repeat yourself)
- ### Omit the definition of Request DTO and Response DTO data structure
- ### Data structure focuses on one interface
- ### Reduce code writing
- ### No need to define variables individually to manipulate data## Code Contributors
|[](https://github.com/BenSeage) |[
](https://github.com/aron-tw) |[
](https://github.com/miles0930) |[
](https://github.com/zxa011023) |
:---:|:---:|:---:|:---:|
[BenSeage](https://github.com/BenSeage)|[Aron](https://github.com/aron-tw)|[Miles](https://github.com/miles0930)|[Lola](https://github.com/zxa011023)|
|creator|designer|maintainer|maintainer