Created
July 24, 2015 09:23
-
-
Save loic-moriame/b321dcc38dba8a40a1f7 to your computer and use it in GitHub Desktop.
node.js + sequelize + sqlite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var Sequelize = require('sequelize'); | |
var sequelize = new Sequelize('mainDB', null, null, { | |
dialect: "sqlite", | |
storage: './test.sqlite', | |
}); | |
sequelize | |
.authenticate() | |
.then(function(err) { | |
console.log('Connection has been established successfully.'); | |
}, function (err) { | |
console.log('Unable to connect to the database:', err); | |
}); | |
// MODELS | |
var User = sequelize.define('User', { | |
username: Sequelize.STRING, | |
password: Sequelize.STRING | |
}); | |
// SYNC SCHEMA | |
sequelize | |
.sync({ force: true }) | |
.then(function(err) { | |
console.log('It worked!'); | |
}, function (err) { | |
console.log('An error occurred while creating the table:', err); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"main": "index.js", | |
"dependencies": { | |
"sequelize": "^3.4.1", | |
"sqlite3": "^3.0.9" | |
} | |
} |
@Moriame: get the [sqlite3](install https://sqlite.org/download.html) binaries and add them to the PATH?
The error does state that you do not have sqlite3 installed. Did just that on Windows 7 and that my log
E:\Projects\test-sqlite
λ node index.js
Executing (default): DROP TABLE IF EXISTS `Users`;
Executing (default): SELECT 1+1 AS result
Executing (default): DROP TABLE IF EXISTS `Users`;
Connection has been established successfully.
Executing (default): CREATE TABLE IF NOT EXISTS `Users` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `username` VARCHAR(255), `password` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL);
Executing (default): PRAGMA INDEX_LIST(`Users`)
It worked!
in windows 10, copy all to one folder, install nodejs, in terminal cmd, type
npm update
... wait to finish installation, this may delay 5 minutes
node index.js
output:
Executing (default): SELECT 1+1 AS result
Connection has been established successfully.
It worked!
@Moriame 3 years still useful. I would love to know the journey after this...
Honestly i can't believe am already falling in love with sequelize
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
running this code on Windows 8:
running this code on Linux: