@@ -179,7 +179,7 @@ class Database {
179179
180180 addRecord = async (
181181 tableName : string ,
182- model : Record < string , string >
182+ model : TableSignatureValue
183183 ) : Promise < [ ResultSet ] | undefined | null > => {
184184 try {
185185 return await this . DB ?. executeSql (
@@ -199,15 +199,33 @@ class Database {
199199
200200 editRecord = async (
201201 tableName : string ,
202- model : Record < string , string >
202+ model : TableSignatureValue ,
203+ initModel : Record < string , any >
203204 ) : Promise < [ ResultSet ] | undefined | null > => {
204205 try {
206+ const where = Object . keys ( initModel ) . reduce ( ( acc , key , index ) => {
207+ const value = initModel [ key ] ;
208+
209+ if ( ! value || ( typeof value === 'string' && value . length > 255 ) || typeof value === 'object' )
210+ return acc ;
211+
212+ return (
213+ acc +
214+ `${ index !== 0 ? ' AND ' : '' } ${ key } =${ typeof value === 'string' ? "'" + value + "'" : value } `
215+ ) ;
216+ } , '' ) ;
217+
218+ const dataToSet = Object . keys ( model ) . reduce ( ( acc , key , index , arr ) => {
219+ const rawValue = model [ key ] ;
220+ if ( typeof rawValue === 'object' || rawValue === null ) return acc ;
221+
222+ const value = typeof rawValue === "string" ? `'${ rawValue } '` : rawValue ;
223+
224+ return acc + ` ${ key } = ${ value } ` + ( index + 1 === arr . length ? "" : "," ) ;
225+ } , "" ) ;
226+
205227 return await this . DB ?. executeSql (
206- `INSERT OR REPLACE INTO ${ tableName } (${ Object . keys ( model ) . join ( ',' ) } )
207- VALUES (${ Object . keys ( model )
208- . map ( ( ) => '?' )
209- . join ( ',' ) } )`,
210- Object . values ( model )
228+ `UPDATE ${ tableName } SET ${ dataToSet } WHERE ${ where } ` ,
211229 ) ;
212230 } catch ( err ) {
213231 const message = getErrorText ( err ) ;
0 commit comments