@getstrata/cli 3.0.8 → 3.1.0
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/CHANGELOG.md +5 -510
- package/README.md +15 -78
- package/package.json +3 -3
- package/src/commands/help.ts +5 -0
- package/src/scaffold/index.ts +3 -0
- package/src/scaffold/makeController.ts +32 -0
- package/src/scaffold/makeFactory.ts +16 -4
- package/src/scaffold/makeModel.ts +15 -18
- package/src/scaffold/makeModule.ts +1 -1
- package/src/scaffold/makePolicy.ts +28 -48
- package/src/scaffold/makeRequest.ts +25 -55
- package/src/scaffold/renderModule.ts +47 -68
- package/src/scaffold/scaffoldFile.ts +3 -2
|
@@ -17,47 +17,28 @@ function renderModuleScaffold({
|
|
|
17
17
|
|
|
18
18
|
const files = new Map<string, string>([
|
|
19
19
|
[
|
|
20
|
-
|
|
21
|
-
`
|
|
22
|
-
id: number;
|
|
23
|
-
name: string;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export type { ${moduleName}Record };
|
|
27
|
-
`,
|
|
28
|
-
],
|
|
29
|
-
[
|
|
30
|
-
"table.ts",
|
|
31
|
-
`import { defineTable } from "@getstrata/core/database/table";
|
|
32
|
-
import type { ${moduleName}Record } from "./types";
|
|
20
|
+
`models/${moduleName}.ts`,
|
|
21
|
+
`import { defineModel, field, type ModelAttributes } from "@getstrata/core/database/model";
|
|
33
22
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
columns: ["id", "name"],
|
|
23
|
+
class ${moduleName} extends defineModel({
|
|
24
|
+
table: "${moduleSlug}",
|
|
25
|
+
fields: { id: field.number(), name: field.string() },
|
|
38
26
|
defaultOrderBy: { column: "id", direction: "ASC" },
|
|
39
|
-
})
|
|
27
|
+
}) {
|
|
28
|
+
static $fillable = ["name"] as const;
|
|
29
|
+
static $timestamps = false;
|
|
30
|
+
}
|
|
40
31
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
[
|
|
45
|
-
"model.ts",
|
|
46
|
-
`import { createModel } from "@getstrata/core/database/model";
|
|
47
|
-
import { ${moduleIdentifier}Table } from "./table";
|
|
48
|
-
const ${moduleName}Definition = createModel({
|
|
49
|
-
name: "${moduleName}", table: ${moduleIdentifier}Table,
|
|
50
|
-
attributes: { fillable: ["name"], timestamps: false },
|
|
51
|
-
});
|
|
52
|
-
const ${moduleIdentifier}Repository = ${moduleName}Definition.repository;
|
|
53
|
-
export { ${moduleName}Definition, ${moduleIdentifier}Repository };
|
|
32
|
+
type ${moduleName}Record = ModelAttributes<${moduleName}>;
|
|
33
|
+
export { ${moduleName} };
|
|
34
|
+
export type { ${moduleName}Record };
|
|
54
35
|
`,
|
|
55
36
|
],
|
|
56
37
|
[
|
|
57
|
-
|
|
38
|
+
`policies/${moduleName}Policy.ts`,
|
|
58
39
|
`import type { AuthUser } from "@getstrata/core/auth/authContext";
|
|
59
40
|
import { Policy } from "@getstrata/core/auth/policy";
|
|
60
|
-
import type { ${moduleName}Record } from "
|
|
41
|
+
import type { ${moduleName}Record } from "../models/${moduleName}";
|
|
61
42
|
|
|
62
43
|
class ${moduleName}Policy extends Policy {
|
|
63
44
|
override create(_user: AuthUser | null): boolean {
|
|
@@ -77,7 +58,7 @@ export default ${moduleName}Policy;
|
|
|
77
58
|
`,
|
|
78
59
|
],
|
|
79
60
|
[
|
|
80
|
-
|
|
61
|
+
`http/requests/${moduleName}Requests.ts`,
|
|
81
62
|
`import { parseJsonBody, parsePositiveIntParam } from "@getstrata/core/http/validation";
|
|
82
63
|
import { parsePaginationQuery } from "@getstrata/core/http/pagination";
|
|
83
64
|
import {
|
|
@@ -159,7 +140,7 @@ export type {
|
|
|
159
140
|
"resources.ts",
|
|
160
141
|
`import { toPaginatedResourceCollection, toResourceCollection } from "@getstrata/core/http/resources";
|
|
161
142
|
import type { PaginationMeta } from "@getstrata/core/pagination";
|
|
162
|
-
import type { ${moduleName}Record } from "./
|
|
143
|
+
import type { ${moduleName}Record } from "./models/${moduleName}";
|
|
163
144
|
|
|
164
145
|
interface ${moduleName}Resource {
|
|
165
146
|
id: number;
|
|
@@ -195,8 +176,8 @@ export type { ${moduleName}Resource };
|
|
|
195
176
|
`,
|
|
196
177
|
],
|
|
197
178
|
[
|
|
198
|
-
|
|
199
|
-
`import type { BusinessRequestPorts } from "@getstrata/bootstrap/
|
|
179
|
+
`http/controllers/${moduleName}Controller.ts`,
|
|
180
|
+
`import type { BusinessRequestPorts } from "@getstrata/bootstrap/persistenceContracts";
|
|
200
181
|
import type { CachedJson } from "@getstrata/core/contracts/di";
|
|
201
182
|
import { bindRouteModel } from "@getstrata/core/http/routeModelBinding";
|
|
202
183
|
import { buildRequestCacheKey } from "@getstrata/core/http/validation";
|
|
@@ -204,17 +185,17 @@ import {type RouteModelBindingPorts, securedBindRouteModel } from "@getstrata/co
|
|
|
204
185
|
import type { RouteRequest } from "@getstrata/core/http/route";
|
|
205
186
|
import { createdResponse, jsonResponse, noContentResponse } from "@getstrata/core/http/response";
|
|
206
187
|
import { NotFoundError } from "@getstrata/core/errors/http";
|
|
207
|
-
import { ${
|
|
188
|
+
import { ${moduleName} } from "../../models/${moduleName}";
|
|
208
189
|
import {
|
|
209
190
|
parseCreate${moduleName}Body,
|
|
210
191
|
parse${moduleName}ListQuery,
|
|
211
192
|
parseUpdate${moduleName}Body,
|
|
212
193
|
type ${moduleName}IdParams,
|
|
213
|
-
} from "
|
|
194
|
+
} from "../requests/${moduleName}Requests";
|
|
214
195
|
import {
|
|
215
196
|
to${moduleName}PaginatedResourceCollection,
|
|
216
197
|
to${moduleName}Resource,
|
|
217
|
-
} from "
|
|
198
|
+
} from "../../resources";
|
|
218
199
|
|
|
219
200
|
const ${moduleIdentifier.toUpperCase()}_CACHE_TAG = "${pluralSlug}";
|
|
220
201
|
|
|
@@ -231,11 +212,11 @@ class ${moduleName}Controller {
|
|
|
231
212
|
readonly destroy:(request:RouteRequest<${moduleName}IdParams>)=>Promise<Response>;
|
|
232
213
|
|
|
233
214
|
private get records() {
|
|
234
|
-
return this.database.
|
|
215
|
+
return this.database.model(${moduleName});
|
|
235
216
|
}
|
|
236
217
|
|
|
237
218
|
private find(id: number) {
|
|
238
|
-
return this.records.
|
|
219
|
+
return this.records.findOrFail(id, () => new NotFoundError("${moduleName} not found."));
|
|
239
220
|
}
|
|
240
221
|
|
|
241
222
|
readonly index = (async (request?: Request) => {
|
|
@@ -245,8 +226,8 @@ class ${moduleName}Controller {
|
|
|
245
226
|
return await this.cachedJson(
|
|
246
227
|
cacheKey,
|
|
247
228
|
async () => {
|
|
248
|
-
const result = await this.records.paginate(query);
|
|
249
|
-
return to${moduleName}PaginatedResourceCollection(result.data, result.meta);
|
|
229
|
+
const result = await this.records.query().paginate(query);
|
|
230
|
+
return to${moduleName}PaginatedResourceCollection(result.data.map((record) => record.toObject()), result.meta);
|
|
250
231
|
},
|
|
251
232
|
[${moduleIdentifier.toUpperCase()}_CACHE_TAG],
|
|
252
233
|
request,
|
|
@@ -258,7 +239,7 @@ class ${moduleName}Controller {
|
|
|
258
239
|
"id",
|
|
259
240
|
(id) => this.find(id),
|
|
260
241
|
async (_request: RouteRequest<${moduleName}IdParams>, record) => {
|
|
261
|
-
return jsonResponse(to${moduleName}Resource(record));
|
|
242
|
+
return jsonResponse(to${moduleName}Resource(record.toObject()));
|
|
262
243
|
},
|
|
263
244
|
)
|
|
264
245
|
);
|
|
@@ -266,7 +247,7 @@ class ${moduleName}Controller {
|
|
|
266
247
|
readonly store = (async (request: Request) => {
|
|
267
248
|
const body = await parseCreate${moduleName}Body(request);
|
|
268
249
|
const record = await this.records.create(body);
|
|
269
|
-
return createdResponse(to${moduleName}Resource(record));
|
|
250
|
+
return createdResponse(to${moduleName}Resource(record.toObject()));
|
|
270
251
|
});
|
|
271
252
|
|
|
272
253
|
private bindUpdate(binding:RouteModelBindingPorts) {return securedBindRouteModel(
|
|
@@ -275,8 +256,8 @@ class ${moduleName}Controller {
|
|
|
275
256
|
{ resource: "${moduleSlug}", action: "update" },
|
|
276
257
|
async (req: RouteRequest<${moduleName}IdParams>, record) => {
|
|
277
258
|
const body = await parseUpdate${moduleName}Body(req);
|
|
278
|
-
const updated = await
|
|
279
|
-
return jsonResponse(to${moduleName}Resource(updated));
|
|
259
|
+
const updated = await record.update(body);
|
|
260
|
+
return jsonResponse(to${moduleName}Resource(updated.toObject()));
|
|
280
261
|
},
|
|
281
262
|
binding,
|
|
282
263
|
);
|
|
@@ -287,7 +268,7 @@ class ${moduleName}Controller {
|
|
|
287
268
|
(id) => this.find(id),
|
|
288
269
|
{ resource: "${moduleSlug}", action: "delete" },
|
|
289
270
|
async (_request: RouteRequest<${moduleName}IdParams>, record) => {
|
|
290
|
-
await
|
|
271
|
+
await record.delete();
|
|
291
272
|
return noContentResponse();
|
|
292
273
|
},
|
|
293
274
|
binding,
|
|
@@ -301,11 +282,11 @@ export default ${moduleName}Controller;
|
|
|
301
282
|
[
|
|
302
283
|
"routes.ts",
|
|
303
284
|
`import type { HttpKernel } from "@getstrata/bootstrap/httpKernel";
|
|
304
|
-
import type { BusinessRequestPorts } from "@getstrata/bootstrap/
|
|
285
|
+
import type { BusinessRequestPorts } from "@getstrata/bootstrap/persistenceContracts";
|
|
305
286
|
import type { RouteRequest } from "@getstrata/core/http/route";
|
|
306
|
-
import type { ${moduleName}IdParams } from "./requests";
|
|
287
|
+
import type { ${moduleName}IdParams } from "./http/requests/${moduleName}Requests";
|
|
307
288
|
import type { CachedJson } from "@getstrata/core/contracts/di";
|
|
308
|
-
${withWeb ? 'import { apiPrefix } from "@getstrata/core/runtime/appKeyPrefix";\n' : ""}import ${moduleName}Controller from "./
|
|
289
|
+
${withWeb ? 'import { apiPrefix } from "@getstrata/core/runtime/appKeyPrefix";\n' : ""}import ${moduleName}Controller from "./http/controllers/${moduleName}Controller";
|
|
309
290
|
|
|
310
291
|
function create${moduleName}Routes(
|
|
311
292
|
cachedJson: CachedJson,
|
|
@@ -342,20 +323,19 @@ export { create${moduleName}Routes };
|
|
|
342
323
|
[
|
|
343
324
|
"index.ts",
|
|
344
325
|
`import { type AppModule } from "@getstrata/bootstrap/contracts";
|
|
345
|
-
import ${moduleName}Controller from "./
|
|
346
|
-
import ${moduleName}Policy from "./
|
|
326
|
+
import ${moduleName}Controller from "./http/controllers/${moduleName}Controller";
|
|
327
|
+
import ${moduleName}Policy from "./policies/${moduleName}Policy";
|
|
347
328
|
import { create${moduleName}Routes } from "./routes";${
|
|
348
329
|
withWeb
|
|
349
330
|
? `
|
|
350
331
|
import { create${moduleName}WebRoutes } from "./webRoutes";`
|
|
351
332
|
: ""
|
|
352
333
|
}
|
|
353
|
-
import { ${moduleIdentifier}Table } from "./table";
|
|
354
334
|
|
|
355
335
|
const ${moduleVariable}: AppModule = {
|
|
356
336
|
name: "${moduleSlug}",
|
|
357
337
|
order: 100,
|
|
358
|
-
tableName: ${
|
|
338
|
+
tableName: "${moduleSlug}",
|
|
359
339
|
cacheTags: ["${pluralSlug}"],
|
|
360
340
|
policies: { "${moduleSlug}": new ${moduleName}Policy() },
|
|
361
341
|
routes({ cachedJson, kernel }) {
|
|
@@ -372,7 +352,7 @@ const ${moduleVariable}: AppModule = {
|
|
|
372
352
|
|
|
373
353
|
export default ${moduleVariable};
|
|
374
354
|
export { ${moduleName}Controller };
|
|
375
|
-
export { parse${moduleName}IdParams, parse${moduleName}ListQuery } from "./requests";
|
|
355
|
+
export { parse${moduleName}IdParams, parse${moduleName}ListQuery } from "./http/requests/${moduleName}Requests";
|
|
376
356
|
export {
|
|
377
357
|
to${moduleName}PaginatedResourceCollection,
|
|
378
358
|
to${moduleName}Resource,
|
|
@@ -384,9 +364,8 @@ export { create${moduleName}Routes } from "./routes";${
|
|
|
384
364
|
export { create${moduleName}WebRoutes } from "./webRoutes";`
|
|
385
365
|
: ""
|
|
386
366
|
}
|
|
387
|
-
export { ${moduleName}
|
|
388
|
-
export { ${
|
|
389
|
-
export type { ${moduleName}Record } from "./types";
|
|
367
|
+
export { ${moduleName} } from "./models/${moduleName}";
|
|
368
|
+
export type { ${moduleName}Record } from "./models/${moduleName}";
|
|
390
369
|
`,
|
|
391
370
|
],
|
|
392
371
|
]);
|
|
@@ -406,22 +385,22 @@ export type { ${moduleName}Record } from "./types";
|
|
|
406
385
|
);
|
|
407
386
|
|
|
408
387
|
webFiles.set(
|
|
409
|
-
|
|
410
|
-
`import type { BusinessRequestPorts } from "@getstrata/bootstrap/
|
|
388
|
+
`http/controllers/${moduleName}WebController.ts`,
|
|
389
|
+
`import type { BusinessRequestPorts } from "@getstrata/bootstrap/persistenceContracts";
|
|
411
390
|
import type { NamespacedViewRenderer } from "@getstrata/core/view";
|
|
412
|
-
import { ${
|
|
413
|
-
import { parse${moduleName}ListQuery } from "
|
|
391
|
+
import { ${moduleName} } from "../../models/${moduleName}";
|
|
392
|
+
import { parse${moduleName}ListQuery } from "../requests/${moduleName}Requests";
|
|
414
393
|
|
|
415
394
|
class ${moduleName}WebController {
|
|
416
395
|
constructor(private readonly views: NamespacedViewRenderer, private readonly database: BusinessRequestPorts["database"]) {}
|
|
417
396
|
|
|
418
397
|
private get records() {
|
|
419
|
-
return this.database.
|
|
398
|
+
return this.database.model(${moduleName});
|
|
420
399
|
}
|
|
421
400
|
|
|
422
401
|
readonly index = (async (request?: Request) => {
|
|
423
402
|
const query = parse${moduleName}ListQuery(request);
|
|
424
|
-
const result = await this.records.paginate(query);
|
|
403
|
+
const result = await this.records.query().paginate(query);
|
|
425
404
|
|
|
426
405
|
return this.views.renderHtmx("index.eta", {
|
|
427
406
|
layout: { title: "${moduleName}s" },
|
|
@@ -441,8 +420,8 @@ export default ${moduleName}WebController;
|
|
|
441
420
|
`import { resolveService, type AppDependencies } from "@getstrata/core/contracts/di";
|
|
442
421
|
import { CORE_VIEW_RENDERER_TOKEN } from "@getstrata/core/view";
|
|
443
422
|
import type { HttpKernel } from "@getstrata/bootstrap/httpKernel";
|
|
444
|
-
import type { BusinessRequestPorts } from "@getstrata/bootstrap/
|
|
445
|
-
import ${moduleName}WebController from "./
|
|
423
|
+
import type { BusinessRequestPorts } from "@getstrata/bootstrap/persistenceContracts";
|
|
424
|
+
import ${moduleName}WebController from "./http/controllers/${moduleName}WebController";
|
|
446
425
|
|
|
447
426
|
function create${moduleName}WebRoutes(dependencies: AppDependencies, kernel: HttpKernel) {
|
|
448
427
|
const views = resolveService(dependencies, CORE_VIEW_RENDERER_TOKEN).namespace("${pluralSlug}");
|
|
@@ -79,8 +79,9 @@ function scaffoldArguments(
|
|
|
79
79
|
"make:factory": "model name",
|
|
80
80
|
"make:job": "job name",
|
|
81
81
|
"make:listener": "listener name",
|
|
82
|
-
"make:policy": "
|
|
83
|
-
"make:request": "
|
|
82
|
+
"make:policy": "policy name",
|
|
83
|
+
"make:request": "request name",
|
|
84
|
+
"make:controller": "controller name",
|
|
84
85
|
} as Record<string, string>
|
|
85
86
|
)[command] ?? "name";
|
|
86
87
|
if (!names[0]) throw new Error(`${command} requires a ${noun}.`);
|