diff --git a/src/Builder.ts b/src/Builder.ts index 3a7d4f7..94de991 100644 --- a/src/Builder.ts +++ b/src/Builder.ts @@ -3,19 +3,27 @@ import { ConflictUpsert, DefaultObject, Delete, - EitherResult, + DeleteReturning, + DeleteWithoutReturning, Insert, + InsertMultiple, + InsertOne, + InsertWithoutReturning, Join, OneResult, RawQuery, + RawQueryFetchAll, + RawQueryFetchOne, + RawQueryWithoutFetching, SelectAll, SelectOne, Update, + UpdateReturning, + UpdateWithoutReturning, Where, } from './interfaces' import { ConflictTypes, FetchTypes, OrderTypes } from './enums' import { Query, Raw } from './tools' -import * as console from 'console' export class QueryBuilder { _debugger = false @@ -81,7 +89,10 @@ export class QueryBuilder { ) } - raw(params: RawQuery): Query> { + raw(params: RawQueryFetchOne): Query> + raw(params: RawQueryFetchAll): Query> + raw(params: RawQueryWithoutFetching): Query + raw(params: RawQuery): unknown { return new Query( (q: Query) => { return this.execute(q) @@ -92,7 +103,10 @@ export class QueryBuilder { ) } - insert(params: Insert): Query> { + insert(params: InsertOne): Query> + insert(params: InsertMultiple): Query> + insert(params: InsertWithoutReturning): Query + insert(params: Insert): unknown { let args: any[] = [] if (typeof params.onConflict === 'object') { @@ -132,7 +146,11 @@ export class QueryBuilder { ) } - update(params: Update): Query> { + update( + params: UpdateReturning + ): Query> + update(params: UpdateWithoutReturning): Query + update(params: Update): unknown { let args = this._parse_arguments(params.data) if (typeof params.where === 'object' && !Array.isArray(params.where) && params.where?.params) { @@ -149,7 +167,11 @@ export class QueryBuilder { ) } - delete(params: Delete): Query> { + delete( + params: DeleteReturning + ): Query> + delete(params: DeleteWithoutReturning): Query + delete(params: Delete): unknown { return new Query( (q: Query) => { return this.execute(q) diff --git a/src/interfaces.ts b/src/interfaces.ts index 97a6230..21ac349 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -1,6 +1,7 @@ import { ConflictTypes, FetchTypes, JoinTypes, OrderTypes } from './enums' import { Raw } from './tools' import { IsEqual, Merge, Primitive, Simplify } from './typefest' +import Any = jasmine.Any export type DefaultObject = Record @@ -37,6 +38,16 @@ export type RawQuery = { fetchType?: FetchTypes } +export type RawQueryFetchOne = Omit & { + fetchType: FetchTypes.ONE +} + +export type RawQueryFetchAll = Omit & { + fetchType: FetchTypes.ALL +} + +export type RawQueryWithoutFetching = Omit + export type SelectAll = SelectOne & { limit?: number } @@ -56,6 +67,20 @@ export type Insert = { onConflict?: string | ConflictTypes | ConflictUpsert } +export type InsertOne = Omit & { + data: Record + returning: string | Array +} + +export type InsertMultiple = Omit & { + data: Array> + returning: string | Array +} + +export type InsertWithoutReturning = Omit + +export type test = I + export type Update = { tableName: string data: Record @@ -64,12 +89,22 @@ export type Update = { onConflict?: string | ConflictTypes } +export type UpdateReturning = Omit & { + returning: string | Array +} +export type UpdateWithoutReturning = Omit + export type Delete = { tableName: string where: Where // This field is optional, but is kept required in type to warn users of delete without where returning?: string | Array } +export type DeleteReturning = Omit & { + returning: string | Array +} +export type DeleteWithoutReturning = Omit + export type D1Result = { changes?: number duration: number @@ -86,7 +121,6 @@ export type PGResult = { export type ArrayResult = Merge }> export type OneResult = Merge -export type EitherResult = Merge | Result }> // Types bellow are WIP to improve even more type hints for raw and insert queries export type GetFetchValue = T extends { fetchType?: infer U } ? U : never