Skip to content

Commit 6ca3056

Browse files
noratobrandonroberts
authored andcommitted
feat(data): add entity config in app module declaration for ng-add (#2133)
1 parent 025578a commit 6ca3056

File tree

6 files changed

+99
-12
lines changed

6 files changed

+99
-12
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { EntityMetadataMap, EntityDataModuleConfig } from '@ngrx/data';
2+
3+
const entityMetadata: EntityMetadataMap = {};
4+
5+
const pluralNames = { };
6+
7+
export const entityConfig: EntityDataModuleConfig = {
8+
entityMetadata,
9+
pluralNames
10+
};

modules/data/schematics/ng-add/index.spec.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,45 @@ describe('Data ng-add Schematic', () => {
6868
expect(thrownError).toBeDefined();
6969
});
7070

71+
it('should add entity-metadata config to EntityDataModule', () => {
72+
const options = { ...defaultOptions, effects: false, entityConfig: true };
73+
74+
const tree = schematicRunner.runSchematic('ng-add', options, appTree);
75+
const content = tree.readContent(`${projectPath}/src/app/app.module.ts`);
76+
expect(content).toMatch(
77+
/import { entityConfig } from '.\/entity-metadata'/
78+
);
79+
expect(content).toMatch(
80+
/EntityDataModuleWithoutEffects.forRoot\(entityConfig\)/
81+
);
82+
});
83+
84+
it('should add entity-metadata config file', () => {
85+
const options = { ...defaultOptions, entityConfig: true };
86+
87+
const tree = schematicRunner.runSchematic('ng-add', options, appTree);
88+
expect(
89+
tree.files.indexOf(`${projectPath}/src/app/entity-metadata.ts`)
90+
).toBeGreaterThanOrEqual(0);
91+
});
92+
93+
it('should add entity-metadata config to EntityDataModule', () => {
94+
const options = { ...defaultOptions, entityConfig: true };
95+
96+
const tree = schematicRunner.runSchematic('ng-add', options, appTree);
97+
const content = tree.readContent(`${projectPath}/src/app/app.module.ts`);
98+
expect(content).toMatch(
99+
/import { entityConfig } from '.\/entity-metadata'/
100+
);
101+
expect(content).toMatch(/EntityDataModule.forRoot\(entityConfig\)/);
102+
});
103+
71104
it('should import EntityDataModuleWithoutEffects into a specified module', () => {
72105
const options = {
73106
...defaultOptions,
74107
module: 'app.module.ts',
75108
effects: false,
109+
entityConfig: false,
76110
};
77111

78112
const tree = schematicRunner.runSchematic('ng-add', options, appTree);
@@ -83,15 +117,15 @@ describe('Data ng-add Schematic', () => {
83117
});
84118

85119
it('should register EntityDataModule in the provided module', () => {
86-
const options = { ...defaultOptions };
120+
const options = { ...defaultOptions, entityConfig: false };
87121

88122
const tree = schematicRunner.runSchematic('ng-add', options, appTree);
89123
const content = tree.readContent(`${projectPath}/src/app/app.module.ts`);
90124
expect(content).toMatch(/EntityDataModule\n/);
91125
});
92126

93127
it('should register EntityDataModuleWithoutEffects in the provided module', () => {
94-
const options = { ...defaultOptions, effects: false };
128+
const options = { ...defaultOptions, effects: false, entityConfig: false };
95129

96130
const tree = schematicRunner.runSchematic('ng-add', options, appTree);
97131
const content = tree.readContent(`${projectPath}/src/app/app.module.ts`);

modules/data/schematics/ng-add/index.ts

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
1-
import * as ts from 'typescript';
1+
import { Path } from '@angular-devkit/core';
22
import {
3-
Rule,
4-
SchematicContext,
5-
Tree,
3+
apply,
4+
applyTemplates,
65
chain,
6+
mergeWith,
7+
move,
78
noop,
9+
Rule,
10+
SchematicContext,
811
SchematicsException,
12+
Tree,
13+
url,
914
} from '@angular-devkit/schematics';
1015
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
1116
import {
17+
addImportToModule,
1218
addPackageToPackageJson,
13-
platformVersion,
19+
commitChanges,
20+
createReplaceChange,
1421
findModuleFromOptions,
15-
insertImport,
1622
getProjectPath,
23+
insertImport,
1724
parseName,
18-
addImportToModule,
19-
createReplaceChange,
25+
platformVersion,
2026
ReplaceChange,
27+
stringUtils,
2128
visitTSSourceFiles,
22-
commitChanges,
2329
} from '@ngrx/data/schematics-core';
30+
import * as ts from 'typescript';
2431
import { Schema as EntityDataOptions } from './schema';
2532

2633
function addNgRxDataToPackageJson() {
@@ -53,6 +60,7 @@ function addEntityDataToNgModule(options: EntityDataOptions): Rule {
5360
const moduleToImport = options.effects
5461
? 'EntityDataModule'
5562
: 'EntityDataModuleWithoutEffects';
63+
5664
const effectsModuleImport = insertImport(
5765
source,
5866
modulePath,
@@ -63,11 +71,24 @@ function addEntityDataToNgModule(options: EntityDataOptions): Rule {
6371
const [dateEntityNgModuleImport] = addImportToModule(
6472
source,
6573
modulePath,
66-
moduleToImport,
74+
options.entityConfig
75+
? [moduleToImport, 'forRoot(entityConfig)'].join('.')
76+
: moduleToImport,
6777
''
6878
);
6979

7080
const changes = [effectsModuleImport, dateEntityNgModuleImport];
81+
82+
if (options.entityConfig) {
83+
const entityConfigImport = insertImport(
84+
source,
85+
modulePath,
86+
'entityConfig',
87+
'./entity-metadata'
88+
);
89+
changes.push(entityConfigImport);
90+
}
91+
7192
commitChanges(host, source.fileName, changes);
7293

7394
return host;
@@ -248,6 +269,18 @@ function throwIfModuleNotSpecified(host: Tree, module?: string) {
248269
}
249270
}
250271

272+
function createEntityConfigFile(options: EntityDataOptions, path: Path) {
273+
return mergeWith(
274+
apply(url('./files'), [
275+
applyTemplates({
276+
...stringUtils,
277+
...options,
278+
}),
279+
move(path),
280+
])
281+
);
282+
}
283+
251284
export default function(options: EntityDataOptions): Rule {
252285
return (host: Tree, context: SchematicContext) => {
253286
(options as any).name = '';
@@ -268,6 +301,9 @@ export default function(options: EntityDataOptions): Rule {
268301
renameNgrxDataModule(),
269302
])
270303
: addEntityDataToNgModule(options),
304+
options.entityConfig
305+
? createEntityConfigFile(options, parsedPath.path)
306+
: noop(),
271307
])(host, context);
272308
};
273309
}

modules/data/schematics/ng-add/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
"default": false,
4040
"description": "Migrate from ngrx-data, will rename modules.",
4141
"alias": "migrate"
42+
},
43+
"entityConfig": {
44+
"type": "boolean",
45+
"default": true,
46+
"description": "Create the Entity config file"
4247
}
4348
},
4449
"required": []

modules/data/schematics/ng-add/schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export interface Schema {
55
project?: string;
66
module?: string;
77
migrateNgrxData?: boolean;
8+
entityConfig?: boolean;
89
}

projects/ngrx.io/content/guide/data/install.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ ng add @ngrx/data
3030
* module - name of file containing the module that you wish to add the import for the `EntityDataModule` to. Can also include the relative path to the file. For example, `src/app/app.module.ts`.
3131
* effects - if `false` it will use the `EntityDataModuleWithoutEffects` module instead of the default `EntityDataModule`.
3232
* migrateNgRxData - if `true` it will replace the `ngrx-data` module with the `@ngrx/data` module.
33+
* entityConfig - if `false` it will not create and declare the `entity-metadata` file.
3334

3435
This command will automate the following steps:
3536

0 commit comments

Comments
 (0)