@currentjs/gen 0.1.2 → 0.2.1

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.
@@ -46,15 +46,18 @@ const packageJsonTemplate = (appName) => JSON.stringify({
46
46
  start: 'node build/app.js',
47
47
  clean: 'rm -rf build',
48
48
  dev: 'ts-node src/app.ts',
49
- devstand: 'npm i && npm link @currentjs/router @currentjs/templating @currentjs/provider-mysql && npm run build'
49
+ // uncomment this if you want to use the devstand script
50
+ // devstand: 'npm i && npm link @currentjs/router @currentjs/templating @currentjs/provider-mysql && npm run build'
51
+ },
52
+ dependencies: {
53
+ '@currentjs/router': 'latest',
54
+ '@currentjs/templating': 'latest',
55
+ '@currentjs/provider-mysql': 'latest',
50
56
  },
51
- dependencies: {},
52
57
  devDependencies: {
53
58
  typescript: '^5.6.3',
54
59
  '@types/node': '^22.7.4',
55
60
  '@koz1024/path-fixer': '^0.2.1',
56
- // '@currentjs/router': '^0.1.0',
57
- // '@currentjs/templating': '^0.1.0'
58
61
  },
59
62
  type: 'module'
60
63
  }, null, 2);
@@ -1119,6 +1122,7 @@ models:
1119
1122
 
1120
1123
  api: # REST API configuration
1121
1124
  prefix: /api/posts # Base URL for API endpoints
1125
+ model: Post # Optional: which model this API serves (defaults to first model)
1122
1126
  endpoints:
1123
1127
  - method: GET # HTTP method
1124
1128
  path: / # Relative path (becomes /api/posts/)
@@ -1138,9 +1142,11 @@ api: # REST API configuration
1138
1142
  - method: POST # Custom endpoint
1139
1143
  path: /:id/publish
1140
1144
  action: publish
1145
+ model: Post # Optional: override model for specific endpoint
1141
1146
 
1142
1147
  routes: # Web interface configuration
1143
1148
  prefix: /posts # Base URL for web pages
1149
+ model: Post # Optional: which model this route serves (defaults to first model)
1144
1150
  strategy: [toast, back] # Default success strategies for forms
1145
1151
  endpoints:
1146
1152
  - path: / # List page
@@ -1155,41 +1161,40 @@ routes: # Web interface configuration
1155
1161
  - path: /:id/edit # Edit form page
1156
1162
  action: get # Load existing data
1157
1163
  view: postUpdate
1164
+ model: Post # Optional: override model for specific endpoint
1158
1165
 
1159
1166
  actions: # Business logic mapping
1160
1167
  list:
1161
- handlers: [default:list] # Use built-in list handler
1168
+ handlers: [Post:default:list] # Use built-in list handler
1162
1169
  get:
1163
- handlers: [default:getById] # Use built-in get handler
1170
+ handlers: [Post:default:get] # Use built-in get handler
1164
1171
  create:
1165
- handlers: [default:create] # Built-in create
1172
+ handlers: [Post:default:create] # Built-in create
1166
1173
  update:
1167
- handlers: [default:update]
1174
+ handlers: [Post:default:update]
1168
1175
  delete:
1169
1176
  handlers: [ # Chain multiple handlers
1170
- service:checkCanDelete, # Custom business logic
1171
- default:delete
1177
+ Post:checkCanDelete, # Custom business logic
1178
+ Post:default:delete
1172
1179
  ]
1173
1180
  publish: # Custom action
1174
1181
  handlers: [
1175
- default:getById, # Fetch entity
1176
- service:validateForPublish, # Custom validation
1177
- service:updatePublishStatus # Custom update logic
1182
+ Post:default:get, # Fetch entity
1183
+ Post:validateForPublish, # Custom validation
1184
+ Post:updatePublishStatus # Custom update logic
1178
1185
  ]
1179
1186
 
1180
1187
  permissions: # Role-based access control
1181
- - action: list
1182
- roles: [all] # Anyone (including anonymous)
1183
- - action: get
1184
- roles: [all] # Anyone can view
1185
- - action: create
1186
- roles: [authenticated] # Must be logged in
1187
- - action: update
1188
- roles: [owner, admin] # Entity owner or admin role
1189
- - action: delete
1190
- roles: [admin] # Only admin role
1191
- - action: publish
1192
- roles: [owner, editor, admin] # Multiple roles allowed
1188
+ - role: all
1189
+ actions: [list, get] # Anyone (including anonymous)
1190
+ - role: authenticated
1191
+ actions: [create] # Must be logged in
1192
+ - role: owner
1193
+ actions: [update, publish] # Entity owner permissions
1194
+ - role: admin
1195
+ actions: [update, delete, publish] # Admin role permissions
1196
+ - role: editor
1197
+ actions: [publish] # Editor role permissions
1193
1198
  \`\`\`
1194
1199
 
1195
1200
  **Make sure no \`ID\`/\`owner id\`/\`is deleted\` fields are present in the model definition, since it's added automatically**
@@ -1200,17 +1205,38 @@ permissions: # Role-based access control
1200
1205
  - \`boolean\` - True/false values
1201
1206
  - \`datetime\` - Date and time values
1202
1207
 
1208
+ **🔄 Handler vs Action Architecture:**
1209
+ - **Handler**: Creates a separate service method (one handler = one service method)
1210
+ - **Action**: Virtual controller concept that calls handler methods step-by-step
1211
+
1203
1212
  **Built-in Action Handlers:**
1204
- - \`default:list\` - Returns paginated list of entities
1205
- - \`default:getById\` - Fetches single entity by ID
1206
- - \`default:create\` - Creates new entity with validation
1207
- - \`default:update\` - Updates existing entity by ID
1208
- - \`default:delete\` - Soft deletes entity
1213
+ - \`ModelName:default:list\` - Creates service method with pagination parameters
1214
+ - \`ModelName:default:get\` - Creates service method named \`get\` with ID parameter
1215
+ - \`ModelName:default:create\` - Creates service method with DTO parameter
1216
+ - \`ModelName:default:update\` - Creates service method with ID and DTO parameters
1217
+ - \`ModelName:default:delete\` - Creates service method with ID parameter
1209
1218
 
1210
1219
  **Custom Action Handlers:**
1211
- - \`service:methodName\` - Calls custom method on service class
1212
- - Automatically generated with proper type signatures
1213
- - Can be chained with built-in handlers
1220
+ - \`ModelName:customMethodName\` - Creates service method that accepts \`result, context\` parameters
1221
+ - \`result\`: Result from previous handler (or \`null\` if it's the first handler)
1222
+ - \`context\`: The request context object
1223
+ - Each handler generates a separate method in the service
1224
+ - User can customize the implementation after generation
1225
+
1226
+ **🔗 Multiple Handlers per Action:**
1227
+ When an action has multiple handlers, each handler generates a separate service method, and the controller action calls them sequentially. The action returns the result from the last handler.
1228
+
1229
+ **Parameter Passing Rules:**
1230
+ - **Default handlers** (\`:default:\`): Receive standard parameters (id, pagination, DTO, etc.)
1231
+ - **Custom handlers**: Receive \`(result, context)\` where:
1232
+ - \`result\`: Result from previous handler, or \`null\` if it's the first handler
1233
+ - \`context\`: Request context object
1234
+
1235
+ **Handler Format Examples:**
1236
+ - \`Post:default:list\` - Creates Post service method \`list(page, limit)\`
1237
+ - \`Post:default:get\` - Creates Post service method \`get(id)\`
1238
+ - \`Post:validateContent\` - Creates Post service method \`validateContent(result, context)\`
1239
+ - \`Comment:notifySubscribers\` - Creates Comment service method \`notifySubscribers(result, context)\`
1214
1240
 
1215
1241
  **Strategy Options (for forms):**
1216
1242
  - \`toast\` - Success toast notification
@@ -1228,14 +1254,21 @@ permissions: # Role-based access control
1228
1254
  - Multiple roles can be specified for each action
1229
1255
 
1230
1256
  **Generated Files from Configuration:**
1231
- - Domain entity class
1232
- - Service class
1233
- - API controller with REST endpoints
1234
- - Web controller with page rendering
1235
- - Store class with database operations
1257
+ - Domain entity class (one per model)
1258
+ - Service class (one per model)
1259
+ - API controller with REST endpoints (one per model)
1260
+ - Web controller with page rendering (one per model)
1261
+ - Store class with database operations (one per model)
1236
1262
  - HTML templates for all views
1237
1263
  - TypeScript interfaces and DTOs
1238
1264
 
1265
+ **Multi-Model Support:**
1266
+ - Each model gets its own service, controller, and store
1267
+ - Use \`model\` parameter in \`api\` and \`routes\` to specify which model to use (defaults to first model)
1268
+ - Use \`model\` parameter on individual endpoints to override model for specific endpoints
1269
+ - Action handlers use \`modelname:action\` format to specify which model's service method to call
1270
+ - Controllers and services are generated per model, not per module
1271
+
1239
1272
  ## Module Structure
1240
1273
  \`\`\`
1241
1274
  src/modules/ModuleName/
@@ -5,6 +5,7 @@ export declare const serviceTemplates: {
5
5
  ownerPermissionCheck: string;
6
6
  defaultImplementations: {
7
7
  list: string;
8
+ get: string;
8
9
  getById: string;
9
10
  create: string;
10
11
  update: string;
@@ -22,6 +22,11 @@ exports.serviceTemplates = {
22
22
  defaultImplementations: {
23
23
  list: `const {{ENTITY_LOWER}}s = await this.{{ENTITY_LOWER}}Store.getAll(page, limit);
24
24
  return {{ENTITY_LOWER}}s;`,
25
+ get: `const {{ENTITY_LOWER}} = await this.{{ENTITY_LOWER}}Store.getById(id);
26
+ if (!{{ENTITY_LOWER}}) {
27
+ throw new Error('{{ENTITY_NAME}} not found');
28
+ }
29
+ return {{ENTITY_LOWER}};`,
25
30
  getById: `const {{ENTITY_LOWER}} = await this.{{ENTITY_LOWER}}Store.getById(id);
26
31
  if (!{{ENTITY_LOWER}}) {
27
32
  throw new Error('{{ENTITY_NAME}} not found');
package/howto.md CHANGED
@@ -114,38 +114,36 @@ routes: # Web interface configuration
114
114
 
115
115
  actions: # Business logic mapping
116
116
  list:
117
- handlers: [default:list] # Use built-in list handler
117
+ handlers: [Post:default:list] # Use built-in list handler
118
118
  get:
119
- handlers: [default:getById] # Use built-in get handler
119
+ handlers: [Post:default:get] # Use built-in get handler
120
120
  create:
121
- handlers: [default:create] # Built-in create
121
+ handlers: [Post:default:create] # Built-in create
122
122
  update:
123
- handlers: [default:update]
123
+ handlers: [Post:default:update]
124
124
  delete:
125
125
  handlers: [ # Chain multiple handlers
126
- service:checkCanDelete, # Custom business logic
127
- default:delete
126
+ Post:checkCanDelete, # Custom business logic
127
+ Post:default:delete
128
128
  ]
129
129
  publish: # Custom action
130
130
  handlers: [
131
- default:getById, # Fetch entity
132
- service:validateForPublish, # Custom validation
133
- service:updatePublishStatus # Custom update logic
131
+ Post:default:get, # Fetch entity
132
+ Post:validateForPublish, # Custom validation
133
+ Post:updatePublishStatus # Custom update logic
134
134
  ]
135
135
 
136
136
  permissions: # Role-based access control
137
- - action: list
138
- roles: [all] # Anyone (including anonymous)
139
- - action: get
140
- roles: [all] # Anyone can view
141
- - action: create
142
- roles: [authenticated] # Must be logged in
143
- - action: update
144
- roles: [owner, admin] # Entity owner or admin role
145
- - action: delete
146
- roles: [admin] # Only admin role
147
- - action: publish
148
- roles: [owner, editor, admin] # Multiple roles allowed
137
+ - role: all
138
+ actions: [list, get] # Anyone (including anonymous)
139
+ - role: authenticated
140
+ actions: [create] # Must be logged in
141
+ - role: owner
142
+ actions: [update, publish] # Entity owner permissions
143
+ - role: admin
144
+ actions: [update, delete, publish] # Admin role permissions
145
+ - role: editor
146
+ actions: [publish] # Editor role permissions
149
147
  ```
150
148
 
151
149
  **Make sure no `ID`/`owner id`/`is deleted` fields are present in the model definition, since it's added automatically**
@@ -156,17 +154,38 @@ permissions: # Role-based access control
156
154
  - `boolean` - True/false values
157
155
  - `datetime` - Date and time values
158
156
 
157
+ **🔄 Handler vs Action Architecture:**
158
+ - **Handler**: Creates a separate service method (one handler = one service method)
159
+ - **Action**: Virtual controller concept that calls handler methods step-by-step
160
+
159
161
  **Built-in Action Handlers:**
160
- - `default:list` - Returns paginated list of entities
161
- - `default:getById` - Fetches single entity by ID
162
- - `default:create` - Creates new entity with validation
163
- - `default:update` - Updates existing entity by ID
164
- - `default:delete` - Soft deletes entity
162
+ - `ModelName:default:list` - Creates service method with pagination parameters
163
+ - `ModelName:default:get` - Creates service method named `get` with ID parameter
164
+ - `ModelName:default:create` - Creates service method with DTO parameter
165
+ - `ModelName:default:update` - Creates service method with ID and DTO parameters
166
+ - `ModelName:default:delete` - Creates service method with ID parameter
165
167
 
166
168
  **Custom Action Handlers:**
167
- - `service:methodName` - Calls custom method on service class
168
- - Automatically generated with proper type signatures
169
- - Can be chained with built-in handlers
169
+ - `ModelName:customMethodName` - Creates service method that accepts `result, context` parameters
170
+ - `result`: Result from previous handler (or `null` if it's the first handler)
171
+ - `context`: The request context object
172
+ - Each handler generates a separate method in the service
173
+ - User can customize the implementation after generation
174
+
175
+ **🔗 Multiple Handlers per Action:**
176
+ When an action has multiple handlers, each handler generates a separate service method, and the controller action calls them sequentially. The action returns the result from the last handler.
177
+
178
+ **Parameter Passing Rules:**
179
+ - **Default handlers** (`:default:`): Receive standard parameters (id, pagination, DTO, etc.)
180
+ - **Custom handlers**: Receive `(result, context)` where:
181
+ - `result`: Result from previous handler, or `null` if it's the first handler
182
+ - `context`: Request context object
183
+
184
+ **Handler Format Examples:**
185
+ - `Post:default:list` - Creates Post service method `list(page, limit)`
186
+ - `Post:default:get` - Creates Post service method `get(id)`
187
+ - `Post:validateContent` - Creates Post service method `validateContent(result, context)`
188
+ - `Comment:notifySubscribers` - Creates Comment service method `notifySubscribers(result, context)`
170
189
 
171
190
  **Strategy Options (for forms):**
172
191
  - `toast` - Success toast notification
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@currentjs/gen",
3
- "version": "0.1.2",
3
+ "version": "0.2.1",
4
4
  "description": "CLI code generator",
5
5
  "license": "LGPL-3.0",
6
6
  "author": "Konstantin Zavalny",