-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(linting): Enabled tslint and fixed errors
- Loading branch information
Jake Ginnivan
committed
Dec 31, 2018
1 parent
9cf6527
commit 6a76def
Showing
8 changed files
with
206 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import * as Knex from 'knex' | ||
|
||
import { Tables, QueryWrapper, TableNames, Query, ExecuteResult } from '.' | ||
|
||
export class QueryExecutor< | ||
TTableNames extends string, | ||
Services extends object | ||
> { | ||
protected tables: Tables<TTableNames> | ||
private wrap: QueryWrapper | ||
|
||
constructor( | ||
public kind: 'read-query-executor' | 'unit-of-work-query-executor', | ||
protected knex: Knex | Knex.Transaction, | ||
protected services: Services, | ||
protected tableNames: TableNames<TTableNames>, | ||
wrapQuery?: QueryWrapper | ||
) { | ||
this.wrap = wrapQuery || ((b: any) => b) | ||
|
||
this.tables = Object.keys(tableNames).reduce<any>((acc, tableName) => { | ||
acc[tableName] = () => this.wrap(knex(tableName)) | ||
|
||
return acc | ||
}, {}) | ||
} | ||
|
||
/** Helper to create type safe queries */ | ||
createQuery<QueryArguments, QueryResult>( | ||
query: Query<QueryArguments, QueryResult, TTableNames, Services> | ||
): Query<QueryArguments, QueryResult, TTableNames, Services> { | ||
return query | ||
} | ||
|
||
execute<Args, Result>( | ||
query: Query<Args, Result, TTableNames, Services> | ||
): ExecuteResult<Args, Result> { | ||
return { | ||
withArgs: async args => | ||
query({ | ||
query: createQuery => | ||
this.wrap(createQuery(this.knex) as any), | ||
queryExecutor: this, | ||
wrapQuery: (builder: Knex.QueryBuilder) => | ||
this.wrap(builder), | ||
tables: this.tables, | ||
args, | ||
tableNames: this.tableNames, | ||
...this.services | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import * as Knex from 'knex' | ||
import { QueryExecutor, TableNames } from '.' | ||
import { UnitOfWorkQueryExecutor } from './unit-of-work-query-executor' | ||
|
||
export class ReadQueryExecutor< | ||
TTableNames extends string, | ||
Services extends object | ||
> extends QueryExecutor<TTableNames, Services> { | ||
kind!: 'read-query-executor' | ||
|
||
constructor( | ||
knex: Knex, | ||
services: Services, | ||
tableNames: TableNames<TTableNames> | ||
) { | ||
super('read-query-executor', knex, services, tableNames) | ||
} | ||
|
||
/** | ||
* Executes some work inside a transaction | ||
* @param work a callback which contains the unit to be executed | ||
* The transaction will be commited if promise resolves, rolled back if rejected | ||
* @example executor.unitOfWork(unit => unit.executeQuery(insertBlah, blah)) | ||
*/ | ||
unitOfWork<T>( | ||
work: ( | ||
executor: UnitOfWorkQueryExecutor<TTableNames, Services> | ||
) => Promise<T> | ||
): PromiseLike<any> { | ||
return this.knex.transaction(trx => { | ||
// knex is aware of promises, and will automatically commit | ||
// or reject based on this callback promise | ||
return work( | ||
new UnitOfWorkQueryExecutor(trx, this.services, this.tableNames) | ||
) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as Knex from 'knex' | ||
import { TableNames, QueryExecutor } from '.' | ||
|
||
export class UnitOfWorkQueryExecutor< | ||
TTableNames extends string, | ||
Services extends object | ||
> extends QueryExecutor<TTableNames, Services> { | ||
// Unit of work executor can be used as a normal query executor | ||
kind!: 'unit-of-work-query-executor' | ||
|
||
constructor( | ||
protected knex: Knex.Transaction, | ||
services: Services, | ||
tableNames: TableNames<TTableNames> | ||
) { | ||
super('unit-of-work-query-executor', knex, services, tableNames) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": ["tslint:latest", "tslint-eslint-rules", "tslint-config-prettier"], | ||
"rules": { | ||
"ordered-imports": false, | ||
"member-ordering": false, | ||
"object-literal-sort-keys": false, | ||
"member-access": false, | ||
"interface-name": false | ||
} | ||
} |
Oops, something went wrong.