@stenodb/browser
v4.0.1
Published
✍ Easy to use local JSON database
Downloads
20
Maintainers
Readme
@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