Skip to content

Templates and Fields ​

From a Template ​

sh
npx megh make:model Product --template ecommerce/product
npx megh make:resource Product --template ecommerce/product   # + DTOs, service, controller
npx megh m:m Product --template                              # pick a domain, then a template

A template is named <domain>/<name>. A bare name works when only one domain has it, and a common template wins (--template category is common/category). See every template on the Domains page, or list them:

sh
npx megh template:list              # domains and templates
npx megh templates ecommerce        # one domain, with each template's columns
npx megh templates --json

For make:model Product --template ecommerce/product:

ts
import { defineMigration } from "@meghjs/core";

export default defineMigration({
  async up(schema) {
    await schema.create("products", (table) => {
      table.id();
      table.foreignId("category_id").constrained("categories").nullOnDelete();
      table.foreignId("brand_id").constrained("brands").nullOnDelete();
      table.string("name");
      table.string("slug").unique();
      table.string("sku", 64).unique();
      table.text("description").nullable();
      table.decimal("price", 12, 2);
      table.decimal("compare_at_price", 12, 2).nullable();
      table.decimal("cost", 12, 2).nullable();
      table.integer("stock").default(0);
      table.decimal("weight", 10, 3).nullable();
      table.string("status", 32).default("draft"); // draft | active | archived
      table.boolean("is_featured").default(false);
      table.timestamps();
      table.softDeletes();
      table.index("status");
    });
  },

  async down(schema) {
    await schema.drop("products");
  },
});

Since no migration creates categories or brands, their migrations (common/category, ecommerce/brand) and models are written first. You are asked before they are added; --no-deps leaves them out (create those tables yourself before migrating).

The model's name sets the table: make:model Item --template ecommerce/product creates items with the product columns. Pass --table <name> to choose it.

Options ​

OptionDescription
--template [ref]The template; without a value, pick one
--fields <list>Columns to add to the template, or to replace its columns of the same name
--table <name>Table name (default: from the model name)
--factoryAlso write a factory for each model
--no-depsDon't create the tables foreign keys point to
-f, --forceOverwrite the model (or the resource's files) if they exist
-y, --yesDon't prompt

From Fields ​

Describe the table on the command line, with or without a template:

sh
npx megh make:resource Ticket --fields "title,body,priority,status:in(open|pending|closed),user_id:cascade"
npx megh make:model Product --template ecommerce/product --fields "barcode?:string(64):unique,weight:decimal(8,2)"

Every table gets an id and timestamps. Foreign keys to tables no migration creates are handled as with templates (user_id above brings in users).

Field Spec ​

A field is written name[?][:type[(args)]][:modifier]..., and a list is separated by commas:

SpecColumn
titleThe title preset: string(255)
slugThe slug preset: string(255), unique
email:uniqueThe email preset, unique
phone?The phone preset, nullable (?)
address?The address group: six columns, all nullable
nickname?:string(50)string(50), nullable
price:moneydecimal(12,2)
hours:decimal(5,1):default(0)decimal(5,1) with a default
status:in(draft|published)string(32) that defaults to draft, the allowed values in a comment
kind:in(a|b)string(32), required
category_idA foreign key to categories, from the name
owner_id:foreign(users):cascadeA foreign key to users, deleted with the user

Field names are snake_case. key, save, fill, delete, restore and related can't be used: they are Model methods.

Types ​

TypeColumn
string(n)VARCHAR(n), 255 by default
textLong text
integer / int, bigInteger / bigintIntegers
decimal(p,s)Exact number, (8,2) by default
moneydecimal(12,2)
percentdecimal(5,2)
float, doubleFloating point
boolean / boolBoolean
date, datetime, timestamp, timeDates and times
jsonJSON (JSONB on PostgreSQL)
uuidUUID
emailstring(255)
phonestring(32)
urlstring(2048)
foreign(table) / foreignIdInteger foreign key with its constraint

Modifiers ​

ModifierEffect
nullable (or ? after the name)Allow NULL
uniqueUnique index
indexIndex
default(value)Default value: a number, true / false, or text
in(a|b|c)Allowed values. status defaults to the first one unless default() is given
cascadeForeign key: delete the row with the referenced row
nullOnDeleteForeign key: set it to NULL (makes it nullable)
restrictForeign key: refuse to delete the referenced row

SQL Server ​

SQL Server refuses a foreign key whose ON DELETE action (CASCADE or SET NULL) would form a cycle, including a table that references itself, or a second cascade path between two tables. When the project's database is SQL Server (from megh.database, DB_DIALECT or the DATABASE_URL scheme), the generated migrations account for the foreign keys already in your migrations: CASCADE is kept first, and a foreign key that would break the rule gets no ON DELETE action, with a comment in the migration and a line in the output:

ts
table.foreignId("parent_id").nullable().constrained("categories"); // no ON DELETE action: SQL Server allows one cascade path between tables

Deleting the referenced row then fails while rows point to it; clear or reassign them first. restrict is written as onDelete("no action"), since SQL Server has no RESTRICT. Other databases get the actions as given.

Allowed values are written as a comment next to the column; the generated DTO doesn't check them. Add @IsIn([...]) to the DTO to validate them.

Presets ​

A bare name that isn't a type picks a preset, the columns shared across models. Add modifiers to change one (email:unique, price?).

GroupPresets
Names and textname, title, slug (unique), code (unique), sku (unique), reference (unique), description, notes, body, summary, first_name, last_name, middle_name, username (unique)
Contactemail, phone, mobile, website, url, image, avatar, logo
Authpassword, remember_token, email_verified_at, last_login_at
Statestatus (default active, indexed), type (indexed), priority (low … urgent), is_active (default true), is_featured, sort_order, color, rating
Money and quantitiesprice, cost, amount, subtotal, discount, tax, total, balance (all money), tax_rate, currency (default USD), quantity, stock, weight
Peoplegender, date_of_birth, national_id
Datesdate, due_date, starts_at, ends_at, published_at, paid_at
Otheruuid, ip_address, metadata, settings (json)
Column groupsaddress (address_line1, address_line2, city, state, postal_code, country), geo (latitude, longitude), person (first_name, last_name)

Optional-sounding presets (description, phone, image, dates such as due_date, …) are nullable. A name ending in _id without a type is a foreign key.

Your Own Templates ​

Put templates in .megh/templates/<domain>.json, one file per domain:

json
{
  "title": "Helpdesk",
  "description": "Support tickets",
  "templates": [
    {
      "name": "ticket",
      "description": "A support request",
      "fields": ["user_id:cascade", "title", "body", "priority", "status:in(open|pending|closed)"],
      "softDeletes": true
    },
    {
      "name": "ticket_tag",
      "fields": ["ticket_id:cascade", "tag_id:cascade"],
      "pivot": true
    }
  ]
}
sh
npx megh make:resource Ticket --template helpdesk/ticket
npx megh sc:domain helpdesk
KeyDescription
domainDomain name (default: the file name)
title, descriptionShown by template:list and the prompts
templates[].namesnake_case singular name
templates[].fieldsField specs, as for --fields
templates[].tableTable name (default: the plural name)
templates[].timestampscreated_at / updated_at (default true; false for a join table)
templates[].softDeletesdeleted_at (default false)
templates[].pivotA join table: no id, the first two foreign keys are the primary key, and no model is generated
templates[].uniqueComposite unique keys, e.g. [["customer_id", "product_id"]]

A file named after a built-in domain adds templates to it, and a template with the same name as a built-in one replaces it.

Released under the MIT License.