【FaunaDB】CLIコマンドチートシート


FaunaDBをCLIから操作するさいのインストール手順と操作手順になります。
ほぼ元のサイトの日本語訳的な感じです。

## インストール
### npm
```bash:console
$ npm install -g fauna-shell
```

### yarn
```bash:console
$ yarn add -g fauna-shell
```

### homebrew
```bash:console
$ brew install fauna-shell
```

## ログイン
すべてのコマンドはログイン後に有効になるため、まずはログインをします。

```bash:console
$ fauna cloud-login
```

このあと、この様に聞かれるので`メールアドレス&パスワード`か`AccountAPIKey`のどちらかを入力します。

```bash:console
For email login, enter your email below, and then your password.
For login with 3rd-party identity providers like Github or Netlify, please acquire a key from Dashboard > Security and enter it below instead.

Email or secret key: 
```


## Databaseを作成する
```bash:console
$ fauna create-database [database名]
```

**例**

```bash:example
$ fauna create-database my_db
creating database my_db

  created database 'my_db'

  To start a shell with your new database, run:

  fauna shell 'my_db'

  Or, to create an application key for your database, run:

  fauna create-key 'my_db'
  
✨  Done in 2.09s.
```


### Databaseを使用する（shellを起動する）
```bash:console
$ fauna shell [database名]
```

**例**

```bash:example
$ fauna shell my_db
Starting shell for database my_db
Connected to https://db.fauna.com
Type Ctrl+D or .exit to exit the shell
my_db> 
```

## Collectionを作成する
```bash:console
my_db> CreateCollection({ name: "[Collection名]" })
```

**例**

```bash:example
my_db> CreateCollection({ name: "posts" })
{
  ref: Collection("posts"),
  ts: 1581161185130000,
  history_days: 30,
  name: 'posts'
}
```

<font color="Red">**注意：コンソール画面からCollectionを作った場合はIndexも同時に作られますが、CLIの場合はIndexは作成されません。**</font>

### Indexを作成する
```bash:console
my_db> CreateIndex({
  name: "[Index名]",
  source: Collection("[作成対象のCollection名]"),
  terms: [{ field: ["data", "title"] }]
})

```

**例**

```bash:example
my_db> CreateIndex({
  name: "posts_by_title",
  source: Collection("posts"),
  terms: [{ field: ["data", "title"] }]
})
{
  ref: Index("posts_by_title"),
  ts: 1581161649920000,
  active: true,
  serialized: true,
  name: 'posts_by_title',
  source: Collection("posts"),
  terms: [ { field: [ 'data', 'title' ] } ],
  partitions: 1
}
```

## Documentを追加する
```bash:console
my_db> Create(
  Collection("[作成対象のCollection名]"),
  { data: { [Key]: "[Value]" } }
)
```

**例**

```bash:example
my_db> Create(
  Collection("posts"),
  { data: { title: "What I had for breakfast .." } }
)
{
  ref: Ref(Collection("posts"), "256799587961930249"),
  ts: 1581162021510000,
  data: { title: 'What I had for breakfast ..' }
}
```

## Roleの追加

```bash:console
my_db> CreateRole({
  name: "MyRole",
  privileges: [
    {
      resource: Collection("posts"),
      actions: {
        read: true,
        write: true,
        create: true,
        delete: true,
        history_read: false,
        history_write: false,
        unrestricted_read: false
      }
    },
    {
      resource: Index("posts_by_title"),
      actions: {
        unrestricted_read: false,
        read: true
      }
    },
    {
      resource: Ref("collections"),
      actions: {
        read: true,
        write: true,
        create: true,
        delete: true,
        history_read: false,
        history_write: false
      }
    }
  ],
  membership: []
})
```

**例**

```bash:example
my_db> CreateRole({
  name: "MyRole",
  privileges: [
    {
      resource: Collection("posts"),
      actions: {
        read: true,
        write: true,
        create: true,
        delete: true,
        history_read: false,
        history_write: false,
        unrestricted_read: false
      }
    },
    {
      resource: Index("posts_by_title"),
      actions: {
        unrestricted_read: false,
        read: true
      }
    },
    {
      resource: Ref("collections"),
      actions: {
        read: true,
        write: true,
        create: true,
        delete: true,
        history_read: false,
        history_write: false
      }
    }
  ],
  membership: []
})
{
  ref: Role("MyRole"),
  ts: 1581572293910000,
  name: 'MyRole',
  privileges: [
    {
      resource: Collection("posts"),
      actions: {
        read: true,
        write: true,
        create: true,
        delete: true,
        history_read: false,
        history_write: false,
        unrestricted_read: false
      }
    },
    {
      resource: Index("posts_by_title"),
      actions: { unrestricted_read: false, read: true }
    },
    {
      resource: Collections(),
      actions: {
        read: true,
        write: true,
        create: true,
        delete: true,
        history_read: false,
        history_write: false
      }
    }
  ],
  membership: []
}
```

## Keyの作成

```bash:console
my_db> CreateKey({
  name: "MyAccessKey",
  role: Role("MyRole")
})
```

**例**

```bash:example
my_db> CreateKey({
  name: "MyAccessKey",
  role: Role("MyRole")
})
{
  ref: Ref(Keys(), "257230138406601217"),
  ts: 1581572626430000,
  name: 'MyAccessKey',
  role: Role("MyRole"),
  secret: 'fnADkd1ysUACAdOAuRp_v0zrY--**************',
  hashed_secret: '$2a$05$kp8mVOtKfyMLnmVUMR6.F.cJ2cT3X3LRQ9EvkRqRvs7HzNkxJx2yq'
}
```

## 検索系コマンド
`my_db`というDatabaseのShellに入っている状態でのコマンドになります。

```bash:console
my_db> Get( Ref(Collection("対象のCollection名"), "[Collection内のDocument番号]"))
```

**例**

```bash:example
my_db> Get( Ref(Collection("posts"), "256799587961930249"))
{
  ref: Ref(Collection("posts"), "256799587961930249"),
  ts: 1581162021510000,
  data: { title: 'What I had for breakfast ..' }
}
```


## 参照元サイト
[Quick start with FaunaDB
](https://docs.fauna.com/fauna/current/start/cloud)