# sdb-ts

> A TypeScript Wrapper for ShareDB

Latest version **0.4.0** (published 2020-08-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install sdb-ts
pnpm add sdb-ts
yarn add sdb-ts
bun add sdb-ts
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2020-08-09 |
| First published | 2018-08-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 233.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Maintainers | soney |

## Links

- npm: https://www.npmjs.com/package/sdb-ts
- Repository: https://github.com/soney/sdb-ts
- Homepage: https://github.com/soney/sdb-ts#readme
- Issues: https://github.com/soney/sdb-ts/issues
- npm.io page: https://npm.io/package/sdb-ts

## Dependencies (5)

- [ws](https://npm.io/package/ws.md) ^7.3.1
- [sharedb](https://npm.io/package/sharedb.md) ^1.4.1
- [isomorphic-ws](https://npm.io/package/isomorphic-ws.md) ^4.0.1
- [immutability-helper](https://npm.io/package/immutability-helper.md) ^3.1.1
- [reconnecting-websocket](https://npm.io/package/reconnecting-websocket.md) ^4.4.0

## Recent versions

- 0.4.0 (latest) — 2020-08-09
- 0.3.9 — 2020-08-09
- 0.3.8 — 2020-01-23
- 0.3.7 — 2020-01-23
- 0.3.6 — 2020-01-22
- 0.3.5 — 2020-01-21
- 0.3.4 — 2020-01-17
- 0.3.3 — 2020-01-17
- 0.3.2 — 2020-01-16
- 0.3.1 — 2020-01-11
- 0.3.1-beta — 2020-01-09
- 0.3.0 — 2019-10-02
- 0.2.9 — 2019-10-01
- 0.2.8 — 2019-10-01
- 0.2.7 — 2019-09-29
- … 8 more at https://npm.io/package/sdb-ts/versions

## README

# SDB-TS
sdb-ts is a TypeScript shim for [ShareDB](https://github.com/share/sharedb).

This project is undergoing sporadic development.

## NPM Install:
To install via npm, run:

```
npm install sdb-ts
```

## Client-side Usage
For client-side code, use [`built/sdb-client-bundle.js`](https://raw.githubusercontent.com/soney/sdb-ts/master/built/sdb-client-bundle.js).

`window.SDB` will contain `SDBClient, SDBDoc, and SDBSubDoc`

## Usage Examples



### Creating a server (in Node.js):

If no argument is provided, the server will pick a port:
```
import { SDBServer } from '../built/index'; 
async(() => {
	const sdbServer = new SDBServer();
	const { port }  = await sdbServer.address();

	await sdbServer.listening();
	console.log(`Created server on port ${port}`);
});
```

Otherwise, the constructor can be called with the server passed in:
```
import * as WebSocket from 'ws';
import * as http from 'http';
import { SDBServer } from '../built/index'; 

const server = http.createServer(8000);
const wss = new WebSocket.Server({ server });
const sdbServer = new SDBServer(wss);
server.listen();
const address = server.address() as AddressInfo;
```

### Working with Documents

```
import { SDBServer, SDBDoc } from '../built/index'; 
//...

interface CounterDoc {
	counter: number;
	sd: {
		strEx: string;
		numEx: number;
	};
}

const sCounterDoc: SDBDoc<CounterDoc> = sdbServer.get('ex', 'counter');

sCounterDoc.createIfEmpty({
	counter: 0,
	sd: {
		strEx: 'ABC',
		numEx: 3
	}
});
scounterDoc.subscribe((eventType: string, ops, source, data) => {
	if(eventType === null) {
		console.log('subscribed', data);
	} else if(eventType === 'create') {
		console.log('create', data);
	} else if(eventType === 'op') {
		console.log(ops);
	}
	console.log(scounterDoc.getData());
	console.log(sCounterDoc.traverse(['sd', 'numEx']));
});

sCounterDoc.submitNumberAddOp(['counter'], 1);

const subDoc = d.subDoc(['sd']);
subDoc.subscribe();

subDoc.submitObjectReplaceOp(['numEx'], 33);
```

### Connecting a Client (in browser):
```
const sdbClient = new SDB.SDBClient(ws);
const doc:SDB.SDBDoc<CounterDoc> = sdbClient.get('ex', 'counter');
doc.subscribe(() => {
	console.log('update');
});
```

## Building
```
npm install .
npx tsc
npx webpack
```
To watch:
```
npx tsc --watch
```
or (if building for client-side)
```
npx webpack --watch
```

## Testing

```
npm test
```

### Auto-generated Documentation

* ["SDBClient"](doc/modules/_sdbclient_.md)
* ["SDBServer"](doc/modules/_sdbserver_.md)
* ["SDBDoc"](doc/modules/_sdbdoc_.md)
* ["SDBSubDoc"](doc/modules/_sdbsubdoc_.md)
<!-- * ["OpSubmittable"](doc/modules/_opsubmittable_.md) -->
<!-- * ["SDB"](doc/modules/_sdb_.md) -->
<!-- * ["index"](doc/modules/_index_.md) -->
<!-- * ["utils"](doc/modules/_utils_.md) -->

---
_Source: https://npm.io/package/sdb-ts · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
