web_lib

Common web application libraries
git clone https://radroots.dev/git/web_lib.git
Log | Files | Refs | LICENSE

commit d716aea942903c0251ab6d52509d260d3b5aed9a
parent b6f10eb380d153949051e3e756f059a14e07b232
Author: triesap <137732411+triesap@users.noreply.github.com>
Date:   Thu, 26 Sep 2024 17:17:08 +0000

client: edit iclientdb models update and delete methods logic

Diffstat:
Mclient/src/capacitor/sqlite.ts | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
1 file changed, 67 insertions(+), 25 deletions(-)

diff --git a/client/src/capacitor/sqlite.ts b/client/src/capacitor/sqlite.ts @@ -1,5 +1,5 @@ import { type capSQLiteChanges, type DBSQLiteValues, SQLiteDBConnection } from "@radroots/capacitor-sqlite"; -import { type IModelsQueryValue, type IModelsQueryParam, type IModelsQueryBindValue, type IModelsQueryBindValueTuple, type IModelsQueryBindValueOpt, parse_location_gcs_form_fields, location_gcs_sort, type ILocationGcsGetList, type ILocationGcsGet, type ILocationGcsUpdate, type ILocationGcsQueryBindValues, type ILocationGcsQueryBindValuesKey, type ILocationGcsQueryBindValuesTuple, parse_location_gcs, parse_location_gcs_list, type LocationGcs, type LocationGcsFields, type LocationGcsFormFields, LocationGcsSchema, parse_trade_product_form_fields, trade_product_sort, type ITradeProductGetList, type ITradeProductGet, type ITradeProductUpdate, type ITradeProductQueryBindValues, type ITradeProductQueryBindValuesKey, type ITradeProductQueryBindValuesTuple, parse_trade_product, parse_trade_product_list, type TradeProduct, type TradeProductFields, type TradeProductFormFields, TradeProductSchema, parse_nostr_profile_form_fields, nostr_profile_sort, type INostrProfileGetList, type INostrProfileGet, type INostrProfileUpdate, type INostrProfileQueryBindValues, type INostrProfileQueryBindValuesKey, type INostrProfileQueryBindValuesTuple, parse_nostr_profile, parse_nostr_profile_list, type NostrProfile, type NostrProfileFields, type NostrProfileFormFields, NostrProfileSchema, NostrProfileMetadataSchema} from "@radroots/models"; +import { type IModelsQueryValue, type IModelsQueryParam, type IModelsQueryBindValue, type IModelsQueryBindValueTuple, type IModelsQueryBindValueOpt, parse_location_gcs_form_fields, location_gcs_sort, type ILocationGcsGetList, type ILocationGcsGet, type ILocationGcsUpdate, type ILocationGcsQueryBindValues, type ILocationGcsQueryBindValuesKey, type ILocationGcsQueryBindValuesTuple, parse_location_gcs, parse_location_gcs_list, type LocationGcs, type LocationGcsFields, type LocationGcsFormFields, LocationGcsSchema, LocationGcsUpdateSchema, parse_trade_product_form_fields, trade_product_sort, type ITradeProductGetList, type ITradeProductGet, type ITradeProductUpdate, type ITradeProductQueryBindValues, type ITradeProductQueryBindValuesKey, type ITradeProductQueryBindValuesTuple, parse_trade_product, parse_trade_product_list, type TradeProduct, type TradeProductFields, type TradeProductFormFields, TradeProductSchema, TradeProductUpdateSchema, parse_nostr_profile_form_fields, nostr_profile_sort, type INostrProfileGetList, type INostrProfileGet, type INostrProfileUpdate, type INostrProfileQueryBindValues, type INostrProfileQueryBindValuesKey, type INostrProfileQueryBindValuesTuple, parse_nostr_profile, parse_nostr_profile_list, type NostrProfile, type NostrProfileFields, type NostrProfileFormFields, NostrProfileSchema, NostrProfileUpdateSchema, NostrProfileMetadataSchema} from "@radroots/models"; import { err_msg, time_created_on, uuidv4 } from "@radroots/utils"; import { sqlite_svc, sqlite_version_svc, type IISQLiteServiceOpenDatabase } from "./sqlite_lib"; @@ -138,7 +138,7 @@ export class CapacitorClientSQLite { ]; for (const field of this.filter_bind_value_fields(fields)) bind_values_tup.push(field); const bind_values = bind_values_tup.map(([_, v]) => v); - const query = `INSERT INTO location_gcs (${bind_values_tup.map(([k]) => k).join(", ")}) VALUES (${bind_values_tup.map((_, num) => `$${1 + num}`).join(", ")});`; + const query = `INSERT INTO location_gcs (${bind_values_tup.map(([k]) => k).join(", ")}) VALUES (${bind_values_tup.map((_, index) => `$${1 + index}`).join(", ")});`; try { const result = await this.execute(query, bind_values); if (typeof result !== "string" && typeof result.changes?.changes === "number" && result.changes.changes > 0) return { id, }; @@ -195,9 +195,9 @@ export class CapacitorClientSQLite { } public async location_gcs_delete(opts: ILocationGcsQueryBindValues): Promise<true | ICapacitorClientSQLiteMessage> { - const bv_tup = this.location_gcs_query_bind_values(opts); - const bind_values = [bv_tup[1]]; - const query = `DELETE FROM location_gcs WHERE ${bv_tup[0]} = $1;`; + const [bv_k, bv_v] = this.location_gcs_query_bind_values(opts); + const bind_values = [bv_v]; + const query = `DELETE FROM location_gcs WHERE ${bv_k} = $1;`; try { const response = await this.execute(query, bind_values); if (typeof response === "string") return response; @@ -208,14 +208,28 @@ export class CapacitorClientSQLite { }; } + private location_gcs_update_validate(fields: Partial<LocationGcsFormFields>): Partial<LocationGcsFields> | string[] { + const fields_r = Object.entries(fields).filter(([_, v]) => !!v).reduce((acc: Record<string, IModelsQueryValue>, i) => { + const [key, val] = parse_location_gcs_form_fields(i); + acc[key] = val; + return acc; + }, {}); + const schema = LocationGcsUpdateSchema; + const parsed_schema = schema.safeParse(fields_r); + if (!parsed_schema.success) return parsed_schema.error.issues.map(i => i.message); + else return { + ...parsed_schema.data + }; + } + public async location_gcs_update(opts: ILocationGcsUpdate): Promise<true | string[] | ICapacitorClientSQLiteMessage> { - const opts_v = this.location_gcs_add_validate(opts.fields); + const opts_v = this.location_gcs_update_validate(opts.fields); if (Array.isArray(opts_v)) return opts_v; const fields = this.filter_bind_value_fields(Object.entries(opts_v)); if (!fields.length) return "*-fields"; - const bv_tup = this.location_gcs_query_bind_values(opts.on); - const bind_values = [bv_tup[1], ...fields.map(([_, v]) => v)]; - const query = `UPDATE location_gcs SET ${fields.map(([k], num) => `${k} = $${1 + num}`).join(", ")} WHERE ${bv_tup[0]} = $1;`; + const [bv_k, bv_v] = this.location_gcs_query_bind_values(opts.on); + const bind_values = [...fields.map(([_, v]) => v), bv_v]; + const query = `UPDATE location_gcs SET ${fields.map(([k], index) => `${k} = $${1 + index}`).join(", ")} WHERE ${bv_k} = $${bind_values.length};`; try { const response = await this.execute(query, bind_values); if (typeof response === "string") return response; @@ -252,7 +266,7 @@ export class CapacitorClientSQLite { ]; for (const field of this.filter_bind_value_fields(fields)) bind_values_tup.push(field); const bind_values = bind_values_tup.map(([_, v]) => v); - const query = `INSERT INTO trade_product (${bind_values_tup.map(([k]) => k).join(", ")}) VALUES (${bind_values_tup.map((_, num) => `$${1 + num}`).join(", ")});`; + const query = `INSERT INTO trade_product (${bind_values_tup.map(([k]) => k).join(", ")}) VALUES (${bind_values_tup.map((_, index) => `$${1 + index}`).join(", ")});`; try { const result = await this.execute(query, bind_values); if (typeof result !== "string" && typeof result.changes?.changes === "number" && result.changes.changes > 0) return { id, }; @@ -309,9 +323,9 @@ export class CapacitorClientSQLite { } public async trade_product_delete(opts: ITradeProductQueryBindValues): Promise<true | ICapacitorClientSQLiteMessage> { - const bv_tup = this.trade_product_query_bind_values(opts); - const bind_values = [bv_tup[1]]; - const query = `DELETE FROM trade_product WHERE ${bv_tup[0]} = $1;`; + const [bv_k, bv_v] = this.trade_product_query_bind_values(opts); + const bind_values = [bv_v]; + const query = `DELETE FROM trade_product WHERE ${bv_k} = $1;`; try { const response = await this.execute(query, bind_values); if (typeof response === "string") return response; @@ -322,14 +336,28 @@ export class CapacitorClientSQLite { }; } + private trade_product_update_validate(fields: Partial<TradeProductFormFields>): Partial<TradeProductFields> | string[] { + const fields_r = Object.entries(fields).filter(([_, v]) => !!v).reduce((acc: Record<string, IModelsQueryValue>, i) => { + const [key, val] = parse_trade_product_form_fields(i); + acc[key] = val; + return acc; + }, {}); + const schema = TradeProductUpdateSchema; + const parsed_schema = schema.safeParse(fields_r); + if (!parsed_schema.success) return parsed_schema.error.issues.map(i => i.message); + else return { + ...parsed_schema.data + }; + } + public async trade_product_update(opts: ITradeProductUpdate): Promise<true | string[] | ICapacitorClientSQLiteMessage> { - const opts_v = this.trade_product_add_validate(opts.fields); + const opts_v = this.trade_product_update_validate(opts.fields); if (Array.isArray(opts_v)) return opts_v; const fields = this.filter_bind_value_fields(Object.entries(opts_v)); if (!fields.length) return "*-fields"; - const bv_tup = this.trade_product_query_bind_values(opts.on); - const bind_values = [bv_tup[1], ...fields.map(([_, v]) => v)]; - const query = `UPDATE trade_product SET ${fields.map(([k], num) => `${k} = $${1 + num}`).join(", ")} WHERE ${bv_tup[0]} = $1;`; + const [bv_k, bv_v] = this.trade_product_query_bind_values(opts.on); + const bind_values = [...fields.map(([_, v]) => v), bv_v]; + const query = `UPDATE trade_product SET ${fields.map(([k], index) => `${k} = $${1 + index}`).join(", ")} WHERE ${bv_k} = $${bind_values.length};`; try { const response = await this.execute(query, bind_values); if (typeof response === "string") return response; @@ -366,7 +394,7 @@ export class CapacitorClientSQLite { ]; for (const field of this.filter_bind_value_fields(fields)) bind_values_tup.push(field); const bind_values = bind_values_tup.map(([_, v]) => v); - const query = `INSERT INTO nostr_profile (${bind_values_tup.map(([k]) => k).join(", ")}) VALUES (${bind_values_tup.map((_, num) => `$${1 + num}`).join(", ")});`; + const query = `INSERT INTO nostr_profile (${bind_values_tup.map(([k]) => k).join(", ")}) VALUES (${bind_values_tup.map((_, index) => `$${1 + index}`).join(", ")});`; try { const result = await this.execute(query, bind_values); if (typeof result !== "string" && typeof result.changes?.changes === "number" && result.changes.changes > 0) return { id, }; @@ -423,9 +451,9 @@ export class CapacitorClientSQLite { } public async nostr_profile_delete(opts: INostrProfileQueryBindValues): Promise<true | ICapacitorClientSQLiteMessage> { - const bv_tup = this.nostr_profile_query_bind_values(opts); - const bind_values = [bv_tup[1]]; - const query = `DELETE FROM nostr_profile WHERE ${bv_tup[0]} = $1;`; + const [bv_k, bv_v] = this.nostr_profile_query_bind_values(opts); + const bind_values = [bv_v]; + const query = `DELETE FROM nostr_profile WHERE ${bv_k} = $1;`; try { const response = await this.execute(query, bind_values); if (typeof response === "string") return response; @@ -436,14 +464,28 @@ export class CapacitorClientSQLite { }; } + private nostr_profile_update_validate(fields: Partial<NostrProfileFormFields>): Partial<NostrProfileFields> | string[] { + const fields_r = Object.entries(fields).filter(([_, v]) => !!v).reduce((acc: Record<string, IModelsQueryValue>, i) => { + const [key, val] = parse_nostr_profile_form_fields(i); + acc[key] = val; + return acc; + }, {}); + const schema = NostrProfileUpdateSchema.and(NostrProfileMetadataSchema); + const parsed_schema = schema.safeParse(fields_r); + if (!parsed_schema.success) return parsed_schema.error.issues.map(i => i.message); + else return { + ...parsed_schema.data + }; + } + public async nostr_profile_update(opts: INostrProfileUpdate): Promise<true | string[] | ICapacitorClientSQLiteMessage> { - const opts_v = this.nostr_profile_add_validate(opts.fields); + const opts_v = this.nostr_profile_update_validate(opts.fields); if (Array.isArray(opts_v)) return opts_v; const fields = this.filter_bind_value_fields(Object.entries(opts_v)); if (!fields.length) return "*-fields"; - const bv_tup = this.nostr_profile_query_bind_values(opts.on); - const bind_values = [bv_tup[1], ...fields.map(([_, v]) => v)]; - const query = `UPDATE nostr_profile SET ${fields.map(([k], num) => `${k} = $${1 + num}`).join(", ")} WHERE ${bv_tup[0]} = $1;`; + const [bv_k, bv_v] = this.nostr_profile_query_bind_values(opts.on); + const bind_values = [...fields.map(([_, v]) => v), bv_v]; + const query = `UPDATE nostr_profile SET ${fields.map(([k], index) => `${k} = $${1 + index}`).join(", ")} WHERE ${bv_k} = $${bind_values.length};`; try { const response = await this.execute(query, bind_values); if (typeof response === "string") return response;