Skip to content

Files

Latest commit

Mar 10, 2023
b73b921 · Mar 10, 2023

History

History

node

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Feb 23, 2023
Feb 23, 2023
Feb 20, 2023
Mar 10, 2023
Feb 23, 2023

@stenodb/node

✍ Easy to use local JSON database for Node.js

Install

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

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 { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { AsyncAdapter, NodeProvider } from '@stenodb/node'
import { Users, User, Post } from './entities.js'

const path = resolve(dirname(fileURLToPath(import.meta.url)), '..', 'db')
const initialData = new Users(new User('John Doe'))
const adapter = new AsyncAdapter('users', Users, initialData)
const provider = new NodeProvider({ path })
const db = await provider.create(adapter)

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

Credits

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

License

MIT - crashmax