Skip to content

Commit 76b0627

Browse files
breaking: add initTokenizer() API method
1 parent 8560210 commit 76b0627

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

lib/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const addon = bindings<{
4242
statementClose(stmt: NativeStatement): void;
4343

4444
databaseOpen(path: string): NativeDatabase;
45+
databaseInitTokenizer(db: NativeDatabase): void;
4546
databaseExec(db: NativeDatabase, query: string): void;
4647
databaseClose(db: NativeDatabase): void;
4748

@@ -350,6 +351,13 @@ export default class Database {
350351
this.#isCacheEnabled = cacheStatements === true;
351352
}
352353

354+
public initTokenizer(): void {
355+
if (this.#native === undefined) {
356+
throw new Error('Database closed');
357+
}
358+
addon.databaseInitTokenizer(this.#native);
359+
}
360+
353361
/**
354362
* Execute one or multiple SQL statements in a given `sql` string.
355363
*

src/addon.cc

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ Napi::Error FormatError(Napi::Env env, const char* format, ...) {
9898

9999
Napi::Object Database::Init(Napi::Env env, Napi::Object exports) {
100100
exports["databaseOpen"] = Napi::Function::New(env, &Database::Open);
101+
exports["databaseInitTokenizer"] =
102+
Napi::Function::New(env, &Database::InitTokenizer);
101103
exports["databaseClose"] = Napi::Function::New(env, &Database::Close);
102104
exports["databaseExec"] = Napi::Function::New(env, &Database::Exec);
103105
return exports;
@@ -159,20 +161,32 @@ Napi::Value Database::Open(const Napi::CallbackInfo& info) {
159161
return db->ThrowSqliteError(env, r);
160162
}
161163

164+
return db->self_ref_.Value();
165+
}
166+
167+
Napi::Value Database::InitTokenizer(const Napi::CallbackInfo& info) {
168+
auto env = info.Env();
169+
170+
auto db = FromExternal(info[0]);
171+
if (db == nullptr) {
172+
return Napi::Value();
173+
}
174+
162175
fts5_api* fts5 = db->GetFTS5API(env);
163176

164177
if (fts5 == nullptr) {
165178
return Napi::Value();
166179
}
167180
SignalTokenizerModule* icu = new SignalTokenizerModule();
168-
r = fts5->xCreateTokenizer(fts5, "signal_tokenizer", icu, &icu->api_object,
181+
int r =
182+
fts5->xCreateTokenizer(fts5, "signal_tokenizer", icu, &icu->api_object,
169183
&SignalTokenizerModule::Destroy);
170184
if (r != SQLITE_OK) {
171185
delete icu;
172186
return db->ThrowSqliteError(env, r);
173187
}
174188

175-
return db->self_ref_.Value();
189+
return Napi::Value();
176190
}
177191

178192
Napi::Value Database::Close(const Napi::CallbackInfo& info) {

src/addon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Database {
2727

2828
static Database* FromExternal(const Napi::Value value);
2929
static Napi::Value Open(const Napi::CallbackInfo& info);
30+
static Napi::Value InitTokenizer(const Napi::CallbackInfo& info);
3031
static Napi::Value Close(const Napi::CallbackInfo& info);
3132
static Napi::Value Exec(const Napi::CallbackInfo& info);
3233

test/memory.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,18 @@ test('bigint mode', () => {
380380
).toEqual(n);
381381
});
382382

383+
test('tokenizer setup', () => {
384+
db.initTokenizer();
385+
});
386+
387+
test('tokenizer setup after close', () => {
388+
db.close();
389+
expect(() => db.initTokenizer()).toThrowError('Database closed');
390+
391+
// Just to fix afterEach
392+
db = new Database();
393+
});
394+
383395
test('signalTokenize', () => {
384396
expect(db.signalTokenize('a b c')).toEqual(['a', 'b', 'c']);
385397
});

0 commit comments

Comments
 (0)