Sebastian
Sebastian4mo 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();
4 Replies
marvinh.
marvinh.4mo 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
Sebastian
SebastianOP3mo ago
Thank you for the answer! I've not encountered any problems so far. However after upgrading to 2.3 I've begun to see a red squilly line under my node:sqlite import: npm package "@types/node" is not installed or doesn't exist. (deno not-installed-npm) All attempts to download @types/node, reseting cache, etc has not made it disapear.
marvinh.
marvinh.3mo ago
Can you file an issue for that here https://github.com/denoland/deno/issues ?
Sebastian
SebastianOP3mo ago
Yes, I'll file an issue after work today or this weekend. Thanks for quick reply! Cannot reproduce the issue, anymore. Might have been some issue on my; or Zed's end.

Did you find this page helpful?