Update CRUD functions type defs

This commit is contained in:
Benjamin Toby 2026-03-02 17:03:00 +01:00
parent 83bc501bd1
commit 077a15ed71
12 changed files with 82 additions and 52 deletions

View File

@ -1,16 +1,16 @@
import type { APIResponseObject, ServerQueryParam } from "../../types"; import type { APIResponseObject, ServerQueryParam } from "../../types";
type Params<T extends { type Params<Schema extends {
[k: string]: any; [k: string]: any;
} = { } = {
[k: string]: any; [k: string]: any;
}> = { }, Table extends string = string> = {
table: string; table: Table;
query?: ServerQueryParam<T>; query?: ServerQueryParam<Schema>;
targetId?: number | string; targetId?: number | string;
}; };
export default function DbDelete<T extends { export default function DbDelete<Schema extends {
[k: string]: any; [k: string]: any;
} = { } = {
[k: string]: any; [k: string]: any;
}>({ table, query, targetId }: Params<T>): Promise<APIResponseObject>; }, Table extends string = string>({ table, query, targetId, }: Params<Schema, Table>): Promise<APIResponseObject>;
export {}; export {};

View File

@ -1,7 +1,7 @@
import DbClient from "."; import DbClient from ".";
import _ from "lodash"; import _ from "lodash";
import sqlGenerator from "../../utils/sql-generator"; import sqlGenerator from "../../utils/sql-generator";
export default async function DbDelete({ table, query, targetId }) { export default async function DbDelete({ table, query, targetId, }) {
try { try {
let finalQuery = query || {}; let finalQuery = query || {};
if (targetId) { if (targetId) {

View File

@ -1,15 +1,15 @@
import type { APIResponseObject } from "../../types"; import type { APIResponseObject } from "../../types";
type Params<T extends { type Params<Schema extends {
[k: string]: any; [k: string]: any;
} = { } = {
[k: string]: any; [k: string]: any;
}> = { }, Table extends string = string> = {
table: string; table: Table;
data: T[]; data: Schema[];
}; };
export default function DbInsert<T extends { export default function DbInsert<Schema extends {
[k: string]: any; [k: string]: any;
} = { } = {
[k: string]: any; [k: string]: any;
}>({ table, data }: Params<T>): Promise<APIResponseObject>; }, Table extends string = string>({ table, data }: Params<Schema, Table>): Promise<APIResponseObject>;
export {}; export {};

View File

@ -1,17 +1,17 @@
import type { APIResponseObject, ServerQueryParam } from "../../types"; import type { APIResponseObject, ServerQueryParam } from "../../types";
type Params<T extends { type Params<Schema extends {
[k: string]: any; [k: string]: any;
} = { } = {
[k: string]: any; [k: string]: any;
}> = { }, Table extends string = string> = {
query?: ServerQueryParam<T>; query?: ServerQueryParam<Schema>;
table: string; table: Table;
count?: boolean; count?: boolean;
targetId?: number | string; targetId?: number | string;
}; };
export default function DbSelect<T extends { export default function DbSelect<Schema extends {
[k: string]: any; [k: string]: any;
} = { } = {
[k: string]: any; [k: string]: any;
}>({ table, query, count, targetId }: Params<T>): Promise<APIResponseObject<T>>; }, Table extends string = string>({ table, query, count, targetId, }: Params<Schema, Table>): Promise<APIResponseObject<Schema>>;
export {}; export {};

View File

@ -2,7 +2,7 @@ import mysql from "mysql";
import DbClient from "."; import DbClient from ".";
import _ from "lodash"; import _ from "lodash";
import sqlGenerator from "../../utils/sql-generator"; import sqlGenerator from "../../utils/sql-generator";
export default async function DbSelect({ table, query, count, targetId }) { export default async function DbSelect({ table, query, count, targetId, }) {
try { try {
let finalQuery = query || {}; let finalQuery = query || {};
if (targetId) { if (targetId) {

View File

@ -1,17 +1,17 @@
import type { APIResponseObject, ServerQueryParam } from "../../types"; import type { APIResponseObject, ServerQueryParam } from "../../types";
type Params<T extends { type Params<Schema extends {
[k: string]: any; [k: string]: any;
} = { } = {
[k: string]: any; [k: string]: any;
}> = { }, Table extends string = string> = {
table: string; table: Table;
data: T; data: Schema;
query?: ServerQueryParam<T>; query?: ServerQueryParam<Schema>;
targetId?: number | string; targetId?: number | string;
}; };
export default function DbUpdate<T extends { export default function DbUpdate<Schema extends {
[k: string]: any; [k: string]: any;
} = { } = {
[k: string]: any; [k: string]: any;
}>({ table, data, query, targetId }: Params<T>): Promise<APIResponseObject>; }, Table extends string = string>({ table, data, query, targetId, }: Params<Schema, Table>): Promise<APIResponseObject>;
export {}; export {};

View File

@ -1,7 +1,7 @@
import DbClient from "."; import DbClient from ".";
import _ from "lodash"; import _ from "lodash";
import sqlGenerator from "../../utils/sql-generator"; import sqlGenerator from "../../utils/sql-generator";
export default async function DbUpdate({ table, data, query, targetId }) { export default async function DbUpdate({ table, data, query, targetId, }) {
try { try {
let finalQuery = query || {}; let finalQuery = query || {};
if (targetId) { if (targetId) {

View File

@ -1,6 +1,6 @@
{ {
"name": "@moduletrace/bun-sqlite", "name": "@moduletrace/bun-sqlite",
"version": "1.0.4", "version": "1.0.5",
"description": "SQLite manager for Bun", "description": "SQLite manager for Bun",
"author": "Benjamin Toby", "author": "Benjamin Toby",
"main": "dist/index.js", "main": "dist/index.js",

View File

@ -3,15 +3,23 @@ import _ from "lodash";
import type { APIResponseObject, ServerQueryParam } from "../../types"; import type { APIResponseObject, ServerQueryParam } from "../../types";
import sqlGenerator from "../../utils/sql-generator"; import sqlGenerator from "../../utils/sql-generator";
type Params<T extends { [k: string]: any } = { [k: string]: any }> = { type Params<
table: string; Schema extends { [k: string]: any } = { [k: string]: any },
query?: ServerQueryParam<T>; Table extends string = string,
> = {
table: Table;
query?: ServerQueryParam<Schema>;
targetId?: number | string; targetId?: number | string;
}; };
export default async function DbDelete< export default async function DbDelete<
T extends { [k: string]: any } = { [k: string]: any }, Schema extends { [k: string]: any } = { [k: string]: any },
>({ table, query, targetId }: Params<T>): Promise<APIResponseObject> { Table extends string = string,
>({
table,
query,
targetId,
}: Params<Schema, Table>): Promise<APIResponseObject> {
try { try {
let finalQuery = query || {}; let finalQuery = query || {};

View File

@ -2,14 +2,18 @@ import DbClient from ".";
import type { APIResponseObject } from "../../types"; import type { APIResponseObject } from "../../types";
import sqlInsertGenerator from "../../utils/sql-insert-generator"; import sqlInsertGenerator from "../../utils/sql-insert-generator";
type Params<T extends { [k: string]: any } = { [k: string]: any }> = { type Params<
table: string; Schema extends { [k: string]: any } = { [k: string]: any },
data: T[]; Table extends string = string,
> = {
table: Table;
data: Schema[];
}; };
export default async function DbInsert< export default async function DbInsert<
T extends { [k: string]: any } = { [k: string]: any }, Schema extends { [k: string]: any } = { [k: string]: any },
>({ table, data }: Params<T>): Promise<APIResponseObject> { Table extends string = string,
>({ table, data }: Params<Schema, Table>): Promise<APIResponseObject> {
try { try {
const finalData: { [k: string]: any }[] = data.map((d) => ({ const finalData: { [k: string]: any }[] = data.map((d) => ({
...d, ...d,

View File

@ -4,16 +4,25 @@ import _ from "lodash";
import type { APIResponseObject, ServerQueryParam } from "../../types"; import type { APIResponseObject, ServerQueryParam } from "../../types";
import sqlGenerator from "../../utils/sql-generator"; import sqlGenerator from "../../utils/sql-generator";
type Params<T extends { [k: string]: any } = { [k: string]: any }> = { type Params<
query?: ServerQueryParam<T>; Schema extends { [k: string]: any } = { [k: string]: any },
table: string; Table extends string = string,
> = {
query?: ServerQueryParam<Schema>;
table: Table;
count?: boolean; count?: boolean;
targetId?: number | string; targetId?: number | string;
}; };
export default async function DbSelect< export default async function DbSelect<
T extends { [k: string]: any } = { [k: string]: any }, Schema extends { [k: string]: any } = { [k: string]: any },
>({ table, query, count, targetId }: Params<T>): Promise<APIResponseObject<T>> { Table extends string = string,
>({
table,
query,
count,
targetId,
}: Params<Schema, Table>): Promise<APIResponseObject<Schema>> {
try { try {
let finalQuery = query || {}; let finalQuery = query || {};
@ -38,10 +47,10 @@ export default async function DbSelect<
const sql = mysql.format(sqlObj.string, sqlObj.values); const sql = mysql.format(sqlObj.string, sqlObj.values);
const res = DbClient.query<T, T[]>(sql); const res = DbClient.query<Schema, Schema[]>(sql);
const batchRes = res.all(); const batchRes = res.all();
let resp: APIResponseObject<T> = { let resp: APIResponseObject<Schema> = {
success: Boolean(batchRes[0]), success: Boolean(batchRes[0]),
payload: batchRes, payload: batchRes,
singleRes: batchRes[0], singleRes: batchRes[0],

View File

@ -3,16 +3,25 @@ import _ from "lodash";
import type { APIResponseObject, ServerQueryParam } from "../../types"; import type { APIResponseObject, ServerQueryParam } from "../../types";
import sqlGenerator from "../../utils/sql-generator"; import sqlGenerator from "../../utils/sql-generator";
type Params<T extends { [k: string]: any } = { [k: string]: any }> = { type Params<
table: string; Schema extends { [k: string]: any } = { [k: string]: any },
data: T; Table extends string = string,
query?: ServerQueryParam<T>; > = {
table: Table;
data: Schema;
query?: ServerQueryParam<Schema>;
targetId?: number | string; targetId?: number | string;
}; };
export default async function DbUpdate< export default async function DbUpdate<
T extends { [k: string]: any } = { [k: string]: any }, Schema extends { [k: string]: any } = { [k: string]: any },
>({ table, data, query, targetId }: Params<T>): Promise<APIResponseObject> { Table extends string = string,
>({
table,
data,
query,
targetId,
}: Params<Schema, Table>): Promise<APIResponseObject> {
try { try {
let finalQuery = query || {}; let finalQuery = query || {};