Last active
December 18, 2015 17:57
-
-
Save hunnycode/ec1927a5dd790a1ccd43 to your computer and use it in GitHub Desktop.
【ハンズオン資料】StrongLoopのLoopBackを使ってBluemix上のdatabaseをAPIで操作(CRUD) ref: http://qiita.com/joohounsong/items/476bf95016a2266d0da8
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
var app = require('../server'); | |
var accounts = [ | |
{ | |
email: 'foo@bar.com', | |
createdAt: new Date(), | |
lastModifiedAt: new Date() | |
}, | |
{ | |
email: 'baz@qux.com', | |
createdAt: new Date(), | |
lastModifiedAt: new Date() | |
} | |
]; | |
// ~/server/datasources.jsonの「accountDb configuration」 | |
var dataSource = app.dataSources.AccountDb; | |
// Account modelのautomigrates | |
dataSource.automigrate('Account', function(err) { | |
if (err) throw err; | |
// ~/common/models/Account.jsonのAccount model | |
var Account = app.models.Account; | |
var count = accounts.length; | |
accounts.forEach(function(account) { | |
// Account tableにデーター挿入 | |
Account.create(account, function(err, record) { | |
if (err) return console.log(err); | |
console.log('Record created:', record); | |
count--; | |
if (count === 0) { | |
console.log('done'); | |
dataSource.disconnect(); | |
} | |
}); | |
}); | |
}); |
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
cf api https://api.ng.bluemix.net | |
cf login -u <user_name> -o <organization> -s <space> | |
cf push joohoun-loopback |
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
{ | |
"db": { | |
"name": "db", | |
"connector": "memory" | |
}, | |
"AccountDb": { | |
"host": "<database_subdomain>.cleardb.net", | |
"port": 3306, | |
"database": "<database>", | |
"password": "<password>", | |
"name": "AccountDb", | |
"user": "<username>", | |
"connector": "mysql" | |
} | |
} |
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
"scripts": { | |
"start": "node server/server.js", | |
"pretest": "jshint ." | |
}, | |
"dependencies": { | |
.. | |
"loopback-connector-mysql": "^1.0.3" | |
"cfenv": "1.0.x" | |
.. |
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
var loopback = require('loopback'); | |
var boot = require('loopback-boot'); | |
// cfenvで、「Cloud Foundry」環境に接続します。 | |
// cfenvの詳細はこちら:https://www.npmjs.com/package/cfenv | |
var cfenv = require('cfenv'); | |
var appEnv = cfenv.getAppEnv(); | |
var app = module.exports = loopback(); | |
app.start = function() { | |
// web server スタート | |
return app.listen(appEnv.port, appEnv.bind, function() { | |
app.emit('started'); | |
console.log('Web server listening at: %s', app.get('url')); | |
}); | |
}; | |
boot(app, __dirname, function(err) { | |
if (err) throw err; | |
if (require.main === module) | |
app.start(); | |
}); |
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
ps -ax | grep node | |
[Result] | |
1301 ttys000 0:01.90 node . | |
4116 ttys002 0:00.01 grep node | |
kill -9 1301 -> PID 1301を終了 |
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
npm install --save loopback-connector-mysql |
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
slc loopback <アプリケーション名(ディレクトリ名を入れる)> |
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
slc loopback:model Account |
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
cd server | |
mkdir bin |
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
cd <directory> |
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
npm install -g strongloop |
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
slc -v |
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
mkdir <directory> |
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
node . |
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
cd <directory> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment