@@ -64,8 +64,9 @@ export const getCommandFunctions = (
6464 tableName : string ,
6565 rowIdColumnName : string ,
6666 table : Table ,
67- deleteEmptyColumns ?: boolean ,
68- deleteEmptyTable ?: boolean ,
67+ deleteEmptyColumns : boolean ,
68+ deleteEmptyTable : boolean ,
69+ partial ?: boolean ,
6970 ) => Promise < void > ,
7071] => {
7172 const schemaMap : Schema = mapNew ( ) ;
@@ -169,25 +170,27 @@ export const getCommandFunctions = (
169170 tableName : string ,
170171 rowIdColumnName : string ,
171172 table : Table ,
172- ) : Promise < void > => await saveTable ( tableName , rowIdColumnName , table ) ;
173+ ) : Promise < void > =>
174+ await saveTable ( tableName , rowIdColumnName , table , false , false , true ) ;
173175
174176 const saveTable = async (
175177 tableName : string ,
176178 rowIdColumnName : string ,
177179 table : Table ,
178- deleteEmptyColumns = false ,
179- deleteEmptyTable = false ,
180+ deleteEmptyColumns : boolean ,
181+ deleteEmptyTable : boolean ,
182+ partial = false ,
180183 ) : Promise < void > => {
181184 const cellIds = setNew < string > ( ) ;
182185 objMap ( table ?? { } , ( row ) =>
183186 arrayMap ( objIds ( row ) , ( cellId ) => setAdd ( cellIds , cellId ) ) ,
184187 ) ;
185- const changingColumnNames = collValues ( cellIds ) ;
188+ const tableColumnNames = collValues ( cellIds ) ;
186189
187190 // Delete the table
188191 if (
189192 deleteEmptyTable &&
190- arrayIsEmpty ( changingColumnNames ) &&
193+ arrayIsEmpty ( tableColumnNames ) &&
191194 collHas ( schemaMap , tableName )
192195 ) {
193196 await cmd ( 'DROP TABLE' + escapeId ( tableName ) ) ;
@@ -196,11 +199,11 @@ export const getCommandFunctions = (
196199 }
197200
198201 // Create the table or alter or drop columns
199- if ( ! arrayIsEmpty ( changingColumnNames ) && ! collHas ( schemaMap , tableName ) ) {
202+ if ( ! arrayIsEmpty ( tableColumnNames ) && ! collHas ( schemaMap , tableName ) ) {
200203 await cmd (
201204 `CREATE TABLE${ escapeId ( tableName ) } (${ escapeId ( rowIdColumnName ) } ` +
202205 `PRIMARY KEY ON CONFLICT REPLACE${ arrayJoin (
203- arrayMap ( changingColumnNames , ( cellId ) => COMMA + escapeId ( cellId ) ) ,
206+ arrayMap ( tableColumnNames , ( cellId ) => COMMA + escapeId ( cellId ) ) ,
204207 ) } );`,
205208 ) ;
206209 mapSet (
@@ -209,7 +212,7 @@ export const getCommandFunctions = (
209212 mapNew ( [
210213 [ rowIdColumnName , EMPTY_STRING ] ,
211214 ...arrayMap (
212- changingColumnNames ,
215+ tableColumnNames ,
213216 ( columnName ) => [ columnName , EMPTY_STRING ] as [ string , string ] ,
214217 ) ,
215218 ] ) ,
@@ -218,7 +221,7 @@ export const getCommandFunctions = (
218221 const tableSchemaMap = mapGet ( schemaMap , tableName ) ;
219222 const columnNamesAccountedFor = setNew ( mapKeys ( tableSchemaMap ) ) ;
220223 await promiseAll ( [
221- ...arrayMap ( changingColumnNames , async ( columnName ) => {
224+ ...arrayMap ( tableColumnNames , async ( columnName ) => {
222225 if ( ! collDel ( columnNamesAccountedFor , columnName ) ) {
223226 await cmd (
224227 `ALTER TABLE${ escapeId ( tableName ) } ADD${ escapeId ( columnName ) } ` ,
@@ -245,33 +248,37 @@ export const getCommandFunctions = (
245248 }
246249
247250 // Insert or update or delete data
248- if ( ! arrayIsEmpty ( changingColumnNames ) ) {
251+ if ( ! arrayIsEmpty ( tableColumnNames ) ) {
249252 const args : any [ ] = [ ] ;
250253 const deleteRowIds : string [ ] = [ ] ;
251- const schemaColumnNames = arrayFilter (
252- mapKeys ( mapGet ( schemaMap , tableName ) ) ,
253- ( columnName ) => columnName != rowIdColumnName ,
254- ) ;
254+ const changingColumnNames = partial
255+ ? tableColumnNames
256+ : arrayFilter (
257+ mapKeys ( mapGet ( schemaMap , tableName ) ) ,
258+ ( columnName ) => columnName != rowIdColumnName ,
259+ ) ;
255260 objMap ( table , ( row , rowId ) => {
256261 arrayPush (
257262 args ,
258263 rowId ,
259- ...arrayMap ( schemaColumnNames , ( cellId ) => row [ cellId ] ) ,
264+ ...arrayMap ( changingColumnNames , ( cellId ) => row [ cellId ] ) ,
260265 ) ;
261266 arrayPush ( deleteRowIds , rowId ) ;
262267 } ) ;
263- await upsert ( cmd , tableName , rowIdColumnName , schemaColumnNames , args ) ;
264- await cmd (
265- 'DELETE FROM' +
266- escapeId ( tableName ) +
267- WHERE +
268- escapeId ( rowIdColumnName ) +
269- 'NOT IN(' +
270- getPlaceholders ( deleteRowIds ) +
271- ')' ,
272- deleteRowIds ,
273- ) ;
274- } else if ( collHas ( schemaMap , tableName ) ) {
268+ await upsert ( cmd , tableName , rowIdColumnName , changingColumnNames , args ) ;
269+ if ( ! partial ) {
270+ await cmd (
271+ 'DELETE FROM' +
272+ escapeId ( tableName ) +
273+ WHERE +
274+ escapeId ( rowIdColumnName ) +
275+ 'NOT IN(' +
276+ getPlaceholders ( deleteRowIds ) +
277+ ')' ,
278+ deleteRowIds ,
279+ ) ;
280+ }
281+ } else if ( ! partial && collHas ( schemaMap , tableName ) ) {
275282 await cmd ( 'DELETE FROM' + escapeId ( tableName ) ) ;
276283 }
277284 } ;
@@ -290,7 +297,7 @@ const upsert = async (
290297 cmd : Cmd ,
291298 tableName : string ,
292299 rowIdColumnName : string ,
293- schemaColumnNames : string [ ] ,
300+ changingColumnNames : string [ ] ,
294301 args : any [ ] ,
295302) =>
296303 await cmd (
@@ -300,21 +307,21 @@ const upsert = async (
300307 escapeId ( rowIdColumnName ) +
301308 arrayJoin (
302309 arrayMap (
303- schemaColumnNames ,
310+ changingColumnNames ,
304311 ( columnName ) => COMMA + escapeId ( columnName ) ,
305312 ) ,
306313 ) +
307314 ')VALUES' +
308315 strRepeat (
309- `,(?${ strRepeat ( ',?' , arrayLength ( schemaColumnNames ) ) } )` ,
310- arrayLength ( args ) / ( arrayLength ( schemaColumnNames ) + 1 ) ,
316+ `,(?${ strRepeat ( ',?' , arrayLength ( changingColumnNames ) ) } )` ,
317+ arrayLength ( args ) / ( arrayLength ( changingColumnNames ) + 1 ) ,
311318 ) . substring ( 1 ) +
312319 'ON CONFLICT(' +
313320 escapeId ( rowIdColumnName ) +
314321 ')DO UPDATE SET' +
315322 arrayJoin (
316323 arrayMap (
317- schemaColumnNames ,
324+ changingColumnNames ,
318325 ( columnName ) =>
319326 escapeId ( columnName ) + '=excluded.' + escapeId ( columnName ) ,
320327 ) ,
0 commit comments