4.0.1 • Published 1 year ago

@stenodb/node v4.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@stenodb/node npm.io

✍ 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

4.0.1

1 year ago

3.4.0

1 year ago

3.3.1

1 year ago

3.4.1

1 year ago

3.3.2

1 year ago

4.0.0

1 year ago

3.3.0

1 year ago

3.2.1

1 year ago

3.2.0

1 year ago

3.1.0

1 year ago

3.0.0

1 year ago

2.0.0

1 year ago