This repository was archived by the owner on Jun 22, 2021. It is now read-only.
File tree 12 files changed +24
-9
lines changed 12 files changed +24
-9
lines changed Original file line number Diff line number Diff line change @@ -38,10 +38,14 @@ const todosFacade = factory<TodoEntity>({
38
38
constructFilter : (filter ) => {
39
39
return filter ;
40
40
},
41
+ // Optional property to construct an initial database query. Defaults to the function below.
42
+ constrctQuery : (table ) => {
43
+ return table ;
44
+ },
41
45
// Optional property to convert an entity sort to a DB sort. Defaults to the function below.
42
46
constructSort : (sort ) => {
43
47
return sort ;
44
- }.
48
+ },
45
49
db: connectToDb ({
46
50
client: ' mysql' ,
47
51
connection: {
Original file line number Diff line number Diff line change @@ -9,6 +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
13
readonly constructSort : ( sort : Sort < E > ) => any ;
13
14
readonly db : ( ) => Promise < knex > ;
14
15
readonly defaultPaginationLimit : number ;
Original file line number Diff line number Diff line change @@ -9,6 +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
13
readonly constructSort ?: ( sort : Sort < E > ) => any ;
13
14
readonly db : ( ) => Promise < knex > ;
14
15
readonly defaultPaginationLimit ?: number ;
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ 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
23
constructSort : ( sort ) => sort ,
23
24
defaultPaginationLimit : 10 ,
24
25
tableName : factoryConfig . entityName ,
Original file line number Diff line number Diff line change @@ -6,8 +6,9 @@ import filterEntities from '../utils/filterEntities';
6
6
export default < E extends Entity > ( config : FacadeConfig < E > ) : CountEntities < E > => {
7
7
return async ( { filter = { } } ) => {
8
8
const table = ( await config . db ( ) ) . table ( config . tableName ) ;
9
+ const query = config . constructQuery ( table ) ;
9
10
const constructedFilter = config . constructFilter ( filter ) ;
10
- const [ result ] = await Promise . resolve ( filterEntities ( table , constructedFilter ) . count ( ) ) ;
11
+ const [ result ] = await Promise . resolve ( filterEntities ( query , constructedFilter ) . count ( ) ) ;
11
12
return { count : result [ 'count(*)' ] } ;
12
13
} ;
13
14
} ;
Original file line number Diff line number Diff line change @@ -8,9 +8,10 @@ const conflictErrorCode = 1062;
8
8
export default < E extends Entity > ( config : FacadeConfig < E > ) : CreateEntity < E > => {
9
9
return async ( { id, entity } ) => {
10
10
const table = ( await config . db ( ) ) . table ( config . tableName ) ;
11
+ const query = config . constructQuery ( table ) ;
11
12
const document = config . constructDocument ( { ...entity as any , id } ) ;
12
13
try {
13
- await Promise . resolve ( table . insert ( document ) ) ;
14
+ await Promise . resolve ( query . insert ( document ) ) ;
14
15
} catch ( err ) {
15
16
if ( err . errno === conflictErrorCode ) {
16
17
throw new ConflictingEntityError ( config . entityName , id ) ;
Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ export default <E extends Entity>(config: FacadeConfig<E>): GetEntities<E> => {
24
24
const defaultSort = { id : asc } as Sort < E > ;
25
25
return async ( { filter = { } , sort = defaultSort , pagination = defaultPagination } ) => {
26
26
const table = ( await config . db ( ) ) . table ( config . tableName ) ;
27
+ const query = config . constructQuery ( table ) ;
27
28
const paginationFilter = createPaginationFilter ( pagination , sort ) ;
28
29
const fullFilter = { $and : [ filter , paginationFilter ] } ;
29
30
const constructedFilter = config . constructFilter ( fullFilter ) ;
@@ -32,7 +33,7 @@ export default <E extends Entity>(config: FacadeConfig<E>): GetEntities<E> => {
32
33
return ! xor ( pagination . direction === forward , sortOrder === asc ) ? 'asc' : 'desc' ;
33
34
} ) ;
34
35
35
- const filterQuery = filterEntities ( table , constructedFilter ) ;
36
+ const filterQuery = filterEntities ( query , constructedFilter ) ;
36
37
const sortQuery = Object . keys ( knexSort ) . reduce ( ( result , sortKey ) => {
37
38
return result . orderBy ( sortKey , ( knexSort as any ) [ sortKey ] ) ;
38
39
} , filterQuery ) ;
Original file line number Diff line number Diff line change @@ -8,8 +8,9 @@ import filterEntities from '../utils/filterEntities';
8
8
export default < E extends Entity > ( config : FacadeConfig < E > ) : GetEntity < E > => {
9
9
return async ( { id, filter = { } } ) => {
10
10
const table = ( await config . db ( ) ) . table ( config . tableName ) ;
11
+ const query = config . constructQuery ( table ) ;
11
12
const constructedFilter = constructIdFilter ( { id, filter, config } ) ;
12
- const document = await Promise . resolve ( filterEntities ( table , constructedFilter ) . first ( ) ) ;
13
+ const document = await Promise . resolve ( filterEntities ( query , constructedFilter ) . first ( ) ) ;
13
14
14
15
if ( document === undefined || document === null ) {
15
16
throw new MissingEntityError ( config . entityName , id ) ;
Original file line number Diff line number Diff line change @@ -8,9 +8,10 @@ import getEntity from './getEntity';
8
8
export default < E extends Entity > ( config : FacadeConfig < E > ) : PatchEntity < E > => {
9
9
return async ( { id, patch, filter = { } } ) => {
10
10
const table = ( await config . db ( ) ) . table ( config . tableName ) ;
11
+ const query = config . constructQuery ( table ) ;
11
12
const document = config . constructDocument ( { ...patch as any , id } ) ;
12
13
const constructedFilter = constructIdFilter ( { id, filter, config } ) ;
13
- await Promise . resolve ( filterEntities ( table , constructedFilter ) . update ( document ) ) ;
14
+ await Promise . resolve ( filterEntities ( query , constructedFilter ) . update ( document ) ) ;
14
15
return getEntity < E > ( config ) ( { id, filter } ) ;
15
16
} ;
16
17
} ;
Original file line number Diff line number Diff line change @@ -6,7 +6,8 @@ import filterEntities from '../utils/filterEntities';
6
6
export default < E extends Entity > ( config : FacadeConfig < E > ) : RemoveEntities < E > => {
7
7
return async ( { filter = { } } ) => {
8
8
const table = ( await config . db ( ) ) . table ( config . tableName ) ;
9
+ const query = config . constructQuery ( table ) ;
9
10
const constructedFilter = config . constructFilter ( filter ) ;
10
- await Promise . resolve ( filterEntities ( table , constructedFilter ) . delete ( ) ) ;
11
+ await Promise . resolve ( filterEntities ( query , constructedFilter ) . delete ( ) ) ;
11
12
} ;
12
13
} ;
Original file line number Diff line number Diff line change @@ -8,8 +8,9 @@ import filterEntities from '../utils/filterEntities';
8
8
export default < E extends Entity > ( config : FacadeConfig < E > ) : RemoveEntity < E > => {
9
9
return async ( { id, filter = { } } ) => {
10
10
const table = ( await config . db ( ) ) . table ( config . tableName ) ;
11
+ const query = config . constructQuery ( table ) ;
11
12
const constructedFilter = constructIdFilter ( { id, filter, config } ) ;
12
- const count = await Promise . resolve ( filterEntities ( table , constructedFilter ) . delete ( ) ) ;
13
+ const count = await Promise . resolve ( filterEntities ( query , constructedFilter ) . delete ( ) ) ;
13
14
14
15
if ( count === 0 ) {
15
16
throw new MissingEntityError ( config . entityName , id ) ;
Original file line number Diff line number Diff line change @@ -8,9 +8,10 @@ import filterEntities from '../utils/filterEntities';
8
8
export default < E extends Entity > ( config : FacadeConfig < E > ) : ReplaceEntity < E > => {
9
9
return async ( { id, entity, filter = { } } ) => {
10
10
const table = ( await config . db ( ) ) . table ( config . tableName ) ;
11
+ const query = config . constructQuery ( table ) ;
11
12
const document = config . constructDocument ( { ...entity as any , id } ) ;
12
13
const constructedFilter = constructIdFilter ( { id, filter, config } ) ;
13
- const res = await Promise . resolve ( filterEntities ( table , constructedFilter ) . update ( document ) ) ;
14
+ const res = await Promise . resolve ( filterEntities ( query , constructedFilter ) . update ( document ) ) ;
14
15
if ( ! res ) {
15
16
throw new MissingEntityError ( config . entityName , id ) ;
16
17
}
You can’t perform that action at this time.
0 commit comments