Ditto JS SDK
Ditto is a cross platform SDK that allows mobile, web, and IoT apps to sync
with and even without connectivity.
IMPORTANT: The Ditto JS SDK is currently in an early alpha stage. As such,
it is neither complete nor stable. Expect the API to change at any time.
Getting Started
Add the Ditto NPM package to your project:
npm install --save @dittolive/ditto
GOTCHA: we only support running in the browser via WebAssembly at this
stage (native Node support is on the way). Because WebAssembly (.wasm
) files
can only be loaded from a proper URL, Ditto won't work when imported locally.
At least the Ditto JS files together with the ditto.wasm
file needs to be
served via a web server. The ditto.wasm
file needs to be located in the exact
same location as the ditto.es6.js
(or ditto.umd.js
if you are using the UMD
module). Note that bundlers combine everything into one main.js
file
(or similar). In that case you'll have to make sure the ditto.wasm
file is
copied to the same location. If you are using Webpack, take a look how we do
it for our data browser: webpack.dev.js.
Once you make sure the ditto.wasm
is served properly, you can import it in
your source and start using it:
import { Ditto } from '@dittolive/ditto'
(async () => {
// Create a Ditto context:
const identity = { environment: 'development', appName: 'live.ditto.playground', siteID: BigInt('123') }
const ditto = await new Ditto(identity, 'playground')
// Get hold of a collection:
const cars = ditto.store.collection('cars')
// Insert an entry:
const fordBlack = {model: "Ford", color: "black"}
await cars.insert({id: "ford-black-123", value: fordBlack})
// Find an entry by ID:
const foundFordBlack = await cars.findByID('ford-black-123')
console.log(foundFordBlack)
// Remove an entry:
await cars.findByID('ford-black-123').remove()
// Done:
console.log("Done, over and out.")
})()
Playground
The NPM package has a built-in bare minimum playground page allowing you to
start playing with the Ditto SDK in your browser right away. Simply cd
into
the package, run npm run play
, and open the printed address in your browser
(http://localhost:XXXX
).
Copyright
Ditto JS SDK is a commercial product. Please consult LICENSE.md
within this
package for license details.
Copyright © 2019 - 2021 DittoLive, Inc. All rights reserved.