@stenodb/browser
TypeScript icon, indicating that this package has built-in type declarations

4.0.1 • Public • Published

@stenodb/browser

Easy to use local JSON database for LocalStorage and SessionStorage.

Install

npm install @stenodb/browser
yarn add @stenodb/browser
pnpm add @stenodb/browser

Usage

// entities.ts
import { Type } from 'class-transformer'

export class Users {
  @Type(() => User)
  users: User[]

  constructor(...users: User[]) {
    this.users = users
  }
}

export class User {
  username: string

  @Type(() => Post)
  posts: Post[]

  constructor(username: string, ...posts: Post[]) {
    this.username = username
    this.posts = posts
  }

  addPost(post: Post) {
    this.posts.push(post)
  }
}

export class Post {
  title: string

  constructor(text: string) {
    this.title = title
  }
}

// index.ts
import 'reflect-metadata'
import { LocalStorage, BrowserProvider } from '@stenodb/browser'
import { Users, User, Post } from './entities.js'

const initialData = new Users(new User('John Doe'))
const adapter = new LocalStorage('users', Users, initialData)
const provider = new BrowserProvider()
const db = provider.create(adapter)

db.read()
db.data?.users[0]?.addPost(new Post('Lorem ipsum'))
db.write()

Credits

  • steno - Specialized fast async file writer.
  • class-transformer - Decorator-based transformation, serialization, and deserialization between objects and classes.
  • class-validator - Decorator-based property validation for classes.

License

MIT - crashmax

Package Sidebar

Install

npm i @stenodb/browser

Weekly Downloads

1

Version

4.0.1

License

MIT

Unpacked Size

20.3 kB

Total Files

14

Last publish

Collaborators

  • crashmax