Bastian
Bastian5d ago

SQLite null prototype

Hi, SQLite returns [Object: null prototype] as it's not an object. What is the ideal way of dealing with this?
import { DatabaseSync } from "node:sqlite";

// Create or open an in-memory SQLite database
const db = new DatabaseSync(":memory:");

// Execute multiple SQL statements:
// 1. Create a table named "users"
// 2. Insert two rows into the "users" table
db.exec(`
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT
);
INSERT INTO users (name) VALUES ('Alice');
INSERT INTO users (name) VALUES ('Bob');
`);

// Query to verify data insertion using .get()
const result = db.prepare(`SELECT * FROM users WHERE name = 'Alice'`)
.get();
console.log(result); // => [Object: null prototype] { id: 1, name: 'Alice' }

db.close();
import { DatabaseSync } from "node:sqlite";

// Create or open an in-memory SQLite database
const db = new DatabaseSync(":memory:");

// Execute multiple SQL statements:
// 1. Create a table named "users"
// 2. Insert two rows into the "users" table
db.exec(`
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT
);
INSERT INTO users (name) VALUES ('Alice');
INSERT INTO users (name) VALUES ('Bob');
`);

// Query to verify data insertion using .get()
const result = db.prepare(`SELECT * FROM users WHERE name = 'Alice'`)
.get();
console.log(result); // => [Object: null prototype] { id: 1, name: 'Alice' }

db.close();
1 Reply
marvinh.
marvinh.5d ago
An object with its prototype set to null is still an object. Setting the prototype to null is a common pattern in runtimes to prevent badly written polyfills from messing with return values of functions. Because the prototype is terminated, there is no fallback lookup into the global Object when a property doesn't exist.
What is the ideal way of dealing with this?
Just continue using it as you normally would. If you're running into any problems, please share

Did you find this page helpful?