0.12.1 • Published 8 months ago

@canvas-js/modeldb-sqlite v0.12.1

Weekly downloads
-
License
-
Repository
-
Last release
8 months ago

@canvas-js/modeldb-sqlite

ModelDB

  • Relational
  • Simple JSON schema
  • Dual SQLite and IndexdDB backends
  • Designed for p2p conlict resolution
  • "Mutable" and "immutable" models
import { ModelDB } from "@canvas-js/modeldb-sqlite"

const db = new ModelDB({
	user: {
		address: "string",
		encryptionPublicKey: "bytes",
		signingPublicKey: "bytes",
	},

	room: {
		creator: "@user",
		members: "@user[]",
		$indexes: ["members"],
	},

	message: {
		room: "@room",
		sender: "@user",
		content: "string",
		timestamp: "integer",
	},
})

const userId = modelDB.add("user", {
	address: "a",
	encryptionPublicKey: new Uint8Array([1, 2, 3]),
	signingPublicKey: new Uint8Array([4, 5, 6]),
})

ModelInit

type PrimitiveType = "integer" | "float" | "string" | "bytes"
type OptionalPrimitiveType = `${PrimitiveType}?`
type ReferenceType = `@${string}`
type OptionalReferenceType = `@${string}?`
type RelationType = `@${string}[]`

type PropertyType = PrimitiveType | OptionalPrimitiveType | ReferenceType | OptionalReferenceType | RelationType

type IndexInit = string | string[]

type ModelsInit = Record<
	string,
	{ $type?: "mutable" | "immutable"; $indexes?: IndexInit[] } & Record<string, PropertyType>
>

ModelDB

interface ModelDBOptions {
	dkLen?: number
	resolve?: (versionA: string, versionB: string) => string
}

declare class ModelDB {
	public constructor(path: string, models: ModelsInit, options?: ModelDBOptions)
	public close(): void

	public get(modelName: string, key: string): ModelValue | null
	public iterate(modelName: string): AsyncIterable<ModelValue>
	public query(modelName: string, query: {}): ModelValue[]

	// Mutable model methods
	public set(modelName: string, key: string, value: ModelValue, options?: { version?: string }): void
	public delete(modelName: string, key: string, options?: { version?: string }): void

	// Immutable model methods
	public add(modelName: string, value: ModelValue, options?: { namespace?: string }): string
	public remove(modelName: string, key: string): void
}
0.13.0-next.16

8 months ago

0.13.0-next.15

8 months ago

0.13.0-next.14

8 months ago

0.13.0-next.13

8 months ago

0.12.1

8 months ago

0.13.0-next.12

8 months ago

0.13.0-next.11

8 months ago

0.13.0-next.10

8 months ago

0.13.0-next.9

8 months ago

0.13.0-next.8

8 months ago

0.13.0-next.1

8 months ago

0.13.0-next.3

8 months ago

0.13.0-next.2

8 months ago

0.13.0-next.5

8 months ago

0.13.0-next.4

8 months ago

0.13.0-next.7

8 months ago

0.13.0-next.6

8 months ago

0.11.0

9 months ago

0.12.0

8 months ago

0.10.10

10 months ago

0.10.0

11 months ago

0.10.9

10 months ago

0.10.1

11 months ago

0.10.2

11 months ago

0.10.3

11 months ago

0.10.4

11 months ago

0.10.5

10 months ago

0.10.6

10 months ago

0.10.7

10 months ago

0.10.0-beta.4

12 months ago

0.10.8

10 months ago

0.6.0-alpha1

2 years ago