forked from boticlaw/SuperBotijo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-sqlite-wasm-correct.js
More file actions
62 lines (48 loc) · 2.12 KB
/
Copy pathtest-sqlite-wasm-correct.js
File metadata and controls
62 lines (48 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Test script for node-sqlite3-wasm with correct API
console.log('Testing node-sqlite3-wasm with correct API...');
try {
// Import with destructuring as per documentation
const { Database } = require("node-sqlite3-wasm");
console.log('✅ node-sqlite3-wasm imported successfully');
// Test basic database operations
const db = new Database(':memory:');
console.log('✅ Database created');
// Create a table
db.exec('CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)');
console.log('✅ Table created');
// Insert data using db.run()
const result1 = db.run('INSERT INTO test (name) VALUES (?)', 'test-value-1');
console.log('✅ Data inserted (run):', result1);
// Insert more data
db.run('INSERT INTO test (name) VALUES (?)', 'test-value-2');
console.log('✅ More data inserted');
// Query data using db.get()
const row = db.get('SELECT * FROM test WHERE id = 1');
console.log('✅ Single row retrieved (get):', row);
// Query all data using db.all()
const rows = db.all('SELECT * FROM test');
console.log('✅ All rows retrieved (all):', rows);
// Test prepared statement (need to finalize manually)
const stmt = db.prepare('INSERT INTO test (name) VALUES (?)');
try {
stmt.run('test-value-3');
console.log('✅ Data inserted via prepared statement');
} finally {
stmt.finalize(); // Important: must finalize manually
console.log('✅ Statement finalized');
}
// Verify all data
const allRows = db.all('SELECT * FROM test');
console.log('✅ All rows after prepared statement:', allRows);
// Close database (Important: must close manually)
db.close();
console.log('✅ Database closed');
console.log('\n✅ All tests passed! node-sqlite3-wasm works with similar API to better-sqlite3');
console.log('\nKey differences to note:');
console.log('- Import: const { Database } = require("node-sqlite3-wasm")');
console.log('- Must call db.close() manually to avoid memory leaks');
console.log('- Must call stmt.finalize() manually for prepared statements');
} catch (error) {
console.error('❌ Error:', error.message);
console.error('Stack:', error.stack);
}