From a4aa20baa473319d968bbdf0f3ea69d56b680212 Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Sun, 12 Apr 2026 14:15:57 +0100 Subject: [PATCH] Update sql gen. Add support for raw sql in joins select and regular select sections --- dist/types/index.d.ts | 8 ++++++++ dist/utils/sql-generator.js | 10 ++++++++-- package.json | 2 +- src/types/index.ts | 8 ++++++++ src/utils/sql-generator.ts | 8 ++++++-- 5 files changed, 31 insertions(+), 5 deletions(-) diff --git a/dist/types/index.d.ts b/dist/types/index.d.ts index 5b2e639..c462962 100644 --- a/dist/types/index.d.ts +++ b/dist/types/index.d.ts @@ -640,6 +640,10 @@ export type ServerQueryParam | (keyof T | ServerQueryParamGroupBy)[]; countSubQueries?: ServerQueryParamsCount[]; fullTextSearch?: ServerQueryParamFullTextSearch; + /** + * Raw SQL to use as select + */ + select_sql?: string; [key: string]: any; }; /** @@ -801,6 +805,10 @@ export type ServerQueryParamsJoin { let str = "SELECT"; - if (genObject?.selectFields?.[0]) { + if (genObject?.select_sql) { + str += ` ${genObject.select_sql}`; + } + else if (genObject?.selectFields?.[0]) { if (genObject.join) { str += ` ${genObject.selectFields ?.map((fld) => typeof fld == "object" @@ -212,7 +215,10 @@ export default function sqlGenerator({ tableName, genObject, dbFullName, count } if (existingJoinTableNames.includes(joinTableName)) return null; existingJoinTableNames.push(joinTableName); - if (joinObj.selectFields) { + if (joinObj.select_sql) { + return joinObj.select_sql; + } + else if (joinObj.selectFields) { return joinObj.selectFields .map((selectField) => { if (typeof selectField == "string") { diff --git a/package.json b/package.json index 79797a7..f5228ae 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/bun-sqlite", - "version": "1.0.34", + "version": "1.0.35", "description": "SQLite manager for Bun", "author": "Benjamin Toby", "main": "dist/index.js", diff --git a/src/types/index.ts b/src/types/index.ts index fa889d0..4cd354c 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -711,6 +711,10 @@ export type ServerQueryParam< | (keyof T | ServerQueryParamGroupBy)[]; countSubQueries?: ServerQueryParamsCount[]; fullTextSearch?: ServerQueryParamFullTextSearch; + /** + * Raw SQL to use as select + */ + select_sql?: string; [key: string]: any; }; @@ -894,6 +898,10 @@ export type ServerQueryParamsJoin< } )[]; operator?: (typeof ServerQueryOperators)[number]; + /** + * Raw SQL to use as join select + */ + select_sql?: string; }; /** diff --git a/src/utils/sql-generator.ts b/src/utils/sql-generator.ts index f60082e..53e584b 100644 --- a/src/utils/sql-generator.ts +++ b/src/utils/sql-generator.ts @@ -221,7 +221,9 @@ export default function sqlGenerator< let queryString = (() => { let str = "SELECT"; - if (genObject?.selectFields?.[0]) { + if (genObject?.select_sql) { + str += ` ${genObject.select_sql}`; + } else if (genObject?.selectFields?.[0]) { if (genObject.join) { str += ` ${genObject.selectFields ?.map((fld) => @@ -310,7 +312,9 @@ export default function sqlGenerator< return null; existingJoinTableNames.push(joinTableName); - if (joinObj.selectFields) { + if (joinObj.select_sql) { + return joinObj.select_sql; + } else if (joinObj.selectFields) { return joinObj.selectFields .map((selectField) => { if (typeof selectField == "string") {