smart-sqlite3-filter
    Preparing search index...

    Function buildWhere

    • Translate a query string into a WHERE condition plus its bound parameters, without running it. This is the composable half of search: the caller owns the statement and can add its own joins, ORDER BY, LIMIT/OFFSET, or wrap it in a COUNT(*) — none of which search can express.

      The emitted parameters are named (:column_i_j), so they can be merged with the caller's own named parameters without colliding on position.

      Parameters

      • queryString: string

        the query string, same syntax as search

      • db: DatabaseSync

        the database whose schema decides how each field is compared

      • options: BuildWhereOptions = {}

        options to customize the parsing

      Returns BuiltWhere

      The WHERE condition and the values to bind

      const { where, values } = buildWhere('name:~ester mw:100..500', db, {
      tableName: 'molecules',
      });
      const sql = `SELECT id FROM molecules WHERE ${where || '1 = 1'} ORDER BY mw LIMIT :limit OFFSET :offset`;
      const rows = db.prepare(sql).all({ ...values, limit: 50, offset: 0 });
      const { total } = db
      .prepare(`SELECT COUNT(*) AS total FROM molecules WHERE ${where || '1 = 1'}`)
      .get(values);