@meghjs/cli 0.5.2 → 0.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/migrate.d.ts +23 -5
- package/dist/commands/migrate.js +44 -6
- package/dist/commands/new.js +7 -1
- package/dist/generators/auth-files.d.ts +37 -0
- package/dist/generators/auth-files.js +198 -0
- package/dist/generators/auth.d.ts +26 -0
- package/dist/generators/auth.js +542 -0
- package/dist/generators/factory.d.ts +25 -0
- package/dist/generators/factory.js +159 -0
- package/dist/generators/make.d.ts +38 -0
- package/dist/generators/make.js +176 -0
- package/dist/generators/migration-schema.d.ts +5 -0
- package/dist/generators/migration-schema.js +25 -2
- package/dist/generators/orm-model.d.ts +6 -1
- package/dist/generators/orm-model.js +31 -2
- package/dist/generators/resource.d.ts +21 -0
- package/dist/generators/resource.js +17 -3
- package/dist/generators/scaffold.d.ts +76 -0
- package/dist/generators/scaffold.js +353 -0
- package/dist/generators/update-model.d.ts +21 -0
- package/dist/generators/update-model.js +129 -0
- package/dist/program.js +175 -6
- package/dist/templates/cascade.d.ts +37 -0
- package/dist/templates/cascade.js +87 -0
- package/dist/templates/catalog/common.d.ts +1 -0
- package/dist/templates/catalog/common.js +63 -0
- package/dist/templates/catalog/construction.d.ts +1 -0
- package/dist/templates/catalog/construction.js +138 -0
- package/dist/templates/catalog/crm.d.ts +1 -0
- package/dist/templates/catalog/crm.js +81 -0
- package/dist/templates/catalog/ecommerce.d.ts +1 -0
- package/dist/templates/catalog/ecommerce.js +143 -0
- package/dist/templates/catalog/education.d.ts +1 -0
- package/dist/templates/catalog/education.js +80 -0
- package/dist/templates/catalog/hr.d.ts +1 -0
- package/dist/templates/catalog/hr.js +83 -0
- package/dist/templates/catalog/logistics.d.ts +1 -0
- package/dist/templates/catalog/logistics.js +60 -0
- package/dist/templates/catalog/marketing.d.ts +1 -0
- package/dist/templates/catalog/marketing.js +104 -0
- package/dist/templates/catalog/medical.d.ts +1 -0
- package/dist/templates/catalog/medical.js +147 -0
- package/dist/templates/catalog/pos.d.ts +1 -0
- package/dist/templates/catalog/pos.js +110 -0
- package/dist/templates/catalog/real-estate.d.ts +1 -0
- package/dist/templates/catalog/real-estate.js +171 -0
- package/dist/templates/fields.d.ts +64 -0
- package/dist/templates/fields.js +331 -0
- package/dist/templates/index.d.ts +27 -0
- package/dist/templates/index.js +175 -0
- package/dist/templates/migration.d.ts +7 -0
- package/dist/templates/migration.js +80 -0
- package/dist/templates/template.d.ts +37 -0
- package/dist/templates/template.js +36 -0
- package/dist/utils/dialect.d.ts +8 -0
- package/dist/utils/dialect.js +57 -0
- package/package.json +17 -5
- package/readme.md +30 -1
- package/stubs/controllers/model-controller.stub +10 -1
- package/stubs/seeders/database-seeder.stub +8 -0
- package/stubs/seeders/seeder.stub +10 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.pos = void 0;
|
|
4
|
+
const template_1 = require("../template");
|
|
5
|
+
exports.pos = (0, template_1.domain)("pos", "Point of sale", "Shops and tills: outlets, shifts, sales, purchases and stock", [
|
|
6
|
+
["outlet", "A shop or branch", ["name", "code", "phone", "email?", "address?", "is_active"]],
|
|
7
|
+
["register", "A till in an outlet", ["outlet_id:cascade", "name", "is_active"]],
|
|
8
|
+
[
|
|
9
|
+
"shift",
|
|
10
|
+
"A cashier's shift on a register",
|
|
11
|
+
[
|
|
12
|
+
"register_id:cascade",
|
|
13
|
+
"user_id:nullOnDelete",
|
|
14
|
+
"opened_at:timestamp",
|
|
15
|
+
"closed_at?:timestamp",
|
|
16
|
+
"opening_cash:money:default(0)",
|
|
17
|
+
"closing_cash?:money",
|
|
18
|
+
"expected_cash?:money",
|
|
19
|
+
"status:in(open|closed)",
|
|
20
|
+
"notes",
|
|
21
|
+
],
|
|
22
|
+
],
|
|
23
|
+
["customer", "A walk-in or loyalty customer", ["name", "email?", "phone", "loyalty_points:integer:default(0)", "notes"]],
|
|
24
|
+
[
|
|
25
|
+
"supplier",
|
|
26
|
+
"A supplier stock is bought from",
|
|
27
|
+
["name", "contact_name?:string", "email?", "phone", "address?", "tax_number?:string(64)", "is_active"],
|
|
28
|
+
],
|
|
29
|
+
["tax_rate", "A sales tax", ["name", "rate:percent", "is_inclusive:boolean:default(false)", "is_active"]],
|
|
30
|
+
[
|
|
31
|
+
"product",
|
|
32
|
+
"An item sold at the till",
|
|
33
|
+
[
|
|
34
|
+
"category_id:nullOnDelete",
|
|
35
|
+
"name",
|
|
36
|
+
"sku",
|
|
37
|
+
"barcode?:string(64):unique",
|
|
38
|
+
"price",
|
|
39
|
+
"cost",
|
|
40
|
+
"stock",
|
|
41
|
+
"reorder_level:integer:default(0)",
|
|
42
|
+
"tax_rate_id:nullOnDelete",
|
|
43
|
+
"is_active",
|
|
44
|
+
],
|
|
45
|
+
],
|
|
46
|
+
[
|
|
47
|
+
"sale",
|
|
48
|
+
"A completed sale",
|
|
49
|
+
[
|
|
50
|
+
"outlet_id:restrict",
|
|
51
|
+
"register_id:nullOnDelete",
|
|
52
|
+
"shift_id:nullOnDelete",
|
|
53
|
+
"customer_id:nullOnDelete",
|
|
54
|
+
"user_id:nullOnDelete",
|
|
55
|
+
"number:string(32):unique",
|
|
56
|
+
"subtotal",
|
|
57
|
+
"discount",
|
|
58
|
+
"tax",
|
|
59
|
+
"total",
|
|
60
|
+
"paid_amount:money:default(0)",
|
|
61
|
+
"change_amount:money:default(0)",
|
|
62
|
+
"status:in(completed|refunded|void)",
|
|
63
|
+
"sold_at:timestamp",
|
|
64
|
+
],
|
|
65
|
+
],
|
|
66
|
+
[
|
|
67
|
+
"sale_item",
|
|
68
|
+
"A line of a sale",
|
|
69
|
+
["sale_id:cascade", "product_id:nullOnDelete", "name", "quantity", "price", "discount", "tax", "total"],
|
|
70
|
+
],
|
|
71
|
+
[
|
|
72
|
+
"sale_payment",
|
|
73
|
+
"A payment towards a sale (cash, card, ...)",
|
|
74
|
+
["sale_id:cascade", "method:string(32):default(cash)", "amount", "reference?:string(64)"],
|
|
75
|
+
],
|
|
76
|
+
[
|
|
77
|
+
"purchase",
|
|
78
|
+
"A purchase order to a supplier",
|
|
79
|
+
[
|
|
80
|
+
"supplier_id:restrict",
|
|
81
|
+
"outlet_id:nullOnDelete",
|
|
82
|
+
"number:string(32):unique",
|
|
83
|
+
"status:in(draft|ordered|received|cancelled)",
|
|
84
|
+
"subtotal",
|
|
85
|
+
"tax",
|
|
86
|
+
"total",
|
|
87
|
+
"ordered_at?:timestamp",
|
|
88
|
+
"received_at?:timestamp",
|
|
89
|
+
"notes",
|
|
90
|
+
],
|
|
91
|
+
],
|
|
92
|
+
[
|
|
93
|
+
"purchase_item",
|
|
94
|
+
"A line of a purchase order",
|
|
95
|
+
["purchase_id:cascade", "product_id:restrict", "quantity:integer", "unit_cost:money", "total"],
|
|
96
|
+
],
|
|
97
|
+
[
|
|
98
|
+
"stock_movement",
|
|
99
|
+
"A change in stock: sale, purchase, adjustment or transfer",
|
|
100
|
+
[
|
|
101
|
+
"product_id:cascade",
|
|
102
|
+
"outlet_id:nullOnDelete",
|
|
103
|
+
"user_id:nullOnDelete",
|
|
104
|
+
"type:in(in|out|adjustment|transfer|sale|purchase)",
|
|
105
|
+
"quantity:integer",
|
|
106
|
+
"reference?:string(64)",
|
|
107
|
+
"notes",
|
|
108
|
+
],
|
|
109
|
+
],
|
|
110
|
+
]);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const realEstate: import("../template").Domain;
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.realEstate = void 0;
|
|
4
|
+
const template_1 = require("../template");
|
|
5
|
+
exports.realEstate = (0, template_1.domain)("real-estate", "Real estate", "Properties, listings, agents, tenants, leases and rent", [
|
|
6
|
+
[
|
|
7
|
+
"agent",
|
|
8
|
+
"A real estate agent",
|
|
9
|
+
[
|
|
10
|
+
"user_id:nullOnDelete",
|
|
11
|
+
"person",
|
|
12
|
+
"email:unique",
|
|
13
|
+
"phone",
|
|
14
|
+
"license_number?:string(64)",
|
|
15
|
+
"agency?:string",
|
|
16
|
+
"commission_rate?:percent",
|
|
17
|
+
"avatar",
|
|
18
|
+
"is_active",
|
|
19
|
+
],
|
|
20
|
+
],
|
|
21
|
+
[
|
|
22
|
+
"property",
|
|
23
|
+
"A building or plot",
|
|
24
|
+
[
|
|
25
|
+
"agent_id:nullOnDelete",
|
|
26
|
+
"title",
|
|
27
|
+
"slug",
|
|
28
|
+
"description",
|
|
29
|
+
"type:in(apartment|house|condo|townhouse|land|commercial|office)",
|
|
30
|
+
"status:in(available|under_contract|sold|rented|off_market)",
|
|
31
|
+
"address",
|
|
32
|
+
"geo",
|
|
33
|
+
"bedrooms:integer:default(0)",
|
|
34
|
+
"bathrooms:decimal(3,1):default(0)",
|
|
35
|
+
"area?:decimal(10,2)",
|
|
36
|
+
"area_unit:string(8):default(sqft)",
|
|
37
|
+
"lot_size?:decimal(12,2)",
|
|
38
|
+
"year_built?:integer",
|
|
39
|
+
"price?",
|
|
40
|
+
"currency",
|
|
41
|
+
"is_featured",
|
|
42
|
+
],
|
|
43
|
+
{ table: "properties", softDeletes: true },
|
|
44
|
+
],
|
|
45
|
+
[
|
|
46
|
+
"unit",
|
|
47
|
+
"A rentable unit of a property",
|
|
48
|
+
[
|
|
49
|
+
"property_id:cascade",
|
|
50
|
+
"unit_number:string(32)",
|
|
51
|
+
"floor?:integer",
|
|
52
|
+
"bedrooms:integer:default(0)",
|
|
53
|
+
"bathrooms:decimal(3,1):default(0)",
|
|
54
|
+
"area?:decimal(10,2)",
|
|
55
|
+
"rent?:money",
|
|
56
|
+
"status:in(vacant|occupied|reserved|maintenance)",
|
|
57
|
+
],
|
|
58
|
+
{ unique: [["property_id", "unit_number"]] },
|
|
59
|
+
],
|
|
60
|
+
["amenity", "A feature such as a pool or parking", ["name", "slug", "icon?:string(64)", "category?:string(64)"]],
|
|
61
|
+
[
|
|
62
|
+
"property_amenity",
|
|
63
|
+
"Which amenities each property has",
|
|
64
|
+
["property_id:cascade", "amenity_id:cascade"],
|
|
65
|
+
{ pivot: true },
|
|
66
|
+
],
|
|
67
|
+
[
|
|
68
|
+
"listing",
|
|
69
|
+
"A property offered for sale or rent",
|
|
70
|
+
[
|
|
71
|
+
"property_id:cascade",
|
|
72
|
+
"agent_id:nullOnDelete",
|
|
73
|
+
"type:in(sale|rent)",
|
|
74
|
+
"price",
|
|
75
|
+
"currency",
|
|
76
|
+
"status:in(draft|active|pending|closed|expired)",
|
|
77
|
+
"listed_at?:timestamp",
|
|
78
|
+
"expires_at?:timestamp",
|
|
79
|
+
"description",
|
|
80
|
+
"views:integer:default(0)",
|
|
81
|
+
],
|
|
82
|
+
],
|
|
83
|
+
[
|
|
84
|
+
"tenant",
|
|
85
|
+
"A person renting a unit",
|
|
86
|
+
[
|
|
87
|
+
"user_id:nullOnDelete",
|
|
88
|
+
"person",
|
|
89
|
+
"email:unique",
|
|
90
|
+
"phone",
|
|
91
|
+
"date_of_birth",
|
|
92
|
+
"national_id",
|
|
93
|
+
"employer?:string",
|
|
94
|
+
"monthly_income?:money",
|
|
95
|
+
"emergency_contact_name?:string",
|
|
96
|
+
"emergency_contact_phone?:phone",
|
|
97
|
+
],
|
|
98
|
+
],
|
|
99
|
+
[
|
|
100
|
+
"lease",
|
|
101
|
+
"A rental agreement for a unit",
|
|
102
|
+
[
|
|
103
|
+
"unit_id:restrict",
|
|
104
|
+
"tenant_id:restrict",
|
|
105
|
+
"start_date:date",
|
|
106
|
+
"end_date:date",
|
|
107
|
+
"rent_amount:money",
|
|
108
|
+
"deposit_amount:money:default(0)",
|
|
109
|
+
"billing_day:integer:default(1)",
|
|
110
|
+
"status:in(draft|active|ended|terminated)",
|
|
111
|
+
"terms?:text",
|
|
112
|
+
"signed_at?:timestamp",
|
|
113
|
+
],
|
|
114
|
+
],
|
|
115
|
+
[
|
|
116
|
+
"rent_payment",
|
|
117
|
+
"A rent payment for a lease",
|
|
118
|
+
[
|
|
119
|
+
"lease_id:cascade",
|
|
120
|
+
"amount",
|
|
121
|
+
"due_date:date",
|
|
122
|
+
"paid_at",
|
|
123
|
+
"method?:string(32)",
|
|
124
|
+
"reference?:string(64)",
|
|
125
|
+
"status:in(pending|paid|late|partial|failed)",
|
|
126
|
+
"late_fee:money:default(0)",
|
|
127
|
+
],
|
|
128
|
+
],
|
|
129
|
+
[
|
|
130
|
+
"inquiry",
|
|
131
|
+
"A question about a property",
|
|
132
|
+
[
|
|
133
|
+
"property_id:cascade",
|
|
134
|
+
"listing_id:nullOnDelete",
|
|
135
|
+
"agent_id:nullOnDelete",
|
|
136
|
+
"name",
|
|
137
|
+
"email",
|
|
138
|
+
"phone",
|
|
139
|
+
"message:text",
|
|
140
|
+
"status:in(new|contacted|scheduled|closed)",
|
|
141
|
+
],
|
|
142
|
+
{ table: "inquiries" },
|
|
143
|
+
],
|
|
144
|
+
[
|
|
145
|
+
"viewing",
|
|
146
|
+
"A scheduled property viewing",
|
|
147
|
+
[
|
|
148
|
+
"property_id:cascade",
|
|
149
|
+
"inquiry_id:foreign(inquiries):nullOnDelete",
|
|
150
|
+
"agent_id:nullOnDelete",
|
|
151
|
+
"scheduled_at:timestamp",
|
|
152
|
+
"status:in(scheduled|completed|cancelled|no_show)",
|
|
153
|
+
"feedback?:text",
|
|
154
|
+
],
|
|
155
|
+
],
|
|
156
|
+
[
|
|
157
|
+
"maintenance_request",
|
|
158
|
+
"A repair requested for a unit",
|
|
159
|
+
[
|
|
160
|
+
"unit_id:cascade",
|
|
161
|
+
"tenant_id:nullOnDelete",
|
|
162
|
+
"title",
|
|
163
|
+
"description",
|
|
164
|
+
"priority",
|
|
165
|
+
"status:in(open|in_progress|resolved|closed)",
|
|
166
|
+
"reported_at?:timestamp",
|
|
167
|
+
"resolved_at?:timestamp",
|
|
168
|
+
"cost",
|
|
169
|
+
],
|
|
170
|
+
],
|
|
171
|
+
]);
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The fields library: column definitions shared by the model templates and
|
|
3
|
+
* the `--fields` option.
|
|
4
|
+
*
|
|
5
|
+
* A field is written as a short spec, `name[?][:type[(args)]][:modifier]...`:
|
|
6
|
+
*
|
|
7
|
+
* title a preset (see FIELD_PRESETS)
|
|
8
|
+
* email:unique a preset with an extra modifier
|
|
9
|
+
* address? a preset group, every column nullable
|
|
10
|
+
* nickname?:string(50) an explicit column, nullable
|
|
11
|
+
* price:money a type alias: decimal(12,2)
|
|
12
|
+
* status:in(draft|published) the status preset limited to these values
|
|
13
|
+
* category_id a foreign key to `categories` (from the name)
|
|
14
|
+
* owner_id:foreign(users):cascade
|
|
15
|
+
*
|
|
16
|
+
* Modifiers: `nullable`, `unique`, `index`, `default(value)`, `in(a|b|c)`,
|
|
17
|
+
* and for foreign keys `cascade`, `nullOnDelete` and `restrict`.
|
|
18
|
+
*/
|
|
19
|
+
/** Megh schema builder column methods a field can use. */
|
|
20
|
+
export declare const COLUMN_TYPES: readonly ["string", "text", "integer", "bigInteger", "decimal", "float", "double", "boolean", "date", "datetime", "timestamp", "time", "json", "uuid", "foreignId"];
|
|
21
|
+
export type ColumnType = (typeof COLUMN_TYPES)[number];
|
|
22
|
+
export type OnDelete = "cascade" | "set null" | "restrict";
|
|
23
|
+
export interface FieldDef {
|
|
24
|
+
name: string;
|
|
25
|
+
type: ColumnType;
|
|
26
|
+
/** `string(length)`. */
|
|
27
|
+
length?: number;
|
|
28
|
+
/** `decimal(precision, scale)`. */
|
|
29
|
+
precision?: number;
|
|
30
|
+
scale?: number;
|
|
31
|
+
nullable?: boolean;
|
|
32
|
+
unique?: boolean;
|
|
33
|
+
index?: boolean;
|
|
34
|
+
default?: string | number | boolean;
|
|
35
|
+
/** Allowed values, written next to the column (the column itself is a string). */
|
|
36
|
+
enum?: string[];
|
|
37
|
+
/** Foreign key target, for `foreignId` columns. */
|
|
38
|
+
references?: {
|
|
39
|
+
table: string;
|
|
40
|
+
onDelete?: OnDelete;
|
|
41
|
+
};
|
|
42
|
+
/** Written next to the column in the migration. */
|
|
43
|
+
comment?: string;
|
|
44
|
+
}
|
|
45
|
+
type Preset = Omit<FieldDef, "name"> & {
|
|
46
|
+
name?: string;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Columns used across models, by the name they are written as. A preset is
|
|
50
|
+
* one column, or a group of columns (`address`, `geo`, `person`).
|
|
51
|
+
*/
|
|
52
|
+
export declare const FIELD_PRESETS: Record<string, Preset | Record<string, Preset>>;
|
|
53
|
+
/** `category_id` → `categories`. */
|
|
54
|
+
export declare function foreignTable(column: string): string;
|
|
55
|
+
/**
|
|
56
|
+
* Parse one field spec into its columns (a preset group gives several).
|
|
57
|
+
* Throws a CliError explaining what is wrong.
|
|
58
|
+
*/
|
|
59
|
+
export declare function parseFieldSpec(spec: string): FieldDef[];
|
|
60
|
+
/** Parse a comma-separated `--fields` list. */
|
|
61
|
+
export declare function parseFieldList(list: string | string[]): FieldDef[];
|
|
62
|
+
/** `base` with `extra` fields added; a field of the same name replaces the template's. */
|
|
63
|
+
export declare function mergeFields(base: FieldDef[], extra: FieldDef[]): FieldDef[];
|
|
64
|
+
export {};
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FIELD_PRESETS = exports.COLUMN_TYPES = void 0;
|
|
4
|
+
exports.foreignTable = foreignTable;
|
|
5
|
+
exports.parseFieldSpec = parseFieldSpec;
|
|
6
|
+
exports.parseFieldList = parseFieldList;
|
|
7
|
+
exports.mergeFields = mergeFields;
|
|
8
|
+
const errors_1 = require("../errors");
|
|
9
|
+
const orm_model_1 = require("../generators/orm-model");
|
|
10
|
+
/**
|
|
11
|
+
* The fields library: column definitions shared by the model templates and
|
|
12
|
+
* the `--fields` option.
|
|
13
|
+
*
|
|
14
|
+
* A field is written as a short spec, `name[?][:type[(args)]][:modifier]...`:
|
|
15
|
+
*
|
|
16
|
+
* title a preset (see FIELD_PRESETS)
|
|
17
|
+
* email:unique a preset with an extra modifier
|
|
18
|
+
* address? a preset group, every column nullable
|
|
19
|
+
* nickname?:string(50) an explicit column, nullable
|
|
20
|
+
* price:money a type alias: decimal(12,2)
|
|
21
|
+
* status:in(draft|published) the status preset limited to these values
|
|
22
|
+
* category_id a foreign key to `categories` (from the name)
|
|
23
|
+
* owner_id:foreign(users):cascade
|
|
24
|
+
*
|
|
25
|
+
* Modifiers: `nullable`, `unique`, `index`, `default(value)`, `in(a|b|c)`,
|
|
26
|
+
* and for foreign keys `cascade`, `nullOnDelete` and `restrict`.
|
|
27
|
+
*/
|
|
28
|
+
/** Megh schema builder column methods a field can use. */
|
|
29
|
+
exports.COLUMN_TYPES = [
|
|
30
|
+
"string",
|
|
31
|
+
"text",
|
|
32
|
+
"integer",
|
|
33
|
+
"bigInteger",
|
|
34
|
+
"decimal",
|
|
35
|
+
"float",
|
|
36
|
+
"double",
|
|
37
|
+
"boolean",
|
|
38
|
+
"date",
|
|
39
|
+
"datetime",
|
|
40
|
+
"timestamp",
|
|
41
|
+
"time",
|
|
42
|
+
"json",
|
|
43
|
+
"uuid",
|
|
44
|
+
"foreignId",
|
|
45
|
+
];
|
|
46
|
+
/** Shorthand types: `price:money` is `decimal(12,2)`. */
|
|
47
|
+
const TYPE_ALIASES = {
|
|
48
|
+
foreign: { type: "foreignId" },
|
|
49
|
+
bigint: { type: "bigInteger" },
|
|
50
|
+
int: { type: "integer" },
|
|
51
|
+
bool: { type: "boolean" },
|
|
52
|
+
money: { type: "decimal", precision: 12, scale: 2 },
|
|
53
|
+
percent: { type: "decimal", precision: 5, scale: 2 },
|
|
54
|
+
email: { type: "string", length: 255 },
|
|
55
|
+
phone: { type: "string", length: 32 },
|
|
56
|
+
url: { type: "string", length: 2048 },
|
|
57
|
+
};
|
|
58
|
+
const str = (length = 255, extra = {}) => ({ type: "string", length, ...extra });
|
|
59
|
+
const money = (extra = {}) => ({ type: "decimal", precision: 12, scale: 2, ...extra });
|
|
60
|
+
const opt = (preset) => ({ ...preset, nullable: true });
|
|
61
|
+
/**
|
|
62
|
+
* Columns used across models, by the name they are written as. A preset is
|
|
63
|
+
* one column, or a group of columns (`address`, `geo`, `person`).
|
|
64
|
+
*/
|
|
65
|
+
exports.FIELD_PRESETS = {
|
|
66
|
+
// Names and text
|
|
67
|
+
name: str(),
|
|
68
|
+
title: str(),
|
|
69
|
+
slug: str(255, { unique: true }),
|
|
70
|
+
code: str(32, { unique: true }),
|
|
71
|
+
sku: str(64, { unique: true }),
|
|
72
|
+
reference: str(64, { unique: true }),
|
|
73
|
+
description: opt({ type: "text" }),
|
|
74
|
+
notes: opt({ type: "text" }),
|
|
75
|
+
body: { type: "text" },
|
|
76
|
+
summary: opt(str(500)),
|
|
77
|
+
first_name: str(100),
|
|
78
|
+
last_name: str(100),
|
|
79
|
+
middle_name: opt(str(100)),
|
|
80
|
+
username: str(64, { unique: true }),
|
|
81
|
+
// Contact
|
|
82
|
+
email: str(255),
|
|
83
|
+
phone: opt(str(32)),
|
|
84
|
+
mobile: opt(str(32)),
|
|
85
|
+
website: opt(str(2048)),
|
|
86
|
+
url: opt(str(2048)),
|
|
87
|
+
image: opt(str(2048)),
|
|
88
|
+
avatar: opt(str(2048)),
|
|
89
|
+
logo: opt(str(2048)),
|
|
90
|
+
// Auth
|
|
91
|
+
password: str(255),
|
|
92
|
+
remember_token: opt(str(100)),
|
|
93
|
+
email_verified_at: opt({ type: "timestamp" }),
|
|
94
|
+
last_login_at: opt({ type: "timestamp" }),
|
|
95
|
+
// State
|
|
96
|
+
status: str(32, { default: "active", index: true }),
|
|
97
|
+
type: str(32, { index: true }),
|
|
98
|
+
priority: str(16, { default: "normal", enum: ["low", "normal", "high", "urgent"] }),
|
|
99
|
+
is_active: { type: "boolean", default: true },
|
|
100
|
+
is_featured: { type: "boolean", default: false },
|
|
101
|
+
sort_order: { type: "integer", default: 0 },
|
|
102
|
+
color: opt(str(32)),
|
|
103
|
+
rating: { type: "integer", default: 0 },
|
|
104
|
+
// Money and quantities
|
|
105
|
+
price: money(),
|
|
106
|
+
cost: money({ nullable: true }),
|
|
107
|
+
amount: money(),
|
|
108
|
+
subtotal: money({ default: 0 }),
|
|
109
|
+
discount: money({ default: 0 }),
|
|
110
|
+
tax: money({ default: 0 }),
|
|
111
|
+
total: money({ default: 0 }),
|
|
112
|
+
balance: money({ default: 0 }),
|
|
113
|
+
tax_rate: { type: "decimal", precision: 5, scale: 2, default: 0 },
|
|
114
|
+
currency: str(3, { default: "USD" }),
|
|
115
|
+
quantity: { type: "integer", default: 1 },
|
|
116
|
+
stock: { type: "integer", default: 0 },
|
|
117
|
+
weight: { type: "decimal", precision: 10, scale: 3, nullable: true },
|
|
118
|
+
// People
|
|
119
|
+
gender: opt(str(16, { enum: ["male", "female", "other"] })),
|
|
120
|
+
date_of_birth: opt({ type: "date" }),
|
|
121
|
+
national_id: opt(str(64)),
|
|
122
|
+
// Dates
|
|
123
|
+
date: { type: "date" },
|
|
124
|
+
due_date: opt({ type: "date" }),
|
|
125
|
+
starts_at: opt({ type: "timestamp" }),
|
|
126
|
+
ends_at: opt({ type: "timestamp" }),
|
|
127
|
+
published_at: opt({ type: "timestamp" }),
|
|
128
|
+
paid_at: opt({ type: "timestamp" }),
|
|
129
|
+
// Other
|
|
130
|
+
uuid: { type: "uuid", unique: true },
|
|
131
|
+
ip_address: opt(str(45)),
|
|
132
|
+
metadata: opt({ type: "json" }),
|
|
133
|
+
settings: opt({ type: "json" }),
|
|
134
|
+
// Groups
|
|
135
|
+
address: {
|
|
136
|
+
address_line1: str(),
|
|
137
|
+
address_line2: opt(str()),
|
|
138
|
+
city: str(100),
|
|
139
|
+
state: opt(str(100)),
|
|
140
|
+
postal_code: opt(str(20)),
|
|
141
|
+
country: str(100),
|
|
142
|
+
},
|
|
143
|
+
geo: {
|
|
144
|
+
latitude: { type: "decimal", precision: 10, scale: 7, nullable: true },
|
|
145
|
+
longitude: { type: "decimal", precision: 10, scale: 7, nullable: true },
|
|
146
|
+
},
|
|
147
|
+
person: {
|
|
148
|
+
first_name: str(100),
|
|
149
|
+
last_name: str(100),
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
const isGroup = (p) => typeof p.type !== "string";
|
|
153
|
+
/** Split on `sep` outside parentheses. */
|
|
154
|
+
function splitTop(spec, sep) {
|
|
155
|
+
const out = [];
|
|
156
|
+
let depth = 0;
|
|
157
|
+
let start = 0;
|
|
158
|
+
for (let i = 0; i < spec.length; i++) {
|
|
159
|
+
const ch = spec[i];
|
|
160
|
+
if (ch === "(")
|
|
161
|
+
depth++;
|
|
162
|
+
else if (ch === ")")
|
|
163
|
+
depth--;
|
|
164
|
+
else if (ch === sep && depth === 0) {
|
|
165
|
+
out.push(spec.slice(start, i).trim());
|
|
166
|
+
start = i + 1;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
out.push(spec.slice(start).trim());
|
|
170
|
+
return out.filter((s) => s !== "");
|
|
171
|
+
}
|
|
172
|
+
/** `money(10, 2)` → `{ head: "money", args: ["10", "2"] }`. */
|
|
173
|
+
function call(segment) {
|
|
174
|
+
const m = segment.match(/^(\w+)\s*(?:\((.*)\))?$/s);
|
|
175
|
+
if (!m)
|
|
176
|
+
throw new errors_1.CliError(`Invalid field part "${segment}".`);
|
|
177
|
+
return { head: m[1], args: m[2] === undefined ? [] : m[2].split(/[,|]/).map((a) => a.trim()) };
|
|
178
|
+
}
|
|
179
|
+
function parseValue(raw) {
|
|
180
|
+
if (raw === "true")
|
|
181
|
+
return true;
|
|
182
|
+
if (raw === "false")
|
|
183
|
+
return false;
|
|
184
|
+
if (/^-?\d+(\.\d+)?$/.test(raw))
|
|
185
|
+
return Number(raw);
|
|
186
|
+
return raw.replace(/^["']|["']$/g, "");
|
|
187
|
+
}
|
|
188
|
+
/** Model instance members a column would shadow. */
|
|
189
|
+
const RESERVED = new Set(["key", "save", "fill", "delete", "restore", "related", "constructor"]);
|
|
190
|
+
const MODIFIERS = new Set(["nullable", "unique", "index", "default", "in", "cascade", "nullOnDelete", "restrict"]);
|
|
191
|
+
function applyModifier(field, segment, spec) {
|
|
192
|
+
const { head, args } = call(segment);
|
|
193
|
+
switch (head) {
|
|
194
|
+
case "nullable":
|
|
195
|
+
field.nullable = true;
|
|
196
|
+
return;
|
|
197
|
+
case "unique":
|
|
198
|
+
field.unique = true;
|
|
199
|
+
return;
|
|
200
|
+
case "index":
|
|
201
|
+
field.index = true;
|
|
202
|
+
return;
|
|
203
|
+
case "default":
|
|
204
|
+
if (args.length === 0)
|
|
205
|
+
throw new errors_1.CliError(`"${spec}": default needs a value, e.g. default(0).`);
|
|
206
|
+
field.default = parseValue(args.join(","));
|
|
207
|
+
return;
|
|
208
|
+
case "in":
|
|
209
|
+
if (args.length === 0)
|
|
210
|
+
throw new errors_1.CliError(`"${spec}": in needs values, e.g. in(draft|published).`);
|
|
211
|
+
field.enum = args;
|
|
212
|
+
if (field.type !== "string")
|
|
213
|
+
Object.assign(field, { type: "string", length: 32 });
|
|
214
|
+
return;
|
|
215
|
+
case "cascade":
|
|
216
|
+
case "nullOnDelete":
|
|
217
|
+
case "restrict": {
|
|
218
|
+
if (field.type !== "foreignId")
|
|
219
|
+
throw new errors_1.CliError(`"${spec}": ${head} only applies to foreign keys.`);
|
|
220
|
+
const onDelete = head === "cascade" ? "cascade" : head === "restrict" ? "restrict" : "set null";
|
|
221
|
+
field.references = { ...field.references, onDelete };
|
|
222
|
+
if (onDelete === "set null")
|
|
223
|
+
field.nullable = true;
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
default:
|
|
227
|
+
throw new errors_1.CliError(`"${spec}": unknown modifier "${head}". Use nullable, unique, index, default(x), in(a|b), cascade, nullOnDelete or restrict.`);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
/** The column for `name:type(args)`. */
|
|
231
|
+
function typedField(name, segment, spec) {
|
|
232
|
+
const { head, args } = call(segment);
|
|
233
|
+
const alias = TYPE_ALIASES[head];
|
|
234
|
+
const type = (alias?.type ?? head);
|
|
235
|
+
if (!alias && !exports.COLUMN_TYPES.includes(head)) {
|
|
236
|
+
throw new errors_1.CliError(`"${spec}": unknown type "${head}". Use one of: ${[...exports.COLUMN_TYPES, ...Object.keys(TYPE_ALIASES)].join(", ")}.`);
|
|
237
|
+
}
|
|
238
|
+
const field = { ...alias, name, type };
|
|
239
|
+
const num = (i) => {
|
|
240
|
+
const n = Number(args[i]);
|
|
241
|
+
if (!Number.isInteger(n) || n <= 0)
|
|
242
|
+
throw new errors_1.CliError(`"${spec}": "${args[i]}" is not a positive integer.`);
|
|
243
|
+
return n;
|
|
244
|
+
};
|
|
245
|
+
if (type === "string" && args.length)
|
|
246
|
+
field.length = num(0);
|
|
247
|
+
if (type === "decimal" && args.length) {
|
|
248
|
+
field.precision = num(0);
|
|
249
|
+
field.scale = args[1] === undefined ? 2 : Number(args[1]);
|
|
250
|
+
}
|
|
251
|
+
if (type === "foreignId") {
|
|
252
|
+
field.references = { table: args[0] || foreignTable(name) };
|
|
253
|
+
}
|
|
254
|
+
return field;
|
|
255
|
+
}
|
|
256
|
+
/** `category_id` → `categories`. */
|
|
257
|
+
function foreignTable(column) {
|
|
258
|
+
return (0, orm_model_1.pluralize)(column.replace(/_id$/, ""));
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Parse one field spec into its columns (a preset group gives several).
|
|
262
|
+
* Throws a CliError explaining what is wrong.
|
|
263
|
+
*/
|
|
264
|
+
function parseFieldSpec(spec) {
|
|
265
|
+
const parts = splitTop(spec.trim(), ":");
|
|
266
|
+
if (parts.length === 0)
|
|
267
|
+
throw new errors_1.CliError("Empty field spec.");
|
|
268
|
+
const rawName = parts[0];
|
|
269
|
+
const nullable = rawName.endsWith("?");
|
|
270
|
+
const name = rawName.replace(/\?$/, "");
|
|
271
|
+
if (!/^[a-z_][a-z0-9_]*$/.test(name)) {
|
|
272
|
+
throw new errors_1.CliError(`"${spec}": field names are snake_case, e.g. first_name.`);
|
|
273
|
+
}
|
|
274
|
+
if (RESERVED.has(name)) {
|
|
275
|
+
throw new errors_1.CliError(`"${spec}": "${name}" is a Model method, so it can't be a column. Use another name, e.g. ${name}_value.`);
|
|
276
|
+
}
|
|
277
|
+
let rest = parts.slice(1);
|
|
278
|
+
let fields;
|
|
279
|
+
const next = rest[0] ? call(rest[0]).head : undefined;
|
|
280
|
+
if (next && !MODIFIERS.has(next)) {
|
|
281
|
+
fields = [typedField(name, rest[0], spec)];
|
|
282
|
+
rest = rest.slice(1);
|
|
283
|
+
}
|
|
284
|
+
else if (exports.FIELD_PRESETS[name]) {
|
|
285
|
+
const preset = exports.FIELD_PRESETS[name];
|
|
286
|
+
fields = isGroup(preset)
|
|
287
|
+
? Object.entries(preset).map(([n, p]) => ({ ...p, name: n }))
|
|
288
|
+
: [{ ...preset, name }];
|
|
289
|
+
}
|
|
290
|
+
else if (/_id$/.test(name)) {
|
|
291
|
+
fields = [{ name, type: "foreignId", references: { table: foreignTable(name) } }];
|
|
292
|
+
}
|
|
293
|
+
else if (rest.some((s) => call(s).head === "in")) {
|
|
294
|
+
// `kind:in(a|b)` needs no type: a short string column.
|
|
295
|
+
fields = [{ name, type: "string", length: 32 }];
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
throw new errors_1.CliError(`"${spec}": "${name}" is not a field preset, so give it a type, e.g. ${name}:string.`);
|
|
299
|
+
}
|
|
300
|
+
const explicitDefault = rest.some((s) => call(s).head === "default");
|
|
301
|
+
for (const field of fields) {
|
|
302
|
+
if (nullable)
|
|
303
|
+
field.nullable = true;
|
|
304
|
+
for (const segment of rest)
|
|
305
|
+
applyModifier(field, segment, spec);
|
|
306
|
+
// A preset's default (status: "active") becomes the first allowed value,
|
|
307
|
+
// as does an explicit default outside them.
|
|
308
|
+
if (field.enum && field.default !== undefined) {
|
|
309
|
+
if (!explicitDefault || !field.enum.includes(String(field.default)))
|
|
310
|
+
field.default = field.enum[0];
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return fields;
|
|
314
|
+
}
|
|
315
|
+
/** Parse a comma-separated `--fields` list. */
|
|
316
|
+
function parseFieldList(list) {
|
|
317
|
+
const specs = Array.isArray(list) ? list : splitTop(list, ",");
|
|
318
|
+
return specs.flatMap((s) => splitTop(s, ",")).flatMap(parseFieldSpec);
|
|
319
|
+
}
|
|
320
|
+
/** `base` with `extra` fields added; a field of the same name replaces the template's. */
|
|
321
|
+
function mergeFields(base, extra) {
|
|
322
|
+
const out = base.map((f) => ({ ...f }));
|
|
323
|
+
for (const field of extra) {
|
|
324
|
+
const i = out.findIndex((f) => f.name === field.name);
|
|
325
|
+
if (i === -1)
|
|
326
|
+
out.push(field);
|
|
327
|
+
else
|
|
328
|
+
out[i] = field;
|
|
329
|
+
}
|
|
330
|
+
return out;
|
|
331
|
+
}
|