Skip to content
This repository has been archived by the owner on Nov 23, 2022. It is now read-only.

Commit

Permalink
feat(docs): add store and shard #20
Browse files Browse the repository at this point in the history
  • Loading branch information
0x77dev committed Feb 3, 2022
1 parent 1911e22 commit bbc47ae
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
72 changes: 72 additions & 0 deletions docs/docs/shard.md
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"}
});
```
44 changes: 44 additions & 0 deletions docs/docs/store.md
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

0 comments on commit bbc47ae

Please sign in to comment.