Replies: 3 comments 2 replies
-
|
SQLite's In Bun, you can register it using import { Database } from "bun:sqlite";
const db = new Database("my.db");
// Unfortunately bun:sqlite doesn't expose create_function directly,
// so use the query builder with LIKE/GLOB as alternatives:
// Option 1: Use GLOB (supports * and ? wildcards)
const rows = db.query("SELECT * FROM my_table WHERE pattern GLOB ?").all("Hello*");
// Option 2: Use LIKE (supports % and _ wildcards)
const rows2 = db.query("SELECT * FROM my_table WHERE column LIKE ?").all("%world%");If you truly need regex, the best approach currently is to filter in JS: import { Database } from "bun:sqlite";
const db = new Database("my.db");
const allRows = db.query("SELECT * FROM my_table").all();
const pattern = /Hello,\s+world/;
const filtered = allRows.filter(row => pattern.test(row.column_name));For actual This is worth filing as a feature request specifically for // Hypothetical API
db.createFunction("regexp", (pattern: string, value: string) => {
return new RegExp(pattern).test(value) ? 1 : 0;
});
// Then this would work:
db.query("SELECT * FROM my_table WHERE column REGEXP ?").all("^Hello");
import Database from "better-sqlite3";
const db = new Database("my.db");
db.function("regexp", (pattern, value) =>
new RegExp(pattern).test(value) ? 1 : 0
);
db.prepare("SELECT * FROM my_table WHERE column REGEXP ?").all("^Hello"); |
Beta Was this translation helpful? Give feedback.
-
|
SQLite doesn't include a built-in With Bun's SQLite API, you can register a custom function to enable it: import { Database } from 'bun:sqlite';
const db = new Database(':memory:');
// Register the REGEXP function
db.run(`SELECT 1`); // ensure db is open
// Unfortunately Bun's bun:sqlite doesn't expose create_function yet,
// so REGEXP isn't directly usable.The underlying issue is that Workarounds:
db.query("SELECT * FROM my_table WHERE pattern LIKE '%world%'").all();
db.query("SELECT * FROM my_table WHERE pattern GLOB '*world*'").all();
const rows = db.query('SELECT * FROM my_table').all();
const pattern = /Hello, world/;
const filtered = rows.filter(row => pattern.test(row.column));
A feature request for |
Beta Was this translation helpful? Give feedback.
-
For detail examples about REGEXP pattern |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
For example:
Looks like it's not currently supported out of the box:
Beta Was this translation helpful? Give feedback.
All reactions