Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(data): add entity config in App module declaration #2133

Merged
merged 4 commits into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions modules/data/schematics/ng-add/files/entity-metadata.ts.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { EntityMetadataMap, EntityDataModuleConfig } from '@ngrx/data';

const entityMetadata: EntityMetadataMap = {};

const pluralNames = { };

export const entityConfig: EntityDataModuleConfig = {
entityMetadata,
pluralNames
};
38 changes: 36 additions & 2 deletions modules/data/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,45 @@ describe('Data ng-add Schematic', () => {
expect(thrownError).toBeDefined();
});

it('should add entity-metadata config to EntityDataModule', () => {
const options = { ...defaultOptions, effects: false, entityConfig: true };

const tree = schematicRunner.runSchematic('ng-add', options, appTree);
const content = tree.readContent(`${projectPath}/src/app/app.module.ts`);
expect(content).toMatch(
/import { entityConfig } from '.\/entity-metadata'/
);
expect(content).toMatch(
/EntityDataModuleWithoutEffects.forRoot\(entityConfig\)/
);
});

it('should add entity-metadata config file', () => {
const options = { ...defaultOptions, entityConfig: true };

const tree = schematicRunner.runSchematic('ng-add', options, appTree);
expect(
tree.files.indexOf(`${projectPath}/src/app/entity-metadata.ts`)
).toBeGreaterThanOrEqual(0);
});

it('should add entity-metadata config to EntityDataModule', () => {
const options = { ...defaultOptions, entityConfig: true };

const tree = schematicRunner.runSchematic('ng-add', options, appTree);
const content = tree.readContent(`${projectPath}/src/app/app.module.ts`);
expect(content).toMatch(
/import { entityConfig } from '.\/entity-metadata'/
);
expect(content).toMatch(/EntityDataModule.forRoot\(entityConfig\)/);
});

it('should import EntityDataModuleWithoutEffects into a specified module', () => {
const options = {
...defaultOptions,
module: 'app.module.ts',
effects: false,
entityConfig: false,
};

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

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

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

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

const tree = schematicRunner.runSchematic('ng-add', options, appTree);
const content = tree.readContent(`${projectPath}/src/app/app.module.ts`);
Expand Down
56 changes: 46 additions & 10 deletions modules/data/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
import * as ts from 'typescript';
import { Path } from '@angular-devkit/core';
import {
Rule,
SchematicContext,
Tree,
apply,
applyTemplates,
chain,
mergeWith,
move,
noop,
Rule,
SchematicContext,
SchematicsException,
Tree,
url,
} from '@angular-devkit/schematics';
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
import {
addImportToModule,
addPackageToPackageJson,
platformVersion,
commitChanges,
createReplaceChange,
findModuleFromOptions,
insertImport,
getProjectPath,
insertImport,
parseName,
addImportToModule,
createReplaceChange,
platformVersion,
ReplaceChange,
stringUtils,
visitTSSourceFiles,
commitChanges,
} from '@ngrx/data/schematics-core';
import * as ts from 'typescript';
import { Schema as EntityDataOptions } from './schema';

function addNgRxDataToPackageJson() {
Expand Down Expand Up @@ -53,6 +60,7 @@ function addEntityDataToNgModule(options: EntityDataOptions): Rule {
const moduleToImport = options.effects
? 'EntityDataModule'
: 'EntityDataModuleWithoutEffects';

const effectsModuleImport = insertImport(
source,
modulePath,
Expand All @@ -63,11 +71,24 @@ function addEntityDataToNgModule(options: EntityDataOptions): Rule {
const [dateEntityNgModuleImport] = addImportToModule(
source,
modulePath,
moduleToImport,
options.entityConfig
? [moduleToImport, 'forRoot(entityConfig)'].join('.')
: moduleToImport,
''
);

const changes = [effectsModuleImport, dateEntityNgModuleImport];

if (options.entityConfig) {
const entityConfigImport = insertImport(
source,
modulePath,
'entityConfig',
'./entity-metadata'
);
changes.push(entityConfigImport);
}

commitChanges(host, source.fileName, changes);

return host;
Expand Down Expand Up @@ -248,6 +269,18 @@ function throwIfModuleNotSpecified(host: Tree, module?: string) {
}
}

function createEntityConfigFile(options: EntityDataOptions, path: Path) {
return mergeWith(
apply(url('./files'), [
applyTemplates({
...stringUtils,
...options,
}),
move(path),
])
);
}

export default function(options: EntityDataOptions): Rule {
return (host: Tree, context: SchematicContext) => {
(options as any).name = '';
Expand All @@ -268,6 +301,9 @@ export default function(options: EntityDataOptions): Rule {
renameNgrxDataModule(),
])
: addEntityDataToNgModule(options),
options.entityConfig
? createEntityConfigFile(options, parsedPath.path)
: noop(),
])(host, context);
};
}
5 changes: 5 additions & 0 deletions modules/data/schematics/ng-add/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
"default": false,
"description": "Migrate from ngrx-data, will rename modules.",
"alias": "migrate"
},
"entityConfig": {
"type": "boolean",
"default": true,
"description": "Create the Entity config file"
}
},
"required": []
Expand Down
1 change: 1 addition & 0 deletions modules/data/schematics/ng-add/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface Schema {
project?: string;
module?: string;
migrateNgrxData?: boolean;
entityConfig?: boolean;
}
1 change: 1 addition & 0 deletions projects/ngrx.io/content/guide/data/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ ng add @ngrx/data
* 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`.
* effects - if `false` it will use the `EntityDataModuleWithoutEffects` module instead of the default `EntityDataModule`.
* migrateNgRxData - if `true` it will replace the `ngrx-data` module with the `@ngrx/data` module.
* entityConfig - if `false` it will not create and declare the `entity-metadata` file.

This command will automate the following steps:

Expand Down