Skip to content

Commit 56d6dfb

Browse files
authored
Merge pull request #12 from crashmax-dev/v3
feat(packages): reworked api
2 parents 62a2c93 + c8bc495 commit 56d6dfb

38 files changed

+431
-375
lines changed

README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ pnpm add stenodb
1818

1919
| Package | Version | Platform |
2020
| ------- | ------ | ----------- |
21-
| [stenodb](./packages/stenodb) | [![](https://img.shields.io/npm/v/stenodb)](https://npm.im/stenodb) | Reexports packages |
2221
| [@stenodb/node](./packages/node) | [![](https://img.shields.io/npm/v/@stenodb/node)](https://npm.im/@stenodb/node) | Node.js |
2322
| [@stenodb/browser](./packages/browser) | [![](https://img.shields.io/npm/v/@stenodb/browser)](https://npm.im/@stenodb/browser) | Browser |
2423

@@ -72,33 +71,34 @@ export class Post {
7271
import 'reflect-metadata'
7372
import { dirname, resolve } from 'node:path'
7473
import { fileURLToPath } from 'node:url'
75-
import { AsyncWriter, NodeDatabase } from '@stenodb/node'
74+
import { AsyncAdapter, NodeProvider } from '@stenodb/node'
7675
import { Users, User, Post } from './entities.js'
7776

7877
const path = resolve(dirname(fileURLToPath(import.meta.url)), '..', 'database')
79-
const adapter = new AsyncWriter('users', Users)
8078
const initialData = new Users(new User('John Doe'))
81-
const database = new NodeDatabase(path)
82-
const databaseUsers = database.create(adapter, initialData)
79+
const adapter = new AsyncAdapter('users', Users, initialData)
80+
const provider = new NodeProvider(path)
81+
const database = provider.createAsync(adapter)
8382

84-
await databaseUsers.read()
85-
databaseUsers.data?.users[0]?.addPost(new Post('Lorem ipsum'))
86-
await databaseUsers.write()
83+
await database.read()
84+
database.data?.users[0]?.addPost(new Post('Lorem ipsum'))
85+
await database.write()
8786
```
8887

8988
### `@stenodb/browser`
9089
```typescript
9190
import 'reflect-metadata'
92-
import { LocalStorage, BrowserDatabase } from '@stenodb/browser'
91+
import { LocalStorage, BrowserProvider } from '@stenodb/browser'
9392
import { Users, User, Post } from './entities.js'
9493

95-
const adapter = new LocalStorage('users', Users)
9694
const initialData = new Users(new User('John Doe'))
97-
const databaseUsers = new BrowserDatabase(adapter, initialData)
95+
const adapter = new LocalStorage('users', Users, initialData)
96+
const provider = new BrowserProvider()
97+
const storage = provider.create(adapter)
9898

99-
databaseUsers.read()
100-
databaseUsers.data?.users[0]?.addPost(new Post('Lorem ipsum'))
101-
databaseUsers.write()
99+
storage.read()
100+
storage.data?.users[0]?.addPost(new Post('Lorem ipsum'))
101+
storage.write()
102102
```
103103

104104
## Credits

examples/with-browser-lodash/src/storage.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
1-
import { BrowserDatabase, LocalStorage } from '@stenodb/browser'
1+
import { BrowserProvider, LocalStorage } from '@stenodb/browser'
2+
import { Steno } from '@stenodb/browser/types'
23
import lodash from 'lodash'
34
import { User, Users } from './entities.js'
4-
import type { BrowserAdapter } from '@stenodb/browser/types'
55

6-
class StorageWithLodash<T> extends BrowserDatabase<T> {
7-
chain: lodash.ExpChain<this['data']> = lodash.chain(this).get('data')
6+
export class BrowserWithLodash<T> {
7+
chain: lodash.ExpChain<T>
88

9-
constructor(adapter: BrowserAdapter<T>, initialData: T) {
10-
super(adapter, initialData)
9+
constructor(private readonly provider: Steno.BrowserProvider<T>) {
10+
this.chain = lodash.chain(provider).get('data')
11+
}
12+
13+
get data(): T | null {
14+
return this.provider.data
15+
}
16+
17+
read(): T | null {
18+
return this.provider.read()
19+
}
20+
21+
write(): void {
22+
this.provider.write()
23+
}
24+
25+
reset(): void {
26+
this.provider.reset()
1127
}
1228
}
1329

14-
const adapter = new LocalStorage('users', Users)
1530
const initialData = new Users(new User(1, 'John'))
31+
const adapter = new LocalStorage('users', Users, initialData)
32+
const provider = new BrowserProvider()
1633

17-
export const storage = new StorageWithLodash(adapter, initialData)
34+
export const storage = new BrowserWithLodash(provider.create(adapter))
1835
storage.read()
1936

2037
export function addUser(user: User): void {
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { BrowserDatabase, LocalStorage } from '@stenodb/browser'
1+
import { BrowserProvider, LocalStorage } from '@stenodb/browser'
22
import { User, Users } from './entities.js'
33

4-
const adapter = new LocalStorage('users', Users)
54
const initialData = new Users(new User(1, 'John'))
6-
export const storage = new BrowserDatabase(adapter, initialData)
5+
const adapter = new LocalStorage('users', Users, initialData)
6+
const provider = new BrowserProvider()
7+
8+
export const storage = provider.create(adapter)
79
storage.read()
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,46 @@
11
import 'reflect-metadata'
22
import { dirname, resolve } from 'node:path'
33
import { fileURLToPath } from 'node:url'
4-
import { AsyncWriter, NodeDatabase } from '@stenodb/node'
4+
import { AsyncAdapter, NodeProvider } from '@stenodb/node'
55
import lodash from 'lodash'
66
import { User, Users } from './entities.js'
7-
import type { NodeProvider } from '@stenodb/node/types'
7+
import type { Steno } from '@stenodb/node/types'
88

99
export class NodeWithLodash<T> {
1010
chain: lodash.ExpChain<T>
1111

12-
constructor(private readonly provider: NodeProvider<T>) {
12+
constructor(private readonly provider: Steno.NodeProvider<T>) {
1313
this.chain = lodash.chain(provider).get('data')
1414
}
1515

1616
get data(): T | null {
1717
return this.provider.data
1818
}
1919

20-
async read(): Promise<void> {
21-
await this.provider.read()
20+
async read(): Promise<T | null> {
21+
return await this.provider.read()
2222
}
2323

2424
async write(): Promise<void> {
2525
await this.provider.write()
2626
}
27+
28+
async reset(): Promise<void> {
29+
await this.provider.reset()
30+
}
2731
}
2832

2933
const path = resolve(dirname(fileURLToPath(import.meta.url)), '..', 'database')
30-
const adapter = new AsyncWriter('users', Users)
3134
const initialData = new Users(new User(1, 'John Doe'))
32-
const database = new NodeDatabase(path)
35+
const adapter = new AsyncAdapter('users', Users, initialData)
36+
const provider = new NodeProvider(path)
3337

34-
const usersDatabase = new NodeWithLodash(database.create(adapter, initialData))
35-
await usersDatabase.read()
38+
const database = new NodeWithLodash(provider.createAsync(adapter))
39+
await database.read()
40+
await database.write()
3641

3742
function findUserById(id: number) {
38-
return usersDatabase.chain.get('users').find({ id }).value()
43+
return database.chain.get('users').find({ id }).value()
3944
}
4045

4146
console.log(findUserById(1))

examples/with-node/src/index.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import 'reflect-metadata'
22
import { dirname, resolve } from 'node:path'
33
import { fileURLToPath } from 'node:url'
4-
import { AsyncWriter, NodeDatabase } from '@stenodb/node'
4+
import { AsyncAdapter, NodeProvider } from '@stenodb/node'
55
import { Post, User, Users } from './entities.js'
66

77
const path = resolve(dirname(fileURLToPath(import.meta.url)), '..', 'database')
8-
const adapter = new AsyncWriter('users', Users)
98
const initialData = new Users(new User('John Doe'))
10-
const database = new NodeDatabase(path)
11-
12-
const usersDatabase = database.create(adapter, initialData)
13-
await usersDatabase.read()
9+
const adapter = new AsyncAdapter('users', Users, initialData)
10+
const provider = new NodeProvider(path)
1411

12+
const database = provider.createAsync(adapter)
13+
await database.read()
1514
const post = new Post('Hello world')
16-
usersDatabase.data?.users[0]?.addPost(post)
17-
await usersDatabase.write()
15+
database.data?.users[0]?.addPost(post)
16+
await database.write()
1817

19-
console.log(usersDatabase.data)
18+
console.log(database.data)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@stenodb/workspace",
3-
"version": "2.0.0",
3+
"version": "3.0.0",
44
"type": "module",
55
"private": true,
66
"workspaces": [

packages/browser/README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,17 @@ export class Post {
6262
```typescript
6363
// index.ts
6464
import 'reflect-metadata'
65-
import { LocalStorage, BrowserDatabase } from '@stenodb/browser'
65+
import { LocalStorage, BrowserProvider } from '@stenodb/browser'
6666
import { Users, User, Post } from './entities.js'
6767

68-
const adapter = new LocalStorage('users', Users)
6968
const initialData = new Users(new User('John Doe'))
70-
const databaseUsers = new BrowserDatabase(adapter, initialData)
69+
const adapter = new LocalStorage('users', Users, initialData)
70+
const provider = new BrowserProvider()
71+
const storage = provider.create(adapter)
7172

72-
databaseUsers.read()
73-
databaseUsers.data?.users[0]?.addPost(new Post('Lorem ipsum'))
74-
databaseUsers.write()
73+
storage.read()
74+
storage.data?.users[0]?.addPost(new Post('Lorem ipsum'))
75+
storage.write()
7576
```
7677

7778
## Credits

packages/browser/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@stenodb/browser",
33
"description": "✍ Easy to use local JSON database",
4-
"version": "2.0.0",
4+
"version": "3.0.0",
55
"type": "module",
66
"files": [
77
"dist"
@@ -49,7 +49,7 @@
4949
"prepublishOnly": "pnpm build"
5050
},
5151
"dependencies": {
52-
"@stenodb/utils": "workspace:2.0.0",
52+
"@stenodb/utils": "workspace:3.0.0",
5353
"class-transformer": "0.5.1"
5454
},
5555
"peerDependencies": {

packages/browser/src/BrowserDatabase.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/browser/src/BrowserProvider.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)