tndrle / node-sqlite3-wasm

WebAssembly port of SQLite3 for Node.js with file system access
MIT License
55 stars 8 forks source link

Question. Is there an ESM version. #13

Closed fprijate closed 10 months ago

fprijate commented 1 year ago

I would like to use it with TypeScript. Is there ESM version (or some other way to use it with TS)

tndrle commented 1 year ago

There is currently no ESM version.

Does the following work for you?

import sqlite from "node-sqlite3-wasm";
const { Database } = sqlite;

const db = new Database();
fprijate commented 1 year ago

Yes it works. It works with .mjs and .ts files. Actualy I have a problem with Deno.

There is simple example:

// test.ts
import sqlite from "npm:node-sqlite3-wasm";

const { Database } = sqlite;

 function dbInsertRow(dbFile: string, table: string) {
  const db = new Database(dbFile);
  let query = 'CREATE TABLE IF NOT EXISTS test (v1 TEXT,v2 TEXT)';
  db.run(query);
  query = 'INSERT INTO test  (v1,v2)   VALUES (?,?);';
  db.run(query, [1,2]);
  db.close();
}

dbInsertRow('test.db','test')

It creates test.db and empty table 'test' within it. But then it trows an error: error: Uncaught SQLite3Error: file is not a database .....

The program is executed on win10 by: deno run -A test.ts

tndrle commented 1 year ago

node-sqlite3-wasm is a Node.js package not a Deno package, so I wouldn't expect full compatability. But did you try this already: https://deno.com/manual@v1.17.2/npm_nodejs/std_node#loading-commonjs-modules ?

fprijate commented 1 year ago

Yes. Here is a code:

Import { createRequire } from "https://deno.land/std@0.120.0/node/module.ts";
const require = createRequire(import.meta.url);
const sqlite=require("node-sqlite3-wasm");

const { Database } = sqlite;

 function dbInsertRow(dbFile: string, table: string) {
  const db = new Database(dbFile);
  let query = 'CREATE TABLE IF NOT EXISTS test (v1 TEXT,v2 TEXT)';
  db.run(query);
  query = 'INSERT INTO test  (v1,v2)   VALUES (?,?);';
  db.run(query, [1,2]);
  db.close();
}

dbInsertRow('test.db','test')

With the same result. I will contact Deno team about this. Thanks.