Skip to content

Instantly share code, notes, and snippets.

@loic-moriame
Created July 24, 2015 09:23
Show Gist options
  • Save loic-moriame/b321dcc38dba8a40a1f7 to your computer and use it in GitHub Desktop.
Save loic-moriame/b321dcc38dba8a40a1f7 to your computer and use it in GitHub Desktop.
node.js + sequelize + sqlite
'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);
});
{
"main": "index.js",
"dependencies": {
"sequelize": "^3.4.1",
"sqlite3": "^3.0.9"
}
}
@loic-moriame
Copy link
Author

running this code on Windows 8:

    throw new Error('The dialect ' + this.getDialect() + ' is not supported. (
          ^
Error: The dialect sqlite is not supported. (Error: Please install sqlite3 package manually)
    at new Sequelize (C:\Users\lmoriame\Dev\labs-sequelize\node_modules\sequelize\lib\sequelize.js:182:11)
    at Object.<anonymous> (C:\Users\lmoriame\Dev\labs-sequelize\index.js:5:17)
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:428:10)
    at Module.load (module.js:335:32)
    at Function.Module._load (module.js:290:12)
    at Function.Module.runMain (module.js:451:10)
    at startup (node.js:124:18)
    at node.js:868:3

running this code on Linux:

loic@nodejs# 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!

@thoroc
Copy link

thoroc commented Jul 29, 2015

@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!

@brakdag
Copy link

brakdag commented Nov 13, 2017

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!

@akvsh-r
Copy link

akvsh-r commented Apr 11, 2018

@Moriame 3 years still useful. I would love to know the journey after this...

@delino12
Copy link

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