Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Publish

on:
push:
branches: [master]

jobs:
version:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
with:
version: 7.2.1
- uses: actions/setup-node@v3
with:
node-version: 18.x
cache: 'pnpm'
- run: pnpm install --frozen-lockfile
- run: |
echo "//registry.npmjs.org/:_authToken="${{secrets.NPM_TOKEN}}"" > ~/.npmrc
shell: sh
- run: pnpm -r --filter='./packages/*' publish --access public
55 changes: 27 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# stenodb [![](https://img.shields.io/npm/v/stenodb)](https://www.npmjs.org/package/stenodb)

> ✍ 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.
> ✍ Easy to use local JSON database. Ready to use in browser (localStorage, sessionStorage) and Node.js.

## Install

Expand All @@ -16,14 +16,20 @@ yarn add stenodb
pnpm add stenodb
```

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

## Usage

> **Warning**\
> 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).


### Entity
### Database entities
```typescript
// entities.ts
import { Type } from 'class-transformer'

export class Users {
Expand Down Expand Up @@ -60,55 +66,48 @@ export class Post {
}
```

### Node.js
### `@stenodb/node`

```typescript
import 'reflect-metadata'
import { join } from 'node:path'
import { NodeProvider } from 'stenodb/node'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { AsyncWriter, NodeDatabase } from '@stenodb/node'
import { Users, User, Post } from './entities.js'

const path = resolve(dirname(fileURLToPath(import.meta.url)), '..', 'database')
const adapter = new AsyncWriter('users', Users)
const initialData = new Users(new User('John Doe'))
const database = new NodeDatabase(path)
const databaseUsers = database.create(adapter, initialData)

const databaseProvider = new NodeProvider({
path,
logger: { enabled: true }
})

const databaseUsers = databaseProvider.createDatabase({
name: 'users',
entity: Users,
initialData: new Users(new User('John Doe'))
})

await databaseUsers.read()
databaseUsers.data?.users[0]?.addPost(new Post('Lorem ipsum'))
databaseUsers.write()
await databaseUsers.write()
```

### Browser
### `@stenodb/browser`
```typescript
import 'reflect-metadata'
import { BrowserProvider, LocalStorage } from 'stenodb/browser'
import { LocalStorage, BrowserDatabase } from '@stenodb/browser'
import { Users, User, Post } from './entities.js'

const adapter = new LocalStorage<Users>('users')

const databaseUsers = new BrowserProvider({
adapter,
entity: Users,
initialData: new Users(new User('John Doe'))
})
const adapter = new LocalStorage('users', Users)
const initialData = new Users(new User('John Doe'))
const databaseUsers = new BrowserDatabase(adapter, initialData)

databaseUsers.read()
databaseUsers.data?.users[0]?.addPost(new Post('Lorem ipsum'))
databaseUsers.write()
```

## Related
## Credits

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

## License

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "with-lodash",
"name": "with-browser-lodash",
"private": true,
"version": "0.0.0",
"type": "module",
Expand All @@ -14,10 +14,10 @@
"vite": "4.0.4"
},
"dependencies": {
"@stenodb/browser": "workspace:*",
"@zero-dependency/dom": "0.12.0",
"class-transformer": "0.5.1",
"lodash": "4.17.21",
"reflect-metadata": "0.1.13",
"stenodb": "workspace:*"
"reflect-metadata": "0.1.13"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'reflect-metadata'
import { el } from '@zero-dependency/dom'
import { User } from './entities.js'
import { storage } from './storage.js'
import { addUser, getLastUserId, storage } from './storage.js'

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

Expand Down Expand Up @@ -48,7 +48,7 @@ const form = el(
return
}

storage.addUser(new User(storage.getLastUserId() + 1, username))
addUser(new User(getLastUserId() + 1, username))
render()
}
},
Expand All @@ -63,7 +63,7 @@ const storagePreview = el('pre')
function render() {
form.reset()
usernameInput.focus()
userIdInput.value = storage.getLastUserId().toString()
userIdInput.value = getLastUserId().toString()
storagePreview.textContent = JSON.stringify(storage.data, null, 2)
}

Expand Down
27 changes: 27 additions & 0 deletions examples/with-browser-lodash/src/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { BrowserDatabase, LocalStorage } from '@stenodb/browser'
import lodash from 'lodash'
import { User, Users } from './entities.js'
import type { BrowserAdapter } from '@stenodb/browser/types'

class StorageWithLodash<T> extends BrowserDatabase<T> {
chain: lodash.ExpChain<this['data']> = lodash.chain(this).get('data')

constructor(adapter: BrowserAdapter<T>, initialData: T) {
super(adapter, initialData)
}
}

const adapter = new LocalStorage('users', Users)
const initialData = new Users(new User(1, 'John'))

export const storage = new StorageWithLodash(adapter, initialData)
storage.read()

export function addUser(user: User): void {
storage.chain.get('users').push(user).value()
storage.write()
}

export function getLastUserId(): number {
return storage.chain.get('users').last().get('id').value()
}
8 changes: 4 additions & 4 deletions examples/with-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
"preview": "vite preview"
},
"devDependencies": {
"typescript": "4.9.4",
"vite": "4.0.4"
"typescript": "4.9.5",
"vite": "4.1.1"
},
"dependencies": {
"@stenodb/browser": "workspace:*",
"@zero-dependency/dom": "0.12.0",
"class-transformer": "0.5.1",
"reflect-metadata": "0.1.13",
"stenodb": "workspace:*"
"reflect-metadata": "0.1.13"
}
}
5 changes: 2 additions & 3 deletions examples/with-browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ const form = el(
usernameInput.focus()
return
}
storage.data!.addUser(
new User(storage.data!.getLastUserId() + 1, username)
)
const user = new User(storage.data!.getLastUserId() + 1, username)
storage.data!.addUser(user)
storage.write()
render()
}
Expand Down
13 changes: 5 additions & 8 deletions examples/with-browser/src/storage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { BrowserProvider, LocalStorage } from 'stenodb/browser'
import { BrowserDatabase, LocalStorage } from '@stenodb/browser'
import { User, Users } from './entities.js'

const adapter = new LocalStorage<Users>('users')

export const storage = new BrowserProvider({
adapter,
entity: Users,
initialData: new Users(new User(1, 'John'))
})
const adapter = new LocalStorage('users', Users)
const initialData = new Users(new User(1, 'John'))
export const storage = new BrowserDatabase(adapter, initialData)
storage.read()
26 changes: 0 additions & 26 deletions examples/with-lodash/src/storage.ts

This file was deleted.

8 changes: 8 additions & 0 deletions examples/with-node-lodash/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"watch": [
"src"
],
"ignore": ["database", "dist"],
"exec": "cross-env TS_NODE_TRANSPILE_ONLY=true NODE_ENV=development node --no-warnings --loader ts-node/esm --enable-source-maps --trace-warnings --inspect=0.0.0.0:9234 --nolazy src/index.ts",
"ext": "ts"
}
20 changes: 20 additions & 0 deletions examples/with-node-lodash/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "with-node-lodash",
"type": "module",
"private": true,
"scripts": {
"start": "pnpm build && node dist/index.js",
"dev": "nodemon",
"build": "del-cli dist && tsc"
},
"dependencies": {
"@stenodb/node": "workspace:*",
"class-transformer": "0.5.1",
"lodash": "4.17.21",
"reflect-metadata": "0.1.13"
},
"devDependencies": {
"@types/node": "18.11.19",
"@types/lodash": "4.14.191"
}
}
20 changes: 20 additions & 0 deletions examples/with-node-lodash/src/entities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Type } from 'class-transformer'

export class Users {
@Type(() => User)
users: User[]

constructor(...users: User[]) {
this.users = users
}
}

export class User {
id: number
username: string

constructor(id: number, username: string) {
this.id = id
this.username = username
}
}
41 changes: 41 additions & 0 deletions examples/with-node-lodash/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'reflect-metadata'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { AsyncWriter, NodeDatabase } from '@stenodb/node'
import lodash from 'lodash'
import { User, Users } from './entities.js'
import type { NodeProvider } from '@stenodb/node/types'

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

constructor(private readonly provider: NodeProvider<T>) {
this.chain = lodash.chain(provider).get('data')
}

get data(): T | null {
return this.provider.data
}

async read(): Promise<void> {
await this.provider.read()
}

async write(): Promise<void> {
await this.provider.write()
}
}

const path = resolve(dirname(fileURLToPath(import.meta.url)), '..', 'database')
const adapter = new AsyncWriter('users', Users)
const initialData = new Users(new User(1, 'John Doe'))
const database = new NodeDatabase(path)

const usersDatabase = new NodeWithLodash(database.create(adapter, initialData))
await usersDatabase.read()

function findUserById(id: number) {
return usersDatabase.chain.get('users').find({ id }).value()
}

console.log(findUserById(1))
12 changes: 12 additions & 0 deletions examples/with-node-lodash/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "@crashmax/tsconfig",
"compilerOptions": {
"moduleResolution": "NodeNext",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"outDir": "dist"
},
"include": [
"src"
]
}
7 changes: 5 additions & 2 deletions examples/with-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
"build": "del-cli dist && tsc"
},
"dependencies": {
"@stenodb/node": "workspace:*",
"class-transformer": "0.5.1",
"reflect-metadata": "0.1.13",
"stenodb": "workspace:*"
"reflect-metadata": "0.1.13"
},
"devDependencies": {
"@types/node": "18.11.19"
}
}
Loading