This repository has been archived by the owner on Nov 23, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
--- | ||
title: Shard | ||
sidebar_position: 2 | ||
--- | ||
|
||
Shard transforms your data into IPFS objects or blocks | ||
|
||
## Creating | ||
|
||
Shard can be binary or object | ||
|
||
```typescript | ||
import { Shard, ShardKind } from '@dstack-js/lib'; | ||
import { Buffer } from 'buffer'; | ||
|
||
const bufShard = await Shard.new<Buffer>( | ||
stack, | ||
ShardKind.Binary, | ||
Buffer.from('hello, world!') | ||
); | ||
|
||
const objShard = await Shard.new<{ hello: string }>(stack, ShardKind.Object, { | ||
hello: 'world', | ||
}); | ||
``` | ||
|
||
## Sharing | ||
|
||
You can get shard path and use in other nodes | ||
|
||
```typescript | ||
const shard = await Shard.new<Buffer>( | ||
stack, | ||
ShardKind.Binary, | ||
Buffer.from('hello, world!') | ||
); | ||
|
||
console.log(shard.toString()); // "/shard/..some..cid../binary" | ||
``` | ||
|
||
```typescript | ||
const shard = await Shard.from<Buffer>(stack, '/shard/..some..cid../binary'); | ||
|
||
console.log(shard.data.toString()); // "hello, world!" | ||
``` | ||
|
||
## Putting a new value | ||
|
||
```typescript | ||
import { Shard, ShardKind } from '@dstack-js/lib'; | ||
import { Buffer } from 'buffer'; | ||
|
||
const shard = await Shard.new<Buffer>( | ||
stack, | ||
ShardKind.Binary, | ||
Buffer.from('hello, world!') | ||
); | ||
|
||
await shard.put(Buffer.from('hello, dstack!')); | ||
``` | ||
|
||
## Events | ||
|
||
### Data update | ||
|
||
```typescript | ||
shard.put({ hello: 'dstack' }); | ||
|
||
shard.on('update', (shard) => { | ||
console.log(shard.data); // {"hello": "dstack"} | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
title: Store | ||
sidebar_position: 1 | ||
--- | ||
|
||
Store provides possibility to store [Shards](shard) bound to keys | ||
|
||
Also store replicates shards/keys to the peers in same stack namespace | ||
|
||
## Setting value | ||
|
||
By providing shard: | ||
|
||
```typescript | ||
const shard = await Shard.new<{ hello: string }>(stack, ShardKind.Object, { | ||
hello: 'world', | ||
}); | ||
|
||
await stack.store.set('hello', shard); | ||
``` | ||
|
||
## Getting value | ||
|
||
```typescript | ||
const shard = await stack.store.get<{ hello: string }>('hello'); | ||
|
||
console.log(shard.data); // {"hello": "world"} | ||
``` | ||
|
||
## Watching for a new value | ||
|
||
```typescript | ||
const shard = await stack.store.get<{ hello: string }>('hello'); | ||
|
||
shard.put({ hello: 'dstack' }); | ||
|
||
shard.on('update', (shard) => { | ||
console.log(shard.data); // {"hello": "dstack"} | ||
}); | ||
``` | ||
|
||
See [Shard](shard) to get more possibilities | ||
|
||
[shard]: /docs/shard |