47 lines
1.7 KiB
TypeScript
Executable File
47 lines
1.7 KiB
TypeScript
Executable File
const typeMap: Record<string, { type: string; format?: string }> = {
|
|
number: { type: 'integer', format: 'int64' },
|
|
string: { type: 'string' },
|
|
Date: { type: 'string', format: 'date-time' },
|
|
boolean: { type: 'boolean' },
|
|
}
|
|
|
|
/** Legacy schema helper retained for callers that need a property map. */
|
|
export async function modelToSchema(model: any) {
|
|
const properties: Record<string, unknown> = {}
|
|
for (const [key, meta] of model.$columnsDefinitions.entries()) {
|
|
const tsType = meta.meta?.designType?.name || 'string'
|
|
properties[meta?.serializeAs || key] = {
|
|
...(typeMap[tsType] ?? { type: 'string' }),
|
|
description: key.replace(/([A-Z])/g, ' $1').trim(),
|
|
}
|
|
}
|
|
return JSON.stringify(properties)
|
|
}
|
|
|
|
/**
|
|
* Produces a deterministic, concrete JSON object for AutoSwagger response
|
|
* annotations. It intentionally omits Lucid columns hidden from serialization.
|
|
*/
|
|
export async function modelToExample(model: any) {
|
|
const example: Record<string, string | number | boolean> = {}
|
|
|
|
for (const [key, meta] of model.$columnsDefinitions.entries()) {
|
|
if (meta.serializeAs === null) continue
|
|
|
|
const serializedKey = meta.serializeAs || key
|
|
const type = meta.meta?.designType?.name || 'string'
|
|
example[serializedKey] = sampleValue(serializedKey, type)
|
|
}
|
|
|
|
return JSON.stringify(example)
|
|
}
|
|
|
|
function sampleValue(key: string, type: string): string | number | boolean {
|
|
if (type === 'number') return key === 'id' || key.endsWith('_id') ? 42 : 100
|
|
if (type === 'boolean') return true
|
|
if (type === 'Date') return '2025-01-15T10:30:00.000Z'
|
|
if (key.includes('email')) return 'user@example.test'
|
|
if (key.includes('url')) return 'https://example.test/resource'
|
|
return `Example ${key.replace(/_/g, ' ')}`
|
|
}
|