Skip to content

breaking: add initTokenizer() API method #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const addon = bindings<{
statementClose(stmt: NativeStatement): void;

databaseOpen(path: string): NativeDatabase;
databaseInitTokenizer(db: NativeDatabase): void;
databaseExec(db: NativeDatabase, query: string): void;
databaseClose(db: NativeDatabase): void;

Expand Down Expand Up @@ -350,6 +351,13 @@ export default class Database {
this.#isCacheEnabled = cacheStatements === true;
}

public initTokenizer(): void {
if (this.#native === undefined) {
throw new Error('Database closed');
}
addon.databaseInitTokenizer(this.#native);
}

/**
* Execute one or multiple SQL statements in a given `sql` string.
*
Expand Down
18 changes: 16 additions & 2 deletions src/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ Napi::Error FormatError(Napi::Env env, const char* format, ...) {

Napi::Object Database::Init(Napi::Env env, Napi::Object exports) {
exports["databaseOpen"] = Napi::Function::New(env, &Database::Open);
exports["databaseInitTokenizer"] =
Napi::Function::New(env, &Database::InitTokenizer);
exports["databaseClose"] = Napi::Function::New(env, &Database::Close);
exports["databaseExec"] = Napi::Function::New(env, &Database::Exec);
return exports;
Expand Down Expand Up @@ -159,20 +161,32 @@ Napi::Value Database::Open(const Napi::CallbackInfo& info) {
return db->ThrowSqliteError(env, r);
}

return db->self_ref_.Value();
}

Napi::Value Database::InitTokenizer(const Napi::CallbackInfo& info) {
auto env = info.Env();

auto db = FromExternal(info[0]);
if (db == nullptr) {
return Napi::Value();
}

fts5_api* fts5 = db->GetFTS5API(env);

if (fts5 == nullptr) {
return Napi::Value();
}
SignalTokenizerModule* icu = new SignalTokenizerModule();
r = fts5->xCreateTokenizer(fts5, "signal_tokenizer", icu, &icu->api_object,
int r =
fts5->xCreateTokenizer(fts5, "signal_tokenizer", icu, &icu->api_object,
&SignalTokenizerModule::Destroy);
if (r != SQLITE_OK) {
delete icu;
return db->ThrowSqliteError(env, r);
}

return db->self_ref_.Value();
return Napi::Value();
}

Napi::Value Database::Close(const Napi::CallbackInfo& info) {
Expand Down
1 change: 1 addition & 0 deletions src/addon.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Database {

static Database* FromExternal(const Napi::Value value);
static Napi::Value Open(const Napi::CallbackInfo& info);
static Napi::Value InitTokenizer(const Napi::CallbackInfo& info);
static Napi::Value Close(const Napi::CallbackInfo& info);
static Napi::Value Exec(const Napi::CallbackInfo& info);

Expand Down
12 changes: 12 additions & 0 deletions test/memory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,18 @@ test('bigint mode', () => {
).toEqual(n);
});

test('tokenizer setup', () => {
db.initTokenizer();
});

test('tokenizer setup after close', () => {
db.close();
expect(() => db.initTokenizer()).toThrowError('Database closed');

// Just to fix afterEach
db = new Database();
});

test('signalTokenize', () => {
expect(db.signalTokenize('a b c')).toEqual(['a', 'b', 'c']);
});
Expand Down
Loading