Home Reference Source Test

src/storage/storage.js

import { appSchema, Database as WatermelonDB } from '@nozbe/watermelondb'
import migrations from './migrations'
import { matrixModels, matrixSchemas } from './SqlStoreSchema'

// const debug = require('debug')('ditto:services:storage')

class Storage {
  _db

  constructor (options) {
    this._db = new WatermelonDB(options)
  }

  async action (func) {
    return this._db.action(func)
  }

  async batch (actions) {
    return this.action(async () => this._db.batch(...actions))
  }

  getCollection (collection) {
    return this._db.collections.get(collection)
  }

  async getItem (key) {
    return this._db.adapter.getLocal(key)
  }

  async setItem (key, value) {
    return this._db.adapter.setLocal(key, value)
  }

  async reset () {
    return this.action(async () => this._db.unsafeResetDatabase())
  }
}

const schema = appSchema({
  version: 4,
  tables: [...matrixSchemas]
})

export { schema, migrations, matrixSchemas, matrixModels, Storage }