diff --git a/dist/index.d.ts b/dist/index.d.ts index 778c289..653e62b 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -3,11 +3,17 @@ import DbInsert from "./lib/sqlite/db-insert"; import DbSelect from "./lib/sqlite/db-select"; import DbSQL from "./lib/sqlite/db-sql"; import DbUpdate from "./lib/sqlite/db-update"; +import grabDbSchema from "./utils/grab-db-schema"; +import grabJoinFieldsFromQueryObject from "./utils/grab-join-fields-from-query-object"; declare const BunSQLite: { readonly select: typeof DbSelect; readonly insert: typeof DbInsert; readonly update: typeof DbUpdate; readonly delete: typeof DbDelete; readonly sql: typeof DbSQL; + readonly utils: { + readonly grab_db_schema: typeof grabDbSchema; + readonly grab_join_fields_from_query_object: typeof grabJoinFieldsFromQueryObject; + }; }; export default BunSQLite; diff --git a/dist/index.js b/dist/index.js index 3bff6fb..32a414a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -3,11 +3,17 @@ import DbInsert from "./lib/sqlite/db-insert"; import DbSelect from "./lib/sqlite/db-select"; import DbSQL from "./lib/sqlite/db-sql"; import DbUpdate from "./lib/sqlite/db-update"; +import grabDbSchema from "./utils/grab-db-schema"; +import grabJoinFieldsFromQueryObject from "./utils/grab-join-fields-from-query-object"; const BunSQLite = { select: DbSelect, insert: DbInsert, update: DbUpdate, delete: DbDelete, sql: DbSQL, + utils: { + grab_db_schema: grabDbSchema, + grab_join_fields_from_query_object: grabJoinFieldsFromQueryObject, + }, }; export default BunSQLite; diff --git a/dist/types/index.d.ts b/dist/types/index.d.ts index ee5db66..e0cc5b8 100644 --- a/dist/types/index.d.ts +++ b/dist/types/index.d.ts @@ -1336,3 +1336,8 @@ export type BunSQLiteConfigReturn = { * Default fields automatically suggested for new tables. */ export declare const DefaultFields: BUN_SQLITE_FieldSchemaType[]; +export type BunSQLiteQueryFieldValues = { + field: F; + table?: T; +}; +export type QueryRawValueType = string | number | null | undefined; diff --git a/dist/utils/grab-db-schema.d.ts b/dist/utils/grab-db-schema.d.ts new file mode 100644 index 0000000..8f9df70 --- /dev/null +++ b/dist/utils/grab-db-schema.d.ts @@ -0,0 +1 @@ +export default function grabDbSchema(): Promise; diff --git a/dist/utils/grab-db-schema.js b/dist/utils/grab-db-schema.js new file mode 100644 index 0000000..4780edb --- /dev/null +++ b/dist/utils/grab-db-schema.js @@ -0,0 +1,5 @@ +import init from "../functions/init"; +export default async function grabDbSchema() { + const { dbSchema } = await init(); + return dbSchema; +} diff --git a/dist/utils/grab-join-fields-from-query-object.d.ts b/dist/utils/grab-join-fields-from-query-object.d.ts new file mode 100644 index 0000000..c768da8 --- /dev/null +++ b/dist/utils/grab-join-fields-from-query-object.d.ts @@ -0,0 +1,6 @@ +import type { BunSQLiteQueryFieldValues, ServerQueryParam } from "../types"; +type Params = Record> = { + query: ServerQueryParam; +}; +export default function grabJoinFieldsFromQueryObject = Record, F extends string = string, T extends string = string>({ query }: Params): BunSQLiteQueryFieldValues[]; +export {}; diff --git a/dist/utils/grab-join-fields-from-query-object.js b/dist/utils/grab-join-fields-from-query-object.js new file mode 100644 index 0000000..54147c3 --- /dev/null +++ b/dist/utils/grab-join-fields-from-query-object.js @@ -0,0 +1,45 @@ +import _ from "lodash"; +export default function grabJoinFieldsFromQueryObject({ query }) { + const fields_values = []; + const new_query = _.cloneDeep(query); + if (new_query.join) { + for (let i = 0; i < new_query.join.length; i++) { + const join = new_query.join[i]; + if (!join) + continue; + if (Array.isArray(join)) { + for (let i = 0; i < join.length; i++) { + const single_join = join[i]; + fields_values.push(...grabSingleJoinData({ + join: single_join, + })); + } + } + else { + fields_values.push(...grabSingleJoinData({ + join: join, + })); + } + } + } + return fields_values; +} +function grabSingleJoinData({ join, }) { + let values = []; + const join_select_fields = join?.selectFields; + if (join_select_fields) { + for (let i = 0; i < join_select_fields.length; i++) { + const select_field = join_select_fields[i]; + if (select_field) { + const value = join.match; + values.push({ + field: typeof select_field == "object" + ? String(select_field.field) + : String(select_field), + table: join.tableName, + }); + } + } + } + return values; +} diff --git a/dist/utils/query-value-parser.d.ts b/dist/utils/query-value-parser.d.ts new file mode 100644 index 0000000..bf5460c --- /dev/null +++ b/dist/utils/query-value-parser.d.ts @@ -0,0 +1,6 @@ +import type { QueryRawValueType, ServerQueryObjectValue } from "../types"; +type Params = { + query_value: ServerQueryObjectValue; +}; +export default function queryValueParser({ query_value, }: Params): QueryRawValueType | QueryRawValueType[]; +export {}; diff --git a/dist/utils/query-value-parser.js b/dist/utils/query-value-parser.js new file mode 100644 index 0000000..dfa68ea --- /dev/null +++ b/dist/utils/query-value-parser.js @@ -0,0 +1,21 @@ +export default function queryValueParser({ query_value, }) { + if (typeof query_value == "string" || typeof query_value == "number") { + return query_value; + } + if (Array.isArray(query_value)) { + let values = []; + for (let i = 0; i < query_value.length; i++) { + const single_value = query_value[i]; + if (single_value) { + const single_parsed_value = queryValueParser({ + query_value: single_value, + }); + if (!Array.isArray(single_parsed_value)) { + values.push(single_parsed_value); + } + } + } + return values; + } + return query_value?.value; +} diff --git a/package.json b/package.json index c9b32b7..2f8040c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/bun-sqlite", - "version": "1.0.36", + "version": "1.0.37", "description": "SQLite manager for Bun", "author": "Benjamin Toby", "main": "dist/index.js", diff --git a/src/index.ts b/src/index.ts index 00bb30c..f3006aa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,8 @@ import DbInsert from "./lib/sqlite/db-insert"; import DbSelect from "./lib/sqlite/db-select"; import DbSQL from "./lib/sqlite/db-sql"; import DbUpdate from "./lib/sqlite/db-update"; +import grabDbSchema from "./utils/grab-db-schema"; +import grabJoinFieldsFromQueryObject from "./utils/grab-join-fields-from-query-object"; const BunSQLite = { select: DbSelect, @@ -10,6 +12,10 @@ const BunSQLite = { update: DbUpdate, delete: DbDelete, sql: DbSQL, + utils: { + grab_db_schema: grabDbSchema, + grab_join_fields_from_query_object: grabJoinFieldsFromQueryObject, + }, } as const; export default BunSQLite; diff --git a/src/types/index.ts b/src/types/index.ts index 2784b16..66dc5ef 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1522,3 +1522,13 @@ export const DefaultFields: BUN_SQLITE_FieldSchemaType[] = [ "The time when the record was updated. (Unix Timestamp)", }, ]; + +export type BunSQLiteQueryFieldValues< + F extends string = string, + T extends string = string, +> = { + field: F; + table?: T; +}; + +export type QueryRawValueType = string | number | null | undefined; diff --git a/src/utils/grab-db-schema.ts b/src/utils/grab-db-schema.ts new file mode 100644 index 0000000..edb2b08 --- /dev/null +++ b/src/utils/grab-db-schema.ts @@ -0,0 +1,6 @@ +import init from "../functions/init"; + +export default async function grabDbSchema() { + const { dbSchema } = await init(); + return dbSchema; +} diff --git a/src/utils/grab-join-fields-from-query-object.ts b/src/utils/grab-join-fields-from-query-object.ts new file mode 100644 index 0000000..6ef5754 --- /dev/null +++ b/src/utils/grab-join-fields-from-query-object.ts @@ -0,0 +1,73 @@ +import _ from "lodash"; +import type { + BunSQLiteQueryFieldValues, + ServerQueryParam, + ServerQueryParamsJoin, +} from "../types"; + +type Params = Record> = { + query: ServerQueryParam; +}; + +export default function grabJoinFieldsFromQueryObject< + Q extends Record = Record, + F extends string = string, + T extends string = string, +>({ query }: Params): BunSQLiteQueryFieldValues[] { + const fields_values: BunSQLiteQueryFieldValues[] = []; + const new_query = _.cloneDeep(query); + + if (new_query.join) { + for (let i = 0; i < new_query.join.length; i++) { + const join = new_query.join[i]; + if (!join) continue; + + if (Array.isArray(join)) { + for (let i = 0; i < join.length; i++) { + const single_join = join[i]; + fields_values.push( + ...(grabSingleJoinData({ + join: single_join as ServerQueryParamsJoin, + }) as BunSQLiteQueryFieldValues[]), + ); + } + } else { + fields_values.push( + ...(grabSingleJoinData({ + join: join as ServerQueryParamsJoin, + }) as BunSQLiteQueryFieldValues[]), + ); + } + } + } + + return fields_values; +} + +function grabSingleJoinData({ + join, +}: { + join: ServerQueryParamsJoin; +}): BunSQLiteQueryFieldValues[] { + let values: BunSQLiteQueryFieldValues[] = []; + + const join_select_fields = join?.selectFields; + + if (join_select_fields) { + for (let i = 0; i < join_select_fields.length; i++) { + const select_field = join_select_fields[i]; + if (select_field) { + const value = join.match; + values.push({ + field: + typeof select_field == "object" + ? String(select_field.field) + : String(select_field), + table: join.tableName, + }); + } + } + } + + return values; +} diff --git a/src/utils/query-value-parser.ts b/src/utils/query-value-parser.ts new file mode 100644 index 0000000..896c3a7 --- /dev/null +++ b/src/utils/query-value-parser.ts @@ -0,0 +1,33 @@ +import type { QueryRawValueType, ServerQueryObjectValue } from "../types"; + +type Params = { + query_value: ServerQueryObjectValue; +}; + +export default function queryValueParser({ + query_value, +}: Params): QueryRawValueType | QueryRawValueType[] { + if (typeof query_value == "string" || typeof query_value == "number") { + return query_value; + } + + if (Array.isArray(query_value)) { + let values: QueryRawValueType[] = []; + + for (let i = 0; i < query_value.length; i++) { + const single_value = query_value[i]; + if (single_value) { + const single_parsed_value = queryValueParser({ + query_value: single_value, + }); + if (!Array.isArray(single_parsed_value)) { + values.push(single_parsed_value); + } + } + } + + return values; + } + + return query_value?.value; +}