Skip to content

Commit

Permalink
Fix return types for insert, update, delete and raw queries
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym committed Jun 1, 2024
1 parent 1c95bf1 commit f61a618
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
34 changes: 28 additions & 6 deletions src/Builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<GenericResultWrapper> {
_debugger = false
Expand Down Expand Up @@ -81,7 +89,10 @@ export class QueryBuilder<GenericResultWrapper> {
)
}

raw<GenericResult = DefaultObject>(params: RawQuery): Query<EitherResult<GenericResultWrapper, GenericResult>> {
raw<GenericResult = DefaultObject>(params: RawQueryFetchOne): Query<OneResult<GenericResultWrapper, GenericResult>>
raw<GenericResult = DefaultObject>(params: RawQueryFetchAll): Query<ArrayResult<GenericResultWrapper, GenericResult>>
raw<GenericResult = DefaultObject>(params: RawQueryWithoutFetching): Query<GenericResultWrapper>
raw<GenericResult = DefaultObject>(params: RawQuery): unknown {
return new Query(
(q: Query) => {
return this.execute(q)
Expand All @@ -92,7 +103,10 @@ export class QueryBuilder<GenericResultWrapper> {
)
}

insert<GenericResult = DefaultObject>(params: Insert): Query<EitherResult<GenericResultWrapper, GenericResult>> {
insert<GenericResult = DefaultObject>(params: InsertOne): Query<OneResult<GenericResultWrapper, GenericResult>>
insert<GenericResult = DefaultObject>(params: InsertMultiple): Query<ArrayResult<GenericResultWrapper, GenericResult>>
insert<GenericResult = DefaultObject>(params: InsertWithoutReturning): Query<GenericResultWrapper>
insert<GenericResult = DefaultObject>(params: Insert): unknown {
let args: any[] = []

if (typeof params.onConflict === 'object') {
Expand Down Expand Up @@ -132,7 +146,11 @@ export class QueryBuilder<GenericResultWrapper> {
)
}

update<GenericResult = DefaultObject>(params: Update): Query<ArrayResult<GenericResultWrapper, GenericResult>> {
update<GenericResult = DefaultObject>(
params: UpdateReturning
): Query<ArrayResult<GenericResultWrapper, GenericResult>>
update<GenericResult = DefaultObject>(params: UpdateWithoutReturning): Query<GenericResultWrapper>
update<GenericResult = DefaultObject>(params: Update): unknown {
let args = this._parse_arguments(params.data)

if (typeof params.where === 'object' && !Array.isArray(params.where) && params.where?.params) {
Expand All @@ -149,7 +167,11 @@ export class QueryBuilder<GenericResultWrapper> {
)
}

delete<GenericResult = DefaultObject>(params: Delete): Query<ArrayResult<GenericResultWrapper, GenericResult>> {
delete<GenericResult = DefaultObject>(
params: DeleteReturning
): Query<ArrayResult<GenericResultWrapper, GenericResult>>
delete<GenericResult = DefaultObject>(params: DeleteWithoutReturning): Query<GenericResultWrapper>
delete<GenericResult = DefaultObject>(params: Delete): unknown {
return new Query(
(q: Query) => {
return this.execute(q)
Expand Down
36 changes: 35 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -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<string, Primitive>

Expand Down Expand Up @@ -37,6 +38,16 @@ export type RawQuery = {
fetchType?: FetchTypes
}

export type RawQueryFetchOne = Omit<RawQuery, 'fetchType'> & {
fetchType: FetchTypes.ONE
}

export type RawQueryFetchAll = Omit<RawQuery, 'fetchType'> & {
fetchType: FetchTypes.ALL
}

export type RawQueryWithoutFetching = Omit<RawQuery, 'fetchType'>

export type SelectAll = SelectOne & {
limit?: number
}
Expand All @@ -56,6 +67,20 @@ export type Insert = {
onConflict?: string | ConflictTypes | ConflictUpsert
}

export type InsertOne = Omit<Insert, 'data' | 'returning'> & {
data: Record<string, string | boolean | number | null | Raw>
returning: string | Array<string>
}

export type InsertMultiple = Omit<Insert, 'data' | 'returning'> & {
data: Array<Record<string, string | boolean | number | null | Raw>>
returning: string | Array<string>
}

export type InsertWithoutReturning = Omit<Insert, 'returning'>

export type test<I extends Insert = Insert> = I

export type Update = {
tableName: string
data: Record<string, string | boolean | number | null | Raw>
Expand All @@ -64,12 +89,22 @@ export type Update = {
onConflict?: string | ConflictTypes
}

export type UpdateReturning = Omit<Update, 'returning'> & {
returning: string | Array<string>
}
export type UpdateWithoutReturning = Omit<Update, 'returning'>

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<string>
}

export type DeleteReturning = Omit<Delete, 'returning'> & {
returning: string | Array<string>
}
export type DeleteWithoutReturning = Omit<Delete, 'returning'>

export type D1Result = {
changes?: number
duration: number
Expand All @@ -86,7 +121,6 @@ export type PGResult = {

export type ArrayResult<ResultWrapper, Result> = Merge<ResultWrapper, { results?: Array<Result> }>
export type OneResult<ResultWrapper, Result> = Merge<ResultWrapper, { results?: Result }>
export type EitherResult<ResultWrapper, Result> = Merge<ResultWrapper, { results?: Array<Result> | Result }>

// Types bellow are WIP to improve even more type hints for raw and insert queries
export type GetFetchValue<T> = T extends { fetchType?: infer U } ? U : never
Expand Down

0 comments on commit f61a618

Please sign in to comment.