@@ -55,12 +55,17 @@ export const getCommandFunctions = (
5555 row : Row | Values ,
5656 ) => Promise < void > ,
5757 loadTable : ( tableName : string , rowIdColumnName : string ) => Promise < Table > ,
58+ savePartialTable : (
59+ tableName : string ,
60+ rowIdColumnName : string ,
61+ table : Table ,
62+ ) => Promise < void > ,
5863 saveTable : (
5964 tableName : string ,
6065 rowIdColumnName : string ,
61- deleteEmptyColumns : boolean ,
62- deleteEmptyTable : boolean ,
6366 table : Table ,
67+ deleteEmptyColumns ?: boolean ,
68+ deleteEmptyTable ?: boolean ,
6469 ) => Promise < void > ,
6570] => {
6671 const schemaMap : Schema = mapNew ( ) ;
@@ -139,7 +144,7 @@ export const getCommandFunctions = (
139144 rowId : Id ,
140145 row : Row | Values ,
141146 ) : Promise < void > =>
142- await saveTable ( tableName , rowIdColumnName , true , true , { [ rowId ] : row } ) ;
147+ await saveTable ( tableName , rowIdColumnName , { [ rowId ] : row } , true , true ) ;
143148
144149 const loadTable = async (
145150 tableName : string ,
@@ -160,22 +165,28 @@ export const getCommandFunctions = (
160165 )
161166 : { } ;
162167
168+ const savePartialTable = async (
169+ tableName : string ,
170+ rowIdColumnName : string ,
171+ table : Table ,
172+ ) : Promise < void > => await saveTable ( tableName , rowIdColumnName , table ) ;
173+
163174 const saveTable = async (
164175 tableName : string ,
165176 rowIdColumnName : string ,
166- deleteEmptyColumns : boolean ,
167- deleteEmptyTable : boolean ,
168177 table : Table ,
178+ deleteEmptyColumns = false ,
179+ deleteEmptyTable = false ,
169180 ) : Promise < void > => {
170181 const cellIds = setNew < string > ( ) ;
171182 objMap ( table ?? { } , ( row ) =>
172183 arrayMap ( objIds ( row ) , ( cellId ) => setAdd ( cellIds , cellId ) ) ,
173184 ) ;
174- const columnNames = collValues ( cellIds ) ;
185+ const changingColumnNames = collValues ( cellIds ) ;
175186
176187 // Delete the table
177188 if (
178- arrayIsEmpty ( columnNames ) &&
189+ arrayIsEmpty ( changingColumnNames ) &&
179190 collHas ( schemaMap , tableName ) &&
180191 deleteEmptyTable
181192 ) {
@@ -185,11 +196,11 @@ export const getCommandFunctions = (
185196 }
186197
187198 // Create the table or alter or drop columns
188- if ( ! arrayIsEmpty ( columnNames ) && ! collHas ( schemaMap , tableName ) ) {
199+ if ( ! arrayIsEmpty ( changingColumnNames ) && ! collHas ( schemaMap , tableName ) ) {
189200 await cmd (
190201 `CREATE TABLE${ escapeId ( tableName ) } (${ escapeId ( rowIdColumnName ) } ` +
191202 `PRIMARY KEY ON CONFLICT REPLACE${ arrayJoin (
192- arrayMap ( columnNames , ( cellId ) => COMMA + escapeId ( cellId ) ) ,
203+ arrayMap ( changingColumnNames , ( cellId ) => COMMA + escapeId ( cellId ) ) ,
193204 ) } );`,
194205 ) ;
195206 mapSet (
@@ -198,7 +209,7 @@ export const getCommandFunctions = (
198209 mapNew ( [
199210 [ rowIdColumnName , EMPTY_STRING ] ,
200211 ...arrayMap (
201- columnNames ,
212+ changingColumnNames ,
202213 ( columnName ) => [ columnName , EMPTY_STRING ] as [ string , string ] ,
203214 ) ,
204215 ] ) ,
@@ -207,7 +218,7 @@ export const getCommandFunctions = (
207218 const tableSchemaMap = mapGet ( schemaMap , tableName ) ;
208219 const columnNamesAccountedFor = setNew ( mapKeys ( tableSchemaMap ) ) ;
209220 await promiseAll ( [
210- ...arrayMap ( columnNames , async ( columnName ) => {
221+ ...arrayMap ( changingColumnNames , async ( columnName ) => {
211222 if ( ! collDel ( columnNamesAccountedFor , columnName ) ) {
212223 await cmd (
213224 `ALTER TABLE${ escapeId ( tableName ) } ADD${ escapeId ( columnName ) } ` ,
@@ -234,22 +245,22 @@ export const getCommandFunctions = (
234245 }
235246
236247 // Insert or update or delete data
237- if ( ! arrayIsEmpty ( columnNames ) ) {
248+ if ( ! arrayIsEmpty ( changingColumnNames ) ) {
238249 const args : any [ ] = [ ] ;
239250 const deleteRowIds : string [ ] = [ ] ;
240- const allColumnNames = arrayFilter (
251+ const schemaColumnNames = arrayFilter (
241252 mapKeys ( mapGet ( schemaMap , tableName ) ) ,
242253 ( columnName ) => columnName != rowIdColumnName ,
243254 ) ;
244255 objMap ( table , ( row , rowId ) => {
245256 arrayPush (
246257 args ,
247258 rowId ,
248- ...arrayMap ( allColumnNames , ( cellId ) => row [ cellId ] ) ,
259+ ...arrayMap ( schemaColumnNames , ( cellId ) => row [ cellId ] ) ,
249260 ) ;
250261 arrayPush ( deleteRowIds , rowId ) ;
251262 } ) ;
252- await upsert ( cmd , tableName , rowIdColumnName , allColumnNames , args ) ;
263+ await upsert ( cmd , tableName , rowIdColumnName , schemaColumnNames , args ) ;
253264 await cmd (
254265 'DELETE FROM' +
255266 escapeId ( tableName ) +
@@ -270,6 +281,7 @@ export const getCommandFunctions = (
270281 loadSingleRowTable ,
271282 saveSingleRowTable ,
272283 loadTable ,
284+ savePartialTable ,
273285 saveTable ,
274286 ] ;
275287} ;
@@ -278,7 +290,7 @@ const upsert = async (
278290 cmd : Cmd ,
279291 tableName : string ,
280292 rowIdColumnName : string ,
281- allColumnNames : string [ ] ,
293+ schemaColumnNames : string [ ] ,
282294 args : any [ ] ,
283295) =>
284296 await cmd (
@@ -287,19 +299,22 @@ const upsert = async (
287299 '(' +
288300 escapeId ( rowIdColumnName ) +
289301 arrayJoin (
290- arrayMap ( allColumnNames , ( columnName ) => COMMA + escapeId ( columnName ) ) ,
302+ arrayMap (
303+ schemaColumnNames ,
304+ ( columnName ) => COMMA + escapeId ( columnName ) ,
305+ ) ,
291306 ) +
292307 ')VALUES' +
293308 strRepeat (
294- `,(?${ strRepeat ( ',?' , arrayLength ( allColumnNames ) ) } )` ,
295- arrayLength ( args ) / ( arrayLength ( allColumnNames ) + 1 ) ,
309+ `,(?${ strRepeat ( ',?' , arrayLength ( schemaColumnNames ) ) } )` ,
310+ arrayLength ( args ) / ( arrayLength ( schemaColumnNames ) + 1 ) ,
296311 ) . substring ( 1 ) +
297312 'ON CONFLICT(' +
298313 escapeId ( rowIdColumnName ) +
299314 ')DO UPDATE SET' +
300315 arrayJoin (
301316 arrayMap (
302- allColumnNames ,
317+ schemaColumnNames ,
303318 ( columnName ) =>
304319 escapeId ( columnName ) + '=excluded.' + escapeId ( columnName ) ,
305320 ) ,
0 commit comments