@drax/crud-back 0.11.5 → 0.12.2
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/builders/CrudSchemaBuilder.js +349 -0
- package/dist/controllers/AbstractFastifyController.js +105 -176
- package/dist/index.js +15 -1
- package/dist/regexs/QueryFilterRegex.js +3 -0
- package/dist/repository/AbstractMongoRepository.js +114 -28
- package/dist/repository/AbstractSqliteRepository.js +131 -41
- package/dist/schemas/DeleteBodyResponseSchema.js +9 -0
- package/dist/schemas/ErrorBodyResponseSchema.js +16 -0
- package/dist/schemas/ExportBodyResponseSchema.js +9 -0
- package/dist/schemas/FindBySchema.js +6 -0
- package/dist/schemas/FindSchema.js +9 -0
- package/dist/schemas/IdParamSchema.js +6 -0
- package/dist/schemas/PaginateBodySchema.js +20 -0
- package/dist/schemas/PaginateSchema.js +17 -0
- package/dist/schemas/SearchSchema.js +5 -0
- package/dist/services/AbstractService.js +8 -5
- package/dist/zod/DeleteBodySchema.js +6 -0
- package/dist/zod/IdParamSchema.js +6 -0
- package/dist/zod/PaginateBodySchema.js +20 -0
- package/package.json +7 -6
- package/src/builders/CrudSchemaBuilder.ts +420 -0
- package/src/controllers/AbstractFastifyController.ts +149 -165
- package/src/index.ts +28 -0
- package/src/regexs/QueryFilterRegex.ts +4 -0
- package/src/repository/AbstractMongoRepository.ts +153 -40
- package/src/repository/AbstractSqliteRepository.ts +196 -72
- package/src/schemas/DeleteBodyResponseSchema.ts +12 -0
- package/src/schemas/ErrorBodyResponseSchema.ts +20 -0
- package/src/schemas/ExportBodyResponseSchema.ts +12 -0
- package/src/schemas/FindBySchema.ts +9 -0
- package/src/schemas/FindSchema.ts +13 -0
- package/src/schemas/IdParamSchema.ts +9 -0
- package/src/schemas/PaginateSchema.ts +21 -0
- package/src/schemas/SearchSchema.ts +8 -0
- package/src/services/AbstractService.ts +42 -33
- package/tsconfig.tsbuildinfo +1 -1
- package/types/builders/CrudSchemaBuilder.d.ts +2401 -0
- package/types/builders/CrudSchemaBuilder.d.ts.map +1 -0
- package/types/controllers/AbstractFastifyController.d.ts +6 -1
- package/types/controllers/AbstractFastifyController.d.ts.map +1 -1
- package/types/index.d.ts +10 -1
- package/types/index.d.ts.map +1 -1
- package/types/regexs/QueryFilterRegex.d.ts +4 -0
- package/types/regexs/QueryFilterRegex.d.ts.map +1 -0
- package/types/repository/AbstractMongoRepository.d.ts +6 -3
- package/types/repository/AbstractMongoRepository.d.ts.map +1 -1
- package/types/repository/AbstractSqliteRepository.d.ts +23 -8
- package/types/repository/AbstractSqliteRepository.d.ts.map +1 -1
- package/types/schemas/DeleteBodyResponseSchema.d.ts +20 -0
- package/types/schemas/DeleteBodyResponseSchema.d.ts.map +1 -0
- package/types/schemas/ErrorBodyResponseSchema.d.ts +60 -0
- package/types/schemas/ErrorBodyResponseSchema.d.ts.map +1 -0
- package/types/schemas/ExportBodyResponseSchema.d.ts +20 -0
- package/types/schemas/ExportBodyResponseSchema.d.ts.map +1 -0
- package/types/schemas/FindBySchema.d.ts +13 -0
- package/types/schemas/FindBySchema.d.ts.map +1 -0
- package/types/schemas/FindSchema.d.ts +19 -0
- package/types/schemas/FindSchema.d.ts.map +1 -0
- package/types/schemas/IdParamSchema.d.ts +11 -0
- package/types/schemas/IdParamSchema.d.ts.map +1 -0
- package/types/schemas/PaginateBodySchema.d.ts +61 -0
- package/types/schemas/PaginateBodySchema.d.ts.map +1 -0
- package/types/schemas/PaginateSchema.d.ts +41 -0
- package/types/schemas/PaginateSchema.d.ts.map +1 -0
- package/types/schemas/SearchSchema.d.ts +10 -0
- package/types/schemas/SearchSchema.d.ts.map +1 -0
- package/types/services/AbstractService.d.ts +5 -5
- package/types/services/AbstractService.d.ts.map +1 -1
- package/types/zod/DeleteBodySchema.d.ts +11 -0
- package/types/zod/DeleteBodySchema.d.ts.map +1 -0
- package/types/zod/IdParamSchema.d.ts +11 -0
- package/types/zod/IdParamSchema.d.ts.map +1 -0
- package/types/zod/PaginateBodySchema.d.ts +61 -0
- package/types/zod/PaginateBodySchema.d.ts.map +1 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import z from "zod"
|
|
2
|
+
|
|
3
|
+
const ErrorBodyResponseSchema = z.object({
|
|
4
|
+
statusCode: z.string(),
|
|
5
|
+
error: z.string(),
|
|
6
|
+
message: z.string(),
|
|
7
|
+
i18nMessage: z.string()
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
const ValidationErrorBodyResponseSchema = ErrorBodyResponseSchema.extend({
|
|
11
|
+
inputErrors: z.array(z.object({
|
|
12
|
+
field: z.string(),
|
|
13
|
+
reason: z.string(),
|
|
14
|
+
value: z.any().optional(),
|
|
15
|
+
})).optional()
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
export default ErrorBodyResponseSchema
|
|
19
|
+
|
|
20
|
+
export {ErrorBodyResponseSchema, ValidationErrorBodyResponseSchema}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import z from "zod"
|
|
2
|
+
import QueryFilterRegex from "../regexs/QueryFilterRegex.js";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
const FindQuerySchema = z.object({
|
|
6
|
+
orderBy: z.string().optional(),
|
|
7
|
+
order: z.enum(["asc", "desc"]).optional(),
|
|
8
|
+
search: z.string().optional(),
|
|
9
|
+
filters: z.string().regex(QueryFilterRegex).optional().describe("Format: field;operator;value|field;operator;value|..."),
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
export {FindQuerySchema}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import z from "zod"
|
|
2
|
+
import QueryFilterRegex from "../regexs/QueryFilterRegex.js";
|
|
3
|
+
|
|
4
|
+
const PaginateQuerySchema = z.object({
|
|
5
|
+
page: z.number().optional(),
|
|
6
|
+
limit: z.number().optional(),
|
|
7
|
+
orderBy: z.string().optional(),
|
|
8
|
+
order: z.enum(["asc", "desc"]).optional(),
|
|
9
|
+
search: z.string().optional(),
|
|
10
|
+
filters: z.string().regex(QueryFilterRegex).optional().describe("Format: field;operator;value|field;operator;value|..."),
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
const PaginateBodyResponseSchema = z.object({
|
|
15
|
+
page: z.number(),
|
|
16
|
+
limit: z.number(),
|
|
17
|
+
total: z.number(),
|
|
18
|
+
items: z.array(z.any())
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export {PaginateQuerySchema, PaginateBodyResponseSchema}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {ZodErrorToValidationError} from "@drax/common-back"
|
|
2
2
|
import {ZodError} from "zod";
|
|
3
|
-
import type {
|
|
3
|
+
import type {ZodObject, ZodRawShape} from "zod";
|
|
4
4
|
import type {
|
|
5
5
|
IDraxPaginateOptions,
|
|
6
6
|
IDraxPaginateResult,
|
|
@@ -17,7 +17,7 @@ import {IDraxFindOneOptions} from "@drax/crud-share/types/interfaces/IDraxFindOn
|
|
|
17
17
|
abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
|
|
18
18
|
|
|
19
19
|
protected _repository: IDraxCrudRepository<T, C, U>
|
|
20
|
-
protected _schema?:
|
|
20
|
+
protected _schema?: ZodObject<ZodRawShape> | undefined
|
|
21
21
|
protected _defaultOrder?: string | undefined
|
|
22
22
|
|
|
23
23
|
transformCreate?: (data: C) => Promise<C>;
|
|
@@ -25,8 +25,7 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
|
|
|
25
25
|
transformRead?: (data: T) => Promise<T>;
|
|
26
26
|
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
constructor(repository: IDraxCrudRepository<T, C, U>, schema?: ZodSchema) {
|
|
28
|
+
constructor(repository: IDraxCrudRepository<T, C, U>, schema?: ZodObject<ZodRawShape>) {
|
|
30
29
|
this._repository = repository
|
|
31
30
|
this._schema = schema
|
|
32
31
|
}
|
|
@@ -37,7 +36,7 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
|
|
|
37
36
|
if (this._schema) {
|
|
38
37
|
await this._schema.parseAsync(data)
|
|
39
38
|
}
|
|
40
|
-
if(this.transformCreate){
|
|
39
|
+
if (this.transformCreate) {
|
|
41
40
|
data = await this.transformCreate(data)
|
|
42
41
|
}
|
|
43
42
|
const item: T = await this._repository.create(data)
|
|
@@ -56,7 +55,7 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
|
|
|
56
55
|
if (this._schema) {
|
|
57
56
|
await this._schema.parseAsync(data)
|
|
58
57
|
}
|
|
59
|
-
if(this.transformUpdate){
|
|
58
|
+
if (this.transformUpdate) {
|
|
60
59
|
data = await this.transformUpdate(data)
|
|
61
60
|
}
|
|
62
61
|
const item: T = await this._repository.update(id, data)
|
|
@@ -73,6 +72,10 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
|
|
|
73
72
|
async updatePartial(id: string, data: any): Promise<T> {
|
|
74
73
|
try {
|
|
75
74
|
|
|
75
|
+
if (this._schema) {
|
|
76
|
+
await this._schema.partial().parseAsync(data)
|
|
77
|
+
}
|
|
78
|
+
|
|
76
79
|
const item: T = await this._repository.updatePartial(id, data)
|
|
77
80
|
|
|
78
81
|
return item
|
|
@@ -88,7 +91,7 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
|
|
|
88
91
|
async delete(id: string): Promise<boolean> {
|
|
89
92
|
try {
|
|
90
93
|
const result: boolean = await this._repository.delete(id);
|
|
91
|
-
if(!result){
|
|
94
|
+
if (!result) {
|
|
92
95
|
throw new Error("error.deletionFailed");
|
|
93
96
|
}
|
|
94
97
|
return result;
|
|
@@ -102,7 +105,7 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
|
|
|
102
105
|
async findById(id: string): Promise<T | null> {
|
|
103
106
|
try {
|
|
104
107
|
let item: T = await this._repository.findById(id);
|
|
105
|
-
if(this.transformRead){
|
|
108
|
+
if (this.transformRead) {
|
|
106
109
|
item = await this.transformRead(item)
|
|
107
110
|
}
|
|
108
111
|
return item
|
|
@@ -115,8 +118,8 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
|
|
|
115
118
|
async findByIds(ids: Array<string>): Promise<T[]> {
|
|
116
119
|
try {
|
|
117
120
|
let items: T[] = await this._repository.findByIds(ids);
|
|
118
|
-
if(this.transformRead){
|
|
119
|
-
items = await Promise.all(items.map(
|
|
121
|
+
if (this.transformRead) {
|
|
122
|
+
items = await Promise.all(items.map(item => this.transformRead(item)))
|
|
120
123
|
}
|
|
121
124
|
return items
|
|
122
125
|
} catch (e) {
|
|
@@ -128,7 +131,7 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
|
|
|
128
131
|
async findOneBy(field: string, value: string): Promise<T | null> {
|
|
129
132
|
try {
|
|
130
133
|
let item: T = await this._repository.findOneBy(field, value);
|
|
131
|
-
if(this.transformRead){
|
|
134
|
+
if (this.transformRead) {
|
|
132
135
|
item = await this.transformRead(item)
|
|
133
136
|
}
|
|
134
137
|
return item
|
|
@@ -139,11 +142,12 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
|
|
|
139
142
|
|
|
140
143
|
}
|
|
141
144
|
|
|
142
|
-
async findBy(field: string, value: string): Promise<T[] | null> {
|
|
145
|
+
async findBy(field: string, value: string, limit: number = 1000): Promise<T[] | null> {
|
|
143
146
|
try {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
+
|
|
148
|
+
let items: T[] = await this._repository.findBy(field, value, limit);
|
|
149
|
+
if (this.transformRead) {
|
|
150
|
+
items = await Promise.all(items.map(item => this.transformRead(item)))
|
|
147
151
|
}
|
|
148
152
|
return items
|
|
149
153
|
} catch (e) {
|
|
@@ -156,8 +160,8 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
|
|
|
156
160
|
async fetchAll(): Promise<T[]> {
|
|
157
161
|
try {
|
|
158
162
|
let items: T[] = await this._repository.fetchAll();
|
|
159
|
-
if(this.transformRead){
|
|
160
|
-
items = await Promise.all(items.map(
|
|
163
|
+
if (this.transformRead) {
|
|
164
|
+
items = await Promise.all(items.map(item => this.transformRead(item)))
|
|
161
165
|
}
|
|
162
166
|
return items
|
|
163
167
|
} catch (e) {
|
|
@@ -169,9 +173,10 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
|
|
|
169
173
|
|
|
170
174
|
async search(value: string, limit: number = 1000, filters: IDraxFieldFilter[] = []): Promise<T[]> {
|
|
171
175
|
try {
|
|
176
|
+
|
|
172
177
|
let items: T[] = await this._repository.search(value, limit, filters);
|
|
173
|
-
if(this.transformRead){
|
|
174
|
-
items = await Promise.all(items.map(
|
|
178
|
+
if (this.transformRead) {
|
|
179
|
+
items = await Promise.all(items.map(item => this.transformRead(item)))
|
|
175
180
|
}
|
|
176
181
|
return items
|
|
177
182
|
} catch (e) {
|
|
@@ -183,17 +188,19 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
|
|
|
183
188
|
|
|
184
189
|
async paginate({
|
|
185
190
|
page = 1,
|
|
186
|
-
limit =
|
|
191
|
+
limit = 10,
|
|
187
192
|
orderBy = this._defaultOrder,
|
|
188
|
-
order =
|
|
193
|
+
order = "asc",
|
|
189
194
|
search = '',
|
|
190
195
|
filters = []
|
|
191
196
|
}: IDraxPaginateOptions): Promise<IDraxPaginateResult<T>> {
|
|
192
197
|
try {
|
|
198
|
+
|
|
199
|
+
|
|
193
200
|
const pagination = await this._repository.paginate({page, limit, orderBy, order, search, filters});
|
|
194
201
|
|
|
195
|
-
if(this.transformRead){
|
|
196
|
-
pagination.items = await Promise.all(pagination.items.map(
|
|
202
|
+
if (this.transformRead) {
|
|
203
|
+
pagination.items = await Promise.all(pagination.items.map(item => this.transformRead(item)))
|
|
197
204
|
}
|
|
198
205
|
|
|
199
206
|
return pagination;
|
|
@@ -208,10 +215,12 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
|
|
|
208
215
|
orderBy = '',
|
|
209
216
|
order = false,
|
|
210
217
|
search = '',
|
|
211
|
-
filters = []
|
|
218
|
+
filters = [],
|
|
219
|
+
limit = 0,
|
|
212
220
|
}: IDraxFindOptions): Promise<T[]> {
|
|
213
221
|
try {
|
|
214
|
-
|
|
222
|
+
|
|
223
|
+
let items = await this._repository.find({orderBy, order, search, filters, limit});
|
|
215
224
|
return items;
|
|
216
225
|
} catch (e) {
|
|
217
226
|
console.error("Error find", e)
|
|
@@ -221,11 +230,11 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
|
|
|
221
230
|
}
|
|
222
231
|
|
|
223
232
|
async findOne({
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
233
|
+
search = '',
|
|
234
|
+
filters = []
|
|
235
|
+
}: IDraxFindOneOptions): Promise<T> {
|
|
227
236
|
try {
|
|
228
|
-
let item = await this._repository.findOne({
|
|
237
|
+
let item = await this._repository.findOne({search, filters});
|
|
229
238
|
return item;
|
|
230
239
|
} catch (e) {
|
|
231
240
|
console.error("Error findOne", e)
|
|
@@ -246,12 +255,12 @@ abstract class AbstractService<T, C, U> implements IDraxCrudService<T, C, U> {
|
|
|
246
255
|
destinationPath: string): Promise<IDraxExportResult> {
|
|
247
256
|
try {
|
|
248
257
|
|
|
249
|
-
let cursor:any
|
|
250
|
-
let exporter:any
|
|
258
|
+
let cursor: any
|
|
259
|
+
let exporter: any
|
|
251
260
|
|
|
252
|
-
if(this._repository.findCursor){
|
|
261
|
+
if (this._repository.findCursor) {
|
|
253
262
|
cursor = await this._repository.findCursor({orderBy, order, search, filters});
|
|
254
|
-
}else{
|
|
263
|
+
} else {
|
|
255
264
|
cursor = await this._repository.find({orderBy, order, search, filters});
|
|
256
265
|
}
|
|
257
266
|
|