Last active
January 3, 2016 07:59
-
-
Save aftercider/cd5321acfd6d2fc08636 to your computer and use it in GitHub Desktop.
NodeJSのORMライブラリSequelizeで、SQLite3のDBを使う
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
// 解説 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