Skip to content

Commit

Permalink
refactor: remove executable trait in favor of executeQuery helper
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jan 12, 2020
1 parent aa38b6e commit cb8067c
Show file tree
Hide file tree
Showing 29 changed files with 759 additions and 638 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ MYSQL_PORT=3306
MYSQL_USER=virk
MYSQL_PASSWORD=password

PG_HOST=pg
PG_HOST=0.0.0.0
PG_PORT=5432
PG_USER=virk
PG_USER=relay
PG_PASSWORD=password
35 changes: 12 additions & 23 deletions adonis-typings/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,6 @@ declare module '@ioc:Adonis/Lucid/Database' {
ModelQueryBuilderContract,
} from '@ioc:Adonis/Lucid/Model'

/**
* A executable query builder will always have these methods on it.
*/
export interface ExcutableQueryBuilderContract<Result extends any> extends Promise<Result> {
debug (debug: boolean): this
timeout (time: number, options?: { cancel: boolean }): this
useTransaction (trx: TransactionClientContract): this
toQuery (): string
exec (): Promise<Result>
toSQL (): knex.Sql
}

/**
* Dialect specfic methods
*/
Expand Down Expand Up @@ -98,7 +86,10 @@ declare module '@ioc:Adonis/Lucid/Database' {
/**
* Returns the query builder for a given model
*/
modelQuery<T extends ModelConstructorContract> (model: T): ModelQueryBuilderContract<T>
modelQuery<
T extends ModelConstructorContract,
Result extends any = T,
> (model: T): ModelQueryBuilderContract<T>

/**
* Returns the knex query builder instance
Expand All @@ -109,22 +100,20 @@ declare module '@ioc:Adonis/Lucid/Database' {
* Get new query builder instance for select, update and
* delete calls
*/
query<Result extends any = any> (
): DatabaseQueryBuilderContract<Result> & ExcutableQueryBuilderContract<Result>,
query<Result extends any = any> (): DatabaseQueryBuilderContract<Result>,

/**
* Get new query builder instance inserts
*/
insertQuery<ReturnColumns extends any = any> (
): InsertQueryBuilderContract & ExcutableQueryBuilderContract<ReturnColumns[]>,
insertQuery<ReturnColumns extends any = any> (): InsertQueryBuilderContract<ReturnColumns[]>,

/**
* Get raw query builder instance
*/
raw<Result extends any = any> (
sql: string,
bindings?: { [key: string]: StrictValuesWithoutRaw } | StrictValuesWithoutRaw,
): RawContract & ExcutableQueryBuilderContract<Result>
): RawContract<Result>

/**
* Truncate a given table
Expand All @@ -142,14 +131,14 @@ declare module '@ioc:Adonis/Lucid/Database' {
* doesn't allow defining the return type and one must use `query` to define
* that.
*/
from: SelectTable<DatabaseQueryBuilderContract & ExcutableQueryBuilderContract<any>>,
from: SelectTable<DatabaseQueryBuilderContract<any>>,

/**
* Same as `insertQuery()`, but also selects the table for the query.
* The `table` method doesn't allow defining the return type and
* one must use `insertQuery` to define that.
*/
table: Table<InsertQueryBuilderContract & ExcutableQueryBuilderContract<any>>,
table: Table<InsertQueryBuilderContract<any>>,

/**
* Get instance of transaction client
Expand Down Expand Up @@ -625,14 +614,14 @@ declare module '@ioc:Adonis/Lucid/Database' {
*/
query<Result extends any = any> (
options?: DatabaseClientOptions,
): DatabaseQueryBuilderContract<Result> & ExcutableQueryBuilderContract<Result>,
): DatabaseQueryBuilderContract<Result>,

/**
* Get insert query builder instance for a given connection.
*/
insertQuery<ReturnColumns extends any = any> (
options?: DatabaseClientOptions,
): InsertQueryBuilderContract & ExcutableQueryBuilderContract<ReturnColumns[]>,
): InsertQueryBuilderContract<ReturnColumns[]>,

/**
* Get raw query builder instance
Expand All @@ -641,7 +630,7 @@ declare module '@ioc:Adonis/Lucid/Database' {
sql: string,
bindings?: { [key: string]: StrictValuesWithoutRaw } | StrictValuesWithoutRaw[],
options?: DatabaseClientOptions,
): RawContract & ExcutableQueryBuilderContract<Result>
): RawContract<Result>

/**
* Selects a table on the default connection by instantiating a new query
Expand Down
21 changes: 10 additions & 11 deletions adonis-typings/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@
*/

declare module '@ioc:Adonis/Lucid/Model' {
import knex from 'knex'
import { ProfilerContract, ProfilerRowContract } from '@ioc:Adonis/Core/Profiler'
import {
Update,
Counter,
StrictValues,
QueryCallback,
ChainableContract,
ExcutableQueryBuilderContract,
} from '@ioc:Adonis/Lucid/DatabaseQueryBuilder'

import {
QueryClientContract,
TransactionClientContract,
ExcutableQueryBuilderContract,
} from '@ioc:Adonis/Lucid/Database'

import {
Expand Down Expand Up @@ -146,7 +147,7 @@ declare module '@ioc:Adonis/Lucid/Model' {
Model extends ModelConstructorContract,
Result extends any = InstanceType<Model>
>
extends ChainableContract
extends ChainableContract, ExcutableQueryBuilderContract<Result[]>
{
model: Model

Expand All @@ -159,6 +160,7 @@ declare module '@ioc:Adonis/Lucid/Model' {
* Reference to query client used for making queries
*/
client: QueryClientContract
knexQuery: knex.QueryBuilder,

/**
* A custom set of sideloaded properties defined on the query
Expand All @@ -185,10 +187,10 @@ declare module '@ioc:Adonis/Lucid/Model' {
/**
* Mutations (update and increment can be one query aswell)
*/
update: Update<ModelQueryBuilderContract<Model> & ExcutableQueryBuilderContract<number[]>>
increment: Counter<ModelQueryBuilderContract<Model> & ExcutableQueryBuilderContract<number[]>>
decrement: Counter<ModelQueryBuilderContract<Model> & ExcutableQueryBuilderContract<number[]>>
del (): ModelQueryBuilderContract<Model> & ExcutableQueryBuilderContract<number[]>
update: Update<ModelQueryBuilderContract<Model, number>>
increment: Counter<ModelQueryBuilderContract<Model, number>>
decrement: Counter<ModelQueryBuilderContract<Model, number>>
del (): ModelQueryBuilderContract<Model, number>

/**
* Define relationships to be preloaded
Expand Down Expand Up @@ -584,10 +586,7 @@ declare module '@ioc:Adonis/Lucid/Model' {
query<
Model extends ModelConstructorContract,
Result extends any = InstanceType<Model>,
> (
this: Model,
options?: ModelAdapterOptions,
): ModelQueryBuilderContract<Model, Result> & ExcutableQueryBuilderContract<Result[]>
> (this: Model, options?: ModelAdapterOptions): ModelQueryBuilderContract<Model, Result>

new (): Model
}
Expand Down Expand Up @@ -633,7 +632,7 @@ declare module '@ioc:Adonis/Lucid/Model' {
query (
modelConstructor: ModelConstructorContract,
options?: ModelAdapterOptions,
): ModelQueryBuilderContract<ModelConstructorContract> & ExcutableQueryBuilderContract<ModelContract[]>
): ModelQueryBuilderContract<ModelConstructorContract, ModelContract>
}

/**
Expand Down
30 changes: 23 additions & 7 deletions adonis-typings/querybuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
declare module '@ioc:Adonis/Lucid/DatabaseQueryBuilder' {
import * as knex from 'knex'
import { Dictionary } from 'ts-essentials'
import { QueryClientContract, TransactionClientContract } from '@ioc:Adonis/Lucid/Database'
import { ProfilerRowContract, ProfilerContract } from '@ioc:Adonis/Core/Profiler'
import { QueryClientContract } from '@ioc:Adonis/Lucid/Database'

/**
* Get one or many of a generic
Expand Down Expand Up @@ -407,12 +407,12 @@ declare module '@ioc:Adonis/Lucid/DatabaseQueryBuilder' {
* Accepts an array of object of named key/value pair and returns an array
* of Generic return columns.
*/
(values: Dictionary<any, string>): Builder
(values: Dictionary<any, string>, returning?: string | string[]): Builder

/**
* Accepts a key/value pair to update.
*/
(column: string, value: any): Builder
(column: string, value: any, returning?: string | string[]): Builder
}

/**
Expand Down Expand Up @@ -590,9 +590,9 @@ declare module '@ioc:Adonis/Lucid/DatabaseQueryBuilder' {
* Shape of the raw query that can also be passed as a value to
* other queries
*/
interface RawContract {
interface RawContract<Result extends any = any> extends ExcutableQueryBuilderContract<Result> {
knexQuery: knex.Raw,
client: QueryClientContract,
toKnex (): knex.Raw,
wrap (before: string, after: string): this
}

Expand All @@ -602,8 +602,9 @@ declare module '@ioc:Adonis/Lucid/DatabaseQueryBuilder' {
*/
export interface DatabaseQueryBuilderContract <
Result extends any = Dictionary<any, string>,
> extends ChainableContract {
> extends ChainableContract, ExcutableQueryBuilderContract<Result[]> {
client: QueryClientContract,
knexQuery: knex.QueryBuilder,

/**
* Clone current query
Expand All @@ -628,7 +629,10 @@ declare module '@ioc:Adonis/Lucid/DatabaseQueryBuilder' {
/**
* Insert query builder to perform database inserts.
*/
export interface InsertQueryBuilderContract {
export interface InsertQueryBuilderContract<
Result extends any = any,
> extends ExcutableQueryBuilderContract<Result> {
knexQuery: knex.QueryBuilder,
client: QueryClientContract,

/**
Expand All @@ -651,4 +655,16 @@ declare module '@ioc:Adonis/Lucid/DatabaseQueryBuilder' {
*/
multiInsert: MultiInsert<this>
}

/**
* A executable query builder will always have these methods on it.
*/
export interface ExcutableQueryBuilderContract<Result extends any> extends Promise<Result> {
debug (debug: boolean): this
timeout (time: number, options?: { cancel: boolean }): this
useTransaction (trx: TransactionClientContract): this
toQuery (): string
exec (): Promise<Result>
toSQL (): knex.Sql
}
}
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
},
"homepage": "https://github.com/adonisjs/adonis-lucid#readme",
"dependencies": {
"@poppinss/traits": "^1.0.1",
"@poppinss/utils": "^2.1.1",
"camelcase": "^5.3.1",
"columnify": "^1.5.4",
Expand Down
7 changes: 3 additions & 4 deletions src/Connection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ export class Connection extends EventEmitter implements ConnectionContract {
})

if (this.readPool !== this.pool) {
this.logger.trace({ connection: this.name }, 'pool destroyed, cleaning up resource')
this.readPool!.on('poolDestroySuccess', () => {
this.logger.trace({ connection: this.name }, 'pool destroyed, cleaning up resource')
this.cleanup()
this.emit('disconnect', this)
this.removeAllListeners()
Expand Down Expand Up @@ -222,7 +222,7 @@ export class Connection extends EventEmitter implements ConnectionContract {
}

/**
* Creates the write connection
* Creates the write connection.
*/
private setupWriteConnection () {
this.client = knex(Object.assign({ log: new Logger(this.logger) }, this.getWriteConfig()))
Expand All @@ -231,7 +231,7 @@ export class Connection extends EventEmitter implements ConnectionContract {

/**
* Creates the read connection. If there aren't any replicas in use, then
* it will use reference the write client
* it will use the write client instead.
*/
private setupReadConnection () {
if (!this.hasReadWriteReplicas) {
Expand All @@ -240,7 +240,6 @@ export class Connection extends EventEmitter implements ConnectionContract {
}

this.logger.trace({ connection: this.name }, 'setting up read/write replicas')

this.readClient = knex(Object.assign({ log: new Logger(this.logger) }, this.getReadConfig()))
patchKnex(this.readClient, this.readConfigResolver.bind(this))
}
Expand Down
Loading

0 comments on commit cb8067c

Please sign in to comment.