from mods.db import db, Catalog, CatalogIndex import re def add_catalog(name): with db.atomic(): catalog = Catalog.create(name=name) CatalogIndex.insert( { CatalogIndex.rowid: catalog.id, CatalogIndex.text: name } ).execute() return catalog def search_catalogs(q): q = re.sub(r'\s+', ' ', q).strip() q = ' '.join([ f'{x}*' for x in q.split(' ')]) with db.atomic(): return (Catalog.select() .join( CatalogIndex, on=(Catalog.id == CatalogIndex.rowid)) .where(CatalogIndex.match(q)) .order_by(CatalogIndex.bm25()))