schema-cast
v1.0.0
Published
Single schema definition → TypeScript + Zod + Mongoose + PostgreSQL. One source of truth.
Maintainers
Readme
schema-cast
A powerful code generation tool to create a single source-of-truth for your application's data layer.
Define your schema once in JSON or YAML, and schema-cast automatically generates:
- TypeScript interfaces/types
- Zod validation schemas
- MongoDB (Mongoose) models
- PostgreSQL
CREATE TABLESQL statements
Installation
npm install -g schema-cast
# OR
npx schema-cast generate ...Usage
1. Define your Schema (user.schema.json)
{
"name": "User",
"fields": [
{ "name": "id", "type": "uuid", "required": true, "primary": true },
{ "name": "email", "type": "string", "required": true, "unique": true },
{ "name": "age", "type": "number", "required": false },
{ "name": "role", "type": "enum", "values": ["admin", "user", "guest"], "default": "user" },
{ "name": "createdAt", "type": "date", "required": true },
{ "name": "profile", "type": "object", "fields": [
{ "name": "bio", "type": "string" },
{ "name": "avatar", "type": "string" }
]}
]
}Supported Field Types
string,number,boolean,date,uuid,email,url,enum,object,array
Options
required: boolean: Is the field required? (default: false)unique: boolean: Enforce unique constraintprimary: boolean: Mark as Primary Key (used in Postgres, mapped to_idin Mongoose)default: any: Specify a default valuevalues: string[]: Used specifically withenumtypefields: FieldDefinition[]: Used specifically withobjecttypeitems: FieldDefinition: Used specifically witharraytype
2. Generate Code
# Generate from a single file
schema-cast generate --input ./schemas/user.schema.json --out ./generated
# Process all schemas in a directory
schema-cast generate --all --input ./schemas/ --out ./generated
# Watch mode - automatically regenerate on save
schema-cast watch --input ./schemas/Generated Files
The CLI will generate four files in the target directory per schema:
user.types.ts: TypeScript definitionuser.zod.ts: Zod schema declarationuser.model.ts: Mongoose model schemauser.sql: PostgreSQL CREATE TABLE command
License
MIT
