ZenStack Documentation

repository·dev·Indexed 25 days ago

https://github.com/zenstackhq/zenstack

A modern TypeScript database toolkit providing a schema-first ORM, built-in access control, and automatic API generation. Built on Kysely, ZenStack includes a CLI for ZModel schema management, a core ORM engine (@zenstackhq/orm), and various client integrations for fetch and TanStack Query (React, Vue, Svelte). It also offers a VS Code extension for .zmodel files and specialized plugins like @zenstackhq/plugin-soft-delete.

Tokens
29.9K
Snippets
71
Records
208
Agent score
83%

What's inside ZenStack

  1. Overview of ZenStack

    dev

    ZenStack is a TypeScript database toolkit for developing full-stack or backend Node.js/Bun applications. It provides a unified data modeling and query solution with the following features:

    • Schema-first ORM: Compatible with Prisma's schema and API.
    • Versatile API: Offers high-level ORM queries and a low-level Kysely query builder.
    • Security: Built-in access control and data validation.
    • Advanced Modeling: Supports patterns like polymorphism.
    • Extensibility: Designed for flexibility and extensibility.
    • Automatic APIs: Generates CRUD web APIs with adapters for popular frameworks.
    • Frontend Integration: Provides automatic TanStack Query hooks for easy CRUD operations.
    • Zod Integration: Generates Zod schemas.
  2. Overview of @zenstackhq/orm features

    dev

    The @zenstackhq/orm is a type-safe database client built on top of Kysely. Key capabilities include:

    • Type-safe CRUD operations: Automatically generated from your ZModel schema.
    • Plugin system: Supports query interception and entity mutation hooks.
    • Multi-dialect support: Works with SQLite (better-sqlite3), PostgreSQL (pg), and MySQL (mysql2).
    • Computed fields: Evaluated at the database level.
    • Custom procedures: Allows encapsulating complex queries and mutations.
  3. ZenStack V3 Architecture and Benefits

    dev

    ZenStack V3 is a major rewrite that replaces Prisma ORM with a custom ORM engine built on top of Kysely, while maintaining a Prisma-compatible query API.

    Key benefits of V3 include:

    • Pure TypeScript: No Rust or WASM components required.
    • Improved Type Inference: More TypeScript type inference and reduced reliance on code generation.
    • Typed Query Builder: A fully-typed Kysely-based query builder serves as a superior escape hatch compared to Prisma's raw SQL or typed SQL.
  4. Project structure of the ZenStack Nuxt Blog Sample

    dev

    The project is organized into the following directories:

    • app/: Contains Nuxt application components and pages.
    • server/: Contains Nuxt server files.
      • api/model/[...path].ts: The ZenStack API endpoint.
      • utils/db.ts: The database client.
    • zenstack/: Contains ZenStack schema and generated files.
      • schema.zmodel: The database schema definition.
      • seed.ts: The database seeding script.
  5. Use @zenstackhq/client-helpers for custom ZenStack clients

    dev
    @zenstackhq/client-helpers provides shared building blocks for implementing clients that consume ZenStack's CRUD service. While it is used internally by @zenstackhq/fetch-client and @zenstackhq/tanstack-query, it is also available for developers who need to build their own custom client implementation to interact with ZenStack services.
  6. Use @zenstackhq/sdk for schema processing and code generation

    dev

    The @zenstackhq/sdk package provides low-level utilities for developers building tools on top of ZenStack. Key capabilities include:

    • ZModel AST traversal: Utilities for working with the ZModel Abstract Syntax Tree and model structures.
    • TypeScript code generation: Generating TypeScript schema output directly from ZModel.
    • CLI plugin interface: An interface for building and extending the zenstack generate command via plugins.
    • Prisma interop: Utilities for interacting with Prisma schema artifacts.
  7. Develop a Svelte project

    dev

    After creating your project and installing dependencies (using npm install, pnpm install, or yarn), start the development server using the dev script. You can use the --open flag to automatically open the application in a new browser tab.

    npm run dev
    
    # or start the server and open the app in a new browser tab
    npm run dev -- --open