Skip to content

realglobe-Inc/claydb

Repository files navigation

Title Banner

Build Status npm Version JS Standard

A distributed NoSQL to handle data as web resource

ClayDB written in pure javascript and use pluggable storage to store data. Storage can be RDMS like MySQL, files like JSON, LocalStorage in browser, or anything which can store data.

ClayDB is a collection of distributed node called Lump, which wraps storage with driver and provide resources to outside. Each Lumps can be merged one another.

Overview

Table of Contents

Requirements

banner banner

Getting Started

Three step to be getting started.

  1. Choose driver and instantiate to connect storage
  2. Create lump with the driver
  3. Access resource in lump

Resources are collection of data like database table or document collection. It provides basic CRUD interface to handle data.

'use strict'

const clayLump = require('clay-lump')
const { SqliteDriver } = require('clay-driver-sqlite')

async function tryExample () {
  let lump01 = clayLump('lump01', {
    driver: new SqliteDriver('tmp/lump-01.db')
  })

  // Access to resource.
  {
    const Dog = lump01.resource('Dog')

    // Add an entity to resource
    let john = await Dog.create({ name: 'John', type: 'Saint Bernard', age: 3 })
    console.log('New dog created:', john) // -> { id: '1a6358694adb4aa89c15f94be50d5b78', name: 'john', type: 'Saint Bernard', age: 3 }

    // List entities from resource
    let dogs = await Dog.list({
      filter: { type: 'Saint Bernard' },
      page: { size: 25, number: 1 }
    })
    console.log('Found dogs:', dogs) // -> { entities: [ /* ... */ ], meta: { /* ... */ } }

    // Get entity with id
    let johnAgain = await Dog.one(john.id)
    console.log('From id', johnAgain)

    // Update date
    await Dog.update(john.id, { name: 'Shinny John' })

    // Destroy data
    await Dog.destroy(john.id)
  }
}

tryExample().catch((err) => console.error(err))

Advanced Usage

Adding Policy to Resource

ClayDB is basically an open-schema and has no restriction on each data structure. However you can use Clay-Policy to validate entities on resource.

'use strict'

const clayLump = require('clay-lump')

async function tryExample () {
  let lump01 = clayLump('lump01', {
    /* ... */
  })

  const Cat = lump01.reosurce('Cat')

  // Register resource policy
  Cat.policy({
    name: {
      type: 'STRING',
      required: true,
      minLength: 2
    },
    breed: {
      type: 'STRING',
      oneOf: [ 'Abyssinian', 'Balinese', ...[ /* ... */ ] ]
    }
  })

  // Throws error if policy not matches
  await Cat.create({ /* ... */ })
}

tryExample().catch((err) => console.error(err))

Syncing Lumps

Clay-Lumps are distributed nodes of ClayDB and can be merge one another. sync command compare the each entities in every resources on lumps and merge records by id.

Sync policy is quite simple. The newer one overcomes order one.

'use strict'

const clayLump = require('clay-lump')
const { SqliteDriver } = require('clay-driver-sqlite')

async function tryExample () {
  let lump01 = clayLump('lump01', {
    driver: new SqliteDriver('tmp/lump-01.db')
  })

  // Access to resource
  {
    const Dog = lump01.resource('Dog')

    let john = await Dog.create({ name: 'john', type: 'Saint Bernard', age: 3 })
    console.log('New dog created:', john) // -> { id: '1a6358694adb4aa89c15f94be50d5b78', name: 'john', type: 'Saint Bernard', age: 3 }
  }

  let lump02 = clayLump('lump02', {
    driver: new SqliteDriver('tmp/lump-02.db')
  })
  {
    const Dog = lump02.resource('Dog')
    let bess = await Dog.create({ name: 'bess', type: 'Chihuahua', age: 1 })
    console.log('New dog created:', bess)
  }

  // Sync lumps01 to lump02
  await lump02.sync(lump01) // Both lumps will be updated
  {
    const Dog = lump02.resource('Dog')
    let [ john ] = (await Dog.list({ filter: { name: 'john' } })).entities // Synced from lump01
    console.log(john) // -> { id: '1a6358694adb4aa89c15f94be50d5b78', name: 'john', type: 'Saint Bernard', age: 3 }
  }
}

tryExample().catch((err) => console.error(err))

API Guides

License

This software is released under the Apache-2.0 License.

Links

About

Distributed database with complete independence

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published