sdb-ts is a TypeScript shim for ShareDB.
This project is undergoing sporadic development.
To install via npm, run:
npm install sdb-ts
For client-side code, use built/sdb-client-bundle.js
.
window.SDB
will contain SDBClient, SDBDoc, and SDBSubDoc
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;
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);
const sdbClient = new SDB.SDBClient(ws);
const doc:SDB.SDBDoc<CounterDoc> = sdbClient.get('ex', 'counter');
doc.subscribe(() => {
console.log('update');
});
npm install .
npx tsc
npx webpack
To watch:
npx tsc --watch
or (if building for client-side)
npx webpack --watch
npm test