@drax/crud-back 2.11.0 → 3.0.0

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 (65) hide show
  1. package/dist/controllers/AbstractFastifyController.js +47 -24
  2. package/dist/repository/AbstractMongoRepository.js +12 -8
  3. package/dist/repository/AbstractSqliteRepository.js +40 -9
  4. package/dist/services/AbstractService.js +9 -6
  5. package/package.json +8 -7
  6. package/src/controllers/AbstractFastifyController.ts +56 -28
  7. package/src/repository/AbstractMongoRepository.ts +17 -8
  8. package/src/repository/AbstractSqliteRepository.ts +61 -10
  9. package/src/services/AbstractService.ts +12 -6
  10. package/test/_mocks/MockRepository.ts +1 -1
  11. package/test/controllers/PersonController.test.ts +547 -0
  12. package/test/people/controllers/CountryController.ts +40 -0
  13. package/test/people/controllers/LanguageController.ts +29 -0
  14. package/test/people/controllers/PersonController.ts +29 -0
  15. package/test/people/factory/services/CountryServiceFactory.ts +41 -0
  16. package/test/people/factory/services/LanguageServiceFactory.ts +41 -0
  17. package/test/people/factory/services/PersonServiceFactory.ts +41 -0
  18. package/test/people/interfaces/ICountry.ts +28 -0
  19. package/test/people/interfaces/ICountryRepository.ts +11 -0
  20. package/test/people/interfaces/ILanguage.ts +32 -0
  21. package/test/people/interfaces/ILanguageRepository.ts +11 -0
  22. package/test/people/interfaces/IPerson.ts +58 -0
  23. package/test/people/interfaces/IPersonRepository.ts +11 -0
  24. package/test/people/models/CountryModel.ts +38 -0
  25. package/test/people/models/LanguageModel.ts +40 -0
  26. package/test/people/models/PersonModel.ts +61 -0
  27. package/test/people/permissions/CountryPermissions.ts +14 -0
  28. package/test/people/permissions/LanguagePermissions.ts +14 -0
  29. package/test/people/permissions/PersonPermissions.ts +18 -0
  30. package/test/people/repository/mongo/CountryMongoRepository.ts +22 -0
  31. package/test/people/repository/mongo/LanguageMongoRepository.ts +22 -0
  32. package/test/people/repository/mongo/PersonMongoRepository.ts +22 -0
  33. package/test/people/repository/sqlite/CountrySqliteRepository.ts +33 -0
  34. package/test/people/repository/sqlite/LanguageSqliteRepository.ts +37 -0
  35. package/test/people/repository/sqlite/PersonSqliteRepository.ts +42 -0
  36. package/test/people/routes/CountryRoutes.ts +34 -0
  37. package/test/people/routes/LanguageRoutes.ts +34 -0
  38. package/test/people/routes/PersonRoutes.ts +40 -0
  39. package/test/people/schemas/CountrySchema.ts +22 -0
  40. package/test/people/schemas/LanguageSchema.ts +22 -0
  41. package/test/people/schemas/PersonSchema.ts +42 -0
  42. package/test/people/services/CountryService.ts +20 -0
  43. package/test/people/services/LanguageService.ts +20 -0
  44. package/test/people/services/PersonService.ts +20 -0
  45. package/test/services/AbstractService.test.ts +11 -7
  46. package/test/setup/MongoInMemory.ts +56 -0
  47. package/test/setup/TestSetup.ts +344 -0
  48. package/test/setup/data/admin-role.ts +13 -0
  49. package/test/setup/data/basic-user.ts +14 -0
  50. package/test/setup/data/one-tenant.ts +6 -0
  51. package/test/setup/data/restricted-role.ts +16 -0
  52. package/test/setup/data/root-user.ts +14 -0
  53. package/test/setup/data/tenant-one-user.ts +13 -0
  54. package/test/setup/data/tenant-two-user.ts +14 -0
  55. package/test/setup/data/two-tenant.ts +6 -0
  56. package/test/workers/ExportCsvWorker.test.ts +1 -1
  57. package/tsconfig.json +2 -1
  58. package/tsconfig.tsbuildinfo +1 -1
  59. package/types/controllers/AbstractFastifyController.d.ts.map +1 -1
  60. package/types/repository/AbstractMongoRepository.d.ts +3 -3
  61. package/types/repository/AbstractMongoRepository.d.ts.map +1 -1
  62. package/types/repository/AbstractSqliteRepository.d.ts +5 -4
  63. package/types/repository/AbstractSqliteRepository.d.ts.map +1 -1
  64. package/types/services/AbstractService.d.ts +4 -2
  65. package/types/services/AbstractService.d.ts.map +1 -1
@@ -0,0 +1,42 @@
1
+
2
+ import {AbstractSqliteRepository} from "@drax/crud-back";
3
+ import type {IPersonRepository} from '../../interfaces/IPersonRepository'
4
+ import type {IPerson, IPersonBase} from "../../interfaces/IPerson";
5
+ import {SqliteTableField} from "@drax/common-back";
6
+
7
+ class PersonSqliteRepository extends AbstractSqliteRepository<IPerson, IPersonBase, IPersonBase> implements IPersonRepository {
8
+
9
+ protected db: any;
10
+ protected tableName: string = 'Person';
11
+ protected dataBaseFile: string;
12
+ protected searchFields: string[] = ['fullname', 'hobbies', 'race', 'interests'];
13
+ protected booleanFields: string[] = ['live'];
14
+ protected identifier: string = '_id';
15
+ protected populateFields = [
16
+ { field: 'nationality', table: 'nationality', identifier: '_id' },
17
+ { field: 'languages', table: 'languages', identifier: '_id' },
18
+ { field: 'tenant', table: 'tenant', identifier: '_id' },
19
+ { field: 'user', table: 'user', identifier: '_id' }
20
+ ]
21
+ protected verbose: boolean = false;
22
+ protected tableFields: SqliteTableField[] = [
23
+ {name: "fullname", type: "TEXT", unique: true, primary: false},
24
+ {name: "live", type: "TEXT", unique: false, primary: false},
25
+ {name: "birthdate", type: "TEXT", unique: false, primary: false},
26
+ {name: "secret", type: "TEXT", unique: false, primary: false},
27
+ {name: "nationality", type: "TEXT", unique: false, primary: false},
28
+ {name: "hobbies", type: "TEXT", unique: false, primary: false},
29
+ {name: "race", type: "TEXT", unique: false, primary: false},
30
+ {name: "interests", type: "TEXT", unique: false, primary: false},
31
+ {name: "languages", type: "TEXT", unique: false, primary: false},
32
+ {name: "address", type: "TEXT", unique: undefined, primary: false},
33
+ {name: "skills", type: "TEXT", unique: undefined, primary: false},
34
+ {name: "tenant", type: "TEXT", unique: false, primary: false},
35
+ {name: "user", type: "TEXT", unique: false, primary: false}
36
+ ]
37
+
38
+ }
39
+
40
+ export default PersonSqliteRepository
41
+ export {PersonSqliteRepository}
42
+
@@ -0,0 +1,34 @@
1
+
2
+ import CountryController from "../controllers/CountryController.js";
3
+ import {CrudSchemaBuilder} from "@drax/crud-back";
4
+ import {CountrySchema, CountryBaseSchema} from '../schemas/CountrySchema.js'
5
+
6
+ async function CountryFastifyRoutes(fastify, options) {
7
+
8
+ const controller: CountryController = new CountryController()
9
+ const schemas = new CrudSchemaBuilder(CountrySchema, CountryBaseSchema,CountryBaseSchema, 'Country', 'openapi-3.0', ['ABM']);
10
+
11
+ fastify.get('/api/countries', {schema: schemas.paginateSchema}, (req,rep) => controller.paginate(req,rep))
12
+
13
+ fastify.get('/api/countries/find', {schema: schemas.findSchema}, (req,rep) => controller.find(req,rep))
14
+
15
+ fastify.get('/api/countries/search', {schema: schemas.searchSchema}, (req,rep) => controller.search(req,rep))
16
+
17
+ fastify.get('/api/countries/:id', {schema: schemas.findByIdSchema}, (req,rep) => controller.findById(req,rep))
18
+
19
+ fastify.get('/api/countries/find-one', {schema: schemas.findOneSchema}, (req,rep) => controller.findOne(req,rep))
20
+
21
+ fastify.get('/api/countries/group-by', {schema: schemas.groupBySchema}, (req,rep) => controller.groupBy(req,rep))
22
+
23
+ fastify.post('/api/countries', {schema: schemas.createSchema}, (req,rep) =>controller.create(req,rep))
24
+
25
+ fastify.put('/api/countries/:id', {schema: schemas.updateSchema}, (req,rep) =>controller.update(req,rep))
26
+
27
+ fastify.patch('/api/countries/:id', {schema: schemas.updateSchema}, (req,rep) =>controller.updatePartial(req,rep))
28
+
29
+ fastify.delete('/api/countries/:id', {schema: schemas.deleteSchema}, (req,rep) =>controller.delete(req,rep))
30
+
31
+ }
32
+
33
+ export default CountryFastifyRoutes;
34
+ export {CountryFastifyRoutes}
@@ -0,0 +1,34 @@
1
+
2
+ import LanguageController from "../controllers/LanguageController.js";
3
+ import {CrudSchemaBuilder} from "@drax/crud-back";
4
+ import {LanguageSchema, LanguageBaseSchema} from '../schemas/LanguageSchema.js'
5
+
6
+ async function LanguageFastifyRoutes(fastify, options) {
7
+
8
+ const controller: LanguageController = new LanguageController()
9
+ const schemas = new CrudSchemaBuilder(LanguageSchema, LanguageBaseSchema,LanguageBaseSchema, 'Language', 'openapi-3.0', ['people']);
10
+
11
+ fastify.get('/api/language', {schema: schemas.paginateSchema}, (req,rep) => controller.paginate(req,rep))
12
+
13
+ fastify.get('/api/language/find', {schema: schemas.findSchema}, (req,rep) => controller.find(req,rep))
14
+
15
+ fastify.get('/api/language/search', {schema: schemas.searchSchema}, (req,rep) => controller.search(req,rep))
16
+
17
+ fastify.get('/api/language/:id', {schema: schemas.findByIdSchema}, (req,rep) => controller.findById(req,rep))
18
+
19
+ fastify.get('/api/language/find-one', {schema: schemas.findOneSchema}, (req,rep) => controller.findOne(req,rep))
20
+
21
+ fastify.get('/api/language/group-by', {schema: schemas.groupBySchema}, (req,rep) => controller.groupBy(req,rep))
22
+
23
+ fastify.post('/api/language', {schema: schemas.createSchema}, (req,rep) =>controller.create(req,rep))
24
+
25
+ fastify.put('/api/language/:id', {schema: schemas.updateSchema}, (req,rep) =>controller.update(req,rep))
26
+
27
+ fastify.patch('/api/language/:id', {schema: schemas.updateSchema}, (req,rep) =>controller.updatePartial(req,rep))
28
+
29
+ fastify.delete('/api/language/:id', {schema: schemas.deleteSchema}, (req,rep) =>controller.delete(req,rep))
30
+
31
+ }
32
+
33
+ export default LanguageFastifyRoutes;
34
+ export {LanguageFastifyRoutes}
@@ -0,0 +1,40 @@
1
+
2
+ import PersonController from "../controllers/PersonController.js";
3
+ import {CrudSchemaBuilder} from "@drax/crud-back";
4
+ import {PersonSchema, PersonBaseSchema} from '../schemas/PersonSchema.js'
5
+
6
+ async function PersonFastifyRoutes(fastify, options) {
7
+
8
+ const controller: PersonController = new PersonController()
9
+ const schemas = new CrudSchemaBuilder(PersonSchema, PersonBaseSchema,PersonBaseSchema, 'Person', 'openapi-3.0', ['people']);
10
+
11
+ fastify.get('/api/person', {schema: schemas.paginateSchema}, (req,rep) => controller.paginate(req,rep))
12
+
13
+ fastify.get('/api/person/find', {schema: schemas.findSchema}, (req,rep) => controller.find(req,rep))
14
+
15
+ fastify.get('/api/person/search', {schema: schemas.searchSchema}, (req,rep) => controller.search(req,rep))
16
+
17
+ fastify.get('/api/person/:id', {schema: schemas.findByIdSchema}, (req,rep) => controller.findById(req,rep))
18
+
19
+ fastify.get('/api/person/find-one', {schema: schemas.findOneSchema}, (req,rep) => controller.findOne(req,rep))
20
+
21
+ fastify.get('/api/person/find-by/:field/:value', {schema: schemas.findBySchema}, (req,rep) => controller.findBy(req,rep))
22
+
23
+ fastify.get('/api/person/find-one-by/:field/:value', {schema: schemas.findOneBySchema}, (req,rep) => controller.findOneBy(req,rep))
24
+
25
+ fastify.get('/api/person/group-by', {schema: schemas.groupBySchema}, (req,rep) => controller.groupBy(req,rep))
26
+
27
+ fastify.post('/api/person', {schema: schemas.createSchema}, (req,rep) =>controller.create(req,rep))
28
+
29
+ fastify.put('/api/person/:id', {schema: schemas.updateSchema}, (req,rep) =>controller.update(req,rep))
30
+
31
+ fastify.patch('/api/person/:id', {schema: schemas.updateSchema}, (req,rep) =>controller.updatePartial(req,rep))
32
+
33
+ fastify.delete('/api/person/:id', {schema: schemas.deleteSchema}, (req,rep) =>controller.delete(req,rep))
34
+
35
+ fastify.get('/api/person/export', (req,rep) =>controller.export(req,rep))
36
+
37
+ }
38
+
39
+ export default PersonFastifyRoutes;
40
+ export {PersonFastifyRoutes}
@@ -0,0 +1,22 @@
1
+
2
+ import { z } from 'zod';
3
+
4
+
5
+ const CountryBaseSchema = z.object({
6
+ name: z.string().min(1,'validation.required'),
7
+ description: z.string().optional().default('Some Description'),
8
+ flag: z.string().optional(),
9
+ metadata: z.record(z.string(),z.unknown()).optional().nullable(),
10
+ tenant: z.coerce.string().optional().nullable(),
11
+ createdBy: z.coerce.string().optional()
12
+ });
13
+
14
+ const CountrySchema = CountryBaseSchema
15
+ .extend({
16
+ _id: z.coerce.string(),
17
+ tenant: z.object({_id: z.coerce.string(), name: z.string()}).nullable().optional(),
18
+ createdBy: z.object({_id: z.coerce.string(), username: z.string()})
19
+ })
20
+
21
+ export default CountrySchema;
22
+ export {CountrySchema, CountryBaseSchema}
@@ -0,0 +1,22 @@
1
+ import {z} from 'zod';
2
+
3
+
4
+ const LanguageBaseSchema = z.object({
5
+ name: z.string().min(1, 'validation.required'),
6
+ icon: z.object({
7
+ filename: z.string().min(1, 'validation.required'),
8
+ filepath: z.string().min(1, 'validation.required'),
9
+ size: z.number().min(1, 'validation.required'),
10
+ mimetype: z.string().optional(),
11
+ url: z.string().min(1, 'validation.required')
12
+ }).optional()
13
+ });
14
+
15
+ const LanguageSchema = LanguageBaseSchema
16
+ .extend({
17
+ _id: z.coerce.string(),
18
+
19
+ })
20
+
21
+ export default LanguageSchema;
22
+ export {LanguageSchema, LanguageBaseSchema}
@@ -0,0 +1,42 @@
1
+ import {z} from 'zod';
2
+
3
+
4
+ const PersonBaseSchema = z.object({
5
+ fullname: z.string().min(1, 'validation.required'),
6
+ live: z.boolean().optional(),
7
+ birthdate: z.coerce.date().nullable().optional(),
8
+ secret: z.string().optional(),
9
+ money: z.number().nullable().optional(),
10
+ nationality: z.coerce.string().optional().nullable(),
11
+ hobbies: z.array(z.string()).optional(),
12
+ race: z.enum(['human', 'elf', 'orc']).optional(),
13
+ interests: z.array(z.enum(['sports', 'music', 'reading', 'travel', 'cooking', 'technology'])).optional().default(["sports", "music"]),
14
+ languages: z.array(z.coerce.string()).optional(),
15
+ address: z.object({
16
+ country: z.string().optional(),
17
+ city: z.string().optional(),
18
+ street: z.string().min(1, 'validation.required'),
19
+ zip: z.number().nullable().optional(),
20
+ casa: z.boolean().optional()
21
+ }),
22
+ skills: z.array(
23
+ z.object({
24
+ name: z.string().min(1, 'validation.required'),
25
+ level: z.number().nullable().optional()
26
+ })
27
+ ).optional(),
28
+ tenant: z.coerce.string().optional().nullable(),
29
+ user: z.coerce.string().optional().nullable()
30
+ });
31
+
32
+ const PersonSchema = PersonBaseSchema
33
+ .extend({
34
+ _id: z.coerce.string(),
35
+ nationality: z.object({_id: z.coerce.string(), name: z.string()}).nullable().optional(),
36
+ languages: z.array(z.object({_id: z.coerce.string(), name: z.string()})).optional(),
37
+ tenant: z.object({_id: z.coerce.string(), name: z.string()}).nullable().optional(),
38
+ user: z.object({_id: z.coerce.string(), username: z.string()}).nullable().optional()
39
+ })
40
+
41
+ export default PersonSchema;
42
+ export {PersonSchema, PersonBaseSchema}
@@ -0,0 +1,20 @@
1
+
2
+ import type{ICountryRepository} from "../interfaces/ICountryRepository";
3
+ import type {ICountryBase, ICountry} from "../interfaces/ICountry";
4
+ import {AbstractService} from "@drax/crud-back";
5
+ import type {ZodObject, ZodRawShape} from "zod";
6
+
7
+ class CountryService extends AbstractService<ICountry, ICountryBase, ICountryBase> {
8
+
9
+
10
+ constructor(CountryRepository: ICountryRepository, baseSchema?: ZodObject<ZodRawShape>, fullSchema?: ZodObject<ZodRawShape>) {
11
+ super(CountryRepository, baseSchema, fullSchema);
12
+
13
+ this._validateOutput = true
14
+
15
+ }
16
+
17
+ }
18
+
19
+ export default CountryService
20
+ export {CountryService}
@@ -0,0 +1,20 @@
1
+
2
+ import type{ILanguageRepository} from "../interfaces/ILanguageRepository";
3
+ import type {ILanguageBase, ILanguage} from "../interfaces/ILanguage";
4
+ import {AbstractService} from "@drax/crud-back";
5
+ import type {ZodObject, ZodRawShape} from "zod";
6
+
7
+ class LanguageService extends AbstractService<ILanguage, ILanguageBase, ILanguageBase> {
8
+
9
+
10
+ constructor(LanguageRepository: ILanguageRepository, baseSchema?: ZodObject<ZodRawShape>, fullSchema?: ZodObject<ZodRawShape>) {
11
+ super(LanguageRepository, baseSchema, fullSchema);
12
+
13
+ this._validateOutput = true
14
+
15
+ }
16
+
17
+ }
18
+
19
+ export default LanguageService
20
+ export {LanguageService}
@@ -0,0 +1,20 @@
1
+
2
+ import type{IPersonRepository} from "../interfaces/IPersonRepository";
3
+ import type {IPersonBase, IPerson} from "../interfaces/IPerson";
4
+ import {AbstractService} from "@drax/crud-back";
5
+ import type {ZodObject, ZodRawShape} from "zod";
6
+
7
+ class PersonService extends AbstractService<IPerson, IPersonBase, IPersonBase> {
8
+
9
+
10
+ constructor(PersonRepository: IPersonRepository, baseSchema?: ZodObject<ZodRawShape>, fullSchema?: ZodObject<ZodRawShape>) {
11
+ super(PersonRepository, baseSchema, fullSchema);
12
+
13
+ this._validateOutput = true
14
+
15
+ }
16
+
17
+ }
18
+
19
+ export default PersonService
20
+ export {PersonService}
@@ -1,19 +1,23 @@
1
- import { test } from 'node:test';
2
- import assert from 'node:assert';
1
+ import { test, assert } from 'vitest';
3
2
  import MockRepository from "../_mocks/MockRepository.js";
4
3
  import {AbstractService} from "../../src/services/AbstractService.js";
5
4
  import {fileURLToPath} from "url";
6
- import path from "path";
5
+ import * as path from "path";
7
6
 
8
7
  //@ts-ignore
9
8
  const __filename = fileURLToPath(import.meta.url);
10
9
  const __dirname = path.dirname(__filename);
11
10
 
12
11
  const mockRepository = new MockRepository();
13
- const abstractService = new AbstractService(mockRepository);
12
+
13
+ class MockService extends AbstractService<any,any,any>{
14
+
15
+ }
16
+
17
+ const service = new MockService(mockRepository);
14
18
 
15
19
  test('create', async () => {
16
- const item: any = await abstractService.create({name: 'John Doe'})
20
+ const item: any = await service.create({name: 'John Doe'})
17
21
  assert.deepStrictEqual(item.name, 'John Doe');
18
22
  })
19
23
 
@@ -22,7 +26,7 @@ test('export', async () => {
22
26
  const file = 'test.csv'
23
27
  const outputPath = path.resolve(__dirname, file);
24
28
 
25
- const result:any = await abstractService.export(
29
+ const result:any = await service.export(
26
30
  {
27
31
  format: 'CSV',
28
32
  headers: ['name'],
@@ -30,7 +34,7 @@ test('export', async () => {
30
34
  limit: 0,
31
35
  search: '',
32
36
  filters: [],
33
- }
37
+ },"/tmp"
34
38
  )
35
39
 
36
40
  console.log("result",result)
@@ -0,0 +1,56 @@
1
+ import {mongoose} from '@drax/common-back';
2
+ import {MongoMemoryServer} from 'mongodb-memory-server';
3
+
4
+ class MongoInMemory {
5
+
6
+ mongoServer: MongoMemoryServer
7
+
8
+ async connect() {
9
+ this.mongoServer = await MongoMemoryServer.create();
10
+ if (this.mongoServer.state == "new") {
11
+ await this.mongoServer.start()
12
+ }
13
+ if (!mongoose.connection.readyState) {
14
+ await mongoose.connect(this.mongoServer.getUri(), {dbName: "verifyMASTER"});
15
+ }
16
+ return
17
+ }
18
+
19
+ get mongooseStatus() {
20
+ return mongoose.connection.readyState
21
+ }
22
+
23
+ get serverStatus() {
24
+ return this.mongoServer.state
25
+ }
26
+
27
+ get status() {
28
+ return mongoose.connection.readyState
29
+ }
30
+
31
+ async disconnect() {
32
+ await mongoose.disconnect();
33
+ }
34
+
35
+ async dropCollections() {
36
+ const collections = await mongoose.connection.listCollections()
37
+ for (let collection of collections) {
38
+ console.log(`Dropping collection: ${collection.name}`)
39
+ await mongoose.connection.dropCollection(collection.name)
40
+ }
41
+ }
42
+
43
+ async dropCollection(name: string) {
44
+ await mongoose.connection.dropCollection(name)
45
+ }
46
+
47
+ async dropAndClose() {
48
+ if (this.mongoServer) {
49
+ await mongoose.connection.dropDatabase();
50
+ await mongoose.connection.close();
51
+ await this.mongoServer.stop();
52
+ }
53
+ }
54
+ }
55
+
56
+ export default MongoInMemory