[MBaaS][Parse] Cloud Codeでバックエンド機能を拡張する [其の1] [準備]

2014.05.11

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

スクリーンショット 2014-05-06 18.37.23

Cloud Code

Parseへデータを永続化して保存し、CRUDできることをざっと紹介しました。 まあ、完全にRESTfulなアプリケーションやサービスが作れれば、それはそれで良いですが、色々なデータを永続化すると色々と組み合わせて処理をしてみたりしたくなります(よね?!)

データストアの基本的な動作を拡張させることができます。より少ないコードで。それが【Cloud Code】です。 利用する言語はJavaScript

導入

開発ツールのSDKの導入方法。 Installing or Updating the Command Line Tool (Mac/Linux)

SDKのインストール

SDKはワンライナーのコマンドで導入できます。MacやLinuxなどは同じコマンドでいけます。

インストールした環境 マシン:Macbook air OS:OS X 10.9.2 メモリ:8 GB

SDKのインストールは以下のコマンドを打ち込み実行するのみです

curl -s https://www.parse.com/downloads/cloud_code/installer.sh | sudo /bin/bash

終わったら実行して確認してみます。

$ parse 
No command specified

Parse Command Line Interface
Version 1.3.1
Copyright 2014 Parse, Inc.
http://parse.com

Usage: parse <command>
       (Arguments in <> are required.  Those in [] are optional.)

  Possible commands are:
  
  add [app]
    Adds a new Parse App through an interactive interface.  If an argument is
    given, the added application can also be referenced by that name.
    -l,--local   Stores the new application in the local config file rather
                 than the global one.
  
  cliversion
    Gets CLI tools version.
  
  default [app]
    Gets the default Parse App. If an argument is given, sets the default Parse App.
  
  deploy [app]
    Deploys the current code to the given app.
    -d, --description   Add an optional description to the deploy.
  
  develop app
    Monitors for changes to source files and uploads updated files to Parse.
    This will also monitor the parse INFO log for any new log messages and write
    out updates to the terminal.  This requires an app to be provided, to
    avoid running develop on production apps accidently.
  
  generate express [option]
    Generates a sample Express app in the current project directory.
    -j --jade       Use Jade templates (EJS templates is the default).
  
  help [command]
    Prints out usage text for a given command, or all commands if none was
    specified.
  
  jssdk [version]
    Sets the Parse JavaScript SDK version to use in Cloud Code.
    Prints the version number currently set if none is specified.
    -a, --all       Shows all available JavaScript SDK version.
  
  list
    Prints the list of apps and aliases associated with this project.
  
  log [app]
    Prints out recent log messages.
    -n              The number of the messages to display.  Defaults to 10.
    -f              Emulates tail -f and streams new messages from the server
    -l, --level     The log level to restrict to.  Can be 'INFO' or 'ERROR'.
                    Defaults to 'INFO'.
  
  new
    Creates a Parse project in the current directory.  An optional argument can
    be given to specify the directory to create.  Otherwise, the 'parse'
    directory is created.
  
  releases [app]
    Prints the releases the server knows about.
  
  rollback [app]
    Rolls back the for the given app.
    -r, --release   Provides an optional release to rollback to.  If no release
                    is provided, rolls back to the previous release.
  
  update
    Updates this tool to the latest version.

はい。確認できました。これでSDKのインストールは完了です

はい。Next

プロジェクト作成

1.Cloud Codeのプロジェクトを作成します。まずはParse上にApplicationを作成しておきます。

スクリーンショット 2014-05-06 20.19.21

2.次にParseコマンドを実行して新しいプロジェクトを作成します。

$ parse new SimpleBlog
Creating a new project in directory /Users/hoge/Develop/CloudCode/SimpleBlog
Creating directory /Users/hoge/Develop/CloudCode/SimpleBlog/config
Creating config file /Users/hoge/Develop/CloudCode/SimpleBlog/config/global.json
Creating directory /Users/hoge/Develop/CloudCode/SimpleBlog/cloud
Writing out sample file /Users/hoge/Develop/CloudCode/SimpleBlog/cloud/main.js
Creating directory /Users/hoge/Develop/CloudCode/SimpleBlog/public
Writing out sample file /Users/hoge/Develop/CloudCode/SimpleBlog/public/index.html
(snip)

必要なファイルが自動生成されます。次に認証を行います

(cont)
Email: youraddress@yours.com
Password: 
(snip)

Parseに登録してあるメールアドレスとパスワードを入力します。 無事認証されれば、次にどのParseアプリケーションを開発するかを選択します。

(cont)
1: TestSample
2: PhotoStore
3: SimpleBlog
Select an App: 3

先ほど作成したアプリケーション以外に先に2つほどParseアプリケーションを作っておいたので、選択肢に3つ出てきました。 開発対象のアプリケーションを選択します。 これで、どのアプリケーションに対してCloud CodeをDeployするかが設定に書き込まれます。

スクリーンショット 2014-05-06 20.40.37

サーバーへデプロイ

作成したプロジェクトはローカルで編集した後、サーバーへDeployすることではじめてバックエンドの動作へ反映させることが出来ます。 作成直後のファイルたちは、一度もDeployされていないのでまだ存在しません。Deployしてみましょう。

$ parse deploy

このコマンドだけでDeployされます。

$ parse deploy
Uploading source files
Finished uploading files
New release is named v1 (using Parse JavaScript SDK v1.2.16)

スクリーンショット 2014-05-06 20.53.31

正常にDeployされてParseに展開されているのが確認できました。

注意事項

注意すべき点は、コマンドを実行する際のディレクトリ位置です。

$ pwd
/Users/hoge/Develop/CloudCode/SimpleBlog

作成したCloud Codeプロジェクト内でなければ、Parseコマンドは無効です。プロジェクトディレクトリ外で実行してみます。

$ parse deploy
/usr/local/bin/parse should be run in a directory containing a Parse project

はい。怒られました。Parseコマンドを実行する場合は、プロジェクトディレクトリ以下で実行しましょう

開発のヒントになるドキュメント(超重要)

Cloud Codeで開発する上で必須のドキュメントはこれ。正直このドキュメントとStackOverflowくらいしか参考になりません。

Parse JavaScript SDK & Cloud Code Reference

ここからうまく組み上げます

まとめ

まずは、Parseのバックエンドの動作を開発するためのSDKの導入までを紹介しました。 次回は、具体的なFunctionの説明、デバッグの方法などを紹介します

参照