Skip to content

Commit

Permalink
Add support for bigint
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym committed Jun 15, 2024
1 parent 5c1cc8f commit 854e192
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 54 deletions.
58 changes: 38 additions & 20 deletions src/Builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ArrayResult,
ConflictUpsert,
DefaultObject,
DefaultReturnObject,
Delete,
DeleteReturning,
DeleteWithoutReturning,
Expand Down Expand Up @@ -63,7 +64,9 @@ export class QueryBuilder<GenericResultWrapper> {
}, `DROP TABLE ${params.ifExists ? 'IF EXISTS' : ''} ${params.tableName}`)
}

fetchOne<GenericResult = DefaultObject>(params: SelectOne): Query<OneResult<GenericResultWrapper, GenericResult>> {
fetchOne<GenericResult = DefaultReturnObject>(
params: SelectOne
): Query<OneResult<GenericResultWrapper, GenericResult>> {
return new Query(
(q: Query) => {
return this.execute(q)
Expand All @@ -76,7 +79,9 @@ export class QueryBuilder<GenericResultWrapper> {
)
}

fetchAll<GenericResult = DefaultObject>(params: SelectAll): Query<ArrayResult<GenericResultWrapper, GenericResult>> {
fetchAll<GenericResult = DefaultReturnObject>(
params: SelectAll
): Query<ArrayResult<GenericResultWrapper, GenericResult>> {
return new Query(
(q: Query) => {
return this.execute(q)
Expand All @@ -89,10 +94,14 @@ export class QueryBuilder<GenericResultWrapper> {
)
}

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 {
raw<GenericResult = DefaultReturnObject>(
params: RawQueryFetchOne
): Query<OneResult<GenericResultWrapper, GenericResult>>
raw<GenericResult = DefaultReturnObject>(
params: RawQueryFetchAll
): Query<ArrayResult<GenericResultWrapper, GenericResult>>
raw<GenericResult = DefaultReturnObject>(params: RawQueryWithoutFetching): Query<GenericResultWrapper>
raw<GenericResult = DefaultReturnObject>(params: RawQuery): unknown {
return new Query(
(q: Query) => {
return this.execute(q)
Expand All @@ -103,10 +112,12 @@ export class QueryBuilder<GenericResultWrapper> {
)
}

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 {
insert<GenericResult = DefaultReturnObject>(params: InsertOne): Query<OneResult<GenericResultWrapper, GenericResult>>
insert<GenericResult = DefaultReturnObject>(
params: InsertMultiple
): Query<ArrayResult<GenericResultWrapper, GenericResult>>
insert<GenericResult = DefaultReturnObject>(params: InsertWithoutReturning): Query<GenericResultWrapper>
insert<GenericResult = DefaultReturnObject>(params: Insert): unknown {
let args: any[] = []

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

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

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

delete<GenericResult = DefaultObject>(
delete<GenericResult = DefaultReturnObject>(
params: DeleteReturning
): Query<ArrayResult<GenericResultWrapper, GenericResult>>
delete<GenericResult = DefaultObject>(params: DeleteWithoutReturning): Query<GenericResultWrapper>
delete<GenericResult = DefaultObject>(params: Delete): unknown {
delete<GenericResult = DefaultReturnObject>(params: DeleteWithoutReturning): Query<GenericResultWrapper>
delete<GenericResult = DefaultReturnObject>(params: Delete): unknown {
return new Query(
(q: Query) => {
return this.execute(q)
Expand All @@ -184,7 +195,7 @@ export class QueryBuilder<GenericResultWrapper> {
)
}

_parse_arguments(row: Record<string, string | boolean | number | null | Raw>): Array<any> {
_parse_arguments(row: DefaultObject): Array<any> {
// Raw parameters are placed directly in the query, and keeping them here would result in more parameters that are
// expected in the query and could result in weird results or outright errors when using PostgreSQL
return Object.values(row).filter((value) => {
Expand Down Expand Up @@ -216,11 +227,18 @@ export class QueryBuilder<GenericResultWrapper> {
_insert(params: Insert): string {
const rows = []

let data: Array<DefaultObject>
if (!Array.isArray(params.data)) {
params.data = [params.data]
data = [params.data]
} else {
data = params.data
}

if (!data || !data[0] || data.length === 0) {
throw new Error('Insert data is undefined')
}

const columns = Object.keys(params.data[0]).join(', ')
const columns = Object.keys(data[0]).join(', ')
let index = 1

let orConflict = '',
Expand All @@ -243,7 +261,7 @@ export class QueryBuilder<GenericResultWrapper> {
orConflict = this._onConflict(params.onConflict)
}

for (const row of params.data) {
for (const row of data) {
const values: Array<string> = []
Object.values(row).forEach((value) => {
if (value instanceof Raw) {
Expand Down
4 changes: 2 additions & 2 deletions src/databases/d1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ export class D1QB extends QueryBuilder<D1Result> {
},
i: number
) => {
if (queryArray[i]) {
if (queryArray && queryArray[i] !== undefined && queryArray[i]?.fetchType) {
return {
changes: resp.meta?.changes,
duration: resp.meta?.duration,
last_row_id: resp.meta?.last_row_id,
served_by: resp.meta?.served_by,
success: resp.success,
results: queryArray[i].fetchType === FetchTypes.ONE ? resp.results?.[0] : resp.results,
results: queryArray[i]?.fetchType === FetchTypes.ONE ? resp.results?.[0] : resp.results,
}
} else {
return {
Expand Down
38 changes: 12 additions & 26 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { ConflictTypes, FetchTypes, JoinTypes, OrderTypes } from './enums'
import { Raw } from './tools'
import { IsEqual, Merge, Primitive, Simplify } from './typefest'
import Any = jasmine.Any
import { Merge } from './typefest'

export type Primitive = null | string | number | boolean | bigint | Raw

export type DefaultObject = Record<string, Primitive>
export type DefaultReturnObject = Record<string, null | string | number | boolean | bigint>

export type Where =
| {
conditions: string | Array<string>
// TODO: enable named parameters Record<string, string | boolean | number | null>
params?: (string | boolean | number | null | Raw)[]
// TODO: enable named parameters with DefaultObject
params?: Primitive[]
}
| string
| Array<string>
Expand All @@ -34,7 +36,7 @@ export type SelectOne = {

export type RawQuery = {
query: string
args?: (string | number | boolean | null | Raw)[]
args?: Primitive[]
fetchType?: FetchTypes
}

Expand All @@ -54,26 +56,24 @@ export type SelectAll = SelectOne & {

export type ConflictUpsert = {
column: string | Array<string>
data: Record<string, string | boolean | number | null | Raw>
data: DefaultObject
where?: Where
}

export type Insert = {
tableName: string
data:
| Record<string, string | boolean | number | null | Raw>
| Array<Record<string, string | boolean | number | null | Raw>>
data: DefaultObject | Array<DefaultObject>
returning?: string | Array<string>
onConflict?: string | ConflictTypes | ConflictUpsert
}

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

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

Expand All @@ -83,7 +83,7 @@ export type test<I extends Insert = Insert> = I

export type Update = {
tableName: string
data: Record<string, string | boolean | number | null | Raw>
data: DefaultObject
where?: Where
returning?: string | Array<string>
onConflict?: string | ConflictTypes
Expand Down Expand Up @@ -121,17 +121,3 @@ export type PGResult = {

export type ArrayResult<ResultWrapper, Result> = Merge<ResultWrapper, { results?: Array<Result> }>
export type OneResult<ResultWrapper, Result> = Merge<ResultWrapper, { results?: 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
export type SwitchFetch<P, A, B> = IsEqual<P, FetchTypes.ALL> extends true
? ArrayResult<A, B>
: IsEqual<P, FetchTypes.ONE> extends true
? OneResult<A, B>
: A

export type FindResult<P, A, B> = Simplify<SwitchFetch<GetFetchValue<P>, A, B>>

// raw<GenericResult=DefaultObject, P extends RawQuery = RawQuery>(params: P): Query<FindResult<P, GenericResultWrapper, GenericResult>> {
//
// }
5 changes: 3 additions & 2 deletions src/tools.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FetchTypes } from './enums'
import { Primitive } from './interfaces'

export class Raw {
public isRaw = true
Expand All @@ -11,13 +12,13 @@ export class Raw {
export class Query<Result = any> {
executeMethod: (query: Query<Result>) => Promise<Result>
public query: string
public arguments?: (string | number | boolean | null | Raw)[]
public arguments?: Primitive[]
public fetchType?: FetchTypes

constructor(
executeMethod: (query: Query<Result>) => Promise<Result>,
query: string,
args?: (string | number | boolean | null | Raw)[],
args?: Primitive[],
fetchType?: FetchTypes
) {
this.executeMethod = executeMethod
Expand Down
4 changes: 0 additions & 4 deletions src/typefest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Code taken from type-fest

export type Primitive = null | undefined | string | number | boolean | symbol | bigint

export type OmitIndexSignature<ObjectType> = {
[KeyType in keyof ObjectType as {} extends Record<KeyType, unknown> ? never : KeyType]: ObjectType[KeyType]
}
Expand All @@ -20,5 +18,3 @@ export type Merge<Destination, Source> = Simplify<
SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>> &
SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
>

export type IsEqual<A, B> = (<G>() => G extends A ? 1 : 2) extends <G>() => G extends B ? 1 : 2 ? true : false

0 comments on commit 854e192

Please sign in to comment.