Update sql gen. Add support for raw sql in joins select and regular select sections

This commit is contained in:
Benjamin Toby 2026-04-12 14:15:57 +01:00
parent a57a063f67
commit a4aa20baa4
5 changed files with 31 additions and 5 deletions

View File

@ -640,6 +640,10 @@ export type ServerQueryParam<T extends {
group?: keyof T | ServerQueryParamGroupBy<T> | (keyof T | ServerQueryParamGroupBy<T>)[]; group?: keyof T | ServerQueryParamGroupBy<T> | (keyof T | ServerQueryParamGroupBy<T>)[];
countSubQueries?: ServerQueryParamsCount[]; countSubQueries?: ServerQueryParamsCount[];
fullTextSearch?: ServerQueryParamFullTextSearch<T>; fullTextSearch?: ServerQueryParamFullTextSearch<T>;
/**
* Raw SQL to use as select
*/
select_sql?: string;
[key: string]: any; [key: string]: any;
}; };
/** /**
@ -801,6 +805,10 @@ export type ServerQueryParamsJoin<Table extends string = string, Field extends o
count?: boolean; count?: boolean;
})[]; })[];
operator?: (typeof ServerQueryOperators)[number]; operator?: (typeof ServerQueryOperators)[number];
/**
* Raw SQL to use as join select
*/
select_sql?: string;
}; };
/** /**
* Defines how a root-table field maps to a join-table field in an `ON` clause. * Defines how a root-table field maps to a join-table field in an `ON` clause.

View File

@ -141,7 +141,10 @@ export default function sqlGenerator({ tableName, genObject, dbFullName, count }
: undefined; : undefined;
let queryString = (() => { let queryString = (() => {
let str = "SELECT"; let str = "SELECT";
if (genObject?.selectFields?.[0]) { if (genObject?.select_sql) {
str += ` ${genObject.select_sql}`;
}
else if (genObject?.selectFields?.[0]) {
if (genObject.join) { if (genObject.join) {
str += ` ${genObject.selectFields str += ` ${genObject.selectFields
?.map((fld) => typeof fld == "object" ?.map((fld) => typeof fld == "object"
@ -212,7 +215,10 @@ export default function sqlGenerator({ tableName, genObject, dbFullName, count }
if (existingJoinTableNames.includes(joinTableName)) if (existingJoinTableNames.includes(joinTableName))
return null; return null;
existingJoinTableNames.push(joinTableName); existingJoinTableNames.push(joinTableName);
if (joinObj.selectFields) { if (joinObj.select_sql) {
return joinObj.select_sql;
}
else if (joinObj.selectFields) {
return joinObj.selectFields return joinObj.selectFields
.map((selectField) => { .map((selectField) => {
if (typeof selectField == "string") { if (typeof selectField == "string") {

View File

@ -1,6 +1,6 @@
{ {
"name": "@moduletrace/bun-sqlite", "name": "@moduletrace/bun-sqlite",
"version": "1.0.34", "version": "1.0.35",
"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

@ -711,6 +711,10 @@ export type ServerQueryParam<
| (keyof T | ServerQueryParamGroupBy<T>)[]; | (keyof T | ServerQueryParamGroupBy<T>)[];
countSubQueries?: ServerQueryParamsCount[]; countSubQueries?: ServerQueryParamsCount[];
fullTextSearch?: ServerQueryParamFullTextSearch<T>; fullTextSearch?: ServerQueryParamFullTextSearch<T>;
/**
* Raw SQL to use as select
*/
select_sql?: string;
[key: string]: any; [key: string]: any;
}; };
@ -894,6 +898,10 @@ export type ServerQueryParamsJoin<
} }
)[]; )[];
operator?: (typeof ServerQueryOperators)[number]; operator?: (typeof ServerQueryOperators)[number];
/**
* Raw SQL to use as join select
*/
select_sql?: string;
}; };
/** /**

View File

@ -221,7 +221,9 @@ export default function sqlGenerator<
let queryString = (() => { let queryString = (() => {
let str = "SELECT"; let str = "SELECT";
if (genObject?.selectFields?.[0]) { if (genObject?.select_sql) {
str += ` ${genObject.select_sql}`;
} else if (genObject?.selectFields?.[0]) {
if (genObject.join) { if (genObject.join) {
str += ` ${genObject.selectFields str += ` ${genObject.selectFields
?.map((fld) => ?.map((fld) =>
@ -310,7 +312,9 @@ export default function sqlGenerator<
return null; return null;
existingJoinTableNames.push(joinTableName); existingJoinTableNames.push(joinTableName);
if (joinObj.selectFields) { if (joinObj.select_sql) {
return joinObj.select_sql;
} else if (joinObj.selectFields) {
return joinObj.selectFields return joinObj.selectFields
.map((selectField) => { .map((selectField) => {
if (typeof selectField == "string") { if (typeof selectField == "string") {