Skip to content

Commit

Permalink
Merge pull request #12 from crashmax-dev/v3
Browse files Browse the repository at this point in the history
feat(packages): reworked api
  • Loading branch information
crashmax-dev authored Feb 7, 2023
2 parents 62a2c93 + c8bc495 commit 56d6dfb
Show file tree
Hide file tree
Showing 38 changed files with 431 additions and 375 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ 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 |

Expand Down Expand Up @@ -72,33 +71,34 @@ export class Post {
import 'reflect-metadata'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { AsyncWriter, NodeDatabase } from '@stenodb/node'
import { AsyncAdapter, NodeProvider } 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 adapter = new AsyncAdapter('users', Users, initialData)
const provider = new NodeProvider(path)
const database = provider.createAsync(adapter)

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

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

const adapter = new LocalStorage('users', Users)
const initialData = new Users(new User('John Doe'))
const databaseUsers = new BrowserDatabase(adapter, initialData)
const adapter = new LocalStorage('users', Users, initialData)
const provider = new BrowserProvider()
const storage = provider.create(adapter)

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

## Credits
Expand Down
33 changes: 25 additions & 8 deletions examples/with-browser-lodash/src/storage.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
import { BrowserDatabase, LocalStorage } from '@stenodb/browser'
import { BrowserProvider, LocalStorage } from '@stenodb/browser'
import { Steno } from '@stenodb/browser/types'
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')
export class BrowserWithLodash<T> {
chain: lodash.ExpChain<T>

constructor(adapter: BrowserAdapter<T>, initialData: T) {
super(adapter, initialData)
constructor(private readonly provider: Steno.BrowserProvider<T>) {
this.chain = lodash.chain(provider).get('data')
}

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

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

write(): void {
this.provider.write()
}

reset(): void {
this.provider.reset()
}
}

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

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

export function addUser(user: User): void {
Expand Down
8 changes: 5 additions & 3 deletions examples/with-browser/src/storage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { BrowserDatabase, LocalStorage } from '@stenodb/browser'
import { BrowserProvider, LocalStorage } from '@stenodb/browser'
import { User, Users } from './entities.js'

const adapter = new LocalStorage('users', Users)
const initialData = new Users(new User(1, 'John'))
export const storage = new BrowserDatabase(adapter, initialData)
const adapter = new LocalStorage('users', Users, initialData)
const provider = new BrowserProvider()

export const storage = provider.create(adapter)
storage.read()
25 changes: 15 additions & 10 deletions examples/with-node-lodash/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,46 @@
import 'reflect-metadata'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { AsyncWriter, NodeDatabase } from '@stenodb/node'
import { AsyncAdapter, NodeProvider } from '@stenodb/node'
import lodash from 'lodash'
import { User, Users } from './entities.js'
import type { NodeProvider } from '@stenodb/node/types'
import type { Steno } from '@stenodb/node/types'

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

constructor(private readonly provider: NodeProvider<T>) {
constructor(private readonly provider: Steno.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 read(): Promise<T | null> {
return await this.provider.read()
}

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

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

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 adapter = new AsyncAdapter('users', Users, initialData)
const provider = new NodeProvider(path)

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

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

console.log(findUserById(1))
17 changes: 8 additions & 9 deletions examples/with-node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import 'reflect-metadata'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { AsyncWriter, NodeDatabase } from '@stenodb/node'
import { AsyncAdapter, NodeProvider } from '@stenodb/node'
import { Post, User, Users } 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 usersDatabase = database.create(adapter, initialData)
await usersDatabase.read()
const adapter = new AsyncAdapter('users', Users, initialData)
const provider = new NodeProvider(path)

const database = provider.createAsync(adapter)
await database.read()
const post = new Post('Hello world')
usersDatabase.data?.users[0]?.addPost(post)
await usersDatabase.write()
database.data?.users[0]?.addPost(post)
await database.write()

console.log(usersDatabase.data)
console.log(database.data)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stenodb/workspace",
"version": "2.0.0",
"version": "3.0.0",
"type": "module",
"private": true,
"workspaces": [
Expand Down
13 changes: 7 additions & 6 deletions packages/browser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,17 @@ export class Post {
```typescript
// index.ts
import 'reflect-metadata'
import { LocalStorage, BrowserDatabase } from '@stenodb/browser'
import { LocalStorage, BrowserProvider } from '@stenodb/browser'
import { Users, User, Post } from './entities.js'

const adapter = new LocalStorage('users', Users)
const initialData = new Users(new User('John Doe'))
const databaseUsers = new BrowserDatabase(adapter, initialData)
const adapter = new LocalStorage('users', Users, initialData)
const provider = new BrowserProvider()
const storage = provider.create(adapter)

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

## Credits
Expand Down
4 changes: 2 additions & 2 deletions packages/browser/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@stenodb/browser",
"description": "✍ Easy to use local JSON database",
"version": "2.0.0",
"version": "3.0.0",
"type": "module",
"files": [
"dist"
Expand Down Expand Up @@ -49,7 +49,7 @@
"prepublishOnly": "pnpm build"
},
"dependencies": {
"@stenodb/utils": "workspace:2.0.0",
"@stenodb/utils": "workspace:3.0.0",
"class-transformer": "0.5.1"
},
"peerDependencies": {
Expand Down
9 changes: 0 additions & 9 deletions packages/browser/src/BrowserDatabase.ts

This file was deleted.

55 changes: 0 additions & 55 deletions packages/browser/src/BrowserProvider.ts

This file was deleted.

6 changes: 3 additions & 3 deletions packages/browser/src/adapter/LocalStorage.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { BrowserStorage } from './WebStorage.js'
import type { Entity } from '../types.js'
import type { Steno } from '../types.js'

export class LocalStorage<T> extends BrowserStorage<T> {
constructor(name: string, entity: Entity<T>) {
super(name, localStorage, entity)
constructor(name: string, entity: Steno.Entity<T>, initialData?: T) {
super(name, localStorage, entity, initialData)
}
}
6 changes: 3 additions & 3 deletions packages/browser/src/adapter/SessionStorage.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { BrowserStorage } from './WebStorage.js'
import type { Entity } from '../types.js'
import type { Steno } from '../types.js'

export class SessionStorage<T> extends BrowserStorage<T> {
constructor(name: string, entity: Entity<T>) {
super(name, sessionStorage, entity)
constructor(name: string, entity: Steno.Entity<T>, initialData?: T) {
super(name, sessionStorage, entity, initialData)
}
}
Loading

0 comments on commit 56d6dfb

Please sign in to comment.