LoginSignup
4
4

More than 5 years have passed since last update.

[sails.js] WaterlineのアダプターのTips

Posted at

はじめに

アドベントカレンダーに遅刻して申し訳ございません。

WaterlineというNode.jsで使えるORMパッケージがあり、そのアダプターのTipsを簡単にまとめます。

アダプターのひな形を生成する

ダミープロジェクトを作る

sails.js adapterでググると

が引っかかり、これを使えばいいのかーと一瞬思いそうなのですが、違います。

実際には下記のコマンドを実行しダミーのプロジェクトを生成してそれを使います。

$ sails generate adapter foo

こののコマンドを実行すると

.
└── api
    └── adapters
        └── foo
            ├── CONTRIBUTING.md
            ├── FAQ.md
            ├── LICENSE
            ├── README.md
            ├── fooAdapter.js
            ├── lib
            │   └── adapter.js ← こいつをいじる
            ├── package.json
            └── test
                ├── integration
                │   └── runner.js
                └── unit
                    ├── README.md
                    └── register.js

のようにファイルが生成されるので、adapter.jsの中身をいじってアダプターを作ります。

ファイルの中身を見るとわかるのですが、

  • boilderplateの方
...
    /**
     * 
     * This method runs when a model is initially registered
     * at server-start-time.  This is the only required method.
     * 
     * @param  {[type]}   collection [description]
     * @param  {Function} cb         [description]
     * @return {[type]}              [description]
     */
    registerCollection: function(collection, cb) {

      // Keep a reference to this collection
      _modelReferences[collection.identity] = collection;

      cb();
    },
...
  • sails generate adapterの方
...
    /**
     *
     * This method runs when a model is initially registered
     * at server-start-time.  This is the only required method.
     *
     * @param  {[type]}   connection [description]
     * @param  {[type]}   collection [description]
     * @param  {Function} cb         [description]
     * @return {[type]}              [description]
     */
    registerConnection: function(connection, collections, cb) {

      if(!connection.identity) return cb(new Error('Connection is missing an identity.'));
      if(connections[connection.identity]) return cb(new Error('Connection is already registered.'));

      // Add in logic here to initialize connection
      // e.g. connections[connection.identity] = new Database(connection, collections);
      connections[connection.identity] = connection;

      cb();
    },
...

という風に関数の名前もシグネチャも違うので、boilderplateの方は使わない方がいいのではないかと思います。

アダプターの中身を書く

Waterlineのアダプターで実装すべき関数は、https://github.com/balderdashy/sails-docs/blob/master/contributing/adapter-specification.md でカテゴリー分けされています。

  • Semantic インタフェース
    • CRUDを提供する
  • Query インターフェース
    • 検索機能を提供する
  • Migratable インターフェース
    • マイグレーションや、テーブルのメタデータを変更する機能を提供する
  • SQL インタフェース
    • 生のSQLを実行する機能を提供する

他にも色々あるようなのですが、そもそもexperimentalなのが多く、使うことはないと思います。また実際に実装する際は、sails generate apdaterで生成されたファイルにすでに書かれている関数の中身を作成するだけで基本十分なのではないかと思います。

で、後は実装していくだけなのですが、モデル情報を取るためには、registerConnectionの第二引数であるcollectionsdefinitionプロパティに、モデル定義のattributesの情報がほぼそのまま入っているので、それを利用します。

まとめ

遅刻したのに微妙な記事で誠に申し訳ございません。

4
4
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
4