@@ -16,8 +16,8 @@ import {
1616 objIsEmpty ,
1717 objMap ,
1818 objNew ,
19+ objValues ,
1920} from '../../common/obj' ;
20- import { Row , Table , Values } from '../../types/store' ;
2121import { SINGLE_ROW_ID , escapeId } from './common' ;
2222import {
2323 arrayFilter ,
@@ -31,6 +31,7 @@ import {collDel, collHas, collValues} from '../../common/coll';
3131import { isUndefined , promiseAll } from '../../common/other' ;
3232import { setAdd , setNew } from '../../common/set' ;
3333import { Id } from '../../types/common' ;
34+ import { Table } from '../../types/store' ;
3435
3536export type Cmd = ( sql : string , args ?: any [ ] ) => Promise < IdObj < any > [ ] > ;
3637type Schema = IdMap2 < string > ;
@@ -48,18 +49,7 @@ export const getCommandFunctions = (
4849 tableName : string ,
4950 rowIdColumnName : string ,
5051 ) => Promise < IdObj < any > | null > ,
51- saveSingleRowTable : (
52- table : string ,
53- rowIdColumnName : string ,
54- rowId : Id ,
55- row : Row | Values ,
56- ) => Promise < void > ,
5752 loadTable : ( tableName : string , rowIdColumnName : string ) => Promise < Table > ,
58- savePartialTable : (
59- tableName : string ,
60- rowIdColumnName : string ,
61- table : Table ,
62- ) => Promise < void > ,
6353 saveTable : (
6454 tableName : string ,
6555 rowIdColumnName : string ,
@@ -139,14 +129,6 @@ export const getCommandFunctions = (
139129 return arrayIsEmpty ( rows ) ? null : objDel ( rows [ 0 ] , rowIdColumnName ) ;
140130 } ;
141131
142- const saveSingleRowTable = async (
143- tableName : string ,
144- rowIdColumnName : string ,
145- rowId : Id ,
146- row : Row | Values ,
147- ) : Promise < void > =>
148- await saveTable ( tableName , rowIdColumnName , { [ rowId ] : row } , true , true ) ;
149-
150132 const loadTable = async (
151133 tableName : string ,
152134 rowIdColumnName : string ,
@@ -166,13 +148,6 @@ export const getCommandFunctions = (
166148 )
167149 : { } ;
168150
169- const savePartialTable = async (
170- tableName : string ,
171- rowIdColumnName : string ,
172- table : Table ,
173- ) : Promise < void > =>
174- await saveTable ( tableName , rowIdColumnName , table , false , false , true ) ;
175-
176151 const saveTable = async (
177152 tableName : string ,
178153 rowIdColumnName : string ,
@@ -249,24 +224,39 @@ export const getCommandFunctions = (
249224
250225 // Insert or update or delete data
251226 if ( ! arrayIsEmpty ( tableColumnNames ) ) {
252- const args : any [ ] = [ ] ;
253- const deleteRowIds : string [ ] = [ ] ;
254- const changingColumnNames = partial
255- ? tableColumnNames
256- : arrayFilter (
257- mapKeys ( mapGet ( schemaMap , tableName ) ) ,
258- ( columnName ) => columnName != rowIdColumnName ,
227+ if ( partial ) {
228+ await promiseAll (
229+ objMap (
230+ table ,
231+ async ( row , rowId ) =>
232+ await upsert ( cmd , tableName , rowIdColumnName , objIds ( row ) , [
233+ rowId ,
234+ ...objValues ( row ) ,
235+ ] ) ,
236+ ) ,
237+ ) ;
238+ } else {
239+ const changingColumnNames = arrayFilter (
240+ mapKeys ( mapGet ( schemaMap , tableName ) ) ,
241+ ( columnName ) => columnName != rowIdColumnName ,
242+ ) ;
243+ const args : any [ ] = [ ] ;
244+ const deleteRowIds : string [ ] = [ ] ;
245+ objMap ( table , ( row , rowId ) => {
246+ arrayPush (
247+ args ,
248+ rowId ,
249+ ...arrayMap ( changingColumnNames , ( cellId ) => row [ cellId ] ) ,
259250 ) ;
260- objMap ( table , ( row , rowId ) => {
261- arrayPush (
251+ arrayPush ( deleteRowIds , rowId ) ;
252+ } ) ;
253+ await upsert (
254+ cmd ,
255+ tableName ,
256+ rowIdColumnName ,
257+ changingColumnNames ,
262258 args ,
263- rowId ,
264- ...arrayMap ( changingColumnNames , ( cellId ) => row [ cellId ] ) ,
265259 ) ;
266- arrayPush ( deleteRowIds , rowId ) ;
267- } ) ;
268- await upsert ( cmd , tableName , rowIdColumnName , changingColumnNames , args ) ;
269- if ( ! partial ) {
270260 await cmd (
271261 'DELETE FROM' +
272262 escapeId ( tableName ) +
@@ -283,14 +273,7 @@ export const getCommandFunctions = (
283273 }
284274 } ;
285275
286- return [
287- refreshSchema ,
288- loadSingleRowTable ,
289- saveSingleRowTable ,
290- loadTable ,
291- savePartialTable ,
292- saveTable ,
293- ] ;
276+ return [ refreshSchema , loadSingleRowTable , loadTable , saveTable ] ;
294277} ;
295278
296279const upsert = async (
0 commit comments