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

Commit 8cb54bf

Browse files
committed
feat: Adds constructQuery.
1 parent 155c28f commit 8cb54bf

12 files changed

+24
-9
lines changed

readme.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@ const todosFacade = factory<TodoEntity>({
3838
constructFilter: (filter) => {
3939
return filter;
4040
},
41+
// Optional property to construct an initial database query. Defaults to the function below.
42+
constrctQuery: (table) => {
43+
return table;
44+
},
4145
// Optional property to convert an entity sort to a DB sort. Defaults to the function below.
4246
constructSort: (sort) => {
4347
return sort;
44-
}.
48+
},
4549
db: connectToDb({
4650
client: 'mysql',
4751
connection: {

src/FacadeConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +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;
1213
readonly constructSort: (sort: Sort<E>) => any;
1314
readonly db: () => Promise<knex>;
1415
readonly defaultPaginationLimit: number;

src/FactoryConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +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;
1213
readonly constructSort?: (sort: Sort<E>) => any;
1314
readonly db: () => Promise<knex>;
1415
readonly defaultPaginationLimit?: number;

src/factory.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default <E extends Entity>(factoryConfig: FactoryConfig<E>): Facade<E> =>
1919
return document;
2020
},
2121
constructFilter: (filter) => filter,
22+
constructQuery: (table) => table,
2223
constructSort: (sort) => sort,
2324
defaultPaginationLimit: 10,
2425
tableName: factoryConfig.entityName,

src/functions/countEntities.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import filterEntities from '../utils/filterEntities';
66
export default <E extends Entity>(config: FacadeConfig<E>): CountEntities<E> => {
77
return async ({ filter = {} }) => {
88
const table = (await config.db()).table(config.tableName);
9+
const query = config.constructQuery(table);
910
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());
1112
return { count: result['count(*)'] };
1213
};
1314
};

src/functions/createEntity.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ const conflictErrorCode = 1062;
88
export default <E extends Entity>(config: FacadeConfig<E>): CreateEntity<E> => {
99
return async ({ id, entity }) => {
1010
const table = (await config.db()).table(config.tableName);
11+
const query = config.constructQuery(table);
1112
const document = config.constructDocument({ ...entity as any, id });
1213
try {
13-
await Promise.resolve(table.insert(document));
14+
await Promise.resolve(query.insert(document));
1415
} catch (err) {
1516
if (err.errno === conflictErrorCode) {
1617
throw new ConflictingEntityError(config.entityName, id);

src/functions/getEntities.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default <E extends Entity>(config: FacadeConfig<E>): GetEntities<E> => {
2424
const defaultSort = { id: asc } as Sort<E>;
2525
return async ({ filter = {}, sort = defaultSort, pagination = defaultPagination }) => {
2626
const table = (await config.db()).table(config.tableName);
27+
const query = config.constructQuery(table);
2728
const paginationFilter = createPaginationFilter(pagination, sort);
2829
const fullFilter = { $and: [filter, paginationFilter] };
2930
const constructedFilter = config.constructFilter(fullFilter);
@@ -32,7 +33,7 @@ export default <E extends Entity>(config: FacadeConfig<E>): GetEntities<E> => {
3233
return !xor(pagination.direction === forward, sortOrder === asc) ? 'asc' : 'desc';
3334
});
3435

35-
const filterQuery = filterEntities(table, constructedFilter);
36+
const filterQuery = filterEntities(query, constructedFilter);
3637
const sortQuery = Object.keys(knexSort).reduce((result, sortKey) => {
3738
return result.orderBy(sortKey, (knexSort as any)[sortKey]);
3839
}, filterQuery);

src/functions/getEntity.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import filterEntities from '../utils/filterEntities';
88
export default <E extends Entity>(config: FacadeConfig<E>): GetEntity<E> => {
99
return async ({ id, filter = {} }) => {
1010
const table = (await config.db()).table(config.tableName);
11+
const query = config.constructQuery(table);
1112
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());
1314

1415
if (document === undefined || document === null) {
1516
throw new MissingEntityError(config.entityName, id);

src/functions/patchEntity.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import getEntity from './getEntity';
88
export default <E extends Entity>(config: FacadeConfig<E>): PatchEntity<E> => {
99
return async ({ id, patch, filter = {} }) => {
1010
const table = (await config.db()).table(config.tableName);
11+
const query = config.constructQuery(table);
1112
const document = config.constructDocument({ ...patch as any, id });
1213
const constructedFilter = constructIdFilter({ id, filter, config });
13-
await Promise.resolve(filterEntities(table, constructedFilter).update(document));
14+
await Promise.resolve(filterEntities(query, constructedFilter).update(document));
1415
return getEntity<E>(config)({ id, filter });
1516
};
1617
};

src/functions/removeEntities.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import filterEntities from '../utils/filterEntities';
66
export default <E extends Entity>(config: FacadeConfig<E>): RemoveEntities<E> => {
77
return async ({ filter = {} }) => {
88
const table = (await config.db()).table(config.tableName);
9+
const query = config.constructQuery(table);
910
const constructedFilter = config.constructFilter(filter);
10-
await Promise.resolve(filterEntities(table, constructedFilter).delete());
11+
await Promise.resolve(filterEntities(query, constructedFilter).delete());
1112
};
1213
};

src/functions/removeEntity.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import filterEntities from '../utils/filterEntities';
88
export default <E extends Entity>(config: FacadeConfig<E>): RemoveEntity<E> => {
99
return async ({ id, filter = {} }) => {
1010
const table = (await config.db()).table(config.tableName);
11+
const query = config.constructQuery(table);
1112
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());
1314

1415
if (count === 0) {
1516
throw new MissingEntityError(config.entityName, id);

src/functions/replaceEntity.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import filterEntities from '../utils/filterEntities';
88
export default <E extends Entity>(config: FacadeConfig<E>): ReplaceEntity<E> => {
99
return async ({ id, entity, filter = {} }) => {
1010
const table = (await config.db()).table(config.tableName);
11+
const query = config.constructQuery(table);
1112
const document = config.constructDocument({ ...entity as any, id });
1213
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));
1415
if (!res) {
1516
throw new MissingEntityError(config.entityName, id);
1617
}

0 commit comments

Comments
 (0)