https://github.com/joris-gallot/zorm
zorm - A minimalist, type-safe ORM powered by Zod
https://github.com/joris-gallot/zorm
orm runtime-validation schema-validation type-inference typescript vue zod
Last synced: 9 months ago
JSON representation
zorm - A minimalist, type-safe ORM powered by Zod
- Host: GitHub
- URL: https://github.com/joris-gallot/zorm
- Owner: joris-gallot
- License: mit
- Created: 2025-03-30T15:01:43.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-04-13T15:09:36.000Z (10 months ago)
- Last Synced: 2025-04-13T15:38:40.250Z (10 months ago)
- Topics: orm, runtime-validation, schema-validation, type-inference, typescript, vue, zod
- Language: TypeScript
- Homepage:
- Size: 568 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# zorm
zorm is a minimalist ORM powered by [Zod](https://zod.dev/). It allows you to define and manipulate entities in a simple and type-safe way, with intuitive relation management.
## Features
- ✅ Type-safe schema definition and validation powered by Zod
- 🔍 Fully typed query builder with:
- Type-safe field names and operators in where clauses
- Autocomplete for relation names in eager loading
- Inferred return types including nested relations
- 🤝 Support for one-to-one and one-to-many relationships
- 🚀 Eager loading of related entities
- 🛡️ Runtime validation through Zod schemas
- ⚡️ Reactivity support for Vue (other frameworks planned)
## Installation
```sh
npm install @zorm-ts/core
```
## Usage
### Define Entities
```ts
import { defineEntity } from '@zorm-ts/core'
import { z } from 'zod'
export const User = defineEntity(
'user',
z.object({
id: z.number(),
email: z.string().email(),
age: z.number(),
username: z.string().optional(),
isAdmin: z.boolean()
})
)
export const Post = defineEntity(
'post',
z.object({
id: z.number(),
title: z.string(),
userId: z.number(),
})
)
```
### Create a Query Builder from relations
```ts
import { defineQueryBuilder } from '@zorm-ts/core'
import { Post, User } from './entities'
export const userQuery = defineQueryBuilder(User, ({ many }) => ({
posts: many(Post, {
reference: Post.fields.userId,
field: User.fields.id,
}),
}))
const users = userQuery.query()
.where(user => user.age > 18)
.orWhere(user => user.isAdmin)
.get()
/*
[{
id: number
email: string
age: number
username?: string
isAdmin: boolean
}]
*/
const usersWithPosts = userQuery.query()
.where(user => user.age > 18)
.orWhere(user => user.isAdmin)
.with('posts')
.get()
/*
[{
id: number
email: string
age: number
username?: string
isAdmin: boolean
posts: Array<{
id: number
title: string
userId: number
}>
}]
*/
```
## Reactivity
Currently it only handles reactivity through Vue integration (see [Vue integration example](packages/vue/src/index.ts)), but support for other frameworks will be added.
### Vue
zorm provides first-class support for Vue through the `@zorm-ts/vue` package. This integration enables reactive queries that automatically update your components when the data changes.
```sh
npm install @zorm-ts/vue
```
For detailed Vue integration instructions, check out the [@zorm-ts/vue documentation](packages/vue/README.md).
## License
MIT