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";
|
import type { BunSQLiteQueryFieldValues, ServerQueryParam } from "../types";
|
||||||
type Params<Q extends Record<string, any> = Record<string, any>> = {
|
type Params<Q extends Record<string, any> = Record<string, any>> = {
|
||||||
query: ServerQueryParam<Q>;
|
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 {};
|
export {};
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
export default function grabJoinFieldsFromQueryObject({ query }) {
|
export default function grabJoinFieldsFromQueryObject({ query, ignore_select_fields, }) {
|
||||||
const fields_values = [];
|
const fields_values = [];
|
||||||
const new_query = _.cloneDeep(query);
|
const new_query = _.cloneDeep(query);
|
||||||
if (new_query.join) {
|
if (new_query.join) {
|
||||||
@ -12,21 +12,26 @@ export default function grabJoinFieldsFromQueryObject({ query }) {
|
|||||||
const single_join = join[i];
|
const single_join = join[i];
|
||||||
fields_values.push(...grabSingleJoinData({
|
fields_values.push(...grabSingleJoinData({
|
||||||
join: single_join,
|
join: single_join,
|
||||||
|
ignore_select_fields,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
fields_values.push(...grabSingleJoinData({
|
fields_values.push(...grabSingleJoinData({
|
||||||
join: join,
|
join: join,
|
||||||
|
ignore_select_fields,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return fields_values;
|
return fields_values;
|
||||||
}
|
}
|
||||||
function grabSingleJoinData({ join, }) {
|
function grabSingleJoinData({ join, ignore_select_fields, }) {
|
||||||
let values = [];
|
let values = [];
|
||||||
const join_select_fields = join?.selectFields;
|
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]) {
|
if (join_select_fields?.[0]) {
|
||||||
for (let i = 0; i < join_select_fields.length; i++) {
|
for (let i = 0; i < join_select_fields.length; i++) {
|
||||||
const select_field = join_select_fields[i];
|
const select_field = join_select_fields[i];
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@moduletrace/bun-sqlite",
|
"name": "@moduletrace/bun-sqlite",
|
||||||
"version": "1.1.6",
|
"version": "1.1.7",
|
||||||
"description": "SQLite manager for Bun",
|
"description": "SQLite manager for Bun",
|
||||||
"author": "Benjamin Toby",
|
"author": "Benjamin Toby",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
|||||||
@ -7,13 +7,17 @@ import type {
|
|||||||
|
|
||||||
type Params<Q extends Record<string, any> = Record<string, any>> = {
|
type Params<Q extends Record<string, any> = Record<string, any>> = {
|
||||||
query: ServerQueryParam<Q>;
|
query: ServerQueryParam<Q>;
|
||||||
|
ignore_select_fields?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function grabJoinFieldsFromQueryObject<
|
export default function grabJoinFieldsFromQueryObject<
|
||||||
Q extends Record<string, any> = Record<string, any>,
|
Q extends Record<string, any> = Record<string, any>,
|
||||||
F extends string = string,
|
F extends string = string,
|
||||||
T 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 fields_values: BunSQLiteQueryFieldValues<F, T>[] = [];
|
||||||
const new_query = _.cloneDeep(query);
|
const new_query = _.cloneDeep(query);
|
||||||
|
|
||||||
@ -29,6 +33,7 @@ export default function grabJoinFieldsFromQueryObject<
|
|||||||
fields_values.push(
|
fields_values.push(
|
||||||
...(grabSingleJoinData({
|
...(grabSingleJoinData({
|
||||||
join: single_join as ServerQueryParamsJoin,
|
join: single_join as ServerQueryParamsJoin,
|
||||||
|
ignore_select_fields,
|
||||||
}) as BunSQLiteQueryFieldValues<F, T>[]),
|
}) as BunSQLiteQueryFieldValues<F, T>[]),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -36,6 +41,7 @@ export default function grabJoinFieldsFromQueryObject<
|
|||||||
fields_values.push(
|
fields_values.push(
|
||||||
...(grabSingleJoinData({
|
...(grabSingleJoinData({
|
||||||
join: join as ServerQueryParamsJoin,
|
join: join as ServerQueryParamsJoin,
|
||||||
|
ignore_select_fields,
|
||||||
}) as BunSQLiteQueryFieldValues<F, T>[]),
|
}) as BunSQLiteQueryFieldValues<F, T>[]),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -47,13 +53,21 @@ export default function grabJoinFieldsFromQueryObject<
|
|||||||
|
|
||||||
function grabSingleJoinData({
|
function grabSingleJoinData({
|
||||||
join,
|
join,
|
||||||
|
ignore_select_fields,
|
||||||
}: {
|
}: {
|
||||||
join: ServerQueryParamsJoin;
|
join: ServerQueryParamsJoin;
|
||||||
|
ignore_select_fields?: boolean;
|
||||||
}): BunSQLiteQueryFieldValues[] {
|
}): BunSQLiteQueryFieldValues[] {
|
||||||
let values: BunSQLiteQueryFieldValues[] = [];
|
let values: BunSQLiteQueryFieldValues[] = [];
|
||||||
|
|
||||||
const join_select_fields = join?.selectFields;
|
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]) {
|
if (join_select_fields?.[0]) {
|
||||||
for (let i = 0; i < join_select_fields.length; i++) {
|
for (let i = 0; i < join_select_fields.length; i++) {
|
||||||
const select_field = join_select_fields[i];
|
const select_field = join_select_fields[i];
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user