4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

sails.js (0.10.x) 起動時にデータを自動的にロードして開発を効率化したい ヽ(゚ー゚*ヽ)(ノ*゚ー゚)ノわぁい | sails.js (0.10.x) で始めるアプリ開発

Last updated at Posted at 2014-12-04

sails.js (0.10.x) 起動時にデータを自動的にロードして開発を効率化したい ヽ(゚ー゚ヽ)(ノ゚ー゚)ノわぁい

この記事では以下の絵文字を大事な個所で使用中

  • :exclamation: 注意点
  • :boom: 予期せぬ例外やエラーメッセージ
  • :sunny: 解決方法・お役立ち情報

皆さん、sails.js 使ってますか?sails.jsはNode.jsのMVCフレームワークです。サクサクつくれるのがポイントで開発も比較的活発に行われておりまス。

Sails.js | Realtime MVC Framework for Node.js - http://sailsjs.org/

:exclamation: sails.jsはアップデート頻度が高い⇒この記事執筆時点と仕様が異なっている可能性があるのでバージョンに注意

記事の概要

sails.js にはDBのmigration機能がフレームワーク自体に組み込まれている。アプリの起動時にデータベースをリフレッシュしてくれるので便利だ(特に開発時)。ただ、テーブル自体の自動生成はできるが、テーブルに含めたいデータ(たとえばマスターデータ等)を更新することはできない。(v0.10.5 時点)

今回は何とかする方法の紹介

:sunny: bootstrap.js に記述する

sails lift した際に実行される config/botstrap.js にデータロード処理を記述することで実現する。

:exclamation: 本番環境 production の場合に間違って処理しないように配慮はすべき

.js
User.find({admin: true}).exec(function(err, adminUser) {
    // adminUser がいたら何もしない
    if (adminUser) { return ; }
    // いないので挿入するとか
    User.create({name:'Admin Taro', admin: true}).exec();
});

ということで、できたよ ヽ(゚ー゚ヽ)(ノ゚ー゚)ノわぁい

この方法がベストなの?

You can use bootstrap.js currently to findOrCreate some data each time the app runs. But I'd be curious to see a proposed spec for what something more powerful might look like?

現時点での公式のコメント↑なので、今後の機能追加の可能性はあるが、とりあえずOK

参考資料等

同じことで困っている人はどこかにいるもの
http://stackoverflow.com/questions/24090135/loading-data-fixtures-the-first-time-a-sails-js-app-is-lifted

sails.jsのプロジェクトでもリクエストきてる
https://trello.com/c/eGIDCCtd/149-advanced-data-bootstrap

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?