1.8.10 • Published 4 months ago

@udraft/core v1.8.10

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

uDraft - Write Once, Use Everywhere

uDraft is a language and stack agnostic code-generation tool that simplifies full-stack development by converting a single YAML file into code for rapid development. In the YAML file, you define models, enums, API endpoints, and validation rules, and uDraft automatically generates classes, database schemas, DTOs, API infrastructure, and even client APIs. This cuts down on repetitive coding and keeps everything consistent.

Using modular renderers, uDraft produces different outputs from the same source. For example, it can create:

TypeScript classes with class-validator decorators for Nest.js Mongoose schemas for MongoDB Plain models or client APIs for front-end applications Its flexible pipeline lets teams add custom renderers (for GraphQL, OpenAPI docs, ORMs, etc.) or use built-in ones. Unlike rigid scaffolding tools, uDraft separates the architecture design from the implementation details, allowing you to maintain one source of truth for your domain logic while easily updating services, databases, and clients.

📜 Core Concepts

Symbol Cheat Sheet

SymbolPurposeExample
/Node attribute/schema: accounts
+Model declaration+user
~Enum declaration~user-roles
&Reference by IDowner[&user]
>Field renamingpasswordHash > pass
$Special operations$pick, $remove

🏗️ Basic Structure

Draft Blueprint

draft:
  /name: Project Name # Root attributes
  /version: 1.0

  module-name: # Component container
    ~enum-name: # Enum definition
      KEY: value
    +model-name: # Data model
      field[type]:
        - validation
    feature-name: # API endpoint
      /http: { method: post }

📦 Modules

Logical grouping of related components

authentication:
  /description: User auth flows
  ~auth-methods: # Enum
    EMAIL: email
    GOOGLE: oauth2-google

🎚️ Enums

Fixed value collections

~user-status:
  ACTIVE: active # Display : Storage
  PENDING: pending
  BANNED: banned

🧬 Models

Base Model

+timestamps:
  createdAt[date]:
    - required
  updatedAt[date]:

Inherited Model

+user(timestamps): # Parenthesis inheritance
  email[string]:
    - unique
    - format(email)

Relationships

posts[&post]: # ID reference (foreign key)
  - array
profile[user-profile]: # Embedded document

🔄 Model Transformations

+user-dto(user): # Full inclusion: user-dto inherits all the user model fields
  $remove: # Field exclusion: remove fields declared in this model by name
    - passwordHash
    - __v

  $pick(user): # Selective inclusion: inherits only specific fields from user model
    - _id > id # Field renaming
    - email

🌐 Features (API Endpoints)

/create-user:
  /http: { method: post, url: /users }
  input:
    +create-user-dto: # Declare new model for request adding some existing user fields
      $pick(user):
        - email
        - password
  output: user-dto # Use aleady declared user-dto model as is

✅ Field Validations

age[int]:
  - min(13)
  - max(120)

password[string]:
  - minLength(8)
  - regex(/[A-Z]/)

🚀 Complete Example

draft:
  /name: Blog Platform

  content:
    ~post-status:
      DRAFT: draft
      PUBLISHED: published

    +post:
      title[string]:
        - required
      content[string]:
        - minLength(100)
      status[post-status]:
        - required(false)

    create-post:
      /http: { method: post, url: /posts }
      input:
        +create-post-request(post):
          $remove:
            - status
      output: +post-response(post)

🛠️ Code Generation

Pipeline Flow

  1. Parse YAML - Load draft configuration
  2. Apply Renderers - Transform components to code
  3. Generate Output - Create files in target directories

Built-in Renderers

RendererOutput
TSClassRendererTypeScript classes
TSClassValidatorRendererTypeScript Validation decorators
TSMongooseSchemaRendererTypeScript MongoDB schemas
TSApiClientRendererTypeScript API Client With Axios
DartClassRendererDart classes
DartApiClientRendererDart API Client With Axios

Using a Renderer

UDraft.load("project.yaml")
  .begin("projects/backend/")
  .pipeline([
    new TSClassRenderer(),
    new TSClassValidatorRenderer(),
    new TSMongooseSchemaRenderer(),
  ])
  .exec();

📁 Sample Output

//  Generated MongoDB Schema: project/schemas/social/post-schema.ts
import { PostStatus } from "../../types/social/post-status";
import { Post } from "../../entities/social/post";

export const PostSchema = new Schema<Post>({
  title: { type: String, required: true },
  content: { type: String, minLength: 100 },
  status: {
    type: String,
    enum: PostType,
  },
});

export const PostModel = mongoose.model<Post>("accounts", PostSchema);

// Generated TypeScript Class with validators: project/entities/social/post.ts
import { PostStatus } from "../../types/post-status";

export class Post {
  @IsString()
  @IsRequired()
  @IsNotEmpty()
  title: string;

  @IsString()
  @IsOptional()
  @MaxLength(100)
  content?: string;

  @IsOptional()
  @IsEnum(PostStatus)
  status?: PostStatus;
}

// Generated TypeScript Enum: project/types/social/post-status.ts
export enum PostStatus {
  Draft = "dratf",
  Publish = "publish",
}

💡 Pro Tips

  1. Reuse Models: Inherit common fields with (parent-model)
  2. API Safety: Use $remove to exclude sensitive fields from DTOs
  3. Validation First: Define rules in YAML for auto-generated checks
  4. Cross-References: Use &model for database relations
  5. Renderer Stack: Combine multiple renderers for full-stack generation
1.8.2

4 months ago

1.5.5

4 months ago

1.3.7

4 months ago

1.8.1

4 months ago

1.5.4

4 months ago

1.3.6

4 months ago

1.8.0

4 months ago

1.5.3

4 months ago

1.3.5

4 months ago

1.7.0

4 months ago

1.5.2

4 months ago

1.4.3

4 months ago

1.3.4

4 months ago

1.6.0

4 months ago

1.5.1

4 months ago

1.4.2

4 months ago

1.3.3

4 months ago

1.5.0

4 months ago

1.4.1

4 months ago

1.4.0

4 months ago

1.3.10

4 months ago

1.3.13

4 months ago

1.3.14

4 months ago

1.3.11

4 months ago

1.3.12

4 months ago

1.8.9

4 months ago

1.8.10

4 months ago

1.8.8

4 months ago

1.8.7

4 months ago

1.8.6

4 months ago

1.8.5

4 months ago

1.8.4

4 months ago

1.3.9

4 months ago

1.8.3

4 months ago

1.3.8

4 months ago

1.2.0

4 months ago

1.3.2

4 months ago

1.3.1

4 months ago

1.3.0

4 months ago

1.1.3

4 months ago

1.1.2

4 months ago

1.1.1

4 months ago

1.1.0

4 months ago

1.0.0

4 months ago