Skip to content

Commit

Permalink
✨ Expose Op and Cond under a util prop on Database
Browse files Browse the repository at this point in the history
  • Loading branch information
s0 committed Dec 2, 2021
1 parent 2de08cf commit 7f98dc4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/db/fetching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type InstanceOf<T extends Database[keyof Database]> = T extends Model<any>
* database, otherwise an error will be thrown
*/
export const findAndOrganizeObjectsByUniqueProperty = <
Table extends Database[keyof Database],
Table extends Database[keyof Omit<Database, 'util'>],
P extends keyof InstanceOf<Table>
>(
table: Table,
Expand All @@ -47,7 +47,7 @@ export const findAndOrganizeObjectsByUniqueProperty = <
* and organize them into an AnnotatedMap by a unique value.
*/
export const findAndOrganizeObjectsByUniqueValue = async <
Table extends Database[keyof Database],
Table extends Database[keyof Omit<Database, 'util'>],
V
>(
table: Table,
Expand Down
12 changes: 12 additions & 0 deletions src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,20 @@ import unitType from './models/unitType';
import usageYear from './models/usageYear';
import workflowStatusOption from './models/workflowStatusOption';
import workflowStatusOptionStep from './models/workflowStatusOptionStep';
import { Op, Cond } from './util/conditions';

export default (conn: Knex) => ({
util: {
/**
* Define `_internals` so that it is present on every object in the Database
* to make iterating over tables easier
*/
_internals: {
type: 'util',
} as const,
Op,
Cond,
} as const,
attachment: attachment(conn),
attachmentPrototype: attachmentPrototype(conn),
attachmentVersion: attachmentVersion(conn),
Expand Down
4 changes: 2 additions & 2 deletions src/db/util/conditions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Knex = require('knex');
/**
* Symbols to use for where condition construction
*/
namespace ConditionSymbols {
export namespace ConditionSymbols {
export const BUILDER = Symbol('builder');
export const AND = Symbol('and');
export const OR = Symbol('or');
Expand All @@ -26,7 +26,7 @@ export const Cond = ConditionSymbols.Cond;
/**
* Symbols to use when constructing conditions for a single property
*/
namespace PropertySymbols {
export namespace PropertySymbols {
export const IN = Symbol('in');
export const NOT_IN = Symbol('not in');

Expand Down

0 comments on commit 7f98dc4

Please sign in to comment.