Update joins parser. Enforce selectFields unless instructed otherwise
This commit is contained in:
parent
64a6d78506
commit
95d567d4dd
@ -1,6 +1,7 @@
|
||||
import type { BunSQLiteQueryFieldValues, ServerQueryParam } from "../types";
|
||||
type Params<Q extends Record<string, any> = Record<string, any>> = {
|
||||
query: ServerQueryParam<Q>;
|
||||
ignore_select_fields?: boolean;
|
||||
};
|
||||
export default function grabJoinFieldsFromQueryObject<Q extends Record<string, any> = Record<string, any>, F extends string = string, T extends string = string>({ query }: Params<Q>): BunSQLiteQueryFieldValues<F, T>[];
|
||||
export default function grabJoinFieldsFromQueryObject<Q extends Record<string, any> = Record<string, any>, F extends string = string, T extends string = string>({ query, ignore_select_fields, }: Params<Q>): BunSQLiteQueryFieldValues<F, T>[];
|
||||
export {};
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import _ from "lodash";
|
||||
export default function grabJoinFieldsFromQueryObject({ query }) {
|
||||
export default function grabJoinFieldsFromQueryObject({ query, ignore_select_fields, }) {
|
||||
const fields_values = [];
|
||||
const new_query = _.cloneDeep(query);
|
||||
if (new_query.join) {
|
||||
@ -12,21 +12,26 @@ export default function grabJoinFieldsFromQueryObject({ query }) {
|
||||
const single_join = join[i];
|
||||
fields_values.push(...grabSingleJoinData({
|
||||
join: single_join,
|
||||
ignore_select_fields,
|
||||
}));
|
||||
}
|
||||
}
|
||||
else {
|
||||
fields_values.push(...grabSingleJoinData({
|
||||
join: join,
|
||||
ignore_select_fields,
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
return fields_values;
|
||||
}
|
||||
function grabSingleJoinData({ join, }) {
|
||||
function grabSingleJoinData({ join, ignore_select_fields, }) {
|
||||
let values = [];
|
||||
const join_select_fields = join?.selectFields;
|
||||
if (!join_select_fields?.[0] && !ignore_select_fields) {
|
||||
throw new Error(`\`selectFields\` required in joins. To ignore this error, pass the \`ignore_select_fields\` parameter`);
|
||||
}
|
||||
if (join_select_fields?.[0]) {
|
||||
for (let i = 0; i < join_select_fields.length; i++) {
|
||||
const select_field = join_select_fields[i];
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@moduletrace/bun-sqlite",
|
||||
"version": "1.1.6",
|
||||
"version": "1.1.7",
|
||||
"description": "SQLite manager for Bun",
|
||||
"author": "Benjamin Toby",
|
||||
"main": "dist/index.js",
|
||||
|
||||
@ -7,13 +7,17 @@ import type {
|
||||
|
||||
type Params<Q extends Record<string, any> = Record<string, any>> = {
|
||||
query: ServerQueryParam<Q>;
|
||||
ignore_select_fields?: boolean;
|
||||
};
|
||||
|
||||
export default function grabJoinFieldsFromQueryObject<
|
||||
Q extends Record<string, any> = Record<string, any>,
|
||||
F extends string = string,
|
||||
T extends string = string,
|
||||
>({ query }: Params<Q>): BunSQLiteQueryFieldValues<F, T>[] {
|
||||
>({
|
||||
query,
|
||||
ignore_select_fields,
|
||||
}: Params<Q>): BunSQLiteQueryFieldValues<F, T>[] {
|
||||
const fields_values: BunSQLiteQueryFieldValues<F, T>[] = [];
|
||||
const new_query = _.cloneDeep(query);
|
||||
|
||||
@ -29,6 +33,7 @@ export default function grabJoinFieldsFromQueryObject<
|
||||
fields_values.push(
|
||||
...(grabSingleJoinData({
|
||||
join: single_join as ServerQueryParamsJoin,
|
||||
ignore_select_fields,
|
||||
}) as BunSQLiteQueryFieldValues<F, T>[]),
|
||||
);
|
||||
}
|
||||
@ -36,6 +41,7 @@ export default function grabJoinFieldsFromQueryObject<
|
||||
fields_values.push(
|
||||
...(grabSingleJoinData({
|
||||
join: join as ServerQueryParamsJoin,
|
||||
ignore_select_fields,
|
||||
}) as BunSQLiteQueryFieldValues<F, T>[]),
|
||||
);
|
||||
}
|
||||
@ -47,13 +53,21 @@ export default function grabJoinFieldsFromQueryObject<
|
||||
|
||||
function grabSingleJoinData({
|
||||
join,
|
||||
ignore_select_fields,
|
||||
}: {
|
||||
join: ServerQueryParamsJoin;
|
||||
ignore_select_fields?: boolean;
|
||||
}): BunSQLiteQueryFieldValues[] {
|
||||
let values: BunSQLiteQueryFieldValues[] = [];
|
||||
|
||||
const join_select_fields = join?.selectFields;
|
||||
|
||||
if (!join_select_fields?.[0] && !ignore_select_fields) {
|
||||
throw new Error(
|
||||
`\`selectFields\` required in joins. To ignore this error, pass the \`ignore_select_fields\` parameter`,
|
||||
);
|
||||
}
|
||||
|
||||
if (join_select_fields?.[0]) {
|
||||
for (let i = 0; i < join_select_fields.length; i++) {
|
||||
const select_field = join_select_fields[i];
|
||||
|
||||
Loading…
Reference in New Issue
Block a user