add example code

Hasn't been tested yet, none of this probably works.
This commit is contained in:
phritz
2017-09-07 15:35:10 -10:00
committed by GitHub
parent a25ea915a7
commit f0077ccbf6

View File

@@ -1,4 +1,4 @@
# How to Use Noms in a Decentralized App WORK IN PROGRESS
# How to Use Noms in a Decentralized App
If youd like to use noms in your project wed love to hear from you:
drop us an email ([noms@attic.io](mailto:noms@attic.io)) or send us a
@@ -22,7 +22,7 @@ The steps youll need to take are:
[IPNS](https://github.com/ipfs/examples/tree/master/examples/ipns)
and pull immediately.
1. Vendor the code into your project.
1. TODO add env and nomsconfig stuff here?
1. Set `NOMS_VERSION_NEXT=1` in your environment.
1. Decide which type of storage you'd like to use: memory (convenient for playing around), disk, IPFS, or S3. (If you'd to implement a store on top of another type of storage that's possible too; email us or reach out on slack and we can help.)
1. Set up and instantiate a database for your storage. Generally, you give a [dataset spec](https://github.com/attic-labs/noms/blob/master/doc/spelling.md) like `mem::mydataset` to a [`config.Resolver`](https://github.com/attic-labs/noms/blob/master/go/config/resolver.go) which gives you a handle to the [`Database`](https://github.com/attic-labs/noms/blob/master/go/datas/database.go) and [`Dataset`](https://github.com/attic-labs/noms/blob/master/go/datas/dataset.go).
* **Memory**: no setup required, just instantiate it:
@@ -46,8 +46,47 @@ The steps youll need to take are:
database := datas.NewDatabase(store)
dataset := database.GetDataset("aws://dynamo-table:s3-bucket/store-name::test") // Dataset name is "test"
```
...TODO
1. Implement using the [Go API](../go-tour.md).
1. Implement using the [Go API](../go-tour.md). If you're just playing around you could try something like this.
```
func newPerson(givenName string, male bool) types.Struct {
return types.NewStruct("Person", types.StructData{
"given": types.String(givenName),
"male": types.Bool(male),
})
}
data := types.NewList(database,
newPerson("Rickon", true),
newPerson("Bran", true),
newPerson("Arya", false),
newPerson("Sansa", false),
)
fmt.Fprintf(os.Stdout, "data type: %v\n", types.TypeOf(data).Describe())
_, err = db.CommitValue(sp.GetDataset(), data)
if err != nil {
fmt.Fprint(os.Stderr, "Error commiting: %s\n", err)
}
sp, err := spec.ForDataset("<spec from above>")
if err != nil {
fmt.Fprintf(os.Stderr, "Could not create dataset: %s\n", err)
return
}
defer sp.Close()
if headValue, ok := sp.GetDataset().MaybeHeadValue(); !ok {
fmt.Fprintf(os.Stdout, "head is empty\n")
} else {
// type assertion to convert Head to List
personList := headValue.(types.List)
// type assertion to convert List Value to Struct
personStruct := personList.Get(0).(types.Struct)
// prints: Rickon
fmt.Fprintf(os.Stdout, "given: %v\n", personStruct.Get("given"))
}
```
1. You can inspect data that you've committed via the [noms command-line interface](https://github.com/attic-labs/noms/blob/master/doc/cli-tour.md). Consider creating a [.nomsconfig](https://github.com/attic-labs/noms/blob/master/samples/cli/nomsconfig/README.md) file to save the trouble of writing database specs on the command line.
* Note that Memory tables won't be inspectable because they exist only in the memory of the process that created them.
1. Implement pull and merge. The [pull API](../../go/datas/pull.go) is used pull changes from a peer and the [merge API](../../go/merge/) is used to merge changes before commit. There's an [example of merging in the IPFS-based-chat sample
app](https://github.com/attic-labs/noms/blob/master/samples/go/ipfs-chat/pubsub.go).