generated from crashmax-dev/node-esm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from crashmax-dev/v3
feat(packages): reworked api
- Loading branch information
Showing
38 changed files
with
431 additions
and
375 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.