Skip to content

Commit

Permalink
feat(plugin-core): add repository type
Browse files Browse the repository at this point in the history
  • Loading branch information
guiseek committed Oct 5, 2021
1 parent 2a6e897 commit 3f707ab
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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> {
Expand All @@ -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));
}

Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down

This file was deleted.

This file was deleted.

35 changes: 33 additions & 2 deletions libs/plugin/core/src/generators/repository/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,52 @@ 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) {
if (!options.name) {
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';
`
);
}
}

Expand All @@ -54,13 +79,15 @@ 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 {
...options,
projectDomain,
projectData,
npmScope,
format,
};
}

Expand All @@ -71,6 +98,7 @@ function addFiles(
target: string
) {
const entity = names(options.name);
const type = names(options.type);

const templateOptions: TemplateOptions = {
...options,
Expand All @@ -79,7 +107,10 @@ function addFiles(
offsetFromRoot: offsetFromRoot(target),
template: '',
entity,
type,
};

generateFiles(host, join(dir, 'files'), target, templateOptions);

return templateOptions;
}
4 changes: 4 additions & 0 deletions libs/plugin/core/src/generators/repository/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -26,6 +29,7 @@ export interface Names {
export interface TemplateOptions extends NormalizedSchema, Names {
offsetFromRoot: string;
entity: Names;
type: Names;
model: string;
template: string;
}
5 changes: 5 additions & 0 deletions libs/plugin/core/src/generators/repository/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down

0 comments on commit 3f707ab

Please sign in to comment.