javascript - connect sequelize.js to node-webkit desktop app using sqlite - Stack Overflow

admin2025-04-18  1

currently I am using node-sqlite3 sqlite binding on my node-webkit desktop app and using it as

var sqlite3 = require('node_sqlite3').verbose();
var db = new sqlite3.Database('file:data.db');
db.run(query);

and on my knowledge this natively piled node-sqlite3 is the only way to use sqlite db with node-webkit.

now I want to use sequelize on the app which is normally used as:

var Sequelize = require('sequelize-sqlite').sequelize
var sqlite    = require('sequelize-sqlite').sqlite
var sequelize = new Sequelize('database', 'username', 'password', {
dialect: 'sqlite',
storage: 'path/to/database.sqlite'
})
sequelize.query("SELECT * FROM myTable").success(function(myTableRows) {
console.log(myTableRows)
})

how can I achieve this ? (ie. use sequelize on node-webkit app with sqlite)

the goal is to make the database life easier by running migrations , use models to manipulate database , or suggest if there are any other javascript libraries (mvc is prefered) , which can work through node-webkit+sqlite (and how to make them work) .

is angularjs an option ? if yes how to do this.

thank yous.

currently I am using node-sqlite3 sqlite binding on my node-webkit desktop app and using it as

var sqlite3 = require('node_sqlite3').verbose();
var db = new sqlite3.Database('file:data.db');
db.run(query);

and on my knowledge this natively piled node-sqlite3 is the only way to use sqlite db with node-webkit.

now I want to use sequelize on the app which is normally used as:

var Sequelize = require('sequelize-sqlite').sequelize
var sqlite    = require('sequelize-sqlite').sqlite
var sequelize = new Sequelize('database', 'username', 'password', {
dialect: 'sqlite',
storage: 'path/to/database.sqlite'
})
sequelize.query("SELECT * FROM myTable").success(function(myTableRows) {
console.log(myTableRows)
})

how can I achieve this ? (ie. use sequelize on node-webkit app with sqlite)

the goal is to make the database life easier by running migrations , use models to manipulate database , or suggest if there are any other javascript libraries (mvc is prefered) , which can work through node-webkit+sqlite (and how to make them work) .

is angularjs an option ? if yes how to do this.

thank yous.

Share edited Aug 19, 2013 at 8:44 sunnyvilles asked Aug 17, 2013 at 17:23 sunnyvillessunnyvilles 3513 silver badges11 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

I found the solution. after using following sqlite3 for node-webkit and rebuilding it as in the

node sqlite 3 for node-webkit
It creates a folder named "node_sqlite3" inside the node_modules . I renamed "node_sqlite3" to "sqlite3". and then did npm-install of sequelize-sqlite .
after this sequelize-sqlite automatically recognizes and gets connected to sqlite3 and it can normally be used.

var Sequelize = require('sequelize-sqlite').sequelize
var sqlite    = require('sequelize-sqlite').sqlite

var sequelize = new Sequelize('database', 'username', '', {
dialect: 'sqlite',
storage: 'file:data.db'
})

var Record = sequelize.define('Record', {
name: Sequelize.STRING,
quantity: Sequelize.INTEGER
})

sequelize.sync()
.success(function(){
console.log('synced')
})

var rec = Record.build({ name: "sunny", quantity: 3 });
rec.save()
.error(function(err) {
// error callback
alert('somethings wrong')
})
.success(function() {
// success callback
console.log('inserted')
});

and the records are in the database.

I also had a problem connecting to SQLite database from node-webkit using sequelize. By trial and error, I have e with the following solution.

var Sequelize = require('sequelize');
var sequelize = new Sequelize('sqlite:mydb.sqlite3', {
  dialect: 'sqlite',
  storage: './mydb.sqlite3'
});

sequelize.query("SELECT * FROM tableName", { type: db.QueryTypes.SELECT })
.then(function(result) {
  console.log(result);
})
.catch(function (e) {
  console.log(e);
});

Note that for SQLite database you have to specify

storage: './mydb.sqlite3'

This is the path to your db file. It can be either absolute path or relative. ./ means project root folder where package.json is located.

Also, the connection string must contain the sqlite: prefix. Otherwise, it will raise an exception: Cannot read property 'replace' of null. The part after sqlite: prefix is irrevelant and can even be omitted because the actual db location is specified in storage option. dialect option can be omitted as well, since db type is mentioned in the connection string prefix. So you can connect simply using

var sequelize = new Sequelize('sqlite:', {
  storage: './mydb.sqlite3'
});

It is also worth noting that .success and .error methods are deprecated. At the time of writing this post, the latest version of Sequelize (3.21) uses .then method to handle operations if the promise is fulfilled and .catch method if the promise is rejected.

In Node-WebKit you can you use modules written totally in JavaScript. For modules containing C/C++ you should build the addons by node-gyp because the ABI (application binary interface) of node-webkit differs from Node's ABI.

Try smth like sequelize-sqlite that 100% JS or find modules in npm go to it GitHub repository and under row mits, branch, releases & contributor click coloured row "Show language statistic".

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744931849a275205.html

最新回复(0)