This repository was archived by the owner on Jun 22, 2021. It is now read-only.
File tree 12 files changed +23
-21
lines changed
12 files changed +23
-21
lines changed Original file line number Diff line number Diff line change @@ -39,8 +39,8 @@ const todosFacade = factory<TodoEntity>({
39
39
return filter ;
40
40
},
41
41
// Optional property to construct an initial database query. Defaults to the function below.
42
- constrctQuery : (table ) => {
43
- return table ;
42
+ constructQuery : (db ) => {
43
+ return db . table ( ' todos ' ) ;
44
44
},
45
45
// Optional property to convert an entity sort to a DB sort. Defaults to the function below.
46
46
constructSort : (sort ) => {
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ export default interface FacadeConfig<E extends Entity> {
9
9
readonly constructDocument : ( patch : Partial < E > ) => Document ;
10
10
readonly constructEntity : ( document : Document ) => E ;
11
11
readonly constructFilter : ( filter : Filter < E > ) => any ;
12
- readonly constructQuery : ( table : knex . QueryBuilder ) => knex . QueryBuilder ;
12
+ readonly constructQuery : ( db : knex ) => knex . QueryBuilder ;
13
13
readonly constructSort : ( sort : Sort < E > ) => any ;
14
14
readonly db : ( ) => Promise < knex > ;
15
15
readonly defaultPaginationLimit : number ;
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ export default interface FacadeConfig<E extends Entity> {
9
9
readonly constructDocument ?: ( patch : Partial < E > ) => Document ;
10
10
readonly constructEntity ?: ( document : Document ) => E ;
11
11
readonly constructFilter ?: ( filter : Filter < E > ) => any ;
12
- readonly constructQuery ?: ( table : knex . QueryBuilder ) => knex . QueryBuilder ;
12
+ readonly constructQuery ?: ( table : knex ) => knex . QueryBuilder ;
13
13
readonly constructSort ?: ( sort : Sort < E > ) => any ;
14
14
readonly db : ( ) => Promise < knex > ;
15
15
readonly defaultPaginationLimit ?: number ;
Original file line number Diff line number Diff line change @@ -19,7 +19,9 @@ export default <E extends Entity>(factoryConfig: FactoryConfig<E>): Facade<E> =>
19
19
return document ;
20
20
} ,
21
21
constructFilter : ( filter ) => filter ,
22
- constructQuery : ( table ) => table ,
22
+ constructQuery : ( db ) => {
23
+ return db . table ( facadeConfig . tableName ) ;
24
+ } ,
23
25
constructSort : ( sort ) => sort ,
24
26
defaultPaginationLimit : 10 ,
25
27
tableName : factoryConfig . entityName ,
Original file line number Diff line number Diff line change @@ -5,8 +5,8 @@ import filterEntities from '../utils/filterEntities';
5
5
6
6
export default < E extends Entity > ( config : FacadeConfig < E > ) : CountEntities < E > => {
7
7
return async ( { filter = { } } ) => {
8
- const table = ( await config . db ( ) ) . table ( config . tableName ) ;
9
- const query = config . constructQuery ( table ) ;
8
+ const db = ( await config . db ( ) ) ;
9
+ const query = config . constructQuery ( db ) ;
10
10
const constructedFilter = config . constructFilter ( filter ) ;
11
11
const [ result ] = await Promise . resolve ( filterEntities ( query , constructedFilter ) . count ( ) ) ;
12
12
return { count : result [ 'count(*)' ] } ;
Original file line number Diff line number Diff line change @@ -7,8 +7,8 @@ const conflictErrorCode = 1062;
7
7
8
8
export default < E extends Entity > ( config : FacadeConfig < E > ) : CreateEntity < E > => {
9
9
return async ( { id, entity } ) => {
10
- const table = ( await config . db ( ) ) . table ( config . tableName ) ;
11
- const query = config . constructQuery ( table ) ;
10
+ const db = ( await config . db ( ) ) ;
11
+ const query = config . constructQuery ( db ) ;
12
12
const document = config . constructDocument ( { ...entity as any , id } ) ;
13
13
try {
14
14
await Promise . resolve ( query . insert ( document ) ) ;
Original file line number Diff line number Diff line change @@ -23,8 +23,8 @@ export default <E extends Entity>(config: FacadeConfig<E>): GetEntities<E> => {
23
23
} ;
24
24
const defaultSort = { id : asc } as Sort < E > ;
25
25
return async ( { filter = { } , sort = defaultSort , pagination = defaultPagination } ) => {
26
- const table = ( await config . db ( ) ) . table ( config . tableName ) ;
27
- const query = config . constructQuery ( table ) ;
26
+ const db = ( await config . db ( ) ) ;
27
+ const query = config . constructQuery ( db ) ;
28
28
const paginationFilter = createPaginationFilter ( pagination , sort ) ;
29
29
const fullFilter = { $and : [ filter , paginationFilter ] } ;
30
30
const constructedFilter = config . constructFilter ( fullFilter ) ;
Original file line number Diff line number Diff line change @@ -7,8 +7,8 @@ import filterEntities from '../utils/filterEntities';
7
7
8
8
export default < E extends Entity > ( config : FacadeConfig < E > ) : GetEntity < E > => {
9
9
return async ( { id, filter = { } } ) => {
10
- const table = ( await config . db ( ) ) . table ( config . tableName ) ;
11
- const query = config . constructQuery ( table ) ;
10
+ const db = ( await config . db ( ) ) ;
11
+ const query = config . constructQuery ( db ) ;
12
12
const constructedFilter = constructIdFilter ( { id, filter, config } ) ;
13
13
const document = await Promise . resolve ( filterEntities ( query , constructedFilter ) . first ( ) ) ;
14
14
Original file line number Diff line number Diff line change @@ -7,8 +7,8 @@ import getEntity from './getEntity';
7
7
8
8
export default < E extends Entity > ( config : FacadeConfig < E > ) : PatchEntity < E > => {
9
9
return async ( { id, patch, filter = { } } ) => {
10
- const table = ( await config . db ( ) ) . table ( config . tableName ) ;
11
- const query = config . constructQuery ( table ) ;
10
+ const db = ( await config . db ( ) ) ;
11
+ const query = config . constructQuery ( db ) ;
12
12
const document = config . constructDocument ( { ...patch as any , id } ) ;
13
13
const constructedFilter = constructIdFilter ( { id, filter, config } ) ;
14
14
await Promise . resolve ( filterEntities ( query , constructedFilter ) . update ( document ) ) ;
Original file line number Diff line number Diff line change @@ -5,8 +5,8 @@ import filterEntities from '../utils/filterEntities';
5
5
6
6
export default < E extends Entity > ( config : FacadeConfig < E > ) : RemoveEntities < E > => {
7
7
return async ( { filter = { } } ) => {
8
- const table = ( await config . db ( ) ) . table ( config . tableName ) ;
9
- const query = config . constructQuery ( table ) ;
8
+ const db = ( await config . db ( ) ) ;
9
+ const query = config . constructQuery ( db ) ;
10
10
const constructedFilter = config . constructFilter ( filter ) ;
11
11
await Promise . resolve ( filterEntities ( query , constructedFilter ) . delete ( ) ) ;
12
12
} ;
Original file line number Diff line number Diff line change @@ -7,8 +7,8 @@ import filterEntities from '../utils/filterEntities';
7
7
8
8
export default < E extends Entity > ( config : FacadeConfig < E > ) : RemoveEntity < E > => {
9
9
return async ( { id, filter = { } } ) => {
10
- const table = ( await config . db ( ) ) . table ( config . tableName ) ;
11
- const query = config . constructQuery ( table ) ;
10
+ const db = ( await config . db ( ) ) ;
11
+ const query = config . constructQuery ( db ) ;
12
12
const constructedFilter = constructIdFilter ( { id, filter, config } ) ;
13
13
const count = await Promise . resolve ( filterEntities ( query , constructedFilter ) . delete ( ) ) ;
14
14
Original file line number Diff line number Diff line change @@ -7,8 +7,8 @@ import filterEntities from '../utils/filterEntities';
7
7
8
8
export default < E extends Entity > ( config : FacadeConfig < E > ) : ReplaceEntity < E > => {
9
9
return async ( { id, entity, filter = { } } ) => {
10
- const table = ( await config . db ( ) ) . table ( config . tableName ) ;
11
- const query = config . constructQuery ( table ) ;
10
+ const db = ( await config . db ( ) ) ;
11
+ const query = config . constructQuery ( db ) ;
12
12
const document = config . constructDocument ( { ...entity as any , id } ) ;
13
13
const constructedFilter = constructIdFilter ( { id, filter, config } ) ;
14
14
const res = await Promise . resolve ( filterEntities ( query , constructedFilter ) . update ( document ) ) ;
You can’t perform that action at this time.
0 commit comments