@zola_do/crud 0.1.9 → 0.1.13
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/README.md +145 -0
- package/package.json +11 -6
package/README.md
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# @zola_do/crud
|
|
2
|
+
|
|
3
|
+
Generic CRUD controllers, services, and repositories for NestJS entities.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# Install individually
|
|
9
|
+
npm install @zola_do/crud
|
|
10
|
+
|
|
11
|
+
# Or via meta package
|
|
12
|
+
npm install @zola_do/nestjs-shared
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Dependencies
|
|
16
|
+
|
|
17
|
+
This package requires:
|
|
18
|
+
|
|
19
|
+
- `@zola_do/core` — Entities, `DataResponseFormat`, `EntityCrudOptions`
|
|
20
|
+
- `@zola_do/collection-query` — `decodeCollectionQuery`, `QueryConstructor`, `FilterOperators`
|
|
21
|
+
- `@zola_do/authorization` — `JwtGuard`, `PermissionsGuard`
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### Entity Setup
|
|
26
|
+
|
|
27
|
+
Extend `CommonEntity` (or `Audit` for tenant-aware entities) and use with the CRUD factories:
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { CommonEntity } from '@zola_do/core';
|
|
31
|
+
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
|
|
32
|
+
|
|
33
|
+
@Entity('products')
|
|
34
|
+
export class Product extends CommonEntity {
|
|
35
|
+
@PrimaryGeneratedColumn('uuid')
|
|
36
|
+
id: string;
|
|
37
|
+
|
|
38
|
+
@Column()
|
|
39
|
+
name: string;
|
|
40
|
+
|
|
41
|
+
@Column({ type: 'decimal', precision: 10, scale: 2 })
|
|
42
|
+
price: number;
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Controller and Service
|
|
47
|
+
|
|
48
|
+
Create a CRUD controller and service for your entity:
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
import { Controller } from '@nestjs/common';
|
|
52
|
+
import {
|
|
53
|
+
EntityCrudController,
|
|
54
|
+
EntityCrudService,
|
|
55
|
+
} from '@zola_do/crud';
|
|
56
|
+
import { Product } from './product.entity';
|
|
57
|
+
|
|
58
|
+
@Controller('products')
|
|
59
|
+
export class ProductsController extends EntityCrudController<Product>({
|
|
60
|
+
createPermission: 'product:create',
|
|
61
|
+
viewPermission: 'product:view',
|
|
62
|
+
updatePermission: 'product:update',
|
|
63
|
+
deletePermission: 'product:delete',
|
|
64
|
+
}) {
|
|
65
|
+
constructor(service: EntityCrudService<Product>) {
|
|
66
|
+
super(service);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { Module } from '@nestjs/common';
|
|
73
|
+
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
74
|
+
import { EntityCrudService } from '@zola_do/crud';
|
|
75
|
+
import { Product } from './product.entity';
|
|
76
|
+
import { ProductsController } from './products.controller';
|
|
77
|
+
|
|
78
|
+
@Module({
|
|
79
|
+
imports: [TypeOrmModule.forFeature([Product])],
|
|
80
|
+
controllers: [ProductsController],
|
|
81
|
+
providers: [EntityCrudService],
|
|
82
|
+
})
|
|
83
|
+
export class ProductsModule {}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Available Endpoints
|
|
87
|
+
|
|
88
|
+
- `POST /` — Create
|
|
89
|
+
- `GET /` — Find all (supports `?q=` collection query)
|
|
90
|
+
- `GET /:id` — Find one
|
|
91
|
+
- `PUT /:id` — Update
|
|
92
|
+
- `DELETE /:id` — Soft delete
|
|
93
|
+
|
|
94
|
+
### EntityCrudOptions
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
import { EntityCrudOptions } from '@zola_do/core';
|
|
98
|
+
|
|
99
|
+
const options: EntityCrudOptions = {
|
|
100
|
+
createDto: CreateProductDto,
|
|
101
|
+
updateDto: UpdateProductDto,
|
|
102
|
+
createPermission: 'product:create',
|
|
103
|
+
viewPermission: 'product:view',
|
|
104
|
+
updatePermission: 'product:update',
|
|
105
|
+
deletePermission: 'product:delete',
|
|
106
|
+
restorePermission: 'product:restore',
|
|
107
|
+
viewArchivedPermission: 'product:view_archived',
|
|
108
|
+
};
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Relation CRUD
|
|
112
|
+
|
|
113
|
+
For many-to-many or relation management:
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
import { RelationCrudController, RelationCrudService } from '@zola_do/crud';
|
|
117
|
+
|
|
118
|
+
@Controller('products/:productId/categories')
|
|
119
|
+
export class ProductCategoriesController extends RelationCrudController(
|
|
120
|
+
{
|
|
121
|
+
firstEntityIdName: 'productId',
|
|
122
|
+
firstInclude: 'product',
|
|
123
|
+
secondEntityIdName: 'categoryId',
|
|
124
|
+
secondInclude: 'category',
|
|
125
|
+
viewPermission: 'product:view',
|
|
126
|
+
},
|
|
127
|
+
ProductCategory,
|
|
128
|
+
) {
|
|
129
|
+
constructor(service: RelationCrudService) {
|
|
130
|
+
super(service);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Exports
|
|
136
|
+
|
|
137
|
+
- **Controllers:** `EntityCrudController`, `RelationCrudController`, `ExtraCrudController`
|
|
138
|
+
- **Services:** `EntityCrudService`, `RelationCrudService`, `ExtraCrudService`
|
|
139
|
+
- **Repositories:** `EntityCrudRepository`, `RelationCrudRepository`, `ExtraCrudRepository`
|
|
140
|
+
|
|
141
|
+
## Related Packages
|
|
142
|
+
|
|
143
|
+
- [@zola_do/core](../core) — Entity base classes, `EntityCrudOptions`
|
|
144
|
+
- [@zola_do/collection-query](../collection-query) — Query decoding for list endpoints
|
|
145
|
+
- [@zola_do/authorization](../authorization) — Guards for protected CRUD
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zola_do/crud",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.13",
|
|
4
4
|
"description": "Generic CRUD controllers and services for NestJS",
|
|
5
5
|
"author": "zolaDO",
|
|
6
6
|
"license": "ISC",
|
|
@@ -15,7 +15,10 @@
|
|
|
15
15
|
"default": "./dist/index.js"
|
|
16
16
|
}
|
|
17
17
|
},
|
|
18
|
-
"files": [
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"README.md"
|
|
21
|
+
],
|
|
19
22
|
"scripts": {
|
|
20
23
|
"build": "rimraf dist && tsc",
|
|
21
24
|
"prepublishOnly": "npm run build"
|
|
@@ -30,12 +33,14 @@
|
|
|
30
33
|
"typeorm-extension": "^3.7.1"
|
|
31
34
|
},
|
|
32
35
|
"peerDependenciesMeta": {
|
|
33
|
-
"@nestjs/swagger": {
|
|
36
|
+
"@nestjs/swagger": {
|
|
37
|
+
"optional": true
|
|
38
|
+
}
|
|
34
39
|
},
|
|
35
40
|
"dependencies": {
|
|
36
|
-
"@zola_do/core": "
|
|
37
|
-
"@zola_do/collection-query": "
|
|
38
|
-
"@zola_do/authorization": "
|
|
41
|
+
"@zola_do/core": "^0.1.9",
|
|
42
|
+
"@zola_do/collection-query": "^0.1.9",
|
|
43
|
+
"@zola_do/authorization": "^0.1.9"
|
|
39
44
|
},
|
|
40
45
|
"devDependencies": {
|
|
41
46
|
"rimraf": "^6.1.0",
|