2023-04-12 07:37:24 +03:00
|
|
|
# @toeverything/y-indexeddb
|
|
|
|
|
2023-06-17 08:58:48 +03:00
|
|
|
## Features
|
|
|
|
|
|
|
|
- persistence data in indexeddb
|
|
|
|
- sub-documents support
|
|
|
|
- fully TypeScript
|
|
|
|
|
2023-04-12 07:37:24 +03:00
|
|
|
## Usage
|
|
|
|
|
|
|
|
```ts
|
2023-05-04 02:16:22 +03:00
|
|
|
import { createIndexedDBProvider, downloadBinary } from '@toeverything/y-indexeddb';
|
2023-04-12 07:37:24 +03:00
|
|
|
import * as Y from 'yjs';
|
2023-06-17 08:58:48 +03:00
|
|
|
|
|
|
|
const yDoc = new Y.Doc({
|
|
|
|
// we use `guid` as unique key
|
|
|
|
guid: 'my-doc',
|
|
|
|
});
|
2023-04-12 07:37:24 +03:00
|
|
|
|
2023-05-04 02:16:22 +03:00
|
|
|
// sync yDoc with indexedDB
|
2023-06-17 08:58:48 +03:00
|
|
|
const provider = createIndexedDBProvider(yDoc);
|
2023-04-12 07:37:24 +03:00
|
|
|
provider.connect();
|
|
|
|
await provider.whenSynced.then(() => {
|
|
|
|
console.log('synced');
|
|
|
|
provider.disconnect();
|
|
|
|
});
|
2023-05-04 02:16:22 +03:00
|
|
|
|
|
|
|
// dowload binary data from indexedDB for once
|
2023-06-17 08:58:48 +03:00
|
|
|
downloadBinary(yDoc.guid).then(blob => {
|
2023-05-04 02:16:22 +03:00
|
|
|
if (blob !== false) {
|
|
|
|
Y.applyUpdate(yDoc, blob);
|
|
|
|
}
|
|
|
|
});
|
2023-04-12 07:37:24 +03:00
|
|
|
```
|