sql-js / sql.js

A javascript library to run SQLite on the web.
http://sql.js.org
Other
12.65k stars 1.06k forks source link

"difference from sql.js", nobody cares #91

Open divinity76 opened 9 years ago

divinity76 commented 9 years ago

i don't care what the difference is from the original sql.js , what i would like to know is how it's different from native SQLite , if at all.. and i don't think im alone

domasx2 commented 8 years ago

I wanted to use this to replace node-sqlite3 for use with sequelize when running tests..

API has some differences, this is how I duck punched sql.js to work with sequelize:

var lib = require('sql.js');
    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 () {

        }
    }
iPotaje commented 8 years ago

I did a small example with this:

https://gist.github.com/iPotaje/89f48328c682b8a4f6f0b6e3874a6902

Thanks @domasx2 !

keyiis commented 7 years ago

@iPotaje @domasx2 I copy code to my project,but tip error:

app.js:90190 EXCEPTION: require(...).verbose is not a functionErrorHandler.handleError @ app.js:90190(anonymous function) @ app.js:67785ZoneDelegate.invoke @ polyfills.js:16463onInvoke @ app.js:57711ZoneDelegate.invoke @ polyfills.js:16462Zone.run @ polyfills.js:16345(anonymous function) @ polyfills.js:16733ZoneDelegate.invokeTask @ polyfills.js:16496onInvokeTask @ app.js:57702ZoneDelegate.invokeTask @ polyfills.js:16495Zone.runTask @ polyfills.js:16385drainMicroTaskQueue @ polyfills.js:16632
app.js:90195 ORIGINAL STACKTRACE:ErrorHandler.handleError @ app.js:90195(anonymous function) @ app.js:67785ZoneDelegate.invoke @ polyfills.js:16463onInvoke @ app.js:57711ZoneDelegate.invoke @ polyfills.js:16462Zone.run @ polyfills.js:16345(anonymous function) @ polyfills.js:16733ZoneDelegate.invokeTask @ polyfills.js:16496onInvokeTask @ app.js:57702ZoneDelegate.invokeTask @ polyfills.js:16495Zone.runTask @ polyfills.js:16385drainMicroTaskQueue @ polyfills.js:16632
app.js:90196 TypeError: require(...).verbose is not a function
    at new ConnectionManager (D:\work\nodejs\angular\angular2-electron\output\build\node_modules\sequelize\lib\dialects\sqlite\connection-manager.js:22:73)
    at new SqliteDialect (D:\work\nodejs\angular\angular2-electron\output\build\node_modules\sequelize\lib\dialects\sqlite\index.js:12:28)
    at new Sequelize (D:\work\nodejs\angular\angular2-electron\output\build\node_modules\sequelize\lib\sequelize.js:233:18)

the error looks like patch.js not work.

haltcase commented 7 years ago

For those still looking for a nicer way to use sql.js, I've just put out a 1.0 rc for a package I've been working on for a while that allows for using sql.js as a knex backend, along with a document store style API.

Trilogy

oney commented 5 years ago

This actually is pretty easy to solve.

@iPotaje 's gist https://gist.github.com/iPotaje/89f48328c682b8a4f6f0b6e3874a6902 should be work.

But for me, because I cannot import sql.js in my react project, I have to import sql.js in html like this

<script type="text/javascript" src="./sql.js"></script>

Then I have to add patch and replace require in node_modules/sequelize/lib/dialects/sqlite/connection-manager.js

// .....
const parserStore = require('../parserStore')('sqlite');

"use strict";

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);
    this.sequelize = sequelize;
    this.config = sequelize.config;
    this.dialect = dialect;
    this.dialectName = this.sequelize.options.dialect;
    this.connections = {};

    // We attempt to parse file location from a connection uri but we shouldn't match sequelize default host.
    if (this.sequelize.options.host === 'localhost') delete this.sequelize.options.host;

    // try {
    //   if (sequelize.config.dialectModulePath) {
    //     this.lib = lib.verbose();
    //   } else {
    //     this.lib = require('sqlite3').verbose();
    //   }
    // } catch (err) {
    //   if (err.code === 'MODULE_NOT_FOUND') {
    //     throw new Error('Please install sqlite3 package manually');
    //   }
    //   throw err;
    // }
    this.lib = lib.verbose();

    this.refreshTypeParser(dataTypes);
  }

But the react webpack compiler complains

./node_modules/sequelize/lib/dialects/mssql/connection-manager.js
Module not found: Can't resolve 'tedious' in '/my-app/node_modules/sequelize/lib/dialects/mssql'

These are just for other dialects for mssql or postgres, so we can just comment any unused require to avoid compiling failure.

catamphetamine commented 1 year ago

Hi. I've made an npm package that could be used to make sql.js look like sqlite3, at least in general. In case anyone's interested: https://www.npmjs.com/package/sql.js-as-sqlite3

Usage:

import Sequelize from 'sequelize'
import sqlJsAsSqlite3 from 'sql.js-as-sqlite3'

const sequelize = new Sequelize('sqlite://:memory:', {
  dialectModule: sqlJsAsSqlite3
})