refactor: migrate from node-sqlite3-wasm to better-sqlite3
Replace all db.run(sql, [params]) calls with db.prepare(sql).run(...params) across all source modules and test files. Also remove the node-sqlite3-wasm import from tests/db.test.ts and update the type annotation accordingly. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -8,10 +8,9 @@ beforeAll(() => {
|
||||
const db = initDb(':memory:');
|
||||
// Create a test user
|
||||
const hash = bcrypt.hashSync('correctpassword', 10);
|
||||
db.run(
|
||||
`INSERT INTO users (username, password_hash) VALUES (?, ?)`,
|
||||
['testuser', hash]
|
||||
);
|
||||
db.prepare(
|
||||
`INSERT INTO users (username, password_hash) VALUES (?, ?)`
|
||||
).run('testuser', hash);
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
|
||||
@@ -9,22 +9,22 @@ beforeAll(() => {
|
||||
const db = initDb(':memory:');
|
||||
|
||||
// Create a layout
|
||||
db.run(
|
||||
db.prepare(
|
||||
`INSERT INTO layouts (id, name, type, width, height)
|
||||
VALUES (1, 'Test Layout', 'free', 800, 480)`
|
||||
);
|
||||
).run();
|
||||
|
||||
// Display without token
|
||||
db.run(
|
||||
db.prepare(
|
||||
`INSERT INTO displays (id, name, layout_id, refresh_sec)
|
||||
VALUES (1, 'Open Display', 1, 60)`
|
||||
);
|
||||
).run();
|
||||
|
||||
// Display with token auth
|
||||
db.run(
|
||||
db.prepare(
|
||||
`INSERT INTO displays (id, name, layout_id, api_token, refresh_sec)
|
||||
VALUES (2, 'Secure Display', 1, 'secret-token-123', 300)`
|
||||
);
|
||||
).run();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
||||
import { Database } from 'node-sqlite3-wasm';
|
||||
import type Database from 'better-sqlite3';
|
||||
import { initDb, closeDb } from '../src/db/index';
|
||||
|
||||
let db: Database;
|
||||
let db: InstanceType<typeof Database>;
|
||||
|
||||
beforeAll(() => {
|
||||
db = initDb(':memory:');
|
||||
@@ -96,10 +96,9 @@ describe('Table creation', () => {
|
||||
|
||||
describe('Broker insert and retrieve', () => {
|
||||
it('should insert and retrieve a broker', () => {
|
||||
const result = db.run(
|
||||
`INSERT INTO mqtt_brokers (name, host, port) VALUES (?, ?, ?)`,
|
||||
['Test Broker', 'mqtt.example.com', 1883]
|
||||
);
|
||||
const result = db.prepare(
|
||||
`INSERT INTO mqtt_brokers (name, host, port) VALUES (?, ?, ?)`
|
||||
).run('Test Broker', 'mqtt.example.com', 1883);
|
||||
expect(result.lastInsertRowid).toBeGreaterThan(0);
|
||||
|
||||
const broker = db
|
||||
@@ -119,22 +118,22 @@ describe('Broker insert and retrieve', () => {
|
||||
});
|
||||
|
||||
it('should cascade delete topics when broker is deleted', () => {
|
||||
const broker = db.run(`INSERT INTO mqtt_brokers (name, host) VALUES (?, ?)`, [
|
||||
const broker = db.prepare(`INSERT INTO mqtt_brokers (name, host) VALUES (?, ?)`).run(
|
||||
'Cascade Broker',
|
||||
'cascade.example.com',
|
||||
]);
|
||||
);
|
||||
|
||||
db.run(`INSERT INTO mqtt_topics (broker_id, topic) VALUES (?, ?)`, [
|
||||
db.prepare(`INSERT INTO mqtt_topics (broker_id, topic) VALUES (?, ?)`).run(
|
||||
broker.lastInsertRowid,
|
||||
'sensors/temp',
|
||||
]);
|
||||
);
|
||||
|
||||
const topicsBefore = db
|
||||
.prepare(`SELECT * FROM mqtt_topics WHERE broker_id = ?`)
|
||||
.all(broker.lastInsertRowid);
|
||||
expect(topicsBefore.length).toBe(1);
|
||||
|
||||
db.run(`DELETE FROM mqtt_brokers WHERE id = ?`, [broker.lastInsertRowid]);
|
||||
db.prepare(`DELETE FROM mqtt_brokers WHERE id = ?`).run(broker.lastInsertRowid);
|
||||
|
||||
const topicsAfter = db
|
||||
.prepare(`SELECT * FROM mqtt_topics WHERE broker_id = ?`)
|
||||
@@ -144,10 +143,9 @@ describe('Broker insert and retrieve', () => {
|
||||
|
||||
it('should reject invalid source_type values', () => {
|
||||
expect(() => {
|
||||
db.run(
|
||||
`INSERT INTO datapoints (name, label, source_type, source_id) VALUES (?, ?, ?, ?)`,
|
||||
['dp1', 'Datapoint 1', 'invalid_source', 1]
|
||||
);
|
||||
db.prepare(
|
||||
`INSERT INTO datapoints (name, label, source_type, source_id) VALUES (?, ?, ?, ?)`
|
||||
).run('dp1', 'Datapoint 1', 'invalid_source', 1);
|
||||
}).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,48 +6,48 @@ beforeAll(() => {
|
||||
const db = initDb(':memory:');
|
||||
|
||||
// Create a layout
|
||||
db.run(
|
||||
db.prepare(
|
||||
`INSERT INTO layouts (id, name, type, width, height, grid_cols, grid_rows)
|
||||
VALUES (1, 'Test Layout', 'grid', 800, 480, 2, 3)`
|
||||
);
|
||||
).run();
|
||||
|
||||
// Create a display with the layout
|
||||
db.run(
|
||||
db.prepare(
|
||||
`INSERT INTO displays (id, name, layout_id, refresh_sec)
|
||||
VALUES (1, 'Test Display', 1, 60)`
|
||||
);
|
||||
).run();
|
||||
|
||||
// Create a display without layout
|
||||
db.run(
|
||||
db.prepare(
|
||||
`INSERT INTO displays (id, name, layout_id, refresh_sec)
|
||||
VALUES (2, 'Empty Display', NULL, 300)`
|
||||
);
|
||||
).run();
|
||||
|
||||
// Create a broker and topic for datapoint
|
||||
db.run(
|
||||
db.prepare(
|
||||
`INSERT INTO mqtt_brokers (id, name, host) VALUES (1, 'Test Broker', 'localhost')`
|
||||
);
|
||||
db.run(
|
||||
).run();
|
||||
db.prepare(
|
||||
`INSERT INTO mqtt_topics (id, broker_id, topic) VALUES (1, 1, 'sensors/temp')`
|
||||
);
|
||||
).run();
|
||||
|
||||
// Create a datapoint
|
||||
db.run(
|
||||
db.prepare(
|
||||
`INSERT INTO datapoints (id, name, label, unit, value, source_type, source_id, topic_id, enabled)
|
||||
VALUES (1, 'temperature', 'Temperature', '°C', '22.5', 'mqtt', 1, 1, 1)`
|
||||
);
|
||||
).run();
|
||||
|
||||
// Create layout elements
|
||||
// Label element
|
||||
db.run(
|
||||
db.prepare(
|
||||
`INSERT INTO layout_elements (id, layout_id, type, x, y, static_text, sort_order)
|
||||
VALUES (1, 1, 'label', 0, 0, 'Hello World', 0)`
|
||||
);
|
||||
).run();
|
||||
// Value element linked to datapoint
|
||||
db.run(
|
||||
db.prepare(
|
||||
`INSERT INTO layout_elements (id, layout_id, datapoint_id, type, x, y, font_size, sort_order)
|
||||
VALUES (2, 1, 1, 'value', 100, 0, 32, 1)`
|
||||
);
|
||||
).run();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
|
||||
Reference in New Issue
Block a user