Skip to content

Instantly share code, notes, and snippets.

@aftercider
Last active January 3, 2016 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aftercider/cd5321acfd6d2fc08636 to your computer and use it in GitHub Desktop.
Save aftercider/cd5321acfd6d2fc08636 to your computer and use it in GitHub Desktop.
NodeJSのORMライブラリSequelizeで、SQLite3のDBを使う
// 解説 http://aftercider.hatenablog.com/entry/2016/01/03/164444
"use strict";
// 使用するSQLite3のDBは以下
// https://drive.google.com/file/d/0BzCcFSX0gJGkWFJUVmczN0ZOX1E/view?usp=sharing
/**
* Sequelizeの導入
*/
var Sequelize = require("sequelize");
/**
* SQLite3の読み込み
*/
var sequelize = new Sequelize(
'database', '', '', {
dialect: 'sqlite',
storage:'./sequelize-sqlite3.db',
}
);
/**
* データ型の宣言 テーブル名: places
*/
var Place = sequelize.define(
"place",
{
name: Sequelize.STRING,
},
{
// createdAt, updatedAtを自動的に入れないようにする
timestamps: false,
}
);
var outputMethod = function(prefectures) {
// JSONで出力
console.log(JSON.stringify(prefectures));
}
// placesを取得
Place.findAll().then(outputMethod);
//// 出力
//[
// {
// "id": 1,
// "name": "札幌市時計台",
// },
// {
// "id": 2,
// "name": "青森県立三沢航空科学館",
// }
//]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment