@dauflo/nest-fixtures 1.0.3 → 1.2.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.
package/README.md CHANGED
@@ -165,6 +165,29 @@ To create dependencies, just add the `getDependencies` method and give the fixtu
165
165
 
166
166
  The `ReferenceRepository` is a `singleton` that will keep track of the data passed from fixtures to fixtures.
167
167
 
168
+ ## Discriminator
169
+ You can use the mongoose discriminator feature in your fixtures. You just need to add put your discriminators class in a folder named for example `discri` and add the following definition when importing the fixtures modules:
170
+
171
+ ```ts
172
+ // src/app.module.ts
173
+
174
+ import { FixturesCommand, FixturesModule } from '@dauflo/nest-fixtures'
175
+
176
+ @Module({
177
+ imports: [
178
+ FixturesModule.forRootAsync(
179
+ 'src/datafixtures/*.fixtures.ts',
180
+ 'src/models/**/*.schema.ts',
181
+ 'discri'
182
+ )
183
+ ],
184
+ providers: [
185
+ FixturesCommand
186
+ ]
187
+ })
188
+ export class AppModule {}
189
+ ```
190
+
168
191
  ## License
169
192
 
170
193
  This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
@@ -1,4 +1,5 @@
1
1
  import { DynamicModule } from '@nestjs/common';
2
2
  export declare class FixturesModule {
3
- static forRootAsync(fixturesPathPattern: string, entitiesPathPattern: string): Promise<DynamicModule>;
3
+ static forRootAsync(fixturesPathPattern: string, entitiesPathPattern: string, discriminatorDir?: string): Promise<DynamicModule>;
4
+ static forRootAsyncMonorepo(fixturesPathPattern: string, entitiesPathPattern: string, discriminatorDir?: string, buildDir?: string): Promise<DynamicModule>;
4
5
  }
@@ -14,7 +14,7 @@ const mongoose_1 = require("@nestjs/mongoose");
14
14
  const glob_1 = require("glob");
15
15
  const fixtures_1 = require("./fixtures");
16
16
  class FixturesModule {
17
- static forRootAsync(fixturesPathPattern, entitiesPathPattern) {
17
+ static forRootAsync(fixturesPathPattern, entitiesPathPattern, discriminatorDir = 'discriminatorDir') {
18
18
  return __awaiter(this, void 0, void 0, function* () {
19
19
  const fixturesPath = glob_1.glob.sync(fixturesPathPattern);
20
20
  const fixturesRelativePath = fixturesPath.map((path) => path.replace('src/', `${process.cwd()}/dist/`)).map((path) => path.replace('.ts', ''));
@@ -25,14 +25,73 @@ class FixturesModule {
25
25
  });
26
26
  const entitiesPath = glob_1.glob.sync(entitiesPathPattern);
27
27
  const entitiesRelativePath = entitiesPath.map((path) => path.replace('src/', `${process.cwd()}/dist/`)).map((path) => path.replace('.ts', ''));
28
+ const nonDiscriminatorsRelativePath = entitiesRelativePath.filter((e) => !e.includes(discriminatorDir));
29
+ const discriminatorsRelativePath = entitiesRelativePath.filter((e) => e.includes(discriminatorDir));
28
30
  const entitiesProviders = [];
29
- const importedEntities = yield Promise.all(entitiesRelativePath.map((path) => Promise.resolve().then(() => require(path))));
30
- importedEntities.forEach((entity) => {
31
- entitiesProviders.push({
32
- name: entity[Object.keys(entity)[0]].name,
33
- schema: entity[Object.keys(entity)[1]],
34
- });
31
+ for (const entity of nonDiscriminatorsRelativePath) {
32
+ const importedEntity = yield Promise.resolve().then(() => require(entity));
33
+ const provider = {
34
+ name: importedEntity[Object.keys(importedEntity)[0]].name,
35
+ schema: importedEntity[Object.keys(importedEntity)[1]],
36
+ };
37
+ const discriminators = discriminatorsRelativePath.filter((e) => e.includes(entity.replace(/(.+)(\/.+)$/gm, '$1')));
38
+ if (discriminators.length !== 0) {
39
+ const importedDiscriminators = yield Promise.all(discriminators.map((path) => Promise.resolve().then(() => require(path))));
40
+ provider['discriminators'] = importedDiscriminators.map((e) => {
41
+ return { name: e[Object.keys(e)[0]].name, schema: e[Object.keys(e)[1]] };
42
+ });
43
+ }
44
+ entitiesProviders.push(provider);
45
+ }
46
+ return {
47
+ module: FixturesModule,
48
+ imports: [mongoose_1.MongooseModule.forFeature(entitiesProviders)],
49
+ providers: [
50
+ fixtures_1.ReferenceRepository,
51
+ ...fixturesProviders,
52
+ {
53
+ provide: 'FIXTURES',
54
+ useFactory(...args) {
55
+ const providersObject = {};
56
+ args.forEach((arg) => (providersObject[arg.constructor.name] = arg));
57
+ return providersObject;
58
+ },
59
+ inject: fixturesProviders,
60
+ },
61
+ ],
62
+ exports: ['FIXTURES'],
63
+ };
64
+ });
65
+ }
66
+ static forRootAsyncMonorepo(fixturesPathPattern, entitiesPathPattern, discriminatorDir = 'discriminators', buildDir = 'dist') {
67
+ return __awaiter(this, void 0, void 0, function* () {
68
+ const fixturesPath = glob_1.glob.sync(fixturesPathPattern);
69
+ const fixturesRelativePath = fixturesPath.map((path) => `${process.cwd()}/${buildDir}/${path}`).map((path) => path.replace('.ts', ''));
70
+ const fixturesProviders = [];
71
+ const importedFixtures = yield Promise.all(fixturesRelativePath.map((path) => Promise.resolve().then(() => require(path))));
72
+ importedFixtures.forEach((fixture) => {
73
+ fixturesProviders.push(fixture[Object.keys(fixture)[0]]);
35
74
  });
75
+ const entitiesPath = glob_1.glob.sync(entitiesPathPattern);
76
+ const entitiesRelativePath = entitiesPath.map((path) => `${process.cwd()}/${buildDir}/${path}`).map((path) => path.replace('.ts', ''));
77
+ const nonDiscriminatorsRelativePath = entitiesRelativePath.filter((e) => !e.includes(discriminatorDir));
78
+ const discriminatorsRelativePath = entitiesRelativePath.filter((e) => e.includes(discriminatorDir));
79
+ const entitiesProviders = [];
80
+ for (const entity of nonDiscriminatorsRelativePath) {
81
+ const importedEntity = yield Promise.resolve().then(() => require(entity));
82
+ const provider = {
83
+ name: importedEntity[Object.keys(importedEntity)[0]].name,
84
+ schema: importedEntity[Object.keys(importedEntity)[1]],
85
+ };
86
+ const discriminators = discriminatorsRelativePath.filter((e) => e.includes(entity.replace(/(.+)(\/.+)$/gm, '$1')));
87
+ if (discriminators.length !== 0) {
88
+ const importedDiscriminators = yield Promise.all(discriminators.map((path) => Promise.resolve().then(() => require(path))));
89
+ provider['discriminators'] = importedDiscriminators.map((e) => {
90
+ return { name: e[Object.keys(e)[0]].name, schema: e[Object.keys(e)[1]] };
91
+ });
92
+ }
93
+ entitiesProviders.push(provider);
94
+ }
36
95
  return {
37
96
  module: FixturesModule,
38
97
  imports: [mongoose_1.MongooseModule.forFeature(entitiesProviders)],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dauflo/nest-fixtures",
3
- "version": "1.0.3",
3
+ "version": "1.2.0",
4
4
  "description": "A module for making fixtures with NestJS. This package is inspired by Doctrine fixtures.",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -4,7 +4,7 @@ import { glob } from 'glob'
4
4
  import { ReferenceRepository } from './fixtures'
5
5
 
6
6
  export class FixturesModule {
7
- static async forRootAsync(fixturesPathPattern: string, entitiesPathPattern: string): Promise<DynamicModule> {
7
+ static async forRootAsync(fixturesPathPattern: string, entitiesPathPattern: string, discriminatorDir: string = 'discriminatorDir'): Promise<DynamicModule> {
8
8
  // import fixtures
9
9
  const fixturesPath = glob.sync(fixturesPathPattern)
10
10
  const fixturesRelativePath = fixturesPath.map((path) => path.replace('src/', `${process.cwd()}/dist/`)).map((path) => path.replace('.ts', ''))
@@ -18,16 +18,97 @@ export class FixturesModule {
18
18
  // import entities
19
19
  const entitiesPath = glob.sync(entitiesPathPattern)
20
20
  const entitiesRelativePath = entitiesPath.map((path) => path.replace('src/', `${process.cwd()}/dist/`)).map((path) => path.replace('.ts', ''))
21
+
22
+ const nonDiscriminatorsRelativePath = entitiesRelativePath.filter((e) => !e.includes(discriminatorDir))
23
+ const discriminatorsRelativePath = entitiesRelativePath.filter((e) => e.includes(discriminatorDir))
24
+
21
25
  const entitiesProviders: any[] = []
22
- const importedEntities = await Promise.all(entitiesRelativePath.map((path) => import(path)))
23
26
 
24
- importedEntities.forEach((entity) => {
25
- entitiesProviders.push({
26
- name: entity[Object.keys(entity)[0]].name,
27
- schema: entity[Object.keys(entity)[1]],
28
- })
27
+ for (const entity of nonDiscriminatorsRelativePath) {
28
+ const importedEntity = await import(entity)
29
+
30
+ const provider = {
31
+ name: importedEntity[Object.keys(importedEntity)[0]].name,
32
+ schema: importedEntity[Object.keys(importedEntity)[1]],
33
+ }
34
+
35
+ const discriminators = discriminatorsRelativePath.filter((e) => e.includes(entity.replace(/(.+)(\/.+)$/gm, '$1')))
36
+
37
+ if (discriminators.length !== 0) {
38
+ const importedDiscriminators = await Promise.all(discriminators.map((path) => import(path)))
39
+ provider['discriminators'] = importedDiscriminators.map((e) => {
40
+ return { name: e[Object.keys(e)[0]].name, schema: e[Object.keys(e)[1]] }
41
+ })
42
+ }
43
+
44
+ entitiesProviders.push(provider)
45
+ }
46
+
47
+ return {
48
+ module: FixturesModule,
49
+ imports: [MongooseModule.forFeature(entitiesProviders)],
50
+ providers: [
51
+ ReferenceRepository,
52
+ ...fixturesProviders,
53
+ {
54
+ provide: 'FIXTURES',
55
+ useFactory(...args) {
56
+ const providersObject = {}
57
+ args.forEach((arg) => (providersObject[arg.constructor.name] = arg))
58
+ return providersObject
59
+ },
60
+ inject: fixturesProviders,
61
+ },
62
+ ],
63
+ exports: ['FIXTURES'],
64
+ }
65
+ }
66
+
67
+ static async forRootAsyncMonorepo(
68
+ fixturesPathPattern: string,
69
+ entitiesPathPattern: string,
70
+ discriminatorDir: string = 'discriminators',
71
+ buildDir: string = 'dist',
72
+ ): Promise<DynamicModule> {
73
+ // import fixtures
74
+ const fixturesPath = glob.sync(fixturesPathPattern)
75
+ const fixturesRelativePath = fixturesPath.map((path) => `${process.cwd()}/${buildDir}/${path}`).map((path) => path.replace('.ts', ''))
76
+ const fixturesProviders: any[] = []
77
+ const importedFixtures = await Promise.all(fixturesRelativePath.map((path) => import(path)))
78
+
79
+ importedFixtures.forEach((fixture) => {
80
+ fixturesProviders.push(fixture[Object.keys(fixture)[0]])
29
81
  })
30
82
 
83
+ // import entities
84
+ const entitiesPath = glob.sync(entitiesPathPattern)
85
+ const entitiesRelativePath = entitiesPath.map((path) => `${process.cwd()}/${buildDir}/${path}`).map((path) => path.replace('.ts', ''))
86
+
87
+ const nonDiscriminatorsRelativePath = entitiesRelativePath.filter((e) => !e.includes(discriminatorDir))
88
+ const discriminatorsRelativePath = entitiesRelativePath.filter((e) => e.includes(discriminatorDir))
89
+
90
+ const entitiesProviders: any[] = []
91
+
92
+ for (const entity of nonDiscriminatorsRelativePath) {
93
+ const importedEntity = await import(entity)
94
+
95
+ const provider = {
96
+ name: importedEntity[Object.keys(importedEntity)[0]].name,
97
+ schema: importedEntity[Object.keys(importedEntity)[1]],
98
+ }
99
+
100
+ const discriminators = discriminatorsRelativePath.filter((e) => e.includes(entity.replace(/(.+)(\/.+)$/gm, '$1')))
101
+
102
+ if (discriminators.length !== 0) {
103
+ const importedDiscriminators = await Promise.all(discriminators.map((path) => import(path)))
104
+ provider['discriminators'] = importedDiscriminators.map((e) => {
105
+ return { name: e[Object.keys(e)[0]].name, schema: e[Object.keys(e)[1]] }
106
+ })
107
+ }
108
+
109
+ entitiesProviders.push(provider)
110
+ }
111
+
31
112
  return {
32
113
  module: FixturesModule,
33
114
  imports: [MongooseModule.forFeature(entitiesProviders)],