Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | /** * Enhanced Model Templates for Coherent.js CLI * Provides additional model generation templates */ /** * Generate a soft-delete model template */ export function generateSoftDeleteModel(modelName, tableName, fields) { const definitionFields = [ ...fields, { name: 'deleted_at', type: 'date', required: false } ].map(({ name, type, primary, required }) => { const fieldLines = [` ${name}: {`, ` type: '${type}'`]; if (primary) fieldLines.push(' primary: true'); if (required) fieldLines.push(' required: true'); fieldLines.push(' }'); return fieldLines.join('\n'); }).join(',\n'); return `import { createModel as createModelFactory } from '@coherentjs/database'; /** * ${modelName} model with soft delete support */ export const ${modelName} = { tableName: '${tableName}', primaryKey: 'id', timestamps: true, softDelete: true, attributes: { ${definitionFields} }, methods: { isDeleted() { return this.deleted_at !== null; }, restore() { this.deleted_at = null; return this; } }, statics: { async findActive({ model }) { return await model.query({ select: '*', where: { deleted_at: null } }); }, async findDeleted({ model }) { return await model.query({ select: '*', where: 'deleted_at IS NOT NULL' }); } } }; export function register${modelName}Model(dbManager) { const modelManager = createModelFactory(dbManager); return modelManager.registerModel('${modelName}', ${modelName}); } export default ${modelName}; `; } /** * Generate a timestamped model template */ export function generateTimestampedModel(modelName, tableName, fields) { const definitionFields = fields .map(({ name, type, primary, required }) => { const fieldLines = [` ${name}: {`, ` type: '${type}'`]; if (primary) fieldLines.push(' primary: true'); if (required) fieldLines.push(' required: true'); fieldLines.push(' }'); return fieldLines.join('\n'); }) .join(',\n'); return `import { createModel as createModelFactory } from '@coherentjs/database'; /** * ${modelName} model with automatic timestamps */ export const ${modelName} = { tableName: '${tableName}', primaryKey: 'id', timestamps: true, attributes: { ${definitionFields} }, hooks: { beforeCreate(data) { data.created_at = new Date(); data.updated_at = new Date(); return data; }, beforeUpdate(data) { data.updated_at = new Date(); return data; } }, methods: { isRecent() { const dayAgo = new Date(Date.now() - 24 * 60 * 60 * 1000); return this.created_at > dayAgo; } } }; export function register${modelName}Model(dbManager) { const modelManager = createModelFactory(dbManager); return modelManager.registerModel('${modelName}', ${modelName}); } export default ${modelName}; `; } /** * Field type mappings with validation */ export const FIELD_TYPE_MAPPINGS = { string: { jsType: 'string', dbType: 'VARCHAR(255)' }, text: { jsType: 'string', dbType: 'TEXT' }, number: { jsType: 'number', dbType: 'INTEGER' }, float: { jsType: 'number', dbType: 'FLOAT' }, boolean: { jsType: 'boolean', dbType: 'BOOLEAN' }, date: { jsType: 'Date', dbType: 'TIMESTAMP' }, json: { jsType: 'object', dbType: 'JSON' }, uuid: { jsType: 'string', dbType: 'UUID' } }; |