Skip to content

Commit 4abf8bc

Browse files
committed
chore: update examples
1 parent 40afa0c commit 4abf8bc

File tree

4 files changed

+33
-31
lines changed

4 files changed

+33
-31
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'reflect-metadata'
22
import { el } from '@zero-dependency/dom'
33
import { User } from './entities.js'
4-
import { storage } from './storage.js'
4+
import { addUser, getLastUserId, storage } from './storage.js'
55

66
const app = document.querySelector('#app')!
77

@@ -48,7 +48,7 @@ const form = el(
4848
return
4949
}
5050

51-
storage.addUser(new User(storage.getLastUserId() + 1, username))
51+
addUser(new User(getLastUserId() + 1, username))
5252
render()
5353
}
5454
},
@@ -63,7 +63,7 @@ const storagePreview = el('pre')
6363
function render() {
6464
form.reset()
6565
usernameInput.focus()
66-
userIdInput.value = storage.getLastUserId().toString()
66+
userIdInput.value = getLastUserId().toString()
6767
storagePreview.textContent = JSON.stringify(storage.data, null, 2)
6868
}
6969

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
import { BrowserDatabase, LocalStorage } from '@stenodb/browser'
22
import lodash from 'lodash'
33
import { User, Users } from './entities.js'
4+
import type { BrowserAdapter } from '@stenodb/browser/types'
45

5-
const adapter = new LocalStorage('users', Users)
6-
const initialData = new Users(new User(1, 'John'))
7-
8-
class StorageWithLodash extends BrowserDatabase<Users> {
6+
class StorageWithLodash<T> extends BrowserDatabase<T> {
97
chain: lodash.ExpChain<this['data']> = lodash.chain(this).get('data')
108

11-
constructor() {
9+
constructor(adapter: BrowserAdapter<T>, initialData: T) {
1210
super(adapter, initialData)
1311
}
12+
}
1413

15-
addUser(user: User): void {
16-
this.chain.get('users').push(user).value()
17-
this.write()
18-
}
14+
const adapter = new LocalStorage('users', Users)
15+
const initialData = new Users(new User(1, 'John'))
1916

20-
getLastUserId(): number {
21-
return this.chain.get('users').last().get('id').value()
22-
}
17+
export const storage = new StorageWithLodash(adapter, initialData)
18+
storage.read()
19+
20+
export function addUser(user: User): void {
21+
storage.chain.get('users').push(user).value()
22+
storage.write()
2323
}
2424

25-
export const storage = new StorageWithLodash()
26-
storage.read()
25+
export function getLastUserId(): number {
26+
return storage.chain.get('users').last().get('id').value()
27+
}

examples/with-node-lodash/src/index.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,10 @@ import lodash from 'lodash'
66
import { User, Users } from './entities.js'
77
import type { NodeProvider } from '@stenodb/node/types'
88

9-
const path = resolve(dirname(fileURLToPath(import.meta.url)), '..', 'database')
10-
const adapter = new AsyncWriter('users', Users)
11-
12-
class NodeWithLodash<T> {
13-
provider: NodeProvider<T>
9+
export class NodeWithLodash<T> {
1410
chain: lodash.ExpChain<T>
1511

16-
constructor(provider: NodeProvider<T>) {
17-
this.provider = provider
12+
constructor(private readonly provider: NodeProvider<T>) {
1813
this.chain = lodash.chain(provider).get('data')
1914
}
2015

@@ -31,11 +26,16 @@ class NodeWithLodash<T> {
3126
}
3227
}
3328

34-
const db = new NodeDatabase(path)
35-
const usersDatabase = new NodeWithLodash(
36-
db.create(adapter, new Users(new User(1, 'John Doe')))
37-
)
29+
const path = resolve(dirname(fileURLToPath(import.meta.url)), '..', 'database')
30+
const adapter = new AsyncWriter('users', Users)
31+
const initialData = new Users(new User(1, 'John Doe'))
32+
const database = new NodeDatabase(path)
3833

34+
const usersDatabase = new NodeWithLodash(database.create(adapter, initialData))
3935
await usersDatabase.read()
40-
const user = usersDatabase.chain.get('users').find({ id: 1 }).value()
41-
console.log(user)
36+
37+
function findUserById(id: number) {
38+
return usersDatabase.chain.get('users').find({ id }).value()
39+
}
40+
41+
console.log(findUserById(1))

examples/with-node/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import { Post, User, Users } from './entities.js'
66

77
const path = resolve(dirname(fileURLToPath(import.meta.url)), '..', 'database')
88
const adapter = new AsyncWriter('users', Users)
9+
const initialData = new Users(new User('John Doe'))
910
const database = new NodeDatabase(path)
1011

11-
const usersDatabase = database.create(adapter, new Users(new User('John Doe')))
12+
const usersDatabase = database.create(adapter, initialData)
1213
await usersDatabase.read()
1314

1415
const post = new Post('Hello world')

0 commit comments

Comments
 (0)