Skip to content

Instantly share code, notes, and snippets.

@oney
Last active March 27, 2023 02:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save oney/904c4a1533174a9514305088b790f71f to your computer and use it in GitHub Desktop.
Save oney/904c4a1533174a9514305088b790f71f to your computer and use it in GitHub Desktop.
Use sql.js in sequelize on web browser

The following patch is generated from patch-package. Apply the patch by patch-package or manually. BTW, patch-package is amazing!

The patch is basically remove require('pg-hstore') because we don't need it and it may cause a require error.

You can also notice that we use window.SQL this thing. That means we need window.SQL be present.

My way is initSqlJs and set it to window.SQL somewhere at the very beginning (before you call sequelize).

Workable codes are like these

import initSqlJs from "sql.js";
async function loadSQL() {
  const SQL = await initSqlJs();
  window.SQL = SQL;

  const sequelize = new Sequelize('sqlite::memory:', {
    dialect: "sqlite",
  });

  class User extends Model {}
  User.init({
    username: DataTypes.STRING,
    birthday: DataTypes.DATE
  }, { sequelize, modelName: 'user' });

  await sequelize.sync();
  const jane = await User.create({
    username: 'janedoe',
    birthday: new Date(1980, 6, 20)
  });
  console.log(jane.toJSON());
}
loadSQL();
// print: {id: 1, username: "janedoe", birthday: ...
diff --git a/node_modules/sequelize/lib/dialects/postgres/hstore.js b/node_modules/sequelize/lib/dialects/postgres/hstore.js
index 77a0346..22e0d8b 100644
--- a/node_modules/sequelize/lib/dialects/postgres/hstore.js
+++ b/node_modules/sequelize/lib/dialects/postgres/hstore.js
@@ -1,15 +1,15 @@
'use strict';
-const hstore = require('pg-hstore')({ sanitize: true });
+// const hstore = require('pg-hstore')({ sanitize: true });
function stringify(data) {
if (data === null) return null;
- return hstore.stringify(data);
+ // return hstore.stringify(data);
}
exports.stringify = stringify;
function parse(value) {
if (value === null) return null;
- return hstore.parse(value);
+ // return hstore.parse(value);
}
exports.parse = parse;
diff --git a/node_modules/sequelize/lib/dialects/sqlite/connection-manager.js b/node_modules/sequelize/lib/dialects/sqlite/connection-manager.js
index 5c7f116..3eb0c2d 100644
--- a/node_modules/sequelize/lib/dialects/sqlite/connection-manager.js
+++ b/node_modules/sequelize/lib/dialects/sqlite/connection-manager.js
@@ -8,6 +8,53 @@ const dataTypes = require('../../data-types').sqlite;
const sequelizeErrors = require('../../errors');
const parserStore = require('../parserStore')('sqlite');
+var lib = window.SQL;
+lib.verbose = function() {
+ return lib;
+}
+
+lib._Database = lib.Database;
+lib.Database = class Db extends lib._Database {
+ constructor(filename, mode, cb) {
+ super();
+ process.nextTick(cb, null);
+ }
+
+ //I'm not 100% sure what this was supposed to do on node-sqlite3, heh.
+ serialize(cb) {
+ process.nextTick(cb);
+ }
+
+ run(sql, params, cb) {
+ super.run(sql, params);
+ var ctx = {};
+ if (sql.toLowerCase().indexOf('insert') !== -1) {
+ var rez = this.exec("select last_insert_rowid();");
+ ctx.lastID = rez[0].values[0][0];
+ }
+ if (cb) {
+ process.nextTick(cb.bind(ctx), null);
+ }
+ return this;
+ }
+
+ all(sql, params, cb) {
+ var result = [];
+ this.each(sql, params,
+ function(r) {
+ result.push(r);
+ },
+ function() {
+ cb(null, result);
+ });
+ return this;
+ }
+
+ close () {
+
+ }
+}
+
class ConnectionManager extends AbstractConnectionManager {
constructor(dialect, sequelize) {
super(dialect, sequelize);
@@ -19,7 +66,7 @@ class ConnectionManager extends AbstractConnectionManager {
}
this.connections = {};
- this.lib = this._loadDialectModule('sqlite3').verbose();
+ this.lib = lib.verbose();
this.refreshTypeParser(dataTypes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment