Skip to content

Commit

Permalink
feat: add type-safety and autocompletion (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
mithleshjs authored Jul 24, 2024
1 parent 7d5cf32 commit 9e1b775
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 157 deletions.
23 changes: 12 additions & 11 deletions src/lib/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@ import type {
IncludeQuery,
Relation,
Relations,
Select,
SelectQuery,
SqlParams,
UpdateQuery,
} from "./types.ts";
import { connect, formatValue, quoteIdentifier } from "./util";
import { where } from "./where";

export function buildSelectQuery(
export function buildSelectQuery<T = unknown, R extends string = "none">(
key: string,
query: SelectQuery | IncludeQuery,
query: SelectQuery<T, R> | IncludeQuery<T, R>,
relations: Relations,
isNested = false,
append?: {
where?: string;
join?: string;
},
): string {
const columns = {
const columns: Select = {
...(query.select || {}),
};
// Directly mutate columns to include all keys from query.include
Expand All @@ -36,7 +37,7 @@ export function buildSelectQuery(
const args: SqlParams = {
select: "",
where: query.where,
groupBy: query.groupBy,
groupBy: query.groupBy as never,
having: query.having,
orderBy: query.orderBy,
limit: query.limit,
Expand Down Expand Up @@ -160,31 +161,31 @@ export function buildSelectQuery(
return sql(args, append);
}

export function buildDeleteQuery(
export function buildDeleteQuery<T = unknown, R extends string = "none">(
table: string,
query?: DeleteQuery,
query?: DeleteQuery<T, R>,
relations?: Relations,
) {
const args: SqlParams = {
delete: `DELETE FROM ${quoteIdentifier(table)}`,
where: query?.where || {},
returning: query?.returning || [],
returning: (query?.returning || []) as never,
relations: relations,
join: "",
};

return sql(args);
}

export function buildUpdateQuery(
export function buildUpdateQuery<T = unknown, R extends string = "none">(
table: string,
query: UpdateQuery,
query: UpdateQuery<T, R>,
relations?: Relations,
) {
const args: SqlParams = {
update: `UPDATE ${quoteIdentifier(table)} SET ${buildUpdateValues(query.data)}`,
update: `UPDATE ${quoteIdentifier(table)} SET ${buildUpdateValues(query.data as never)}`,
where: query?.where || {},
returning: query?.returning || [],
returning: (query?.returning || []) as never,
relations: relations,
};

Expand Down
10 changes: 8 additions & 2 deletions src/lib/delete.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { buildDeleteQuery } from "./build";
import type { DeleteManyParams } from "./types";

export function deleteMany(params: DeleteManyParams): string {
export function deleteMany(params: DeleteManyParams): string;
export function deleteMany<T, R extends string = "none">(
params: DeleteManyParams<T, R>,
): string;
export function deleteMany<T = unknown, R extends string = "none">(
params: DeleteManyParams<T, R>,
): string {
const { table, query = {}, relations = {} } = params;
return buildDeleteQuery(table, query, relations);
return buildDeleteQuery<T, R>(table, query, relations);
}
10 changes: 8 additions & 2 deletions src/lib/find.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { buildSelectQuery } from "./build";
import type { FindManyParams } from "./types";

export function findMany(params: FindManyParams): string {
export function findMany(params: FindManyParams): string;
export function findMany<T, R extends string = "none">(
params: FindManyParams<T, R>,
): string;
export function findMany<T = unknown, R extends string = "none">(
params: FindManyParams<T, R>,
): string {
const { table, query, relations = {} } = params;
return buildSelectQuery(table, query, relations, false);
return buildSelectQuery<T, R>(table, query, relations, false);
}
12 changes: 8 additions & 4 deletions src/lib/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { buildColumns, buildValues } from "./build";
import type { InsertManyParams, InsertOneParams } from "./types";
import { quoteIdentifier } from "./util";

export function insertOne(params: InsertOneParams): string {
export function insertOne(params: InsertOneParams): string;
export function insertOne<T>(params: InsertOneParams<T>): string;
export function insertOne<T = unknown>(params: InsertOneParams<T>): string {
const { table, data, returning = [] } = params;

if (!table || table.trim() === "") {
Expand All @@ -27,7 +29,9 @@ export function insertOne(params: InsertOneParams): string {
)} (${columns}) VALUES (${values})${_returning}`;
}

export function insertMany(params: InsertManyParams): string {
export function insertMany(params: InsertManyParams): string;
export function insertMany<T>(params: InsertManyParams<T>): string;
export function insertMany<T = unknown>(params: InsertManyParams<T>): string {
const { table, data, returning = [] } = params;

if (!table || table.trim() === "") {
Expand All @@ -38,9 +42,9 @@ export function insertMany(params: InsertManyParams): string {
throw new Error("Data must be a non-empty array");
}

const columns = buildColumns(Object.keys(data[0]));
const columns = buildColumns(Object.keys(data[0] as never));
const values = data
.map((row) => `(${buildValues(Object.values(row))})`)
.map((row) => `(${buildValues(Object.values(row as never))})`)
.join(", ");
const _returning =
returning.length > 0 ? ` RETURNING ${buildColumns(returning)}` : "";
Expand Down
Loading

0 comments on commit 9e1b775

Please sign in to comment.