@@ -631,7 +631,7 @@ class MDStore {
631631 * @property {1|-1 } [order]
632632 * @property {boolean } [pagination]
633633 *
634- * @returns { nb.ObjectMD[] }
634+ * @returns { Promise< nb.ObjectMD[]> }
635635 */
636636 async find_objects ( {
637637 bucket_id,
@@ -695,6 +695,80 @@ class MDStore {
695695 } ) ;
696696 }
697697
698+ /**
699+ * TODO add support for versioning or add another function to support versioning.
700+ * @typedef {Object } DeleteObjectsParams
701+ * @property {nb.ID } bucket_id
702+ * @property {RegExp } key
703+ * @property {number } [max_create_time]
704+ * @property {number } [max_size]
705+ * @property {number } [min_size]
706+ * @property {Array<{ key: string; value: string; }> } [tagging]
707+ * @property {number } [limit]
708+ * @property {boolean } [return_results]
709+ *
710+ * @param {DeleteObjectsParams } params
711+ * @returns {Promise<nb.ObjectMD[]> }
712+ */
713+ async delete_objects_by_query ( {
714+ bucket_id,
715+ key,
716+ max_create_time,
717+ max_size,
718+ min_size,
719+ tagging,
720+ limit,
721+ return_results = false ,
722+ } ) {
723+ const params = [ new Date ( ) ] ;
724+ const sql_conditions = [ ] ;
725+ if ( key ) {
726+ params . push ( key . source ) ;
727+ sql_conditions . push ( `data->>'key' ~ $${ params . length } ` ) ;
728+ }
729+ if ( max_size !== undefined ) {
730+ params . push ( max_size ) ;
731+ sql_conditions . push ( `(data->>'size')::BIGINT < $${ params . length } ` ) ;
732+ }
733+ if ( min_size !== undefined ) {
734+ params . push ( min_size ) ;
735+ sql_conditions . push ( `(data->>'size')::BIGINT > $${ params . length } ` ) ;
736+ }
737+ if ( tagging && tagging . length ) {
738+ params . push ( JSON . stringify ( tagging ) ) ;
739+ sql_conditions . push ( `(data->>'tagging')::jsonb @> $${ params . length } ::jsonb` ) ;
740+ }
741+ if ( max_create_time ) {
742+ params . push ( new Date ( moment . unix ( max_create_time ) . toISOString ( ) ) . toISOString ( ) ) ;
743+ sql_conditions . push ( `data->>'create_time' < $${ params . length } ` ) ;
744+ }
745+
746+ const sql_limit = limit === undefined ? "" : `LIMIT ${ limit } ` ;
747+
748+ let query = `
749+ WITH rows AS (
750+ SELECT _id
751+ FROM ${ this . _objects . name }
752+ WHERE
753+ ${ sql_and_conditions (
754+ `data->>'bucket' = '${ bucket_id } '` ,
755+ ...sql_conditions ,
756+ `data->'deleted' IS NULL` ,
757+ `data->'upload_started' IS NULL` ,
758+ `data->'version_enabled' IS NULL` ,
759+ ) }
760+ ${ sql_limit }
761+ )
762+ UPDATE ${ this . _objects . name }
763+ SET data = jsonb_set(data, '{deleted}', to_jsonb($1::text), true)
764+ WHERE _id IN (
765+ SELECT rows._id FROM rows
766+ )` ;
767+ query += return_results ? ' RETURNING *;' : ';' ;
768+ const result = await this . _objects . executeSQL ( query , params ) ;
769+ return return_results ? result . rows : [ ] ;
770+ }
771+
698772 async find_unreclaimed_objects ( limit ) {
699773 const results = await this . _objects . find ( {
700774 deleted : { $exists : true } ,
0 commit comments