Skip to content

Commit 40665c4

Browse files
authored
Merge pull request #11 from crashmax-dev/feat/node-browser-packages
stenodb v2
2 parents 05d8c43 + b0945c4 commit 40665c4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1364
-949
lines changed

.github/workflows/publish.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Publish
2+
3+
on:
4+
push:
5+
branches: [master]
6+
7+
jobs:
8+
version:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v3
12+
- uses: pnpm/action-setup@v2
13+
with:
14+
version: 7.2.1
15+
- uses: actions/setup-node@v3
16+
with:
17+
node-version: 18.x
18+
cache: 'pnpm'
19+
- run: pnpm install --frozen-lockfile
20+
- run: |
21+
echo "//registry.npmjs.org/:_authToken="${{secrets.NPM_TOKEN}}"" > ~/.npmrc
22+
shell: sh
23+
- run: pnpm -r --filter='./packages/*' publish --access public

README.md

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# stenodb [![](https://img.shields.io/npm/v/stenodb)](https://www.npmjs.org/package/stenodb)
22

3-
> ✍ Easy to use local JSON database. Ready to use with [LocalStorage](https://developer.mozilla.org/ru/docs/Web/API/Window/localStorage), [SessionStorage](https://developer.mozilla.org/ru/docs/Web/API/Window/sessionStorage) and Node.js.
3+
> ✍ Easy to use local JSON database. Ready to use in browser (localStorage, sessionStorage) and Node.js.
44
55
## Install
66

@@ -16,14 +16,20 @@ yarn add stenodb
1616
pnpm add stenodb
1717
```
1818

19+
| Package | Version | Platform |
20+
| ------- | ------ | ----------- |
21+
| [stenodb](./packages/stenodb) | [![](https://img.shields.io/npm/v/stenodb)](https://npm.im/stenodb) | Reexports packages |
22+
| [@stenodb/node](./packages/node) | [![](https://img.shields.io/npm/v/@stenodb/node)](https://npm.im/@stenodb/node) | Node.js |
23+
| [@stenodb/browser](./packages/browser) | [![](https://img.shields.io/npm/v/@stenodb/browser)](https://npm.im/@stenodb/browser) | Browser |
24+
1925
## Usage
2026

2127
> **Warning**\
2228
> stenodb is a pure ESM package. If you're having trouble using it in your project, please [read this](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
2329
24-
25-
### Entity
30+
### Database entities
2631
```typescript
32+
// entities.ts
2733
import { Type } from 'class-transformer'
2834

2935
export class Users {
@@ -60,55 +66,48 @@ export class Post {
6066
}
6167
```
6268

63-
### Node.js
69+
### `@stenodb/node`
6470

6571
```typescript
6672
import 'reflect-metadata'
67-
import { join } from 'node:path'
68-
import { NodeProvider } from 'stenodb/node'
73+
import { dirname, resolve } from 'node:path'
74+
import { fileURLToPath } from 'node:url'
75+
import { AsyncWriter, NodeDatabase } from '@stenodb/node'
6976
import { Users, User, Post } from './entities.js'
7077

7178
const path = resolve(dirname(fileURLToPath(import.meta.url)), '..', 'database')
79+
const adapter = new AsyncWriter('users', Users)
80+
const initialData = new Users(new User('John Doe'))
81+
const database = new NodeDatabase(path)
82+
const databaseUsers = database.create(adapter, initialData)
7283

73-
const databaseProvider = new NodeProvider({
74-
path,
75-
logger: { enabled: true }
76-
})
77-
78-
const databaseUsers = databaseProvider.createDatabase({
79-
name: 'users',
80-
entity: Users,
81-
initialData: new Users(new User('John Doe'))
82-
})
83-
84+
await databaseUsers.read()
8485
databaseUsers.data?.users[0]?.addPost(new Post('Lorem ipsum'))
85-
databaseUsers.write()
86+
await databaseUsers.write()
8687
```
8788

88-
### Browser
89+
### `@stenodb/browser`
8990
```typescript
9091
import 'reflect-metadata'
91-
import { BrowserProvider, LocalStorage } from 'stenodb/browser'
92+
import { LocalStorage, BrowserDatabase } from '@stenodb/browser'
9293
import { Users, User, Post } from './entities.js'
9394

94-
const adapter = new LocalStorage<Users>('users')
95-
96-
const databaseUsers = new BrowserProvider({
97-
adapter,
98-
entity: Users,
99-
initialData: new Users(new User('John Doe'))
100-
})
95+
const adapter = new LocalStorage('users', Users)
96+
const initialData = new Users(new User('John Doe'))
97+
const databaseUsers = new BrowserDatabase(adapter, initialData)
10198

99+
databaseUsers.read()
102100
databaseUsers.data?.users[0]?.addPost(new Post('Lorem ipsum'))
103101
databaseUsers.write()
104102
```
105103

106-
## Related
104+
## Credits
107105

108106
- [steno](https://github.com/typicode/steno) - Specialized fast async file writer.
109107
- [class-transformer](https://github.com/typestack/class-transformer) - Decorator-based transformation, serialization, and deserialization between objects and classes.
110108
- [class-validator](https://github.com/typestack/class-validator) - Decorator-based property validation for classes.
111109
- [json-difference](https://github.com/lukascivil/json-difference) - A simple way to find the difference between two objects or json diff.
110+
- [tslog](https://github.com/fullstack-build/tslog) - Universal Logger for TypeScript and JavaScript.
112111

113112
## License
114113

File renamed without changes.

examples/with-lodash/package.json renamed to examples/with-browser-lodash/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "with-lodash",
2+
"name": "with-browser-lodash",
33
"private": true,
44
"version": "0.0.0",
55
"type": "module",
@@ -14,10 +14,10 @@
1414
"vite": "4.0.4"
1515
},
1616
"dependencies": {
17+
"@stenodb/browser": "workspace:*",
1718
"@zero-dependency/dom": "0.12.0",
1819
"class-transformer": "0.5.1",
1920
"lodash": "4.17.21",
20-
"reflect-metadata": "0.1.13",
21-
"stenodb": "workspace:*"
21+
"reflect-metadata": "0.1.13"
2222
}
2323
}

examples/with-lodash/src/index.ts renamed to 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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { BrowserDatabase, LocalStorage } from '@stenodb/browser'
2+
import lodash from 'lodash'
3+
import { User, Users } from './entities.js'
4+
import type { BrowserAdapter } from '@stenodb/browser/types'
5+
6+
class StorageWithLodash<T> extends BrowserDatabase<T> {
7+
chain: lodash.ExpChain<this['data']> = lodash.chain(this).get('data')
8+
9+
constructor(adapter: BrowserAdapter<T>, initialData: T) {
10+
super(adapter, initialData)
11+
}
12+
}
13+
14+
const adapter = new LocalStorage('users', Users)
15+
const initialData = new Users(new User(1, 'John'))
16+
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()
23+
}
24+
25+
export function getLastUserId(): number {
26+
return storage.chain.get('users').last().get('id').value()
27+
}
File renamed without changes.

examples/with-browser/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
"preview": "vite preview"
1010
},
1111
"devDependencies": {
12-
"typescript": "4.9.4",
13-
"vite": "4.0.4"
12+
"typescript": "4.9.5",
13+
"vite": "4.1.1"
1414
},
1515
"dependencies": {
16+
"@stenodb/browser": "workspace:*",
1617
"@zero-dependency/dom": "0.12.0",
1718
"class-transformer": "0.5.1",
18-
"reflect-metadata": "0.1.13",
19-
"stenodb": "workspace:*"
19+
"reflect-metadata": "0.1.13"
2020
}
2121
}

0 commit comments

Comments
 (0)