diff --git a/libs/plugin/core/src/generators/repository/files/data/files/lib/inmemory/__model__.inmemory.repository.spec.ts__template__ b/libs/plugin/core/src/generators/repository/files/data/files/lib/__format__/__model__.__format__.repository.spec.ts__template__ similarity index 78% rename from libs/plugin/core/src/generators/repository/files/data/files/lib/inmemory/__model__.inmemory.repository.spec.ts__template__ rename to libs/plugin/core/src/generators/repository/files/data/files/lib/__format__/__model__.__format__.repository.spec.ts__template__ index 63e1040..9fe1266 100644 --- a/libs/plugin/core/src/generators/repository/files/data/files/lib/inmemory/__model__.inmemory.repository.spec.ts__template__ +++ b/libs/plugin/core/src/generators/repository/files/data/files/lib/__format__/__model__.__format__.repository.spec.ts__template__ @@ -1,12 +1,12 @@ -import { <%= entity.className %>InMemoryRepository } from './<%= entity.propertyName %>.inmemory.repository'; +import { <%= entity.className %><%= type.className %>Repository } from './<%= entity.fileName %>.<%= type.fileName %>.repository'; import { <%= entity.className %>Repository } from '@<%= npmScope %>/<%= projectDomain %>'; import { forkJoin } from 'rxjs'; -describe('<%= entity.className %> In Memory Repository', () => { +describe('<%= entity.className %> <%= type.className %> Repository', () => { let repo: <%= entity.className %>Repository; beforeEach(() => { - repo = new <%= entity.className %>InMemoryRepository([ + repo = new <%= entity.className %><%= type.className %>Repository([ { id: '1', title: 'one' diff --git a/libs/plugin/core/src/generators/repository/files/data/files/lib/inmemory/__model__.inmemory.repository.ts__template__ b/libs/plugin/core/src/generators/repository/files/data/files/lib/__format__/__model__.__format__.repository.ts__template__ similarity index 62% rename from libs/plugin/core/src/generators/repository/files/data/files/lib/inmemory/__model__.inmemory.repository.ts__template__ rename to libs/plugin/core/src/generators/repository/files/data/files/lib/__format__/__model__.__format__.repository.ts__template__ index 9db9e43..bd8551e 100644 --- a/libs/plugin/core/src/generators/repository/files/data/files/lib/inmemory/__model__.inmemory.repository.ts__template__ +++ b/libs/plugin/core/src/generators/repository/files/data/files/lib/__format__/__model__.__format__.repository.ts__template__ @@ -1,16 +1,16 @@ import { <%= entity.className %>Repository, <%= entity.className %>Entity } from '@<%= npmScope %>/<%= projectDomain %>'; -import { <%= entity.className %>MockMapper } from './mapper/<%= entity.fileName %>-mock.mapper'; -import { <%= entity.className %>MockDto } from './dto/<%= entity.fileName %>-mock.dto'; +import { <%= entity.className %><%= type.className %>Mapper } from './mapper/<%= entity.fileName %>-<%= type.fileName %>.mapper'; +import { <%= entity.className %><%= type.className %>Dto } from './dto/<%= entity.fileName %>-<%= type.fileName %>.dto'; import { Observable, of } from 'rxjs'; import { map } from 'rxjs/operators'; -export class <%= entity.className %>InMemoryRepository implements <%= entity.className %>Repository { - constructor(private data: <%= entity.className %>MockDto[] = []) {} +export class <%= entity.className %><%= type.className %>Repository implements <%= entity.className %>Repository { + constructor(private data: <%= entity.className %><%= type.className %>Dto[] = []) {} - private mapper = new <%= entity.className %>MockMapper(); + private mapper = new <%= entity.className %><%= type.className %>Mapper(); public getAll<%= entity.className %>s(): Observable<<%= entity.className %>Entity[]> { - return of(this.data).pipe(map((mocks) => mocks.map(this.mapper.mapTo))); + return of(this.data).pipe(map((<%= type.propertyName %>s) => <%= type.propertyName %>s.map(this.mapper.mapTo))); } public add<%= entity.className %>({ name }: Pick<<%= entity.className %>Entity, 'name'>): Observable<<%= entity.className %>Entity> { @@ -22,8 +22,8 @@ export class <%= entity.className %>InMemoryRepository implements <%= entity.cla } public get<%= entity.className %>ById(id: string): Observable<<%= entity.className %>Entity> { - return of<<%= entity.className %>MockDto>( - this.data.find((<%= entity.propertyName %>) => <%= entity.propertyName %>.id === id) as <%= entity.className %>MockDto + return of<<%= entity.className %><%= type.className %>Dto>( + this.data.find((<%= entity.propertyName %>) => <%= entity.propertyName %>.id === id) as <%= entity.className %><%= type.className %>Dto ).pipe(map(this.mapper.mapTo)); } @@ -39,6 +39,6 @@ export class <%= entity.className %>InMemoryRepository implements <%= entity.cla this.data.splice(idx, 1); - return of<<%= entity.className %>MockDto>(<%= entity.propertyName %> as <%= entity.className %>MockDto).pipe(map(this.mapper.mapTo)); + return of<<%= entity.className %><%= type.className %>Dto>(<%= entity.propertyName %> as <%= entity.className %><%= type.className %>Dto).pipe(map(this.mapper.mapTo)); } } diff --git a/libs/plugin/core/src/generators/repository/files/data/files/lib/__format__/dto/__model__-__format__.dto.ts__template__ b/libs/plugin/core/src/generators/repository/files/data/files/lib/__format__/dto/__model__-__format__.dto.ts__template__ new file mode 100644 index 0000000..559e18e --- /dev/null +++ b/libs/plugin/core/src/generators/repository/files/data/files/lib/__format__/dto/__model__-__format__.dto.ts__template__ @@ -0,0 +1,9 @@ +export class <%= entity.className %><%= type.className %>Dto { + id: string; + title: string; + + constructor(params: <%= entity.className %><%= type.className %>Dto) { + this.id = params.id; + this.title = params.title; + } +} diff --git a/libs/plugin/core/src/generators/repository/files/data/files/lib/inmemory/mapper/__model__-mock.mapper.ts__template__ b/libs/plugin/core/src/generators/repository/files/data/files/lib/__format__/mapper/__model__-__format__.mapper.ts__template__ similarity index 51% rename from libs/plugin/core/src/generators/repository/files/data/files/lib/inmemory/mapper/__model__-mock.mapper.ts__template__ rename to libs/plugin/core/src/generators/repository/files/data/files/lib/__format__/mapper/__model__-__format__.mapper.ts__template__ index 1d102df..b339406 100644 --- a/libs/plugin/core/src/generators/repository/files/data/files/lib/inmemory/mapper/__model__-mock.mapper.ts__template__ +++ b/libs/plugin/core/src/generators/repository/files/data/files/lib/__format__/mapper/__model__-__format__.mapper.ts__template__ @@ -1,16 +1,16 @@ -import { <%= entity.className %>MockDto } from '../dto/<%= entity.propertyName %>-mock.dto'; +import { <%= entity.className %><%= type.className %>Dto } from '../dto/<%= entity.fileName %>-<%= type.fileName %>.dto'; import { <%= entity.className %>Entity } from '@<%= npmScope %>/<%= projectDomain %>'; import { Mapper } from '@nx-clean/core'; -export class <%= entity.className %>MockMapper implements Mapper<<%= entity.className %>Entity, <%= entity.className %>MockDto> { - mapFrom(input: <%= entity.className %>Entity): <%= entity.className %>MockDto { +export class <%= entity.className %><%= type.className %>Mapper implements Mapper<<%= entity.className %>Entity, <%= entity.className %><%= type.className %>Dto> { + mapFrom(input: <%= entity.className %>Entity): <%= entity.className %><%= type.className %>Dto { return { id: input?.id, title: input?.name }; } - mapTo(input: <%= entity.className %>MockDto): <%= entity.className %>Entity { + mapTo(input: <%= entity.className %><%= type.className %>Dto): <%= entity.className %>Entity { const <%= entity.propertyName %> = <%= entity.className %>Entity.create({ id: input?.id, name: input?.title diff --git a/libs/plugin/core/src/generators/repository/files/data/files/lib/inmemory/dto/__model__-mock.dto.ts__template__ b/libs/plugin/core/src/generators/repository/files/data/files/lib/inmemory/dto/__model__-mock.dto.ts__template__ deleted file mode 100644 index df7a7df..0000000 --- a/libs/plugin/core/src/generators/repository/files/data/files/lib/inmemory/dto/__model__-mock.dto.ts__template__ +++ /dev/null @@ -1,9 +0,0 @@ -export class <%= entity.className %>MockDto { - id: string; - title: string; - - constructor(params: <%= entity.className %>MockDto) { - this.id = params.id; - this.title = params.title; - } -} diff --git a/libs/plugin/core/src/generators/repository/files/src/index.ts__template__ b/libs/plugin/core/src/generators/repository/files/src/index.ts__template__ deleted file mode 100644 index dde3cb6..0000000 --- a/libs/plugin/core/src/generators/repository/files/src/index.ts__template__ +++ /dev/null @@ -1 +0,0 @@ -const variable = "<%= projectName %>"; \ No newline at end of file diff --git a/libs/plugin/core/src/generators/repository/generator.ts b/libs/plugin/core/src/generators/repository/generator.ts index b8cce9b..9be809b 100644 --- a/libs/plugin/core/src/generators/repository/generator.ts +++ b/libs/plugin/core/src/generators/repository/generator.ts @@ -25,12 +25,22 @@ export default function ( if (options.name && options.domain) { const domain = readProjectConfiguration(host, options.domain); - addFiles( + const { model } = addFiles( host, normalizedOptions, __dirname + '/files/domain', domain.sourceRoot ); + + const contents = host.read(domain.sourceRoot + '/index.ts'); + host.write( + domain.sourceRoot + '/index.ts', + contents.toString() + + ` +export * from './lib/entity/${model}.entity'; +export * from './lib/repository/${model}.repository'; +` + ); } if (options.impl && options.data) { @@ -38,14 +48,29 @@ export default function ( throw new Error('You need to add a name'); } + if (!options.type) { + throw new Error('You need to add a type of repository'); + } + const data = readProjectConfiguration(host, options.data); - addFiles( + const { model, format } = addFiles( host, normalizedOptions, __dirname + '/files/data', data.sourceRoot ); + + const contents = host.read(data.sourceRoot + '/index.ts'); + host.write( + data.sourceRoot + '/index.ts', + contents.toString() + + ` +export * from './lib/${format}/${model}.${format}.repository'; +export * from './lib/${format}/mapper/${model}-${format}.mapper'; +export * from './lib/${format}/dto/${model}-${format}.dto'; +` + ); } } @@ -54,6 +79,7 @@ function normalizeOptions( options: ImplRepositoryGeneratorSchema ): NormalizedSchema { const npmScope = readWorkspaceConfiguration(host).npmScope; + const format = names(options.type).fileName; const projectDomain = options.domain; const projectData = options.data; return { @@ -61,6 +87,7 @@ function normalizeOptions( projectDomain, projectData, npmScope, + format, }; } @@ -71,6 +98,7 @@ function addFiles( target: string ) { const entity = names(options.name); + const type = names(options.type); const templateOptions: TemplateOptions = { ...options, @@ -79,7 +107,10 @@ function addFiles( offsetFromRoot: offsetFromRoot(target), template: '', entity, + type, }; generateFiles(host, join(dir, 'files'), target, templateOptions); + + return templateOptions; } diff --git a/libs/plugin/core/src/generators/repository/schema.d.ts b/libs/plugin/core/src/generators/repository/schema.d.ts index cd1b151..86b518e 100644 --- a/libs/plugin/core/src/generators/repository/schema.d.ts +++ b/libs/plugin/core/src/generators/repository/schema.d.ts @@ -2,16 +2,19 @@ export interface RepositoryGeneratorSchema { name: string; domain: string; impl?: boolean; + type?: string; } export interface ImplRepositoryGeneratorSchema extends RepositoryGeneratorSchema { impl: boolean; data: string; + type: string; } export interface NormalizedSchema extends RepositoryGeneratorSchema { projectDomain: string; projectData: string; + format: string; npmScope: string; } @@ -26,6 +29,7 @@ export interface Names { export interface TemplateOptions extends NormalizedSchema, Names { offsetFromRoot: string; entity: Names; + type: Names; model: string; template: string; } diff --git a/libs/plugin/core/src/generators/repository/schema.json b/libs/plugin/core/src/generators/repository/schema.json index 7ae279c..7518862 100644 --- a/libs/plugin/core/src/generators/repository/schema.json +++ b/libs/plugin/core/src/generators/repository/schema.json @@ -26,6 +26,11 @@ "description": "Would you like to add a implementation?", "default": false }, + "type": { + "type": "string", + "description": "Repository type (ex.: in memory, http)", + "x-prompt": "What type would you like to use?" + }, "data": { "type": "string", "description": "The name of the data library.",