Skip to content

Update knex #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 41 additions & 32 deletions definitions/npm/knex_v0.12.x/flow_v0.38.x-/knex_v0.12.x.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
declare class Knex$Transaction mixins Knex$QueryBuilder, events$EventEmitter, Promise {
declare class Knex$Migrate {
latest(): Promise<any>,
rollback(): Promise<any>
}

declare type Knex$TransactionBody<T> = (tx:Knex$Transaction<T>) => Promise<T>;
declare class Knex$Transaction<T> mixins Knex$QueryBuilder<T>, events$EventEmitter, Promise {
commit(connection?: any, value?: any): Promise<any>,
rollback(): Promise<any>,
savepoint(connection?: any): Promise<any>
}

declare type Knex$QueryBuilderFn = (
qb: Knex$QueryBuilder
) => Knex$QueryBuilder | void;
declare type Knex$QueryBuilderFn<T> = (
qb: Knex$QueryBuilder<T>
) => Knex$QueryBuilder<T> | void;

declare class Knex$QueryBuilder mixins Promise {
declare class Knex$QueryBuilder<T> mixins Promise<T> {
select(key?: string[]): this,
select(...key: string[]): this,
timeout(ms: number, options?: { cancel: boolean }): this,
column(key: string[]): this,
column(...key: string[]): this,
with(alias: string, w: string | Knex$QueryBuilderFn): this,
with(alias: string, w: string | Knex$QueryBuilderFn<T>): this,
withSchema(schema: string): this,
returning(column: string): this,
returning(...columns: string[]): this,
returning(columns: string[]): this,
as(name: string): this,
transacting(trx: ?Knex$Transaction): this,
where(builder: Knex$QueryBuilderFn): this,
transacting(trx: ?Knex$Transaction<T>): this,
where(builder: Knex$QueryBuilderFn<T>): this,
where(column: string, value: any): this,
where(column: string, operator: string, value: any): this,
whereNot(builder: Knex$QueryBuilderFn): this,
where(conditions: {[column: string]: any}): this,
whereNot(builder: Knex$QueryBuilderFn<T>): this,
whereNot(column: string, value: any): this,
whereNot(column: string, operator: string, value: any): this,
whereIn(column: string, values: any[]): this,
Expand All @@ -36,10 +43,10 @@ declare class Knex$QueryBuilder mixins Promise {
whereBetween(column: string, range: number[]): this,
whereNotBetween(column: string, range: number[]): this,
whereRaw(sql: string, bindings?: Knex$RawBindings): this,
orWhere(builder: Knex$QueryBuilderFn): this,
orWhere(builder: Knex$QueryBuilderFn<T>): this,
orWhere(column: string, value: any): this,
orWhere(column: string, operator: string, value: any): this,
orWhereNot(builder: Knex$QueryBuilderFn): this,
orWhereNot(builder: Knex$QueryBuilderFn<T>): this,
orWhereNot(column: string, value: any): this,
orWhereNot(column: string, operator: string, value: any): this,
orWhereIn(column: string, values: any[]): this,
Expand All @@ -53,30 +60,30 @@ declare class Knex$QueryBuilder mixins Promise {
orWhereRaw(sql: string, bindings?: Knex$RawBindings): this,
innerJoin(table: string, c1: string, operator: string, c2: string): this,
innerJoin(table: string, c1: string, c2: string): this,
innerJoin(builder: Knex$QueryBuilder | Knex$QueryBuilderFn, c1?: string, c2?: string): this,
innerJoin(table: string, builder: Knex$QueryBuilderFn): this,
innerJoin(builder: Knex$QueryBuilder<T>, c1?: string, c2?: string): this,
innerJoin(table: string, builder: Knex$QueryBuilderFn<T>): this,
leftJoin(table: string, c1: string, operator: string, c2: string): this,
leftJoin(table: string, c1: string, c2: string): this,
leftJoin(builder: Knex$QueryBuilder): this,
leftJoin(table: string, builder: Knex$QueryBuilderFn): this,
leftJoin(builder: Knex$QueryBuilder<T>): this,
leftJoin(table: string, builder: Knex$QueryBuilderFn<T>): this,
leftOuterJoin(table: string, c1: string, operator: string, c2: string): this,
leftOuterJoin(table: string, c1: string, c2: string): this,
leftOuterJoin(table: string, builder: Knex$QueryBuilderFn): this,
leftOuterJoin(table: string, builder: Knex$QueryBuilderFn<T>): this,
rightJoin(table: string, c1: string, operator: string, c2: string): this,
rightJoin(table: string, c1: string, c2: string): this,
rightJoin(table: string, builder: Knex$QueryBuilderFn): this,
rightJoin(table: string, builder: Knex$QueryBuilderFn<T>): this,
rightOuterJoin(table: string, c1: string, operator: string, c2: string): this,
rightOuterJoin(table: string, c1: string, c2: string): this,
rightOuterJoin(table: string, builder: Knex$QueryBuilderFn): this,
rightOuterJoin(table: string, builder: Knex$QueryBuilderFn<T>): this,
outerJoin(table: string, c1: string, operator: string, c2: string): this,
outerJoin(table: string, c1: string, c2: string): this,
outerJoin(table: string, builder: Knex$QueryBuilderFn): this,
outerJoin(table: string, builder: Knex$QueryBuilderFn<T>): this,
fullOuterJoin(table: string, c1: string, operator: string, c2: string): this,
fullOuterJoin(table: string, c1: string, c2: string): this,
fullOuterJoin(table: string, builder: Knex$QueryBuilderFn): this,
fullOuterJoin(table: string, builder: Knex$QueryBuilderFn<T>): this,
crossJoin(column: string, c1: string, c2: string): this,
crossJoin(column: string, c1: string, operator: string, c2: string): this,
crossJoin(table: string, builder: Knex$QueryBuilderFn): this,
crossJoin(table: string, builder: Knex$QueryBuilderFn<T>): this,
joinRaw(sql: string, bindings?: Knex$RawBindings): this,
distinct(): this,
groupBy(column: string): this,
Expand All @@ -90,8 +97,8 @@ declare class Knex$QueryBuilder mixins Promise {
havingNotIn(column: string, values: Array<mixed>): this,
havingNull(column: string): this,
havingNotNull(column: string): this,
havingExists(builder: Knex$QueryBuilderFn | Knex$QueryBuilder): this,
havingNotExists(builder: Knex$QueryBuilderFn | Knex$QueryBuilder): this,
havingExists(builder: Knex$QueryBuilderFn<T> | Knex$QueryBuilder<T>): this,
havingNotExists(builder: Knex$QueryBuilderFn<T> | Knex$QueryBuilder<T>): this,
havingBetween<T>(column: string, range: [T, T]): this,
havingNotBetween<T>(column: string, range: [T, T]): this,
havingRaw(column: string, operator: string, value: mixed): this,
Expand All @@ -110,23 +117,24 @@ declare class Knex$QueryBuilder mixins Promise {
pluck(column: string): this,
first(): this,
from(table: string): this,
from(builder: Knex$QueryBuilderFn | Knex$Knex | Knex$QueryBuilder): this,

insert(val: Object | Object[]): this,
from(builder: Knex$QueryBuilderFn<T> | Knex$Knex<T> | Knex$QueryBuilder<T>): this,
insert(val: Object | Object[], returning?: string|Array<string>): this,
del(): this,
delete(): this,
update(column: string, value: any): this,
update(val: Object): this,
update(val: Object, returning?: string|Array<string>): this,
returning(columns: string[]): this
}

declare class Knex$Knex mixins Knex$QueryBuilder, Promise, events$EventEmitter {
static (config: Knex$Config): Knex$Knex,
declare class Knex$Knex<T> mixins Knex$QueryBuilder<T>, Promise, events$EventEmitter {
static (config: Knex$Config): Knex$Knex<T>,
Copy link

@briancavalier briancavalier Jul 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, this all seems very tricky. There are at least a couple things I think we need to consider:

  1. We don't want to over-constrain the type parameter of Knex$Knex, since we'd like it to represent the whole database, which could contain many tables (yelp-gil-service's db is an outlier with only 1 table).
  2. Joins seem especially tricky, since they are a single operation, e.g. innerJoin that involves at least 2 types--one for each table involved in the join. They may even involve 3 types--one for each table, plus one for the result type if the two tables are joined in a projection (i.e. a subset of fields from each table). Sometimes that 3rd type might be a proper intersection of the two other types, but not always.

I have some ideas, but nothing concrete yet ... let's talk about it tomorrow if we have time.

static QueryBuilder: typeof Knex$QueryBuilder,
$call: (tableName: string) => Knex$QueryBuilder,
$call: (tableName: string) => Knex$QueryBuilder<T>,
raw(sqlString: string, bindings?: Knex$RawBindings): any,
client: any,
destroy(): Promise<void>
destroy(): Promise<void>,
transaction(run:Knex$TransactionBody<T>): Knex$Transaction<T>,
migrate: Knex$Migrate
}

declare type Knex$PostgresConfig = {
Expand Down Expand Up @@ -184,6 +192,7 @@ declare module 'knex' {
line: string,
routine: string
};
declare type $QueryBuilder = Knex$QueryBuilder;
declare export type $Transaction<T> = Knex$Transaction<T>;
declare export type $QueryBuilder<T> = Knex$QueryBuilder<T>;
declare var exports: typeof Knex$Knex;
}
9 changes: 9 additions & 0 deletions definitions/npm/knex_v0.12.x/test_knex-v0.12.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ knex.crossJoin('bar', function() {
knex('foo').insert({
a: 1,
});
knex('foo').insert({a: 1}, 'name');

const q = knex('foo')
.update({a: 1})
.where({b: 2});
knex('foo')
.update({a: 1}, 'c')
.where({b: 2});

knex('bar').del();
// $ExpectError
knex.from();
Expand Down