Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions .changeset/odd-spoons-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@nlfmt/stormdb": major
---

Allow multiple validation libraries using typeschema

This release adds the capabilities to use any validation library to define schemas in StormDB with the help of the typeschema library, an abstraction over the most common validation libraries
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ it doesnt do any querying optimizations, like indexing.


## Quickstart
1. Install necessary packages `npm install @nlfmt/stormdb zod`
2. Initialize the database
1. Install StormDB `npm install @nlfmt/stormdb`
2. Install packages for you validation library, here are some examples:
- `npm install zod @typeschema/zod`
- `npm install valibot @typeschema/valibot`
3. Initialize the database
```ts
import StormDB from '@nlfmt/stormdb';
import { z } from 'zod';
Expand All @@ -27,7 +30,7 @@ const userModel = z.object({

const db = StormDB({ user: userModel }, { storage: "db.json" });
```
3. Use the querying API
4. Use the querying API
```ts
// Add a user
let usr = await db.user.create({ name: "John", age: 20 });
Expand Down
23 changes: 18 additions & 5 deletions examples/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,27 @@ import StormDB, {
DocType,
$contains,
$push,
InDocType,
} from "@nlfmt/stormdb"
import { z } from "zod"

const userModel = z.object({
name: z.string().min(1),
age: z.number(),
hobbies: z.array(z.string()).optional().default([]),
// example using valibot
import * as v from "valibot"

const userModel = v.object({
name: v.pipe(v.string(), v.minLength(1)),
age: v.number(),
hobbies: v.optional(v.array(v.string()), [])
})

// same example using zod, comment out the above import and uncomment this one to use
// import { z } from "zod"

// const userModel = z.object({
// name: z.string().min(1),
// age: z.number(),
// hobbies: z.array(z.string()).optional().default([]),
// })

type User = DocType<typeof userModel>

const db = StormDB(
Expand Down
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,18 @@
"author": "nlfmt <nlfmt@gmx.de>",
"license": "MIT",
"dependencies": {
"zod": "^3.22.4"
"@typeschema/main": "^0.14.1"
},
"devDependencies": {
"@changesets/cli": "^2.27.1",
"@nlfmt/stormdb": "link:",
"@types/node": "^20.11.16",
"@typeschema/valibot": "^0.14.0",
"@typeschema/zod": "^0.14.0",
"tsup": "^8.0.1",
"typescript": "^5.3.3"
}
"typescript": "^5.3.3",
"valibot": "1.0.0-beta.15",
"zod": "^3.24.1"
},
"packageManager": "pnpm@10.2.0+sha512.0d27364e0139c6aadeed65ada153135e0ca96c8da42123bd50047f961339dc7a758fc2e944b428f52be570d1bd3372455c1c65fa2e7aa0bfbf931190f9552001"
}
Loading