diff --git a/modules/data/schematics/ng-add/files/entity-metadata.ts.template b/modules/data/schematics/ng-add/files/entity-metadata.ts.template new file mode 100644 index 0000000000..c35e88e682 --- /dev/null +++ b/modules/data/schematics/ng-add/files/entity-metadata.ts.template @@ -0,0 +1,10 @@ +import { EntityMetadataMap, EntityDataModuleConfig } from '@ngrx/data'; + +const entityMetadata: EntityMetadataMap = {}; + +const pluralNames = { }; + +export const entityConfig: EntityDataModuleConfig = { + entityMetadata, + pluralNames +}; diff --git a/modules/data/schematics/ng-add/index.spec.ts b/modules/data/schematics/ng-add/index.spec.ts index 5495b9da57..cd4407615c 100644 --- a/modules/data/schematics/ng-add/index.spec.ts +++ b/modules/data/schematics/ng-add/index.spec.ts @@ -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); @@ -83,7 +117,7 @@ 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`); @@ -91,7 +125,7 @@ describe('Data ng-add Schematic', () => { }); 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`); diff --git a/modules/data/schematics/ng-add/index.ts b/modules/data/schematics/ng-add/index.ts index ff55dbbdc8..fce7bc9fb2 100644 --- a/modules/data/schematics/ng-add/index.ts +++ b/modules/data/schematics/ng-add/index.ts @@ -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() { @@ -53,6 +60,7 @@ function addEntityDataToNgModule(options: EntityDataOptions): Rule { const moduleToImport = options.effects ? 'EntityDataModule' : 'EntityDataModuleWithoutEffects'; + const effectsModuleImport = insertImport( source, modulePath, @@ -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; @@ -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 = ''; @@ -268,6 +301,9 @@ export default function(options: EntityDataOptions): Rule { renameNgrxDataModule(), ]) : addEntityDataToNgModule(options), + options.entityConfig + ? createEntityConfigFile(options, parsedPath.path) + : noop(), ])(host, context); }; } diff --git a/modules/data/schematics/ng-add/schema.json b/modules/data/schematics/ng-add/schema.json index 71244aaefc..c94c82cc92 100644 --- a/modules/data/schematics/ng-add/schema.json +++ b/modules/data/schematics/ng-add/schema.json @@ -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": [] diff --git a/modules/data/schematics/ng-add/schema.ts b/modules/data/schematics/ng-add/schema.ts index 1885f1247a..4e30919a21 100644 --- a/modules/data/schematics/ng-add/schema.ts +++ b/modules/data/schematics/ng-add/schema.ts @@ -5,4 +5,5 @@ export interface Schema { project?: string; module?: string; migrateNgrxData?: boolean; + entityConfig?: boolean; } diff --git a/projects/ngrx.io/content/guide/data/install.md b/projects/ngrx.io/content/guide/data/install.md index 4b2377c5fb..34755bae90 100644 --- a/projects/ngrx.io/content/guide/data/install.md +++ b/projects/ngrx.io/content/guide/data/install.md @@ -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: