UNPKG

14.5 kBMarkdownView Raw
1fauna-shell
2===========
3<!-- [![Version](https://img.shields.io/npm/v/fauna.svg)](https://npmjs.org/package/fauna)
4[![CircleCI](https://circleci.com/gh/fauna/fauna/tree/master.svg?style=shield)](https://circleci.com/gh/fauna/fauna/tree/master)
5[![Appveyor CI](https://ci.appveyor.com/api/projects/status/github/fauna/fauna?branch=master&svg=true)](https://ci.appveyor.com/project/fauna/fauna/branch/master)
6[![Codecov](https://codecov.io/gh/fauna/fauna/branch/master/graph/badge.svg)](https://codecov.io/gh/fauna/fauna)
7[![Downloads/week](https://img.shields.io/npm/dw/fauna.svg)](https://npmjs.org/package/fauna)
8[![License](https://img.shields.io/npm/l/fauna.svg)](https://github.com/fauna/fauna/blob/master/package.json) -->
9
10This tools gives you access to [FaunaDB](http://fauna.com/) directly from your CLI.
11
12It also includes a [Shell](#shell) so you can issue queries to FaunaDB without needing to install additional libraries.
13
14You can install it via npm like this:
15
16```sh-session
17$ npm install -g fauna-shell
18```
19
20<!-- toc -->
21* [Usage](#usage)
22* [Shell](#shell)
23* [Command Details](#command-details)
24* [Connecting to different endpoints](#connecting-to-different-endpoints)
25* [Overriding Connection Parameters](#overriding-connection-parameters)
26* [List of Commands](#list-of-commands)
27<!-- tocstop -->
28
29# Usage
30
31The **fauna-shell** allows you to do things like _creating_, _deleting_ and _listings_ databases.
32
33First lets configure an endpoint to connect to the FaunaDB cloud. (If you don't have an account, you can create a free account [here](https://fauna.com/sign-up)).
34
35Let's run the following command:
36
37```sh-session
38$ fauna add-endpoint "https://db.fauna.com"
39Endpoint Key: ****************************************
40Endpoint Alias [db.fauna.com]: cloud
41```
42
43When you are prompted for your `Endpoint Key` enter actual key from your [FaunaDB Cloud](https://dashboard.fauna.com/) account.
44
45The _Endpoint Alias_ should be a name that helps you remember the purpose of this endpoint, in this case we use `cloud`.
46
47Now that we have an endpoint to connect to we can try to create a database to start playing with FaunaDB.
48
49This is how you can create a database called `my_app`:
50
51```sh-session
52$ fauna create-database my_app
53creating database my_app
54
55created database 'my_app'
56
57To start a shell with your new database, run:
58
59 fauna shell 'my_app'
60
61Or, to create an application key for your database, run:
62
63 fauna create-key 'my_app'
64```
65
66And then listing your databases:
67
68```sh-session
69$ fauna list-databases
70listing databases
71my_app
72my_second_app
73my_other_app
74```
75
76You can also delete a particular database:
77
78```sh-session
79$ fauna delete-database my_other_app
80deleting database 'my_other_app'
81database 'my_other_app' deleted
82```
83
84You can also `create`, `list`, and `delete` _keys_.
85
86This is how you create a key for the database `my_app`:
87
88```sh-session
89$ fauna create-key my_app
90creating key for database 'my_app' with role 'admin'
91
92created key for database 'my_app' with role 'admin'.
93secret: ****************************************
94
95To access 'my_app' with this key, create a client using
96the driver library for your language of choice using
97the above secret.
98```
99
100This is how to list keys (the results may differ from what you see in your instance of FaunaDB)
101
102```sh-session
103$ fauna list-keys
104listing keys
105Key ID Database Role
106203269476002562560 my_app admin
107203269731203940864 my_app admin
108203269732275585536 my_app admin
109203269735610057216 test admin
110```
111
112And then delete the key with id: `200219702370238976`:
113
114```sh-session
115fauna delete-key 200219702370238976
116deleting key 200219702370238976
117key 200219702370238976 deleted
118```
119
120See [Commands](#commands) for a list of commands and help on their usage.
121
122# Shell
123
124The Fauna Shell lets you issue queries directly to your FaunaDB instance without the need for installing additional libraries.
125
126Let's create a database and then we'll jump straight into the Shell to start playing with FaunaDB's data model.
127
128```sh-session
129fauna create-database my_app
130```
131
132Our next step is to start the shell for a specific database, in this case `my_app`:
133
134```sh-session
135fauna shell my_app
136starting shell for database my_app
137faunadb>
138```
139
140Once you have the prompt ready, you can start issues queries against your FaunaDB instance. (Note that the results shown here might vary from the ones you see while running the examples).
141
142```javascript
143faunadb> CreateClass({ name: "posts" })
144faunadb>
145{
146 "ref": Class("posts"),
147 "ts": 1530112459345314,
148 "history_days": 30,
149 "name": "posts"
150}
151```
152
153Let's create an index for our _posts_.
154
155```javascript
156faunadb> CreateIndex(
157 {
158 name: "posts_by_title",
159 source: Class("posts"),
160 terms: [{ field: ["data", "title"] }]
161 })
162faunadb>
163{
164 "ref": Index("posts_by_title"),
165 "ts": 1530112490241958,
166 "active": false,
167 "partitions": 1,
168 "name": "posts_by_title",
169 "source": Class("posts"),
170 "terms": [
171 {
172 "field": [
173 "data",
174 "title"
175 ]
176 }
177 ]
178}
179```
180
181Let's insert a _post_ item:
182
183```javascript
184faunadb> Create(
185 Class("posts"),
186 { data: { title: "What I had for breakfast .." } })
187faunadb>
188{
189 "ref": Ref(Class("posts"), "203270302096949760"),
190 "ts": 1530112516389297,
191 "data": {
192 "title": "What I had for breakfast .."
193 }
194}
195```
196
197We can also insert items in bulk by using the `Map` function.
198
199```javascript
200faunadb> Map(
201 [
202 "My cat and other marvels",
203 "Pondering during a commute",
204 "Deep meanings in a latte"
205 ],
206 Lambda("post_title",
207 Create(
208 Class("posts"), { data: { title: Var("post_title") } }
209 ))
210 )
211faunadb>
212[
213 {
214 "ref": Ref(Class("posts"), "203270317027623424"),
215 "ts": 1530112530627575,
216 "data": {
217 "title": "My cat and other marvels"
218 }
219 },
220 {
221 "ref": Ref(Class("posts"), "203270317027624448"),
222 "ts": 1530112530627575,
223 "data": {
224 "title": "Pondering during a commute"
225 }
226 },
227 {
228 "ref": Ref(Class("posts"), "203270317028672000"),
229 "ts": 1530112530627575,
230 "data": {
231 "title": "Deep meanings in a latte"
232 }
233 }
234]
235```
236
237Now let's try to fetch our post about _latte_. We need to access it by _id_ like this:
238
239```javascript
240faunadb> Get(Ref(Class("posts"),"203270317028672000"))
241{
242 "ref": Ref(Class("posts"), "203270317028672000"),
243 "ts": 1530112530627575,
244 "data": {
245 "title": "Deep meanings in a latte"
246 }
247}
248```
249
250Now let's update our post about our cat, by adding some tags:
251
252```javascript
253faunadb> Update(
254 Ref(Class("posts"), "203270317027623424"),
255 { data: { tags: ["pet", "cute"] } })
256faunadb>
257{
258 "ref": Ref(Class("posts"), "203270317027623424"),
259 "ts": 1530112657679644,
260 "data": {
261 "title": "My cat and other marvels",
262 "tags": [
263 "pet",
264 "cute"
265 ]
266 }
267}
268```
269
270And now let's try to change the content of that post:
271
272```javascript
273faunadb> Replace(
274 Ref(Class("posts"), "203270317027623424"),
275 { data: { title: "My dog and other marvels" } })
276{
277 "ref": Ref(Class("posts"), "203270317027623424"),
278 "ts": 1530112733994661,
279 "data": {
280 "title": "My dog and other marvels"
281 }
282}
283faunadb>
284
285```
286
287Now let's try to delete our post about _latte_:
288
289```javascript
290faunadb> Delete(Ref(Class("posts"), "203270317028672000"))
291faunadb>
292{
293 "ref": Ref(Class("posts"), "203270317028672000"),
294 "ts": 1530112530627575,
295 "data": {
296 "title": "Deep meanings in a latte"
297 }
298}
299```
300
301If we try to fetch it, we will receive an error:
302
303```javascript
304faunadb> Get(Ref(Class("posts"), "203270317028672000"))
305faunadb>
306 Error: instance not found
307```
308
309Finally you can exit the _shell_ by pressing `ctrl+d`.
310
311# Command Details
312<!-- details -->
313```sh-session
314$ fauna COMMAND
315running command...
316$ fauna (-v|--version|version)
317fauna/0.0.1 darwin-x64 node-v8.11.1
318$ fauna --help [COMMAND]
319USAGE
320 $ fauna COMMAND
321...
322```
323
324# Connecting to different endpoints
325
326We can add endpoints by calling the following command `add-endpoint`. We will be prompted to enter the authentication key and an alias for the endpoint.
327
328```sh-session
329$ fauna add-endpoint "https://db.fauna.com"
330Endpoint Key: ****************************************
331Endpoint Alias [db.fauna.com]: cloud
332```
333
334If we have defined many endpoints, we could set one of them as the default one with the `default-endpoint` command:
335
336```sh-session
337$ fauna default-endpoint cloud
338```
339
340The _default endpoint_ will be used by the shell to connect to FaunaDB.
341
342Endpoints can be listed with the `list-endpoints` command like this:
343
344```sh-session
345$ fauna list-endpoints
346localhost
347cloud *
348cluster-us-east
349```
350
351Theere we see that the `cloud` endpoint has a `*` next to its name, meaning that it's the current default one.
352
353Finally, endpoints will be saved to a `~/.fauna-shell` file like this:
354
355```ini
356default=cloud
357
358[localhost]
359domain=127.0.0.1
360port=8443
361scheme=http
362secret=the_secret
363
364[cloud]
365domain=db.fauna.com
366scheme=https
367secret=FAUNA_SECRET_KEY
368
369[cluster-us-east]
370domain=cluster-us-east.example.com
371port=443
372scheme=https
373secret=OTHER_FAUNA_SECRET
374```
375
376# Overriding Connection Parameters
377
378Most commands support the following options. You can specify them if you want to connect to your local FaunaDB instance.
379
380```
381OPTIONS
382 --domain=domain [default: db.fauna.com] FaunaDB server domain
383 --port=port [default: 443] Connection port
384 --scheme=https|http [default: https] Connection scheme
385 --secret=secret FaunaDB secret key
386 --timeout=timeout [default: 80] Connection timeout in milliseconds
387```
388
389They can be used like this:
390
391```sh-session
392fauna create-database testdb --domain=127.0.0.1 port=8443 --scheme=http --secret=YOUR_FAUNA_SECRET_KEY --timeout=42
393```
394
395Options provided via the CLI will override the values set in the `.fauna-shell` config file.
396
397Any options that are not specified either via the `.fauna-shell` config file or the CLI will be set to the defaults offered by the [faunadb-js client](https://github.com/fauna/faunadb-js).
398
399<!-- detailsstop -->
400# List of Commands
401<!-- commands -->
402* [`fauna add-endpoint ENDPOINT`](#fauna-add-endpoint-endpoint)
403* [`fauna create-database DBNAME`](#fauna-create-database-dbname)
404* [`fauna create-key DBNAME [ROLE]`](#fauna-create-key-dbname-role)
405* [`fauna default-endpoint ENDPOINT_ALIAS`](#fauna-default-endpoint-endpoint-alias)
406* [`fauna delete-database DBNAME`](#fauna-delete-database-dbname)
407* [`fauna delete-key KEYNAME`](#fauna-delete-key-keyname)
408* [`fauna help [COMMAND]`](#fauna-help-command)
409* [`fauna list-databases`](#fauna-list-databases)
410* [`fauna list-endpoints`](#fauna-list-endpoints)
411* [`fauna list-keys`](#fauna-list-keys)
412* [`fauna shell DBNAME`](#fauna-shell-dbname)
413
414## `fauna add-endpoint ENDPOINT`
415
416Adds a connection endpoint for FaunaDB
417
418```
419USAGE
420 $ fauna add-endpoint ENDPOINT
421
422ARGUMENTS
423 ENDPOINT FaunaDB server endpoint
424
425DESCRIPTION
426 Adds a connection endpoint for FaunaDB
427
428
429EXAMPLE
430 $ fauna-shell add-endpoint https://db.fauna.com:443
431```
432
433_See code: [src/commands/add-endpoint.js](https://github.com/fauna/fauna-shell/blob/v0.1.3/src/commands/add-endpoint.js)_
434
435## `fauna create-database DBNAME`
436
437Creates a database
438
439```
440USAGE
441 $ fauna create-database DBNAME
442
443ARGUMENTS
444 DBNAME database name
445
446DESCRIPTION
447 Creates a database
448
449
450EXAMPLE
451 $ fauna-shell create-database dbname
452```
453
454_See code: [src/commands/create-database.js](https://github.com/fauna/fauna-shell/blob/v0.1.3/src/commands/create-database.js)_
455
456## `fauna create-key DBNAME [ROLE]`
457
458Creates a key for the specified database
459
460```
461USAGE
462 $ fauna create-key DBNAME [ROLE]
463
464ARGUMENTS
465 DBNAME database name
466 ROLE key user role
467
468DESCRIPTION
469 Creates a key for the specified database
470
471
472EXAMPLE
473 $ fauna-shell create-key dbname admin
474```
475
476_See code: [src/commands/create-key.js](https://github.com/fauna/fauna-shell/blob/v0.1.3/src/commands/create-key.js)_
477
478## `fauna default-endpoint ENDPOINT_ALIAS`
479
480Sets an endpoint as the default one
481
482```
483USAGE
484 $ fauna default-endpoint ENDPOINT_ALIAS
485
486ARGUMENTS
487 ENDPOINT_ALIAS FaunaDB server endpoint
488
489DESCRIPTION
490 Sets an endpoint as the default one
491
492
493EXAMPLE
494 $ fauna-shell default-endpoint endpoint
495```
496
497_See code: [src/commands/default-endpoint.js](https://github.com/fauna/fauna-shell/blob/v0.1.3/src/commands/default-endpoint.js)_
498
499## `fauna delete-database DBNAME`
500
501Deletes a database
502
503```
504USAGE
505 $ fauna delete-database DBNAME
506
507ARGUMENTS
508 DBNAME database name
509
510DESCRIPTION
511 Deletes a database
512
513
514EXAMPLE
515 $ fauna-shell delete-database dbname
516```
517
518_See code: [src/commands/delete-database.js](https://github.com/fauna/fauna-shell/blob/v0.1.3/src/commands/delete-database.js)_
519
520## `fauna delete-key KEYNAME`
521
522Deletes a key
523
524```
525USAGE
526 $ fauna delete-key KEYNAME
527
528ARGUMENTS
529 KEYNAME key name
530
531DESCRIPTION
532 Deletes a key
533
534
535EXAMPLE
536 $ fauna-shell delete-key 123456789012345678
537```
538
539_See code: [src/commands/delete-key.js](https://github.com/fauna/fauna-shell/blob/v0.1.3/src/commands/delete-key.js)_
540
541## `fauna help [COMMAND]`
542
543display help for fauna
544
545```
546USAGE
547 $ fauna help [COMMAND]
548
549ARGUMENTS
550 COMMAND command to show help for
551
552OPTIONS
553 --all see all commands in CLI
554```
555
556_See code: [@oclif/plugin-help](https://github.com/oclif/plugin-help/blob/v1.2.11/src/commands/help.ts)_
557
558## `fauna list-databases`
559
560Lists top level databases
561
562```
563USAGE
564 $ fauna list-databases
565
566DESCRIPTION
567 Lists top level databases
568
569
570EXAMPLE
571 $ fauna-shell list-databases
572```
573
574_See code: [src/commands/list-databases.js](https://github.com/fauna/fauna-shell/blob/v0.1.3/src/commands/list-databases.js)_
575
576## `fauna list-endpoints`
577
578Lists FaunaDB connection endpoints
579
580```
581USAGE
582 $ fauna list-endpoints
583
584DESCRIPTION
585 Lists FaunaDB connection endpoints
586
587
588EXAMPLE
589 $ fauna-shell list-endpoints
590```
591
592_See code: [src/commands/list-endpoints.js](https://github.com/fauna/fauna-shell/blob/v0.1.3/src/commands/list-endpoints.js)_
593
594## `fauna list-keys`
595
596Lists top level keys
597
598```
599USAGE
600 $ fauna list-keys
601
602DESCRIPTION
603 Lists top level keys
604
605
606EXAMPLE
607 $ fauna-shell list-keys
608```
609
610_See code: [src/commands/list-keys.js](https://github.com/fauna/fauna-shell/blob/v0.1.3/src/commands/list-keys.js)_
611
612## `fauna shell DBNAME`
613
614Starts a FaunaDB shell
615
616```
617USAGE
618 $ fauna shell DBNAME
619
620ARGUMENTS
621 DBNAME database name
622
623DESCRIPTION
624 Starts a FaunaDB shell
625
626
627EXAMPLE
628 $ fauna-shell dbname
629```
630
631_See code: [src/commands/shell.js](https://github.com/fauna/fauna-shell/blob/v0.1.3/src/commands/shell.js)_
632<!-- commandsstop -->