2
1

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.

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

2
Last updated at Posted at 2020-02-08

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

インストール

npm

console
$ npm install -g fauna-shell

yarn

console
$ yarn add -g fauna-shell

homebrew

console
$ brew install fauna-shell

ログイン

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

console
$ fauna cloud-login

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

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を作成する

console
$ fauna create-database [database名]

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を起動する)

console
$ fauna shell [database名]

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を作成する

console
my_db> CreateCollection({ name: "[Collection名]" })

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

注意:コンソール画面からCollectionを作った場合はIndexも同時に作られますが、CLIの場合はIndexは作成されません。

Indexを作成する

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

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を追加する

console
my_db> Create(
  Collection("[作成対象のCollection名]"),
  { data: { [Key]: "[Value]" } }
)

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の追加

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: []
})

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の作成

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

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に入っている状態でのコマンドになります。

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

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

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?