Skip to content
This repository was archived by the owner on Jun 22, 2021. It is now read-only.

Commit 002719e

Browse files
committed
fix(constructQuery): Takes the db instead of the table as the parameter.
1 parent 8cb54bf commit 002719e

12 files changed

+23
-21
lines changed

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ const todosFacade = factory<TodoEntity>({
3939
return filter;
4040
},
4141
// 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');
4444
},
4545
// Optional property to convert an entity sort to a DB sort. Defaults to the function below.
4646
constructSort: (sort) => {

src/FacadeConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default interface FacadeConfig<E extends Entity> {
99
readonly constructDocument: (patch: Partial<E>) => Document;
1010
readonly constructEntity: (document: Document) => E;
1111
readonly constructFilter: (filter: Filter<E>) => any;
12-
readonly constructQuery: (table: knex.QueryBuilder) => knex.QueryBuilder;
12+
readonly constructQuery: (db: knex) => knex.QueryBuilder;
1313
readonly constructSort: (sort: Sort<E>) => any;
1414
readonly db: () => Promise<knex>;
1515
readonly defaultPaginationLimit: number;

src/FactoryConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default interface FacadeConfig<E extends Entity> {
99
readonly constructDocument?: (patch: Partial<E>) => Document;
1010
readonly constructEntity?: (document: Document) => E;
1111
readonly constructFilter?: (filter: Filter<E>) => any;
12-
readonly constructQuery?: (table: knex.QueryBuilder) => knex.QueryBuilder;
12+
readonly constructQuery?: (table: knex) => knex.QueryBuilder;
1313
readonly constructSort?: (sort: Sort<E>) => any;
1414
readonly db: () => Promise<knex>;
1515
readonly defaultPaginationLimit?: number;

src/factory.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ export default <E extends Entity>(factoryConfig: FactoryConfig<E>): Facade<E> =>
1919
return document;
2020
},
2121
constructFilter: (filter) => filter,
22-
constructQuery: (table) => table,
22+
constructQuery: (db) => {
23+
return db.table(facadeConfig.tableName);
24+
},
2325
constructSort: (sort) => sort,
2426
defaultPaginationLimit: 10,
2527
tableName: factoryConfig.entityName,

src/functions/countEntities.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import filterEntities from '../utils/filterEntities';
55

66
export default <E extends Entity>(config: FacadeConfig<E>): CountEntities<E> => {
77
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);
1010
const constructedFilter = config.constructFilter(filter);
1111
const [result] = await Promise.resolve(filterEntities(query, constructedFilter).count());
1212
return { count: result['count(*)'] };

src/functions/createEntity.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const conflictErrorCode = 1062;
77

88
export default <E extends Entity>(config: FacadeConfig<E>): CreateEntity<E> => {
99
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);
1212
const document = config.constructDocument({ ...entity as any, id });
1313
try {
1414
await Promise.resolve(query.insert(document));

src/functions/getEntities.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ export default <E extends Entity>(config: FacadeConfig<E>): GetEntities<E> => {
2323
};
2424
const defaultSort = { id: asc } as Sort<E>;
2525
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);
2828
const paginationFilter = createPaginationFilter(pagination, sort);
2929
const fullFilter = { $and: [filter, paginationFilter] };
3030
const constructedFilter = config.constructFilter(fullFilter);

src/functions/getEntity.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import filterEntities from '../utils/filterEntities';
77

88
export default <E extends Entity>(config: FacadeConfig<E>): GetEntity<E> => {
99
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);
1212
const constructedFilter = constructIdFilter({ id, filter, config });
1313
const document = await Promise.resolve(filterEntities(query, constructedFilter).first());
1414

src/functions/patchEntity.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import getEntity from './getEntity';
77

88
export default <E extends Entity>(config: FacadeConfig<E>): PatchEntity<E> => {
99
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);
1212
const document = config.constructDocument({ ...patch as any, id });
1313
const constructedFilter = constructIdFilter({ id, filter, config });
1414
await Promise.resolve(filterEntities(query, constructedFilter).update(document));

src/functions/removeEntities.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import filterEntities from '../utils/filterEntities';
55

66
export default <E extends Entity>(config: FacadeConfig<E>): RemoveEntities<E> => {
77
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);
1010
const constructedFilter = config.constructFilter(filter);
1111
await Promise.resolve(filterEntities(query, constructedFilter).delete());
1212
};

src/functions/removeEntity.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import filterEntities from '../utils/filterEntities';
77

88
export default <E extends Entity>(config: FacadeConfig<E>): RemoveEntity<E> => {
99
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);
1212
const constructedFilter = constructIdFilter({ id, filter, config });
1313
const count = await Promise.resolve(filterEntities(query, constructedFilter).delete());
1414

src/functions/replaceEntity.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import filterEntities from '../utils/filterEntities';
77

88
export default <E extends Entity>(config: FacadeConfig<E>): ReplaceEntity<E> => {
99
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);
1212
const document = config.constructDocument({ ...entity as any, id });
1313
const constructedFilter = constructIdFilter({ id, filter, config });
1414
const res = await Promise.resolve(filterEntities(query, constructedFilter).update(document));

0 commit comments

Comments
 (0)