Skip to content
This repository was archived by the owner on Nov 11, 2018. It is now read-only.

Commit bc50679

Browse files
author
Gery Hirschfeld
committed
refactor(builder): Change builders to models
1 parent 68ffe0d commit bc50679

File tree

10 files changed

+54
-54
lines changed

10 files changed

+54
-54
lines changed

src/builders/abstact.builder.ts

-4
This file was deleted.
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { name } from 'faker';
22

3-
import { AuthorBuilder } from '../../builders/author.builder';
3+
import { AuthorModel } from '../../models/author.model';
44

55

6-
export const makeAuthor = (): AuthorBuilder => {
7-
return (new AuthorBuilder())
6+
export const makeAuthor = (): AuthorModel => {
7+
return (new AuthorModel())
88
.setFirstName(name.firstName())
99
.setLastName(name.lastName());
1010
};

src/database/factories/book.factory.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { lorem, random, date } from 'faker';
22

3-
import { BookBuilder } from '../../builders/book.builder';
3+
import { BookModel } from '../../models/book.model';
44

55

6-
export const makeBook = (authorId: number): BookBuilder => {
7-
return (new BookBuilder())
6+
export const makeBook = (authorId: number): BookModel => {
7+
return (new BookModel())
88
.setTitle(lorem.word())
99
.setDescription(lorem.sentences(5))
1010
.setPrice(random.number(120) + (random.number(99) / 100))

src/models/abstact.model.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface AbstactModel<Attributes, RawAttributes> {
2+
toJson(): Attributes;
3+
toDatabaseObject(): RawAttributes;
4+
}

src/builders/author.builder.ts src/models/author.model.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { models } from 'models';
22

3-
import { AbstactBuilder } from './abstact.builder';
3+
import { AbstactModel } from './abstact.model';
44

55

6-
export class AuthorBuilder implements AbstactBuilder<models.author.Attributes, models.author.RawAttributes> {
6+
export class AuthorModel implements AbstactModel<models.author.Attributes, models.author.RawAttributes> {
77

88
private id?: number;
99
private firstName: string;
@@ -14,9 +14,9 @@ export class AuthorBuilder implements AbstactBuilder<models.author.Attributes, m
1414
constructor(attributes?: models.author.Attributes | models.author.RawAttributes, isRaw = true) {
1515
if (attributes) {
1616
if (isRaw) {
17-
this.mapRaw(attributes);
17+
this.mapDatabaseObject(attributes);
1818
} else {
19-
this.map(attributes);
19+
this.mapJson(attributes);
2020
}
2121
}
2222
}
@@ -41,31 +41,31 @@ export class AuthorBuilder implements AbstactBuilder<models.author.Attributes, m
4141
return this.createdAt;
4242
};
4343

44-
public setId(id: number): AuthorBuilder {
44+
public setId(id: number): AuthorModel {
4545
this.id = id;
4646
return this;
4747
};
4848

49-
public setFirstName(firstName: string): AuthorBuilder {
49+
public setFirstName(firstName: string): AuthorModel {
5050
this.firstName = firstName;
5151
return this;
5252
};
53-
public setLastName(lastName: string): AuthorBuilder {
53+
public setLastName(lastName: string): AuthorModel {
5454
this.lastName = lastName;
5555
return this;
5656
};
5757

58-
public setUpdatedAt(updatedAt: Date): AuthorBuilder {
58+
public setUpdatedAt(updatedAt: Date): AuthorModel {
5959
this.updatedAt = updatedAt;
6060
return this;
6161
};
6262

63-
public setCreatedAt(createdAt: Date): AuthorBuilder {
63+
public setCreatedAt(createdAt: Date): AuthorModel {
6464
this.createdAt = createdAt;
6565
return this;
6666
};
6767

68-
public map(attributes: models.author.Attributes): AuthorBuilder {
68+
public mapJson(attributes: models.author.Attributes): AuthorModel {
6969
if (attributes !== undefined) {
7070
this.setId(attributes.id);
7171
this.setFirstName(attributes.firstName);
@@ -76,7 +76,7 @@ export class AuthorBuilder implements AbstactBuilder<models.author.Attributes, m
7676
return this;
7777
}
7878

79-
public mapRaw(attributes: models.author.RawAttributes): AuthorBuilder {
79+
public mapDatabaseObject(attributes: models.author.RawAttributes): AuthorModel {
8080
if (attributes !== undefined) {
8181
this.setId(attributes.id);
8282
this.setFirstName(attributes.first_name);
@@ -91,11 +91,11 @@ export class AuthorBuilder implements AbstactBuilder<models.author.Attributes, m
9191
return !!this.firstName && !!this.lastName;
9292
}
9393

94-
public build(): Author {
94+
public toJson(): Author {
9595
return new Author(this);
9696
}
9797

98-
public buildRaw(): RawAuthor {
98+
public toDatabaseObject(): RawAuthor {
9999
return new RawAuthor(this);
100100
}
101101

@@ -108,7 +108,7 @@ export class Author implements models.author.Attributes {
108108
public updatedAt?: Date;
109109
public createdAt?: Date;
110110

111-
constructor(builder: AuthorBuilder) {
111+
constructor(builder: AuthorModel) {
112112
this.id = builder.Id;
113113
this.firstName = builder.FirstName;
114114
this.lastName = builder.LastName;
@@ -124,7 +124,7 @@ export class RawAuthor implements models.author.RawAttributes {
124124
public updated_at?: Date;
125125
public created_at?: Date;
126126

127-
constructor(builder: AuthorBuilder) {
127+
constructor(builder: AuthorModel) {
128128
this.id = builder.Id;
129129
this.first_name = builder.FirstName;
130130
this.last_name = builder.LastName;

src/builders/book.builder.ts src/models/book.model.ts

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { models } from 'models';
22

3-
import { AbstactBuilder } from './abstact.builder';
3+
import { AbstactModel } from './abstact.model';
44

55

6-
export class BookBuilder implements AbstactBuilder<models.book.Attributes, models.book.RawAttributes> {
6+
export class BookModel implements AbstactModel<models.book.Attributes, models.book.RawAttributes> {
77

88
private id?: number;
99
private title?: string;
@@ -17,9 +17,9 @@ export class BookBuilder implements AbstactBuilder<models.book.Attributes, model
1717
constructor(attributes?: models.book.Attributes | models.book.RawAttributes, isRaw = true) {
1818
if (attributes) {
1919
if (isRaw) {
20-
this.mapRaw(attributes);
20+
this.mapDatabaseObject(attributes);
2121
} else {
22-
this.map(attributes);
22+
this.mapJson(attributes);
2323
}
2424
}
2525
}
@@ -56,47 +56,47 @@ export class BookBuilder implements AbstactBuilder<models.book.Attributes, model
5656
return this.createdAt;
5757
};
5858

59-
public setId(id: number): BookBuilder {
59+
public setId(id: number): BookModel {
6060
this.id = id;
6161
return this;
6262
};
6363

64-
public setTitle(title: string): BookBuilder {
64+
public setTitle(title: string): BookModel {
6565
this.title = title;
6666
return this;
6767
};
6868

69-
public setDescription(description: string): BookBuilder {
69+
public setDescription(description: string): BookModel {
7070
this.description = description;
7171
return this;
7272
};
7373

74-
public setPrice(price: number): BookBuilder {
74+
public setPrice(price: number): BookModel {
7575
this.price = price;
7676
return this;
7777
};
7878

79-
public setAuthorId(authorId: number): BookBuilder {
79+
public setAuthorId(authorId: number): BookModel {
8080
this.authorId = authorId;
8181
return this;
8282
};
8383

84-
public setPublishedAt(publishedAt: Date): BookBuilder {
84+
public setPublishedAt(publishedAt: Date): BookModel {
8585
this.publishedAt = publishedAt;
8686
return this;
8787
};
8888

89-
public setUpdatedAt(updatedAt: Date): BookBuilder {
89+
public setUpdatedAt(updatedAt: Date): BookModel {
9090
this.updatedAt = updatedAt;
9191
return this;
9292
};
9393

94-
public setCreatedAt(createdAt: Date): BookBuilder {
94+
public setCreatedAt(createdAt: Date): BookModel {
9595
this.createdAt = createdAt;
9696
return this;
9797
};
9898

99-
public map(attributes: models.book.Attributes): BookBuilder {
99+
public mapJson(attributes: models.book.Attributes): BookModel {
100100
if (attributes !== undefined) {
101101
this.setId(attributes.id);
102102
this.setTitle(attributes.title);
@@ -110,7 +110,7 @@ export class BookBuilder implements AbstactBuilder<models.book.Attributes, model
110110
return this;
111111
}
112112

113-
public mapRaw(attributes: models.book.RawAttributes): BookBuilder {
113+
public mapDatabaseObject(attributes: models.book.RawAttributes): BookModel {
114114
if (attributes !== undefined) {
115115
this.setId(attributes.id);
116116
this.setTitle(attributes.title);
@@ -128,11 +128,11 @@ export class BookBuilder implements AbstactBuilder<models.book.Attributes, model
128128
// TODO Check id all required attributes ar given
129129
}
130130

131-
public build() {
131+
public toJson() {
132132
return new Book(this);
133133
}
134134

135-
public buildRaw() {
135+
public toDatabaseObject() {
136136
return new RawBook(this);
137137
}
138138

@@ -148,7 +148,7 @@ export class Book implements models.book.Attributes {
148148
public updatedAt?: Date;
149149
public createdAt?: Date;
150150

151-
constructor(builder: BookBuilder) {
151+
constructor(builder: BookModel) {
152152
this.id = builder.Id;
153153
this.title = builder.Title;
154154
this.description = builder.Description;
@@ -170,7 +170,7 @@ export class RawBook implements models.book.RawAttributes {
170170
public updated_at?: Date;
171171
public created_at?: Date;
172172

173-
constructor(builder: BookBuilder) {
173+
constructor(builder: BookModel) {
174174
this.id = builder.Id;
175175
this.title = builder.Title;
176176
this.description = builder.Description;

src/repositories/author.repository.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Knex from 'knex';
22

33
import { models } from 'models';
44
import { AUTHOR } from '../common/tables';
5-
import { AuthorBuilder } from '../builders/author.builder';
5+
import { AuthorModel } from '../models/author.model';
66
import { AbstractRepository } from './abstract.repository';
77
import { Utils } from '../common/utils';
88

@@ -26,7 +26,7 @@ export class AuthorRepository extends AbstractRepository<Knex> {
2626
.from(AUTHOR)
2727
.limit(options.limit)
2828
.offset(options.offset);
29-
return results.map((result) => new AuthorBuilder(result).build());
29+
return results.map((result) => new AuthorModel(result).toJson());
3030
}
3131

3232
/**
@@ -41,7 +41,7 @@ export class AuthorRepository extends AbstractRepository<Knex> {
4141
log.debug('findAuthorById called with id=', id);
4242
const results = await this.db.select().from(AUTHOR).where('id', id);
4343
Utils.assertResults(results, id);
44-
return (new AuthorBuilder(Utils.single(results))).build();
44+
return (new AuthorModel(Utils.single(results))).toJson();
4545
}
4646

4747
/**
@@ -56,7 +56,7 @@ export class AuthorRepository extends AbstractRepository<Knex> {
5656
log.debug('findAuthorByIds called with ids=', ids);
5757
const results = await this.db.select().from(AUTHOR).whereIn('id', ids);
5858
Utils.assertResults(results, ids);
59-
return results.map((result) => new AuthorBuilder(result).build());
59+
return results.map((result) => new AuthorModel(result).toJson());
6060
}
6161

6262
}

src/repositories/book.repository.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Knex from 'knex';
22

33
import { models } from 'models';
44
import { BOOK } from '../common/tables';
5-
import { BookBuilder } from '../builders/book.builder';
5+
import { BookModel } from '../models/book.model';
66
import { AbstractRepository } from './abstract.repository';
77
import { Utils } from '../common/utils';
88

@@ -26,7 +26,7 @@ export class BookRepository extends AbstractRepository<Knex> {
2626
.from(BOOK)
2727
.limit(options.limit)
2828
.offset(options.offset);
29-
return results.map((result) => new BookBuilder(result).build());
29+
return results.map((result) => new BookModel(result).toJson());
3030
}
3131

3232
/**
@@ -41,7 +41,7 @@ export class BookRepository extends AbstractRepository<Knex> {
4141
log.debug('findBookById called with id=', id);
4242
const results = await this.db.select().from(BOOK).where('id', id);
4343
Utils.assertResults(results, id);
44-
return (new BookBuilder(Utils.single(results))).build();
44+
return (new BookModel(Utils.single(results))).toJson();
4545
}
4646

4747
/**
@@ -55,7 +55,7 @@ export class BookRepository extends AbstractRepository<Knex> {
5555
public async findBookByAuthorId(id: number): Promise<models.book.Attributes> {
5656
log.debug('findBookByAuthorId called with id=', id);
5757
const results = await this.db.select().from(BOOK).where('author_id', id);
58-
return results.map((result) => new BookBuilder(result).build());
58+
return results.map((result) => new BookModel(result).toJson());
5959
}
6060

6161
/**
@@ -70,7 +70,7 @@ export class BookRepository extends AbstractRepository<Knex> {
7070
log.debug('findBooksByIds called with ids=', ids);
7171
const results = await this.db.select().from(BOOK).whereIn('id', ids);
7272
Utils.assertResults(results, ids);
73-
return results.map((result) => new BookBuilder(result).build());
73+
return results.map((result) => new BookModel(result).toJson());
7474
}
7575

7676
}

src/schemas/author/author.field.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { GraphQLFieldDefinition } from 'graphql';
33
import { Context } from '../../context';
44
import { AbstractField, IGraphQLField } from '../abstract.field';
55
import { AuthorType } from '../author/author.type';
6-
import { Book } from '../../builders/book.builder';
6+
import { Book } from '../../models/book.model';
77

88
import { Logger } from '../../core/logger';
99
const log = Logger('app:schemas:author:AuthorField');

src/schemas/book/books.field.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { GraphQLFieldDefinition, GraphQLList } from 'graphql';
33
import { Context } from '../../context';
44
import { AbstractField, IGraphQLField } from '../abstract.field';
55
import { BookType } from '../book/book.type';
6-
import { Author } from '../../builders/author.builder';
6+
import { Author } from '../../models/author.model';
77

88
import { Logger } from '../../core/logger';
99
const log = Logger('app:schemas:book:BooksField');

0 commit comments

Comments
 (0)