@drax/crud-back 0.11.5 → 0.12.1

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.
Files changed (74) hide show
  1. package/dist/builders/CrudSchemaBuilder.js +349 -0
  2. package/dist/controllers/AbstractFastifyController.js +105 -176
  3. package/dist/index.js +15 -1
  4. package/dist/regexs/QueryFilterRegex.js +3 -0
  5. package/dist/repository/AbstractMongoRepository.js +114 -28
  6. package/dist/repository/AbstractSqliteRepository.js +131 -41
  7. package/dist/schemas/DeleteBodyResponseSchema.js +9 -0
  8. package/dist/schemas/ErrorBodyResponseSchema.js +16 -0
  9. package/dist/schemas/ExportBodyResponseSchema.js +9 -0
  10. package/dist/schemas/FindBySchema.js +6 -0
  11. package/dist/schemas/FindSchema.js +9 -0
  12. package/dist/schemas/IdParamSchema.js +6 -0
  13. package/dist/schemas/PaginateBodySchema.js +20 -0
  14. package/dist/schemas/PaginateSchema.js +17 -0
  15. package/dist/schemas/SearchSchema.js +5 -0
  16. package/dist/services/AbstractService.js +8 -5
  17. package/dist/zod/DeleteBodySchema.js +6 -0
  18. package/dist/zod/IdParamSchema.js +6 -0
  19. package/dist/zod/PaginateBodySchema.js +20 -0
  20. package/package.json +7 -6
  21. package/src/builders/CrudSchemaBuilder.ts +420 -0
  22. package/src/controllers/AbstractFastifyController.ts +149 -165
  23. package/src/index.ts +28 -0
  24. package/src/regexs/QueryFilterRegex.ts +4 -0
  25. package/src/repository/AbstractMongoRepository.ts +153 -40
  26. package/src/repository/AbstractSqliteRepository.ts +196 -72
  27. package/src/schemas/DeleteBodyResponseSchema.ts +12 -0
  28. package/src/schemas/ErrorBodyResponseSchema.ts +20 -0
  29. package/src/schemas/ExportBodyResponseSchema.ts +12 -0
  30. package/src/schemas/FindBySchema.ts +9 -0
  31. package/src/schemas/FindSchema.ts +13 -0
  32. package/src/schemas/IdParamSchema.ts +9 -0
  33. package/src/schemas/PaginateSchema.ts +21 -0
  34. package/src/schemas/SearchSchema.ts +8 -0
  35. package/src/services/AbstractService.ts +42 -33
  36. package/tsconfig.tsbuildinfo +1 -1
  37. package/types/builders/CrudSchemaBuilder.d.ts +2401 -0
  38. package/types/builders/CrudSchemaBuilder.d.ts.map +1 -0
  39. package/types/controllers/AbstractFastifyController.d.ts +6 -1
  40. package/types/controllers/AbstractFastifyController.d.ts.map +1 -1
  41. package/types/index.d.ts +10 -1
  42. package/types/index.d.ts.map +1 -1
  43. package/types/regexs/QueryFilterRegex.d.ts +4 -0
  44. package/types/regexs/QueryFilterRegex.d.ts.map +1 -0
  45. package/types/repository/AbstractMongoRepository.d.ts +6 -3
  46. package/types/repository/AbstractMongoRepository.d.ts.map +1 -1
  47. package/types/repository/AbstractSqliteRepository.d.ts +23 -8
  48. package/types/repository/AbstractSqliteRepository.d.ts.map +1 -1
  49. package/types/schemas/DeleteBodyResponseSchema.d.ts +20 -0
  50. package/types/schemas/DeleteBodyResponseSchema.d.ts.map +1 -0
  51. package/types/schemas/ErrorBodyResponseSchema.d.ts +60 -0
  52. package/types/schemas/ErrorBodyResponseSchema.d.ts.map +1 -0
  53. package/types/schemas/ExportBodyResponseSchema.d.ts +20 -0
  54. package/types/schemas/ExportBodyResponseSchema.d.ts.map +1 -0
  55. package/types/schemas/FindBySchema.d.ts +13 -0
  56. package/types/schemas/FindBySchema.d.ts.map +1 -0
  57. package/types/schemas/FindSchema.d.ts +19 -0
  58. package/types/schemas/FindSchema.d.ts.map +1 -0
  59. package/types/schemas/IdParamSchema.d.ts +11 -0
  60. package/types/schemas/IdParamSchema.d.ts.map +1 -0
  61. package/types/schemas/PaginateBodySchema.d.ts +61 -0
  62. package/types/schemas/PaginateBodySchema.d.ts.map +1 -0
  63. package/types/schemas/PaginateSchema.d.ts +41 -0
  64. package/types/schemas/PaginateSchema.d.ts.map +1 -0
  65. package/types/schemas/SearchSchema.d.ts +10 -0
  66. package/types/schemas/SearchSchema.d.ts.map +1 -0
  67. package/types/services/AbstractService.d.ts +5 -5
  68. package/types/services/AbstractService.d.ts.map +1 -1
  69. package/types/zod/DeleteBodySchema.d.ts +11 -0
  70. package/types/zod/DeleteBodySchema.d.ts.map +1 -0
  71. package/types/zod/IdParamSchema.d.ts +11 -0
  72. package/types/zod/IdParamSchema.d.ts.map +1 -0
  73. package/types/zod/PaginateBodySchema.d.ts +61 -0
  74. package/types/zod/PaginateBodySchema.d.ts.map +1 -0
@@ -0,0 +1,349 @@
1
+ import z from 'zod';
2
+ import { zodToJsonSchema } from 'zod-to-json-schema';
3
+ import { IdParamSchema, DeleteBodyResponseSchema, PaginateQuerySchema, PaginateBodyResponseSchema, FindQuerySchema, SearchQuerySchema, FindByParamSchema, ErrorBodyResponseSchema, ValidationErrorBodyResponseSchema, ExportBodyResponseSchema } from '../index.js';
4
+ export class CrudSchemaBuilder {
5
+ constructor(entitySchema, entityCreateSchema, entityUpdateSchema, entityName, target = 'openApi3', tags = []) {
6
+ this.target = 'openApi3'; //"jsonSchema7" | "jsonSchema2019-09" | "openApi3" | "openAi"
7
+ this.entitySchema = entitySchema;
8
+ this.entityCreateSchema = entityCreateSchema;
9
+ this.entityUpdateSchema = entityUpdateSchema;
10
+ this.entityName = entityName;
11
+ this.tags = tags;
12
+ this.target = target;
13
+ }
14
+ get getTags() {
15
+ if (this.tags.length > 0) {
16
+ return { tags: this.tags };
17
+ }
18
+ return [];
19
+ }
20
+ get jsonEntityCreateSchema() {
21
+ return zodToJsonSchema(this.entityCreateSchema, { target: this.target });
22
+ }
23
+ get jsonEntityUpdateSchema() {
24
+ return zodToJsonSchema(this.entityUpdateSchema, { target: this.target });
25
+ }
26
+ get jsonEntitySchema() {
27
+ return zodToJsonSchema(this.entitySchema, { target: this.target });
28
+ }
29
+ get jsonEntityArraySchema() {
30
+ return zodToJsonSchema(z.array(this.entitySchema), { target: this.target });
31
+ }
32
+ get jsonExportBodyResponse() {
33
+ return zodToJsonSchema(ExportBodyResponseSchema, { target: this.target });
34
+ }
35
+ get jsonErrorBodyResponse() {
36
+ return zodToJsonSchema(ErrorBodyResponseSchema, { target: this.target });
37
+ }
38
+ get jsonValidationErrorBodyResponse() {
39
+ return zodToJsonSchema(ValidationErrorBodyResponseSchema, { target: this.target });
40
+ }
41
+ get jsonFindQuerySchema() {
42
+ return zodToJsonSchema(FindQuerySchema, { target: this.target });
43
+ }
44
+ get jsonSearchQuerySchema() {
45
+ return zodToJsonSchema(SearchQuerySchema, { target: this.target });
46
+ }
47
+ get jsonPaginateQuerySchema() {
48
+ return zodToJsonSchema(PaginateQuerySchema, { target: this.target });
49
+ }
50
+ get jsonDeleteBodyResponseSchema() {
51
+ return zodToJsonSchema(DeleteBodyResponseSchema, { target: this.target });
52
+ }
53
+ get jsonFindByParamSchema() {
54
+ return zodToJsonSchema(FindByParamSchema, { target: this.target });
55
+ }
56
+ get jsonPaginateBodyResponseSchema() {
57
+ return zodToJsonSchema(PaginateBodyResponseSchema.extend({
58
+ items: z.array(this.entitySchema)
59
+ }), { target: this.target });
60
+ }
61
+ get jsonIdParamSchema() {
62
+ return zodToJsonSchema(IdParamSchema, { target: this.target });
63
+ }
64
+ /**
65
+ * Get JSON schema for export
66
+ */
67
+ get exportSchema() {
68
+ return {
69
+ ...(this.getTags),
70
+ query: this.jsonFindQuerySchema,
71
+ response: {
72
+ 200: this.jsonExportBodyResponse,
73
+ 400: this.jsonErrorBodyResponse,
74
+ 401: this.jsonErrorBodyResponse,
75
+ 403: this.jsonErrorBodyResponse,
76
+ 500: this.jsonErrorBodyResponse
77
+ }
78
+ };
79
+ }
80
+ /**
81
+ * Get JSON schema for finding an entity by ID
82
+ */
83
+ get findByIdSchema() {
84
+ return {
85
+ ...(this.getTags),
86
+ params: this.jsonIdParamSchema,
87
+ response: {
88
+ 200: this.jsonEntitySchema,
89
+ 400: this.jsonErrorBodyResponse,
90
+ 401: this.jsonErrorBodyResponse,
91
+ 403: this.jsonErrorBodyResponse,
92
+ 500: this.jsonErrorBodyResponse
93
+ }
94
+ };
95
+ }
96
+ /**
97
+ * Get JSON schema for finding an entity by IDs
98
+ */
99
+ get findByIdsSchema() {
100
+ return {
101
+ ...(this.getTags),
102
+ params: zodToJsonSchema(z.object({
103
+ ids: z.string().regex(/^[^,]+(,[^,]+)*$/, "Debe ser una lista de valores separados por coma sin comas consecutivas")
104
+ }), { target: this.target }),
105
+ response: {
106
+ 200: this.jsonEntityArraySchema,
107
+ 400: this.jsonErrorBodyResponse,
108
+ 401: this.jsonErrorBodyResponse,
109
+ 403: this.jsonErrorBodyResponse,
110
+ 500: this.jsonErrorBodyResponse
111
+ }
112
+ };
113
+ }
114
+ /**
115
+ * Get JSON schema for search an entity
116
+ */
117
+ get searchSchema() {
118
+ return {
119
+ ...(this.getTags),
120
+ query: this.jsonSearchQuerySchema,
121
+ response: {
122
+ 200: this.jsonEntityArraySchema,
123
+ 400: this.jsonErrorBodyResponse,
124
+ 401: this.jsonErrorBodyResponse,
125
+ 403: this.jsonErrorBodyResponse,
126
+ 500: this.jsonErrorBodyResponse
127
+ }
128
+ };
129
+ }
130
+ /**
131
+ * Get JSON schema for find entities
132
+ */
133
+ get findSchema() {
134
+ return {
135
+ ...(this.getTags),
136
+ query: this.jsonFindQuerySchema,
137
+ response: {
138
+ 200: this.jsonEntityArraySchema,
139
+ 400: this.jsonErrorBodyResponse,
140
+ 401: this.jsonErrorBodyResponse,
141
+ 403: this.jsonErrorBodyResponse,
142
+ 500: this.jsonErrorBodyResponse
143
+ }
144
+ };
145
+ }
146
+ /**
147
+ * Get JSON schema for find one entity
148
+ */
149
+ get findOneSchema() {
150
+ return {
151
+ ...(this.getTags),
152
+ params: this.jsonFindQuerySchema,
153
+ response: {
154
+ 200: this.jsonEntitySchema,
155
+ 400: this.jsonErrorBodyResponse,
156
+ 401: this.jsonErrorBodyResponse,
157
+ 403: this.jsonErrorBodyResponse,
158
+ 500: this.jsonErrorBodyResponse
159
+ }
160
+ };
161
+ }
162
+ /**
163
+ * Get JSON schema for findBy entities
164
+ */
165
+ get findBySchema() {
166
+ return {
167
+ ...(this.getTags),
168
+ params: this.jsonFindByParamSchema,
169
+ response: {
170
+ 200: this.jsonEntityArraySchema,
171
+ 401: this.jsonErrorBodyResponse,
172
+ 403: this.jsonErrorBodyResponse,
173
+ 500: this.jsonErrorBodyResponse
174
+ }
175
+ };
176
+ }
177
+ /**
178
+ * Get JSON schema for findBy entities
179
+ */
180
+ get findOneBySchema() {
181
+ return {
182
+ ...(this.getTags),
183
+ params: this.jsonFindByParamSchema,
184
+ response: {
185
+ 200: this.jsonEntitySchema,
186
+ 401: this.jsonErrorBodyResponse,
187
+ 403: this.jsonErrorBodyResponse,
188
+ 500: this.jsonErrorBodyResponse
189
+ }
190
+ };
191
+ }
192
+ /**
193
+ * Get JSON schema for paginating entities
194
+ */
195
+ get paginateSchema() {
196
+ return {
197
+ ...(this.getTags),
198
+ query: this.jsonPaginateQuerySchema,
199
+ response: {
200
+ 200: this.jsonPaginateBodyResponseSchema,
201
+ 400: this.jsonErrorBodyResponse,
202
+ 401: this.jsonErrorBodyResponse,
203
+ 403: this.jsonErrorBodyResponse,
204
+ 500: this.jsonErrorBodyResponse
205
+ }
206
+ };
207
+ }
208
+ /**
209
+ * Get JSON schema for all entities
210
+ */
211
+ get allSchema() {
212
+ return {
213
+ ...(this.getTags),
214
+ response: {
215
+ 200: this.jsonEntityArraySchema,
216
+ 401: this.jsonErrorBodyResponse,
217
+ 403: this.jsonErrorBodyResponse,
218
+ 500: this.jsonErrorBodyResponse
219
+ }
220
+ };
221
+ }
222
+ /**
223
+ * Get JSON schema for creating an entity
224
+ */
225
+ get createSchema() {
226
+ return {
227
+ ...(this.getTags),
228
+ body: this.jsonEntityCreateSchema,
229
+ response: {
230
+ 200: this.jsonEntitySchema,
231
+ 401: this.jsonErrorBodyResponse,
232
+ 403: this.jsonErrorBodyResponse,
233
+ 422: this.jsonValidationErrorBodyResponse,
234
+ 500: this.jsonErrorBodyResponse
235
+ }
236
+ };
237
+ }
238
+ /**
239
+ * Get JSON schema for updating an entity
240
+ */
241
+ get updateSchema() {
242
+ return {
243
+ ...(this.getTags),
244
+ params: this.jsonIdParamSchema,
245
+ body: this.jsonEntityUpdateSchema,
246
+ response: {
247
+ 200: this.jsonEntitySchema,
248
+ 401: this.jsonErrorBodyResponse,
249
+ 403: this.jsonErrorBodyResponse,
250
+ 422: this.jsonValidationErrorBodyResponse,
251
+ 500: this.jsonErrorBodyResponse
252
+ }
253
+ };
254
+ }
255
+ /**
256
+ * Get JSON schema for updating an entity
257
+ */
258
+ get updatePartialSchema() {
259
+ return {
260
+ ...(this.getTags),
261
+ params: this.jsonIdParamSchema,
262
+ body: zodToJsonSchema(this.entityUpdateSchema.partial(), { target: this.target }),
263
+ response: {
264
+ 200: this.jsonEntitySchema,
265
+ 401: this.jsonErrorBodyResponse,
266
+ 403: this.jsonErrorBodyResponse,
267
+ 422: this.jsonValidationErrorBodyResponse,
268
+ 500: this.jsonErrorBodyResponse
269
+ }
270
+ };
271
+ }
272
+ /**
273
+ * Get JSON schema for deleting an entity
274
+ */
275
+ get deleteSchema() {
276
+ return {
277
+ ...(this.getTags),
278
+ params: this.jsonIdParamSchema,
279
+ response: {
280
+ 200: this.jsonDeleteBodyResponseSchema,
281
+ 400: this.jsonErrorBodyResponse,
282
+ 401: this.jsonErrorBodyResponse,
283
+ 403: this.jsonErrorBodyResponse,
284
+ 500: this.jsonErrorBodyResponse
285
+ }
286
+ };
287
+ }
288
+ /**
289
+ * Generate route builder function for Fastify with configurable endpoints
290
+ * @param options Configuration options for route generation
291
+ * @param basePath Base API path for the entity
292
+ */
293
+ generateRoutes(options = {
294
+ paginate: true,
295
+ all: true,
296
+ export: true,
297
+ search: true,
298
+ findById: true,
299
+ create: true,
300
+ update: true,
301
+ delete: true
302
+ }, basePath = `/api/${this.entityName.toLowerCase()}s`) {
303
+ return (fastify, controller) => {
304
+ // Get all entities with pagination
305
+ if (options.paginate !== false) {
306
+ fastify.get(basePath, {
307
+ schema: this.paginateSchema
308
+ }, (req, rep) => controller.paginate(req, rep));
309
+ }
310
+ // Get all entities without pagination
311
+ if (options.all !== false) {
312
+ fastify.get(`${basePath}/all`, (req, rep) => controller.all(req, rep));
313
+ }
314
+ // Export entities
315
+ if (options.export !== false) {
316
+ fastify.get(`${basePath}/export`, (req, rep) => controller.export(req, rep));
317
+ }
318
+ // Search entities
319
+ if (options.search !== false) {
320
+ fastify.get(`${basePath}/search`, (req, rep) => controller.search(req, rep));
321
+ }
322
+ // Get entity by ID
323
+ if (options.findById !== false) {
324
+ fastify.get(`${basePath}/:id`, {
325
+ schema: this.findByIdSchema
326
+ }, (req, rep) => controller.findById(req, rep));
327
+ }
328
+ // Create entity
329
+ if (options.create !== false) {
330
+ fastify.post(basePath, {
331
+ schema: this.createSchema
332
+ }, (req, rep) => controller.create(req, rep));
333
+ }
334
+ // Update entity
335
+ if (options.update !== false) {
336
+ fastify.put(`${basePath}/:id`, {
337
+ schema: this.updateSchema
338
+ }, (req, rep) => controller.update(req, rep));
339
+ }
340
+ // Delete entity
341
+ if (options.delete !== false) {
342
+ fastify.delete(`${basePath}/:id`, {
343
+ schema: this.deleteSchema
344
+ }, (req, rep) => controller.delete(req, rep));
345
+ }
346
+ };
347
+ }
348
+ }
349
+ export default CrudSchemaBuilder;