5757 UnknownModelException ,
5858)
5959from invokeai .app .services .shared .pagination import PaginatedResults
60+ from invokeai .app .services .shared .sqlite .sqlite_common import SQLiteDirection
6061from invokeai .app .services .shared .sqlite .sqlite_database import SqliteDatabase
6162from invokeai .backend .model_manager .configs .factory import AnyModelConfig , ModelConfigFactory
6263from invokeai .backend .model_manager .taxonomy import BaseModelType , ModelFormat , ModelType
@@ -257,6 +258,7 @@ def search_by_attr(
257258 model_type : Optional [ModelType ] = None ,
258259 model_format : Optional [ModelFormat ] = None ,
259260 order_by : ModelRecordOrderBy = ModelRecordOrderBy .Default ,
261+ direction : SQLiteDirection = SQLiteDirection .Ascending ,
260262 ) -> List [AnyModelConfig ]:
261263 """
262264 Return models matching name, base and/or type.
@@ -266,18 +268,24 @@ def search_by_attr(
266268 :param model_type: Filter by type of model (optional)
267269 :param model_format: Filter by model format (e.g. "diffusers") (optional)
268270 :param order_by: Result order
271+ :param direction: Result direction
269272
270273 If none of the optional filters are passed, will return all
271274 models in the database.
272275 """
273276 with self ._db .transaction () as cursor :
274277 assert isinstance (order_by , ModelRecordOrderBy )
278+ order_dir = "DESC" if direction == SQLiteDirection .Descending else "ASC"
275279 ordering = {
276- ModelRecordOrderBy .Default : "type, base, name, format" ,
280+ ModelRecordOrderBy .Default : f "type { order_dir } , base COLLATE NOCASE { order_dir } , name COLLATE NOCASE { order_dir } , format" ,
277281 ModelRecordOrderBy .Type : "type" ,
278- ModelRecordOrderBy .Base : "base" ,
279- ModelRecordOrderBy .Name : "name" ,
282+ ModelRecordOrderBy .Base : "base COLLATE NOCASE " ,
283+ ModelRecordOrderBy .Name : "name COLLATE NOCASE " ,
280284 ModelRecordOrderBy .Format : "format" ,
285+ ModelRecordOrderBy .Size : "IFNULL(json_extract(config, '$.file_size'), 0)" ,
286+ ModelRecordOrderBy .DateAdded : "created_at" ,
287+ ModelRecordOrderBy .DateModified : "updated_at" ,
288+ ModelRecordOrderBy .Path : "path" ,
281289 }
282290
283291 where_clause : list [str ] = []
@@ -301,7 +309,7 @@ def search_by_attr(
301309 SELECT config
302310 FROM models
303311 { where }
304- ORDER BY { ordering [order_by ]} -- using ? to bind doesn't work here for some reason;
312+ ORDER BY { ordering [order_by ]} { order_dir } -- using ? to bind doesn't work here for some reason;
305313 """ ,
306314 tuple (bindings ),
307315 )
@@ -357,17 +365,26 @@ def search_by_hash(self, hash: str) -> List[AnyModelConfig]:
357365 return results
358366
359367 def list_models (
360- self , page : int = 0 , per_page : int = 10 , order_by : ModelRecordOrderBy = ModelRecordOrderBy .Default
368+ self ,
369+ page : int = 0 ,
370+ per_page : int = 10 ,
371+ order_by : ModelRecordOrderBy = ModelRecordOrderBy .Default ,
372+ direction : SQLiteDirection = SQLiteDirection .Ascending ,
361373 ) -> PaginatedResults [ModelSummary ]:
362374 """Return a paginated summary listing of each model in the database."""
363375 with self ._db .transaction () as cursor :
364376 assert isinstance (order_by , ModelRecordOrderBy )
377+ order_dir = "DESC" if direction == SQLiteDirection .Descending else "ASC"
365378 ordering = {
366- ModelRecordOrderBy .Default : "type, base, name, format" ,
379+ ModelRecordOrderBy .Default : f "type { order_dir } , base COLLATE NOCASE { order_dir } , name COLLATE NOCASE { order_dir } , format" ,
367380 ModelRecordOrderBy .Type : "type" ,
368- ModelRecordOrderBy .Base : "base" ,
369- ModelRecordOrderBy .Name : "name" ,
381+ ModelRecordOrderBy .Base : "base COLLATE NOCASE " ,
382+ ModelRecordOrderBy .Name : "name COLLATE NOCASE " ,
370383 ModelRecordOrderBy .Format : "format" ,
384+ ModelRecordOrderBy .Size : "IFNULL(json_extract(config, '$.file_size'), 0)" ,
385+ ModelRecordOrderBy .DateAdded : "created_at" ,
386+ ModelRecordOrderBy .DateModified : "updated_at" ,
387+ ModelRecordOrderBy .Path : "path" ,
371388 }
372389
373390 # Lock so that the database isn't updated while we're doing the two queries.
@@ -385,7 +402,7 @@ def list_models(
385402 f"""--sql
386403 SELECT config
387404 FROM models
388- ORDER BY { ordering [order_by ]} -- using ? to bind doesn't work here for some reason
405+ ORDER BY { ordering [order_by ]} { order_dir } -- using ? to bind doesn't work here for some reason
389406 LIMIT ?
390407 OFFSET ?;
391408 """ ,
0 commit comments