Skip to content

Database ​

Uva provides a really simple Database for storing and updating JSON files.

js
world.db.set(key, Object) -> Promise: {status: 'okay | error'}
js
world.db.get(key) -> Promise: Object | null

Each file is signed by the person who creates it, and any changes can only be made by that person.

Internally this database is used for storing project and user data.

You can use the database for anything you want, or use your own database.

Please note that document names are scoped to a specific project. For example, if you use:

js
await world.db.get('myKey')

in different worlds, each call will return different results. This ensures that documents are stored securely, without the risk of other projects overwriting or modifying your files.

Local first ​

All documents are saved using IndexedDB and any changes you make offline are sychronised when you re-connect to the internet.

Examples ​

Get a document ​

js
// Get a document you have uploaded
const object = await world.db.get('objectKey')

console.log(object) -> {very: 'meaowy!'}

Set a document ​

js
// Create or update a file
const data = { player: 'data' }
const result = await world.db.set('someKey', data)

Another example,

Lego brick game: Anyone who uses your app can save the maps they create:

js

const save = (bricks, mapName)=>{
    // Document containing the map
    const fileKey = world.username + '_' + mapName

    const data = { map: mapName, bricks: bricks }
    const result = await world.db.set(fileKey, data)

    // Another document storing the maps they have created
    usedData.maps.[mapName] = { version: 1}
    const result = await world.db.set(world.username, userData)
}

let bricks = [
	{ x: 0, y: 1, z: 3 },
	{ x: 5, y: -1, z: 2 },
]

save(bricks, 'utopia')

Listening to changes ​

js
world.db.onChange('documentName', object => {
    console.log(object) -> {some: 'updated document'}
})