@@ -24,160 +24,172 @@ pnpm add stenodb
2424| [ @stenodb/lodash ] ( ./packages/lodash ) | [ ![ ] ( https://img.shields.io/npm/v/@stenodb/lodash )] ( https://npm.im/@stenodb/lodash ) | Lodash wrapper |
2525| [ @stenodb/logger ] ( ./packages/logger ) | [ ![ ] ( https://img.shields.io/npm/v/@stenodb/logger )] ( https://npm.im/@stenodb/logger ) | Logger |
2626
27- ## Usage
27+ ## Examples
2828
29- ### Database entities
30- ``` typescript
31- // entities.ts
32- import { Type } from ' class-transformer'
29+ > ** Note**
30+ > You can find more detailed examples [ here] ( ./examples )
3331
34- export class Users {
35- @Type (() => User )
36- users: User []
32+ <details >
33+ <summary >class-transformer entity</summary >
3734
38- constructor (... users : User []) {
39- this .users = users
40- }
41- }
42-
43- export class User {
44- username: string
45-
46- @Type (() => Post )
47- posts: Post []
35+ ``` ts
36+ // entities.ts
37+ import { Type } from ' class-transformer'
4838
49- constructor (username : string , ... posts : Post []) {
50- this .username = username
51- this .posts = posts
52- }
39+ export class Users {
40+ @Type (() => User )
41+ users: User []
5342
54- addPost(post : Post ) {
55- this .posts .push (post )
43+ constructor (... users : User []) {
44+ this .users = users
45+ }
5646 }
57- }
5847
59- export class Post {
60- title : string
48+ export class User {
49+ username : string
6150
62- constructor (text : string ) {
63- this .title = title
64- }
65- }
66- ```
67-
68- ### ` @stenodb/node `
69-
70- ``` typescript
71- import ' reflect-metadata'
72- import { dirname , resolve } from ' node:path'
73- import { fileURLToPath } from ' node:url'
74- import { AsyncAdapter , NodeProvider } from ' @stenodb/node'
75- import { Users , User , Post } from ' ./entities.js'
76-
77- const path = resolve (dirname (fileURLToPath (import .meta .url )), ' ..' , ' database' )
78- const initialData = new Users (new User (' John Doe' ))
79- const adapter = new AsyncAdapter (' users' , Users , initialData )
80- const provider = new NodeProvider ({ path })
81- const database = await provider .create (adapter )
82-
83- await database .read ()
84- database .data ?.users [0 ]?.addPost (new Post (' Lorem ipsum' ))
85- await database .write ()
86- ```
51+ @Type (() => Post )
52+ posts: Post []
8753
88- ### ` @stenodb/browser `
54+ constructor (username : string , ... posts : Post []) {
55+ this .username = username
56+ this .posts = posts
57+ }
8958
90- ``` typescript
91- import ' reflect-metadata'
92- import { LocalStorage , BrowserProvider } from ' @stenodb/browser'
93- import { Users , User , Post } from ' ./entities.js'
94-
95- const initialData = new Users (new User (' John Doe' ))
96- const adapter = new LocalStorage (' users' , Users , initialData )
97- const provider = new BrowserProvider ()
98- const storage = provider .create (adapter )
99-
100- storage .read ()
101- storage .data ?.users [0 ]?.addPost (new Post (' Lorem ipsum' ))
102- storage .write ()
103- ```
104-
105- ### ` @stenodb/nest `
106-
107- ``` typescript
108- // users.dto.ts
109- import { Exclude , Type } from ' class-transformer'
110- import { Length , Max , Min } from ' class-validator'
59+ addPost(post : Post ) {
60+ this .posts .push (post )
61+ }
62+ }
11163
112- export class Users {
113- @Type (() => CreateUserDto )
114- users: CreateUserDto [] = []
64+ export class Post {
65+ title: string
11566
116- constructor (... users : CreateUserDto []) {
117- this .users = users
67+ constructor (text : string ) {
68+ this .title = title
69+ }
70+ }
71+ ```
72+ </details >
73+
74+ <details >
75+ <summary >@stenodb/node</summary >
76+
77+ ``` ts
78+ import ' reflect-metadata'
79+ import { dirname , resolve } from ' node:path'
80+ import { fileURLToPath } from ' node:url'
81+ import { AsyncAdapter , NodeProvider } from ' @stenodb/node'
82+ import { Users , User , Post } from ' ./entities.js'
83+
84+ const path = resolve (dirname (fileURLToPath (import .meta .url )), ' ..' , ' db' )
85+ const initialData = new Users (new User (' John Doe' ))
86+ const adapter = new AsyncAdapter (' users' , Users , initialData )
87+ const provider = new NodeProvider ({ path })
88+ const db = await provider .create (adapter )
89+
90+ await db .read ()
91+ db .data ?.users [0 ]?.addPost (new Post (' Lorem ipsum' ))
92+ await db .write ()
93+ ```
94+ </details >
95+
96+ <details >
97+ <summary >@stenodb/browser</summary >
98+
99+ ``` ts
100+ import ' reflect-metadata'
101+ import { LocalStorage , BrowserProvider } from ' @stenodb/browser'
102+ import { Users , User , Post } from ' ./entities.js'
103+
104+ const initialData = new Users (new User (' John Doe' ))
105+ const adapter = new LocalStorage (' users' , Users , initialData )
106+ const provider = new BrowserProvider ()
107+ const db = provider .create (adapter )
108+
109+ db .read ()
110+ db .data ?.users [0 ]?.addPost (new Post (' Lorem ipsum' ))
111+ db .write ()
112+ ```
113+ </details >
114+
115+ <details >
116+ <summary >@stenodb/nest</summary >
117+
118+ ``` ts
119+ // users.dto.ts
120+ import { Exclude , Type } from ' class-transformer'
121+ import { Length , Max , Min } from ' class-validator'
122+
123+ export class Users {
124+ @Type (() => CreateUserDto )
125+ users: CreateUserDto [] = []
126+
127+ constructor (... users : CreateUserDto []) {
128+ this .users = users
129+ }
118130 }
119- }
120131
121- export class CreateUserDto {
122- @Exclude ({ toPlainOnly: true })
123- id: number
132+ export class CreateUserDto {
133+ @Exclude ({ toPlainOnly: true })
134+ id: number
124135
125- @Length (1 , 20 )
126- name: string
136+ @Length (1 , 20 )
137+ name: string
127138
128- @Min (12 )
129- @Max (100 )
130- age: number
139+ @Min (12 )
140+ @Max (100 )
141+ age: number
131142
132- constructor (id : number , name : string , age : number ) {
133- this .id = id
134- this .name = name
135- this .age = age
143+ constructor (id : number , name : string , age : number ) {
144+ this .id = id
145+ this .name = name
146+ this .age = age
147+ }
136148 }
137- }
138-
139- // app.module.ts
140- import { resolve } from ' node:path '
141- import { Module } from ' @nestjs/common '
142- import { StenoModule } from ' @stenodb/nest '
143-
144- @ Module ({
145- imports: [
146- StenoModule . register ({
147- path: resolve ( process . cwd (), ' db ' )
148- })
149- ]
150- })
151- export class AppModule {}
152-
153- // users.service.ts
154- import { Injectable , OnModuleInit } from ' @nestjs/common '
155- import { Steno , StenoService } from ' @stenodb/nest '
156- import { Users , CreateUserDto } from ' ./users.dto '
157-
158- @ Injectable ()
159- export class UsersService implements OnModuleInit {
160- private usersProvider : Steno . NodeProvider < Users >
161-
162- constructor ( private readonly stenoService : StenoService ) {}
163-
164- async onModuleInit() : Promise < void > {
165- this . usersProvider = await this . stenoService . create (
166- ' users ' ,
167- Users ,
168- new Users (
169- new CreateUserDto ( 1 , ' John ' , 22 )
149+
150+ // app.module.ts
151+ import { resolve } from ' node:path '
152+ import { Module } from ' @nestjs/common '
153+ import { StenoModule } from ' @stenodb/nest '
154+
155+ @ Module ({
156+ imports: [
157+ StenoModule . register ({
158+ path: resolve ( process . cwd (), ' db ' )
159+ } )
160+ ]
161+ })
162+ export class AppModule {}
163+
164+ // users.service.ts
165+ import { Injectable , OnModuleInit } from ' @nestjs/common '
166+ import { Steno , StenoService } from ' @stenodb/nest '
167+ import { Users , CreateUserDto } from ' ./users.dto '
168+
169+ @ Injectable ()
170+ export class UsersService implements OnModuleInit {
171+ private usersProvider : Steno . NodeProvider < Users >
172+
173+ constructor ( private readonly stenoService : StenoService ) {}
174+
175+ async onModuleInit() : Promise < void > {
176+ this . usersProvider = await this . stenoService . create (
177+ ' users ' ,
178+ Users ,
179+ new Users (
180+ new CreateUserDto ( 1 , ' John ' , 22 )
181+ )
170182 )
171- )
172183
173- await this .usersProvider .read ()
174- }
184+ await this .usersProvider .read ()
185+ }
175186
176- get users(): CreateUserDto [] {
177- return this .usersProvider .data .users
187+ get users(): CreateUserDto [] {
188+ return this .usersProvider .data .users
189+ }
178190 }
179- }
180- ```
191+ ```
192+ </ details >
181193
182194## Credits
183195
0 commit comments