Skip to content

Commit ca6aaf4

Browse files
author
Marcus Pousette
committed
refactor: enhance debug logging in createDatabase for better traceability
1 parent 3d87844 commit ca6aaf4

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

dist-tests/vec0-native.spec.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,22 @@ test('vec0 create/insert/query works (native ext present)', async () => {
1515

1616
const toVec = () => new Float32Array([Math.random(), Math.random(), Math.random()]);
1717
const ins = await db.prepare('INSERT INTO v(rowid,vector) VALUES(?1,?2)');
18-
for (let i = 1; i <= 4; i++) ins.run([i, toVec().buffer]);
18+
for (let i = 1; i <= 4; i++) {
19+
const vec = toVec();
20+
if (process.env.SQLITE3_VEC_DEBUG === '1') {
21+
console.log('[test][insert]', { rowid: i, vector: `Float32Array(${vec.length})` });
22+
}
23+
ins.run([i, vec.buffer]);
24+
}
1925

2026
const probe = toVec();
2127
const q = await db.prepare(
2228
'SELECT rowid, vec_distance_l2(vector, ?1) AS d FROM v ORDER BY d LIMIT 2',
2329
);
2430
const rows = q.all([probe.buffer]);
31+
if (process.env.SQLITE3_VEC_DEBUG === '1') {
32+
console.log('[test][query]', { probe: `Float32Array(${probe.length})`, rows });
33+
}
2534
assert.ok(Array.isArray(rows) && rows.length === 2);
2635
assert.ok(typeof rows[0].d === 'number');
2736
await db.close();

src/unified-node.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export async function createDatabase(
2525
let db: any;
2626
const statements = new Map<string, Statement>();
2727
const dbFileName = database;
28+
const DEBUG = process.env.SQLITE3_VEC_DEBUG === '1';
29+
const dlog = (...args: any[]) => {
30+
if (DEBUG) console.log('[sqlite3-vec][node]', ...args);
31+
};
2832

2933
const open = () => {
3034
if (db && db.open) return db;
@@ -37,7 +41,8 @@ export async function createDatabase(
3741
} catch {}
3842
const extPath = loadExtension || resolveNativeExtensionPath();
3943
if (extPath) {
40-
loadVecExtension(db, extPath, process.env.SQLITE3_VEC_DEBUG === '1');
44+
dlog('loading vec extension from', extPath);
45+
loadVecExtension(db, extPath, DEBUG);
4146
}
4247
return db;
4348
};
@@ -76,6 +81,24 @@ export async function createDatabase(
7681
}
7782
return v;
7883
};
84+
const summarize = (v: any): any => {
85+
if (v == null) return v;
86+
if (typeof v === 'number' || typeof v === 'boolean') return v;
87+
if (typeof v === 'string') return `string(len=${v.length})`;
88+
if (typeof Buffer !== 'undefined' && Buffer.isBuffer?.(v)) return `Buffer(${v.byteLength})`;
89+
if (v instanceof ArrayBuffer) return `ArrayBuffer(${v.byteLength})`;
90+
if (typeof ArrayBuffer !== 'undefined' && ArrayBuffer.isView?.(v)) {
91+
const ctor = (v as any).constructor?.name || 'TypedArray';
92+
const len = (v as any).length ?? (v.byteLength ?? '?');
93+
return `${ctor}(${len})`;
94+
}
95+
if (typeof v === 'object') {
96+
const out: Record<string, any> = {};
97+
for (const k of Object.keys(v)) out[k] = summarize((v as any)[k]);
98+
return out;
99+
}
100+
return typeof v;
101+
};
79102
const normVals = (values?: any[] | undefined): any[] | undefined =>
80103
values ? values.map((x) => toBuffer(x)) : values;
81104
const normObj = (obj: any): any => {
@@ -99,6 +122,7 @@ export async function createDatabase(
99122
};
100123
const callWithParams = (stmt: any, method: Method, v: any) => {
101124
const params = mapParams(v);
125+
if (DEBUG) dlog(`stmt.${method}()`, { params: summarize(params) });
102126
if (params === undefined) return stmt[method]();
103127
// Deterministic: bind first, then call without inline params.
104128
stmt.bind(params);
@@ -140,10 +164,12 @@ export async function createDatabase(
140164
close,
141165
drop,
142166
exec(sql: string) {
143-
return open().exec(sql);
167+
if (DEBUG) dlog('exec', sql);
168+
return open().exec(sql);
144169
},
145170
async prepare(sql: string, id?: string) {
146171
open();
172+
if (DEBUG) dlog('prepare', { id, sql });
147173
if (id != null && statements.has(id)) {
148174
const prev = statements.get(id)!;
149175
await (prev.reset?.() as any);

0 commit comments

Comments
 (0)