Skip to content

Commit aa62540

Browse files
committed
feat(examples): add with-node-lodash
1 parent 05a4330 commit aa62540

File tree

8 files changed

+107
-6
lines changed

8 files changed

+107
-6
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"watch": [
3+
"src"
4+
],
5+
"ignore": ["database", "dist"],
6+
"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",
7+
"ext": "ts"
8+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "with-node-lodash",
3+
"type": "module",
4+
"private": true,
5+
"scripts": {
6+
"start": "pnpm build && node dist/index.js",
7+
"dev": "nodemon",
8+
"build": "del-cli dist && tsc"
9+
},
10+
"dependencies": {
11+
"@stenodb/node": "workspace:*",
12+
"class-transformer": "0.5.1",
13+
"lodash": "4.17.21",
14+
"reflect-metadata": "0.1.13"
15+
},
16+
"devDependencies": {
17+
"@types/node": "18.11.19",
18+
"@types/lodash": "4.14.191"
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Type } from 'class-transformer'
2+
3+
export class Users {
4+
@Type(() => User)
5+
users: User[]
6+
7+
constructor(...users: User[]) {
8+
this.users = users
9+
}
10+
}
11+
12+
export class User {
13+
id: number
14+
username: string
15+
16+
constructor(id: number, username: string) {
17+
this.id = id
18+
this.username = username
19+
}
20+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import 'reflect-metadata'
2+
import { dirname, resolve } from 'node:path'
3+
import { fileURLToPath } from 'node:url'
4+
import { AsyncWriter, NodeDatabase } from '@stenodb/node'
5+
import lodash from 'lodash'
6+
import { User, Users } from './entities.js'
7+
import type { NodeProvider } from '@stenodb/node/types'
8+
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>
14+
chain: lodash.ExpChain<T>
15+
16+
constructor(provider: NodeProvider<T>) {
17+
this.provider = provider
18+
this.chain = lodash.chain(provider).get('data')
19+
}
20+
21+
get data(): T | null {
22+
return this.provider.data
23+
}
24+
25+
async read(): Promise<void> {
26+
await this.provider.read()
27+
}
28+
29+
async write(): Promise<void> {
30+
await this.provider.write()
31+
}
32+
}
33+
34+
const db = new NodeDatabase(path)
35+
const usersDatabase = new NodeWithLodash(
36+
db.create(adapter, new Users(new User(1, 'John Doe')))
37+
)
38+
39+
await usersDatabase.read()
40+
const user = usersDatabase.chain.get('users').find({ id: 1 }).value()
41+
console.log(user)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "@crashmax/tsconfig",
3+
"compilerOptions": {
4+
"moduleResolution": "NodeNext",
5+
"experimentalDecorators": true,
6+
"emitDecoratorMetadata": true,
7+
"outDir": "dist"
8+
},
9+
"include": [
10+
"src"
11+
]
12+
}

examples/with-node/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@
1111
"@stenodb/node": "workspace:*",
1212
"class-transformer": "0.5.1",
1313
"reflect-metadata": "0.1.13"
14+
},
15+
"devDependencies": {
16+
"@types/node": "18.11.19"
1417
}
1518
}

examples/with-node/tsconfig.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
"extends": "@crashmax/tsconfig",
33
"compilerOptions": {
44
"moduleResolution": "NodeNext",
5-
"strictNullChecks": true,
6-
"noPropertyAccessFromIndexSignature": false,
7-
"noImplicitOverride": false,
85
"experimentalDecorators": true,
96
"emitDecoratorMetadata": true,
107
"outDir": "dist"

packages/node/src/DirectoryProvider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ export class DirectoryProvider {
88
temporaryPath: string
99

1010
constructor(path: string) {
11-
if (!lstatSync(path).isDirectory()) {
12-
throw new Error('Path must be a directory')
13-
}
11+
// if (!lstatSync(path).isDirectory()) {
12+
// throw new Error('Path must be a directory')
13+
// }
1414

1515
this.databasePath = path
1616
this.temporaryPath = join(this.databasePath, 'temp')

0 commit comments

Comments
 (0)