@@ -1368,13 +1368,13 @@ module.exports.insertData = async (req, res) => {
13681368 delete incomingData . isDeleted ;
13691369 delete incomingData . deletedAt ;
13701370
1371- let docSize = 0 ;
1371+ let estimatedDocSize = 0 ;
13721372 if ( ! project . resources . db . isExternal ) {
1373- docSize = Buffer . byteLength ( JSON . stringify ( incomingData ) ) ;
1373+ estimatedDocSize = Buffer . byteLength ( JSON . stringify ( incomingData ) ) ;
13741374
13751375 const limit = project . databaseLimit || 20 * 1024 * 1024 ;
13761376
1377- if ( ( project . databaseUsed || 0 ) + docSize > limit ) {
1377+ if ( ( project . databaseUsed || 0 ) + estimatedDocSize > limit ) {
13781378 return res
13791379 . status ( 403 )
13801380 . json ( { error : "Database limit exceeded. Delete some data." } ) ;
@@ -1392,7 +1392,8 @@ module.exports.insertData = async (req, res) => {
13921392 const result = await model . create ( incomingData ) ;
13931393
13941394 if ( ! project . resources . db . isExternal ) {
1395- project . databaseUsed = ( project . databaseUsed || 0 ) + docSize ;
1395+ const actualDocSize = mongoose . mongo . BSON . calculateObjectSize ( result . toObject ( ) ) ;
1396+ project . databaseUsed = ( project . databaseUsed || 0 ) + actualDocSize ;
13961397 }
13971398 await project . save ( ) ;
13981399
@@ -1443,30 +1444,55 @@ module.exports.deleteRow = async (req, res, next) => {
14431444 project . resources . db . isExternal ,
14441445 ) ;
14451446
1446- const result = await Model . findOneAndUpdate (
1447- { _id : id , isDeleted : { $ne : true } } ,
1448- {
1449- $set : {
1450- isDeleted : true ,
1451- deletedAt : new Date ( )
1452- }
1453- } ,
1454- { new : false }
1455- ) . lean ( ) ;
1447+ const isPermanent = req . query . permanent === 'true' ;
1448+
1449+ let result ;
1450+ if ( isPermanent ) {
1451+ result = await Model . findOneAndDelete ( { _id : id } ) . lean ( ) ;
1452+ } else {
1453+ result = await Model . findOneAndUpdate (
1454+ { _id : id , isDeleted : { $ne : true } } ,
1455+ {
1456+ $set : {
1457+ isDeleted : true ,
1458+ deletedAt : new Date ( )
1459+ }
1460+ } ,
1461+ { new : false }
1462+ ) . lean ( ) ;
1463+ }
14561464
14571465 if ( ! result ) {
14581466 return next ( new AppError ( 404 , "Document not found." ) ) ;
14591467 }
14601468
1461- // We don't decrement databaseUsed here because the document still occupies space.
1462- // It will be decremented during hard delete in the background worker.
1463- try {
1464- await enqueueCollectionCleanup ( projectId , collectionName ) ;
1465- } catch ( err ) {
1466- console . error ( "Failed to enqueue trash cleanup job" , { projectId, collectionName, err } ) ;
1469+ if ( isPermanent ) {
1470+ if ( ! project . resources . db . isExternal ) {
1471+ try {
1472+ const docSize = mongoose . mongo . BSON . calculateObjectSize ( result ) ;
1473+ await Project . updateOne (
1474+ { _id : project . _id } ,
1475+ { $inc : { databaseUsed : - docSize } }
1476+ ) ;
1477+ } catch ( sizeErr ) {
1478+ console . warn ( "Failed to calculate document size on permanent delete" , sizeErr ) ;
1479+ }
1480+ }
1481+ } else {
1482+ // We don't decrement databaseUsed here because the document still occupies space.
1483+ // It will be decremented during hard delete in the background worker.
1484+ try {
1485+ await enqueueCollectionCleanup ( projectId , collectionName ) ;
1486+ } catch ( err ) {
1487+ console . error ( "Failed to enqueue trash cleanup job" , { projectId, collectionName, err } ) ;
1488+ }
14671489 }
14681490
1469- res . json ( { success : true , data : { id : result . _id } , message : "Document moved to trash" } ) ;
1491+ res . json ( {
1492+ success : true ,
1493+ data : { id : result . _id } ,
1494+ message : isPermanent ? "Document permanently deleted" : "Document moved to trash"
1495+ } ) ;
14701496 } catch ( err ) {
14711497 console . error ( "Delete Error:" , err ) ;
14721498 next ( new AppError ( 500 , "Failed to delete document" ) ) ;
@@ -1593,11 +1619,11 @@ module.exports.editRow = async (req, res) => {
15931619 delete req . body . isDeleted ;
15941620 delete req . body . deletedAt ;
15951621
1596- const oldSize = Buffer . byteLength ( JSON . stringify ( docToEdit . toObject ( ) ) ) ;
1622+ const oldSize = mongoose . mongo . BSON . calculateObjectSize ( docToEdit . toObject ( ) ) ;
15971623
15981624 docToEdit . set ( req . body ) ;
15991625
1600- const newSize = Buffer . byteLength ( JSON . stringify ( docToEdit . toObject ( ) ) ) ;
1626+ const newSize = mongoose . mongo . BSON . calculateObjectSize ( docToEdit . toObject ( ) ) ;
16011627 const sizeDiff = newSize - oldSize ;
16021628
16031629 if ( ! project . resources . db . isExternal ) {
0 commit comments