11import { COMMA , EMPTY_STRING , strRepeat } from '../../common/strings' ;
2+ import { Cell , Table } from '../../types/store' ;
23import {
34 IdMap2 ,
45 mapEnsure ,
@@ -30,7 +31,6 @@ import {collDel, collHas, collValues} from '../../common/coll';
3031import { isUndefined , promiseAll } from '../../common/other' ;
3132import { setAdd , setNew } from '../../common/set' ;
3233import { Id } from '../../types/common' ;
33- import { Table } from '../../types/store' ;
3434import { escapeId } from './common' ;
3535
3636export type Cmd = ( sql : string , args ?: any [ ] ) => Promise < IdObj < any > [ ] > ;
@@ -49,7 +49,7 @@ export const getCommandFunctions = (
4949 saveTable : (
5050 tableName : string ,
5151 rowIdColumnName : string ,
52- table : Table ,
52+ table : Table | { [ rowId : Id ] : { [ cellId : Id ] : Cell | null } | null } | null ,
5353 deleteEmptyColumns : boolean ,
5454 deleteEmptyTable : boolean ,
5555 partial ?: boolean ,
@@ -130,19 +130,20 @@ export const getCommandFunctions = (
130130 const saveTable = async (
131131 tableName : string ,
132132 rowIdColumnName : string ,
133- table : Table ,
133+ table : Table | { [ rowId : Id ] : { [ cellId : Id ] : Cell | null } | null } | null ,
134134 deleteEmptyColumns : boolean ,
135135 deleteEmptyTable : boolean ,
136136 partial = false ,
137137 ) : Promise < void > => {
138- const cellIds = setNew < string > ( ) ;
138+ const tableCellIds = setNew < string > ( ) ;
139139 objMap ( table ?? { } , ( row ) =>
140- arrayMap ( objIds ( row ) , ( cellId ) => setAdd ( cellIds , cellId ) ) ,
140+ arrayMap ( objIds ( row ?? { } ) , ( cellId ) => setAdd ( tableCellIds , cellId ) ) ,
141141 ) ;
142- const tableColumnNames = collValues ( cellIds ) ;
142+ const tableColumnNames = collValues ( tableCellIds ) ;
143143
144144 // Delete the table
145145 if (
146+ ! partial &&
146147 deleteEmptyTable &&
147148 arrayIsEmpty ( tableColumnNames ) &&
148149 collHas ( schemaMap , tableName )
@@ -183,7 +184,7 @@ export const getCommandFunctions = (
183184 mapSet ( tableSchemaMap , columnName , EMPTY_STRING ) ;
184185 }
185186 } ) ,
186- ...( deleteEmptyColumns
187+ ...( ! partial && deleteEmptyColumns
187188 ? arrayMap (
188189 collValues ( columnNamesAccountedFor ) ,
189190 async ( columnName ) => {
@@ -202,30 +203,43 @@ export const getCommandFunctions = (
202203 }
203204
204205 // Insert or update or delete data
205- if ( ! arrayIsEmpty ( tableColumnNames ) ) {
206- if ( partial ) {
206+ if ( partial ) {
207+ if ( isUndefined ( table ) ) {
208+ await cmd ( 'DELETE FROM' + escapeId ( tableName ) + 'WHERE 1' ) ;
209+ } else {
207210 await promiseAll (
208- objMap (
209- table ,
210- async ( row , rowId ) =>
211+ objMap ( table , async ( row , rowId ) => {
212+ if ( isUndefined ( row ) ) {
213+ await cmd (
214+ 'DELETE FROM' +
215+ escapeId ( tableName ) +
216+ WHERE +
217+ escapeId ( rowIdColumnName ) +
218+ '=?' ,
219+ [ rowId ] ,
220+ ) ;
221+ } else if ( ! arrayIsEmpty ( tableColumnNames ) ) {
211222 await upsert ( cmd , tableName , rowIdColumnName , objIds ( row ) , [
212223 rowId ,
213224 ...objValues ( row ) ,
214- ] ) ,
215- ) ,
225+ ] ) ;
226+ }
227+ } ) ,
216228 ) ;
217- } else {
229+ }
230+ } else {
231+ if ( ! arrayIsEmpty ( tableColumnNames ) ) {
218232 const changingColumnNames = arrayFilter (
219233 mapKeys ( mapGet ( schemaMap , tableName ) ) ,
220234 ( columnName ) => columnName != rowIdColumnName ,
221235 ) ;
222236 const args : any [ ] = [ ] ;
223237 const deleteRowIds : string [ ] = [ ] ;
224- objMap ( table , ( row , rowId ) => {
238+ objMap ( table ?? { } , ( row , rowId ) => {
225239 arrayPush (
226240 args ,
227241 rowId ,
228- ...arrayMap ( changingColumnNames , ( cellId ) => row [ cellId ] ) ,
242+ ...arrayMap ( changingColumnNames , ( cellId ) => row ?. [ cellId ] ) ,
229243 ) ;
230244 arrayPush ( deleteRowIds , rowId ) ;
231245 } ) ;
@@ -246,9 +260,9 @@ export const getCommandFunctions = (
246260 ')' ,
247261 deleteRowIds ,
248262 ) ;
263+ } else if ( collHas ( schemaMap , tableName ) ) {
264+ await cmd ( 'DELETE FROM' + escapeId ( tableName ) + 'WHERE 1' ) ;
249265 }
250- } else if ( ! partial && collHas ( schemaMap , tableName ) ) {
251- await cmd ( 'DELETE FROM' + escapeId ( tableName ) ) ;
252266 }
253267 } ;
254268
0 commit comments