@specverse/engines 6.5.4 → 6.7.8
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/libs/instance-factories/applications/templates/generic/backend-package-json-generator.js +26 -10
- package/dist/libs/instance-factories/controllers/templates/fastify/server-generator.js +1 -1
- package/dist/libs/instance-factories/services/mongodb-native-services.yaml +84 -0
- package/dist/libs/instance-factories/services/templates/mongodb-native/client-generator.js +43 -0
- package/dist/libs/instance-factories/services/templates/mongodb-native/controller-generator.js +252 -0
- package/dist/libs/instance-factories/services/templates/mongodb-native/service-generator.js +64 -0
- package/dist/libs/instance-factories/services/templates/prisma/ai-behaviors-generator.js +167 -26
- package/dist/realize/library/library.d.ts.map +1 -1
- package/dist/realize/library/library.js +11 -0
- package/dist/realize/library/library.js.map +1 -1
- package/libs/instance-factories/applications/templates/generic/backend-package-json-generator.ts +48 -12
- package/libs/instance-factories/controllers/templates/fastify/server-generator.ts +1 -1
- package/libs/instance-factories/services/mongodb-native-services.yaml +84 -0
- package/libs/instance-factories/services/templates/mongodb-native/__tests__/controller-generator.test.ts +113 -0
- package/libs/instance-factories/services/templates/mongodb-native/client-generator.ts +51 -0
- package/libs/instance-factories/services/templates/mongodb-native/controller-generator.ts +319 -0
- package/libs/instance-factories/services/templates/mongodb-native/service-generator.ts +83 -0
- package/libs/instance-factories/services/templates/prisma/ai-behaviors-generator.ts +231 -36
- package/package.json +4 -5
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MongoDB Native Driver — client singleton
|
|
3
|
+
*
|
|
4
|
+
* Emits a small connection module shared by all controllers and services.
|
|
5
|
+
* Collection naming convention: lowercase + 's' suffix (User → users,
|
|
6
|
+
* Category → categorys), matching the lowercase-pluralized scheme declared
|
|
7
|
+
* in the factory yaml. Models with custom collection names can override
|
|
8
|
+
* via spec — the controller-generator looks at `model.storage?.collection`.
|
|
9
|
+
*/
|
|
10
|
+
import type { TemplateContext } from '@specverse/types';
|
|
11
|
+
|
|
12
|
+
export default function generateMongoClient(_context: TemplateContext): string {
|
|
13
|
+
return `/**
|
|
14
|
+
* MongoDB native driver — singleton client + helpers.
|
|
15
|
+
*
|
|
16
|
+
* Picks up MONGODB_URI / MONGODB_DB from the environment. The client is
|
|
17
|
+
* lazily initialised on first use and reused across requests; \`disconnect\`
|
|
18
|
+
* is exposed for graceful-shutdown wiring.
|
|
19
|
+
*/
|
|
20
|
+
import { MongoClient, type Db, type Collection, type Document } from 'mongodb';
|
|
21
|
+
|
|
22
|
+
const uri = process.env.MONGODB_URI || 'mongodb://localhost:27017';
|
|
23
|
+
const dbName = process.env.MONGODB_DB || 'specverse';
|
|
24
|
+
|
|
25
|
+
let client: MongoClient | null = null;
|
|
26
|
+
let database: Db | null = null;
|
|
27
|
+
|
|
28
|
+
export async function getDb(): Promise<Db> {
|
|
29
|
+
if (database) return database;
|
|
30
|
+
client = new MongoClient(uri);
|
|
31
|
+
await client.connect();
|
|
32
|
+
database = client.db(dbName);
|
|
33
|
+
return database;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function getCollection<T extends Document = Document>(
|
|
37
|
+
name: string,
|
|
38
|
+
): Promise<Collection<T>> {
|
|
39
|
+
const db = await getDb();
|
|
40
|
+
return db.collection<T>(name);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function disconnect(): Promise<void> {
|
|
44
|
+
if (client) {
|
|
45
|
+
await client.close();
|
|
46
|
+
client = null;
|
|
47
|
+
database = null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
`;
|
|
51
|
+
}
|
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MongoDB Native Driver — Controller Generator
|
|
3
|
+
*
|
|
4
|
+
* Generates per-model controllers exposing CURVED operations via the native
|
|
5
|
+
* MongoDB driver (collection-level insertOne / findOne / updateOne / deleteOne).
|
|
6
|
+
*
|
|
7
|
+
* Scope (MVP — TODO #43):
|
|
8
|
+
* - Validate / Create / Retrieve / Update / Delete fully wired
|
|
9
|
+
* - Evolve emits a lifecycle-aware update when the model declares states;
|
|
10
|
+
* transitions are validated against the declared flow before update
|
|
11
|
+
* - Custom actions emit AI-behavior stubs (callable, but the body is left
|
|
12
|
+
* as a TODO with the spec steps inline) — same pattern Prisma uses
|
|
13
|
+
*
|
|
14
|
+
* Out of scope here (deferred follow-up TODOs):
|
|
15
|
+
* - $transaction wrapping for multi-document writes (MongoDB sessions)
|
|
16
|
+
* - Aggregation-pipeline-driven retrieve variants (joins via $lookup)
|
|
17
|
+
* - Inline behavior-step → driver-call generation parity with the
|
|
18
|
+
* prisma step-conventions library
|
|
19
|
+
*
|
|
20
|
+
* Collection naming: lowercase + 's' (User → users) unless the model
|
|
21
|
+
* declares `storage.collection`. ID field is ObjectId-aware: a 24-hex
|
|
22
|
+
* string is parsed via `new ObjectId(id)`; anything else is kept as-is so
|
|
23
|
+
* specs that use string ids (UUIDs, slugs) keep working unchanged.
|
|
24
|
+
*/
|
|
25
|
+
import type { TemplateContext } from '@specverse/types';
|
|
26
|
+
import { buildTransitionMap, isAutoField } from '@specverse/types/spec-rules';
|
|
27
|
+
|
|
28
|
+
export default function generateMongoNativeController(context: TemplateContext): string {
|
|
29
|
+
const { controller, model } = context;
|
|
30
|
+
if (!controller) throw new Error('Controller is required in template context');
|
|
31
|
+
if (!model) throw new Error('Model is required for controller generation');
|
|
32
|
+
|
|
33
|
+
const controllerName = controller.name;
|
|
34
|
+
const modelName = model.name;
|
|
35
|
+
const modelVar = lowerFirst(modelName);
|
|
36
|
+
const collection = collectionName(model);
|
|
37
|
+
const curedOps = controller.cured || {};
|
|
38
|
+
|
|
39
|
+
const customActions = generateCustomActions(controller);
|
|
40
|
+
|
|
41
|
+
const validate = generateValidateMethod(model, modelName);
|
|
42
|
+
const create = curedOps.create ? generateCreateMethod(model, modelName, modelVar, collection) : '';
|
|
43
|
+
const retrieve = curedOps.retrieve ? generateRetrieveMethod(modelName, modelVar, collection) : '';
|
|
44
|
+
const update = curedOps.update ? generateUpdateMethod(modelName, modelVar, collection) : '';
|
|
45
|
+
const evolve = curedOps.evolve ? generateEvolveMethod(model, modelName, modelVar, collection) : '';
|
|
46
|
+
const del = curedOps.delete ? generateDeleteMethod(modelName, modelVar, collection) : '';
|
|
47
|
+
|
|
48
|
+
const hasEventPublishing =
|
|
49
|
+
curedOps.create || curedOps.update || curedOps.evolve || curedOps.delete;
|
|
50
|
+
|
|
51
|
+
return `/**
|
|
52
|
+
* ${controllerName}
|
|
53
|
+
* Model-specific business logic for ${modelName} (MongoDB native driver)
|
|
54
|
+
* ${controller.description || ''}
|
|
55
|
+
*/
|
|
56
|
+
import { ObjectId, type Filter, type Document } from 'mongodb';
|
|
57
|
+
import { getCollection } from '../db/mongoClient.js';
|
|
58
|
+
${hasEventPublishing ? `import { eventBus } from '../events/eventBus.js';` : ''}
|
|
59
|
+
|
|
60
|
+
const COLLECTION_NAME = '${collection}';
|
|
61
|
+
|
|
62
|
+
/** Parse an id string to the value the collection expects: ObjectId for
|
|
63
|
+
* 24-hex-character ids, otherwise pass-through (UUIDs, slugs, ints-as-string). */
|
|
64
|
+
function parseId(id: string): ObjectId | string {
|
|
65
|
+
if (typeof id === 'string' && /^[a-fA-F0-9]{24}$/.test(id)) return new ObjectId(id);
|
|
66
|
+
return id;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** _id-aware filter builder. */
|
|
70
|
+
function byId(id: string): Filter<Document> {
|
|
71
|
+
return { _id: parseId(id) as any };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export class ${controllerName} {
|
|
75
|
+
${validate}
|
|
76
|
+
${create}
|
|
77
|
+
${retrieve}
|
|
78
|
+
${update}
|
|
79
|
+
${evolve}
|
|
80
|
+
${del}
|
|
81
|
+
${customActions.code}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export const ${modelVar}Controller = new ${controllerName}();
|
|
85
|
+
export default ${modelVar}Controller;
|
|
86
|
+
`;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function lowerFirst(s: string): string {
|
|
90
|
+
return s.charAt(0).toLowerCase() + s.slice(1);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function collectionName(model: any): string {
|
|
94
|
+
if (model?.storage?.collection) return String(model.storage.collection);
|
|
95
|
+
// Lowercase-pluralized: User → users, OrderItem → orderitems
|
|
96
|
+
return model.name.toLowerCase() + 's';
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function generateValidateMethod(model: any, modelName: string): string {
|
|
100
|
+
return `
|
|
101
|
+
/**
|
|
102
|
+
* Validate ${modelName} data — runs before create / update / evolve.
|
|
103
|
+
*/
|
|
104
|
+
public validate(
|
|
105
|
+
_data: any,
|
|
106
|
+
_context: { operation: 'create' | 'update' | 'evolve' }
|
|
107
|
+
): { valid: boolean; errors: string[] } {
|
|
108
|
+
const errors: string[] = [];
|
|
109
|
+
${generateValidationLogic(model)}
|
|
110
|
+
return { valid: errors.length === 0, errors };
|
|
111
|
+
}
|
|
112
|
+
`;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function generateValidationLogic(model: any): string {
|
|
116
|
+
if (!model.attributes) return ' // No validation rules defined';
|
|
117
|
+
const attrList = Array.isArray(model.attributes)
|
|
118
|
+
? model.attributes.map((a: any) => [a.name, a])
|
|
119
|
+
: Object.entries(model.attributes);
|
|
120
|
+
const out: string[] = [];
|
|
121
|
+
attrList.forEach(([name, attr]: [string, any]) => {
|
|
122
|
+
if (attr.required && !isAutoField(name, attr)) {
|
|
123
|
+
out.push(` if (_context.operation === 'create' && !_data.${name}) errors.push('${name} is required');`);
|
|
124
|
+
}
|
|
125
|
+
if (attr.type === 'String' || attr.type === 'string') {
|
|
126
|
+
if (attr.min) out.push(` if (_data.${name} && _data.${name}.length < ${attr.min}) errors.push('${name} must be at least ${attr.min} characters');`);
|
|
127
|
+
if (attr.max) out.push(` if (_data.${name} && _data.${name}.length > ${attr.max}) errors.push('${name} must be at most ${attr.max} characters');`);
|
|
128
|
+
}
|
|
129
|
+
if (attr.values && Array.isArray(attr.values)) {
|
|
130
|
+
const values = attr.values.map((v: string) => `'${v}'`).join(', ');
|
|
131
|
+
out.push(` if (_data.${name} && ![${values}].includes(_data.${name})) errors.push('${name} must be one of: ${attr.values.join(', ')}');`);
|
|
132
|
+
}
|
|
133
|
+
if (attr.format === 'email') {
|
|
134
|
+
out.push(` if (_data.${name} && !/^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/.test(_data.${name})) errors.push('${name} must be a valid email address');`);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
return out.join('\n') || ' // No validation rules defined';
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function generateCreateMethod(model: any, modelName: string, modelVar: string, collection: string): string {
|
|
141
|
+
return `
|
|
142
|
+
/**
|
|
143
|
+
* Create a new ${modelName}.
|
|
144
|
+
*/
|
|
145
|
+
public async create(data: any): Promise<any> {
|
|
146
|
+
const validation = this.validate(data, { operation: 'create' });
|
|
147
|
+
if (!validation.valid) throw new Error(\`Validation failed: \${validation.errors.join(', ')}\`);
|
|
148
|
+
|
|
149
|
+
const collection = await getCollection(COLLECTION_NAME);
|
|
150
|
+
const result = await collection.insertOne({ ...data });
|
|
151
|
+
const ${modelVar} = { _id: result.insertedId, ...data };
|
|
152
|
+
|
|
153
|
+
await eventBus.publish('${modelName}Created', { ...${modelVar}, timestamp: new Date().toISOString() } as any);
|
|
154
|
+
return ${modelVar};
|
|
155
|
+
}
|
|
156
|
+
`;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function generateRetrieveMethod(modelName: string, modelVar: string, collection: string): string {
|
|
160
|
+
return `
|
|
161
|
+
/**
|
|
162
|
+
* Retrieve ${modelName} by id. Returns null when not found.
|
|
163
|
+
*/
|
|
164
|
+
public async retrieve(id: string): Promise<any | null> {
|
|
165
|
+
const collection = await getCollection(COLLECTION_NAME);
|
|
166
|
+
return await collection.findOne(byId(id));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Retrieve a page of ${modelName}s.
|
|
171
|
+
*/
|
|
172
|
+
public async retrieveAll(options: { skip?: number; take?: number } = {}): Promise<any[]> {
|
|
173
|
+
const collection = await getCollection(COLLECTION_NAME);
|
|
174
|
+
const cursor = collection.find({});
|
|
175
|
+
if (options.skip) cursor.skip(options.skip);
|
|
176
|
+
if (options.take) cursor.limit(options.take);
|
|
177
|
+
return await cursor.toArray();
|
|
178
|
+
}
|
|
179
|
+
`;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function generateUpdateMethod(modelName: string, modelVar: string, collection: string): string {
|
|
183
|
+
return `
|
|
184
|
+
/**
|
|
185
|
+
* Update ${modelName}.
|
|
186
|
+
*/
|
|
187
|
+
public async update(id: string, data: any): Promise<any> {
|
|
188
|
+
const validation = this.validate(data, { operation: 'update' });
|
|
189
|
+
if (!validation.valid) throw new Error(\`Validation failed: \${validation.errors.join(', ')}\`);
|
|
190
|
+
|
|
191
|
+
// Strip nested objects + id — only scalar fields are written.
|
|
192
|
+
const updateData: any = {};
|
|
193
|
+
for (const [key, value] of Object.entries(data)) {
|
|
194
|
+
if (key === 'id' || key === '_id') continue;
|
|
195
|
+
if (Array.isArray(value)) continue;
|
|
196
|
+
if (value !== null && typeof value === 'object' && !(value instanceof Date)) continue;
|
|
197
|
+
updateData[key] = value;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const collection = await getCollection(COLLECTION_NAME);
|
|
201
|
+
await collection.updateOne(byId(id), { $set: updateData });
|
|
202
|
+
const ${modelVar} = await collection.findOne(byId(id));
|
|
203
|
+
if (!${modelVar}) throw new Error('${modelName} not found after update');
|
|
204
|
+
|
|
205
|
+
await eventBus.publish('${modelName}Updated', { ...${modelVar}, timestamp: new Date().toISOString() } as any);
|
|
206
|
+
return ${modelVar};
|
|
207
|
+
}
|
|
208
|
+
`;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function generateEvolveMethod(model: any, modelName: string, modelVar: string, collection: string): string {
|
|
212
|
+
const lifecycles = Array.isArray(model.lifecycles)
|
|
213
|
+
? model.lifecycles
|
|
214
|
+
: (model.lifecycles ? Object.entries(model.lifecycles).map(([name, lc]: [string, any]) => ({ name, ...lc })) : []);
|
|
215
|
+
const lifecycle = lifecycles[0];
|
|
216
|
+
const lifecycleName = lifecycle?.name || 'status';
|
|
217
|
+
const validTransitions = lifecycle ? buildTransitionMap(lifecycle) : {};
|
|
218
|
+
// Derive states from the transition map when the spec used `flow:` shorthand
|
|
219
|
+
// (states is undefined on the wire, but buildTransitionMap expands the flow).
|
|
220
|
+
const states = lifecycle?.states && lifecycle.states.length > 0
|
|
221
|
+
? lifecycle.states.map((s: any) => typeof s === 'string' ? s : s.name)
|
|
222
|
+
: Array.from(new Set([
|
|
223
|
+
...Object.keys(validTransitions),
|
|
224
|
+
...Object.values(validTransitions).flat(),
|
|
225
|
+
]));
|
|
226
|
+
|
|
227
|
+
return `
|
|
228
|
+
/**
|
|
229
|
+
* Evolve ${modelName} through lifecycle "${lifecycleName}"
|
|
230
|
+
* States: ${states.join(' → ') || '(none declared)'}
|
|
231
|
+
*/
|
|
232
|
+
public async evolve(id: string, data: any): Promise<any> {
|
|
233
|
+
const collection = await getCollection(COLLECTION_NAME);
|
|
234
|
+
const current = await collection.findOne(byId(id));
|
|
235
|
+
if (!current) throw new Error('${modelName} not found');
|
|
236
|
+
|
|
237
|
+
const targetLifecycle = data?.lifecycleName || '${lifecycleName}';
|
|
238
|
+
const targetState = data?.toState ?? data?.state ?? data?.[targetLifecycle];
|
|
239
|
+
if (!targetState) throw new Error('evolve requires toState (or ${lifecycleName}) in the request body');
|
|
240
|
+
|
|
241
|
+
${states.length > 0 ? `
|
|
242
|
+
const currentState = (current as any)[targetLifecycle];
|
|
243
|
+
const validTransitions: Record<string, string[]> = ${JSON.stringify(validTransitions)};
|
|
244
|
+
const allowed = validTransitions[currentState] || [];
|
|
245
|
+
if (!allowed.includes(targetState)) {
|
|
246
|
+
throw new Error(\`Invalid transition: \${currentState} → \${targetState}. Allowed: \${allowed.join(', ') || 'none'}\`);
|
|
247
|
+
}
|
|
248
|
+
` : ''}
|
|
249
|
+
|
|
250
|
+
await collection.updateOne(byId(id), { $set: { [targetLifecycle]: targetState } });
|
|
251
|
+
const ${modelVar} = await collection.findOne(byId(id));
|
|
252
|
+
if (!${modelVar}) throw new Error('${modelName} not found after evolve');
|
|
253
|
+
|
|
254
|
+
await eventBus.publish('${modelName}Evolved', { ...${modelVar}, timestamp: new Date().toISOString() } as any);
|
|
255
|
+
return ${modelVar};
|
|
256
|
+
}
|
|
257
|
+
`;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function generateDeleteMethod(modelName: string, modelVar: string, collection: string): string {
|
|
261
|
+
return `
|
|
262
|
+
/**
|
|
263
|
+
* Delete ${modelName}.
|
|
264
|
+
*/
|
|
265
|
+
public async delete(id: string): Promise<void> {
|
|
266
|
+
const collection = await getCollection(COLLECTION_NAME);
|
|
267
|
+
const ${modelVar} = await collection.findOne(byId(id));
|
|
268
|
+
await collection.deleteOne(byId(id));
|
|
269
|
+
if (${modelVar}) {
|
|
270
|
+
await eventBus.publish('${modelName}Deleted', { ...${modelVar}, timestamp: new Date().toISOString() } as any);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
`;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
interface CustomActionsResult {
|
|
277
|
+
code: string;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Custom actions emit TODO stubs that throw "not implemented".
|
|
282
|
+
*
|
|
283
|
+
* Why not delegate to `aiBehaviors.<actionName>`? Because the AI-behaviors
|
|
284
|
+
* generator only emits functions for STEPS that didn't match a convention
|
|
285
|
+
* pattern — not for actions themselves. So `aiBehaviors.rotate` would not
|
|
286
|
+
* exist even when an action named `rotate` is declared on the controller.
|
|
287
|
+
*
|
|
288
|
+
* Real implementation of action bodies is deferred to the MongoDB-native
|
|
289
|
+
* step-conventions library (#43F follow-up — mirror Prisma's
|
|
290
|
+
* step-conventions.ts but emit native-driver collection calls).
|
|
291
|
+
*/
|
|
292
|
+
function generateCustomActions(controller: any): CustomActionsResult {
|
|
293
|
+
if (!controller.actions || Object.keys(controller.actions).length === 0) {
|
|
294
|
+
return { code: '' };
|
|
295
|
+
}
|
|
296
|
+
const out: string[] = [];
|
|
297
|
+
for (const [actionName, action] of Object.entries<any>(controller.actions)) {
|
|
298
|
+
const stepsHeader = (action.steps && action.steps.length > 0)
|
|
299
|
+
? action.steps.map((s: any) => ` * - ${typeof s === 'string' ? s : (s.action || JSON.stringify(s))}`).join('\n')
|
|
300
|
+
: ' * (no spec steps declared)';
|
|
301
|
+
out.push(`
|
|
302
|
+
/**
|
|
303
|
+
* ${actionName}
|
|
304
|
+
* ${action.description || ''}
|
|
305
|
+
*
|
|
306
|
+
* Spec steps:
|
|
307
|
+
${stepsHeader}
|
|
308
|
+
*/
|
|
309
|
+
public async ${actionName}(_args: any = {}): Promise<any> {
|
|
310
|
+
// TODO (#43F): translate spec steps into native MongoDB driver calls
|
|
311
|
+
// via a mongodb-native step-conventions library (mirror of the prisma
|
|
312
|
+
// one). For now this is a stub so realize completes and the action
|
|
313
|
+
// surface is callable for parity tests.
|
|
314
|
+
throw new Error('${controller.name}.${actionName} is not implemented');
|
|
315
|
+
}
|
|
316
|
+
`);
|
|
317
|
+
}
|
|
318
|
+
return { code: out.join('\n') };
|
|
319
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MongoDB Native Driver — Service Generator
|
|
3
|
+
*
|
|
4
|
+
* Generates abstract business-logic services that use `getCollection` /
|
|
5
|
+
* `getDb` directly. The service body is intentionally skeletal: each
|
|
6
|
+
* declared operation gets a typed method, but the body is a TODO stub
|
|
7
|
+
* containing the spec steps as comments. Real-implementation generation
|
|
8
|
+
* is a separate concern (#43 follow-up: behavior-step → driver-call
|
|
9
|
+
* conventions parity with the prisma step library).
|
|
10
|
+
*/
|
|
11
|
+
import type { TemplateContext } from '@specverse/types';
|
|
12
|
+
|
|
13
|
+
export default function generateMongoNativeService(context: TemplateContext): string {
|
|
14
|
+
const { service } = context;
|
|
15
|
+
if (!service) throw new Error('Service is required in template context');
|
|
16
|
+
|
|
17
|
+
const serviceName = service.name;
|
|
18
|
+
const operationsCode = generateOperations(service);
|
|
19
|
+
const usesDb = /\bgetDb\b|\bgetCollection\b/.test(operationsCode);
|
|
20
|
+
const hasEvents = (service.publishes && service.publishes.length > 0) ||
|
|
21
|
+
(service.subscribes && service.subscribes.length > 0);
|
|
22
|
+
|
|
23
|
+
return `/**
|
|
24
|
+
* ${serviceName}
|
|
25
|
+
* Abstract business logic service (MongoDB native driver)
|
|
26
|
+
* ${service.description || ''}
|
|
27
|
+
*/
|
|
28
|
+
${usesDb ? `import { getDb, getCollection } from '../db/mongoClient.js';` : ''}
|
|
29
|
+
${hasEvents ? `import { eventBus } from '../events/eventBus.js';` : ''}
|
|
30
|
+
|
|
31
|
+
export class ${serviceName} {
|
|
32
|
+
${operationsCode}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const ${lowerFirst(serviceName)} = new ${serviceName}();
|
|
36
|
+
export const ${lowerFirst(serviceName)}Instance = ${lowerFirst(serviceName)};
|
|
37
|
+
export default ${lowerFirst(serviceName)};
|
|
38
|
+
`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function lowerFirst(s: string): string {
|
|
42
|
+
return s.charAt(0).toLowerCase() + s.slice(1);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function generateOperations(service: any): string {
|
|
46
|
+
const ops = service.operations;
|
|
47
|
+
if (!ops || (Array.isArray(ops) && ops.length === 0) || (!Array.isArray(ops) && Object.keys(ops).length === 0)) {
|
|
48
|
+
return `
|
|
49
|
+
/**
|
|
50
|
+
* Default service entrypoint — replace with real operations once declared
|
|
51
|
+
* on the service's spec.
|
|
52
|
+
*/
|
|
53
|
+
public async execute(_params: any = {}): Promise<any> {
|
|
54
|
+
throw new Error('${service.name}.execute is not implemented');
|
|
55
|
+
}
|
|
56
|
+
`;
|
|
57
|
+
}
|
|
58
|
+
const entries: [string, any][] = Array.isArray(ops)
|
|
59
|
+
? ops.map((op: any) => [op.name, op])
|
|
60
|
+
: Object.entries(ops);
|
|
61
|
+
return entries.map(([name, op]) => generateOperation(name, op, service.name)).join('\n');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function generateOperation(operationName: string, operation: any, serviceName: string): string {
|
|
65
|
+
const stepsHeader = (operation.steps && operation.steps.length > 0)
|
|
66
|
+
? operation.steps.map((s: any) => ` * - ${typeof s === 'string' ? s : (s.action || JSON.stringify(s))}`).join('\n')
|
|
67
|
+
: ' * (no spec steps declared)';
|
|
68
|
+
return `
|
|
69
|
+
/**
|
|
70
|
+
* ${operationName}
|
|
71
|
+
* ${operation.description || ''}
|
|
72
|
+
*
|
|
73
|
+
* Spec steps:
|
|
74
|
+
${stepsHeader}
|
|
75
|
+
*/
|
|
76
|
+
public async ${operationName}(_args: any = {}): Promise<any> {
|
|
77
|
+
// TODO (#43 follow-up): translate spec steps into native MongoDB driver
|
|
78
|
+
// calls. For now this is a stub so realize completes and the service
|
|
79
|
+
// surface is callable for parity tests.
|
|
80
|
+
throw new Error('${serviceName}.${operationName} is not implemented');
|
|
81
|
+
}
|
|
82
|
+
`;
|
|
83
|
+
}
|