Last active
January 18, 2020 08:24
-
-
Save ryandwi/c8bc6e12c67d629df38e4e868ad11b23 to your computer and use it in GitHub Desktop.
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
Pastikan sudah menginstall nodejs | |
1. install express generator | |
npm i -g express-generator | |
2. buat aplikasi expressjs | |
express nama_aplikasi | |
3. install aplikasi yang telah dibuat | |
npm install | |
4. masuk ke direktori aplikasi_kita | |
cd nama_aplikasi | |
5. edit package.json mengunakan nodemon supaya ketika edit langsung terjadi perubahan | |
{ | |
"name": "lirik", | |
"version": "0.0.0", | |
"private": true, | |
"scripts": { | |
"start": "nodemon ./bin/www" | |
}, | |
"dependencies": { | |
"cookie-parser": "~1.4.4", | |
"debug": "~2.6.9", | |
"express": "~4.16.1", | |
"http-errors": "~1.6.3", | |
"morgan": "~1.9.1", | |
"pug": "2.0.0-beta11" | |
} | |
} | |
5. Menjalakan aplikasi | |
npm start | |
6. install sequelize di folder aplikasi | |
npm install --save sequelize | |
7. install sequelize-cli | |
npm install --save sequelize-cli | |
8. pilih database yang digunakan | |
# One of the following: | |
$ npm install --save pg pg-hstore # Postgres | |
$ npm install --save mysql2 | |
$ npm install --save mariadb | |
$ npm install --save sqlite3 | |
$ npm install --save tedious # Microsoft SQL Server | |
9. melakukan initialiasi sequelize sehingga menghasilkan folder, config, models, migrations, dan seeders. | |
sequelize init | |
10. edit config/config.json sesuai database kita, edit bagian developemnt untuk tahan development | |
{ | |
"development": { | |
"username": "ryandwi", | |
"password": "kalitelo123", | |
"database": "lirik", | |
"host": "127.0.0.1", | |
"port" : "5432", | |
"dialect": "postgres", | |
"operatorsAliases": false | |
}, | |
"test": { | |
"username": "root", | |
"password": null, | |
"database": "database_test", | |
"host": "127.0.0.1", | |
"dialect": "mysql", | |
"operatorsAliases": false | |
}, | |
"production": { | |
"username": "root", | |
"password": null, | |
"database": "database_production", | |
"host": "127.0.0.1", | |
"dialect": "mysql", | |
"operatorsAliases": false | |
} | |
} | |
11. membuat model | |
sequelize model:create --name users --attributes name:string,email:string,phone_number:string,gender:boolean | |
https://sequelize.org/master/manual/model-basics.html#data-types | |
12. mengisi database kita dari file json | |
'use strict'; | |
module.exports = { | |
up: (queryInterface, Sequelize) => { | |
const lirik_json = require('../data.json'); | |
let lirik_array = []; | |
lirik_json.forEach(lirik => { | |
lirik_array.push({ | |
lirik_title : lirik.title, | |
lirik_artis: lirik.artis, | |
lirik_content: lirik.lirik, | |
createdAt: new Date(), | |
updatedAt: new Date() | |
}) | |
}); | |
return queryInterface.bulkInsert('liriks', lirik_array); | |
}, | |
down: (queryInterface, Sequelize) => { | |
return queryInterface.bulkDelete('liriks', null); | |
} | |
}; | |
14. menjalakan seed | |
sequelize db:seed --seed seeders/20200117185100-lirik_seed.js | |
15. membuat seed baru dengan nama user | |
sequelize seed:generate --name users | |
16. sampai di sini kita sudah mempunyai database |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment