@3fv/dexie-orm 中文文档教程

发布于 3年前 浏览 18 项目主页 更新于 3年前

Dexie ORM/Database Builder

Overview

Dexie Database Builder是一个快速创建的工具 dexie 数据库实例,具有基于 class 的模式

它采用 dexie,并将 builder 模式应用到 简化配置 (目前没有升级,但考虑到现有的 dexie 支持和 DexieDatabaseBuilder 的实现,这相当简单)。

Examples

节点 节点与单个 & tuple key example 可用

React React example 可用

import assert from "assert"
import dexieDatabaseBuilder from "@3fv/dexie-orm"

export class ExampleObject {
  id: string

  constructor(o: Partial<ExampleObject> = {}) {
    Object.assign(this, o)
  }
}


/**
 * Returns a promise with the value type
 * as follows:
 * 
 * Dexie & {
 *  objects: Dexie.Table<ExampleObject, "id">
 * }
 */ 
export async function createExampleDatabase() { 
  const db = await dexieDatabaseBuilder("example", 1)
    .table("objects", ExampleObject, "id" as const, "&id")
    .open()

  return db
}

/**
 * Add & read an `ExampleObject`
 */ 
export async function run() {
  const db = await createExampleDatabase() 

  // OBJECTS TABLE
  const objectsTable = db.objects

  // ADD
  const obj1 = new ExampleObject({
    id: "example-01"
  })

  const obj1Id = await objectsTable.add(obj1)
  const obj1Read = await objectsTable.get(obj1Id)
  assert(obj1Read instanceof ExampleObject, "not instance of ExampleObject")
  assert(obj1Read?.id === obj1?.id, "id's do not match")
  assert.deepEqual(obj1, obj1Read, "doesn't match deeply")

}

run().catch(err => {
  console.error("failed", err)
})

Notes

这是一个正确配置的混合模块, 所以 commonjs & 模块/esm 运行时 得到充分支持 & 生成的库将 只要不产生副作用,就支持 tree shaking。

这意味着,它可以在浏览器中工作,无论是否使用 webpack,在节点中,无论是否使用 webpack(您将需要一个用于 indexeddb 的 polyfill)并且 将仅包括额外资源中的最低限度, 因为它的模块化实现完全支持 tree-shaking(我知道我重复了一遍)。

Credit

@jglanz 3FV 在#NYC 用爱写成。 享受

Dexie ORM/Database Builder

Overview

Dexie Database Builder is a tool for quickly creating dexie database instances with class based schema's

It takes dexie, and applies a builder pattern to simplify configuration (sans upgrade at the moment, but that's fairly straightforward given the existing dexie support & the implementation of DexieDatabaseBuilder).

Examples

Node Node with single & tuple key example is available

React React example is available

import assert from "assert"
import dexieDatabaseBuilder from "@3fv/dexie-orm"

export class ExampleObject {
  id: string

  constructor(o: Partial<ExampleObject> = {}) {
    Object.assign(this, o)
  }
}


/**
 * Returns a promise with the value type
 * as follows:
 * 
 * Dexie & {
 *  objects: Dexie.Table<ExampleObject, "id">
 * }
 */ 
export async function createExampleDatabase() { 
  const db = await dexieDatabaseBuilder("example", 1)
    .table("objects", ExampleObject, "id" as const, "&id")
    .open()

  return db
}

/**
 * Add & read an `ExampleObject`
 */ 
export async function run() {
  const db = await createExampleDatabase() 

  // OBJECTS TABLE
  const objectsTable = db.objects

  // ADD
  const obj1 = new ExampleObject({
    id: "example-01"
  })

  const obj1Id = await objectsTable.add(obj1)
  const obj1Read = await objectsTable.get(obj1Id)
  assert(obj1Read instanceof ExampleObject, "not instance of ExampleObject")
  assert(obj1Read?.id === obj1?.id, "id's do not match")
  assert.deepEqual(obj1, obj1Read, "doesn't match deeply")

}

run().catch(err => {
  console.error("failed", err)
})

Notes

It's a properly configured hybrid module, so both commonjs & module/esm runtimes are fully supported & the resulting library will support tree shaking as long as side-effects are not created.

Which means, it'll work in the browser, with webpack or without, in node, with webpack or without (you will need a polyfill for indexeddb) AND will only include the base minimum in additional resources, because its modular implementation fully supports tree-shaking (I know I repeated myself).

Credit

Written with love in #NYC by @jglanz 3FV. Enjoy

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文