Procházet zdrojové kódy

feat(database): 更新 OCR 记录和属性相关表结构

-为 ocr_records、ocr_channels 和 ocr_devices 表的 created_at 字段添加默认值
- 新增 properties 表,用于存储属性信息
- 更新 Property 模型,添加 PropertyType 枚举和 remark 字段
- 修改 PropertiesController,支持新增的属性类型和备注字段
wui před 9 měsíci
rodič
revize
ef2804d75d

+ 5 - 7
app/Controllers/Http/PropertiesController.ts

@@ -1,5 +1,5 @@
 import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
-import Property from 'App/Models/Property'
+import Property, { PropertyType } from 'App/Models/Property'
 import PaginationService from 'App/Services/PaginationService'
 import { schema, rules } from '@ioc:Adonis/Core/Validator'
 import { Exception } from '@adonisjs/core/build/standalone'
@@ -15,13 +15,10 @@ export default class PropertiesController {
         await bouncer.authorize('admin')
         const data = await request.validate({
             schema: schema.create({
-                id: schema.string({ trim: true }, [
-                    rules.regex(/^[a-zA-Z0-9_]+$/),
-                    rules.maxLength(72)
-                ]),
                 name: schema.string(),
                 value: schema.string(),
-                type: schema.string()
+                remark: schema.string(),
+                type: schema.enum(Object.values(PropertyType))
             })
         })
 
@@ -49,8 +46,9 @@ export default class PropertiesController {
         const data = await request.validate({
             schema: schema.create({
                 name: schema.string.optional([]),
+                remark: schema.string.optional(),
                 value: schema.string.optional(),
-                type: schema.string.optional()
+                type: schema.enum(Object.values(PropertyType))
             })
         })
         const property = await Property.findOrFail(params.id)

+ 16 - 2
app/Models/Property.ts

@@ -1,9 +1,20 @@
 import { DateTime } from 'luxon'
 import { BaseModel, column } from '@ioc:Adonis/Lucid/Orm'
 
+export enum PropertyType {
+    String = 'string',
+    Date = 'date',
+    Number = 'number',
+    Boolean = 'boolean',
+    Object = 'object',
+    File = 'file',
+    TimeRange = 'time_range',
+    Range = 'range'
+}
+
 export default class Property extends BaseModel {
     @column({ isPrimary: true })
-    public id: string
+    public id: number
 
     @column.dateTime({ autoCreate: true })
     public createdAt: DateTime
@@ -18,5 +29,8 @@ export default class Property extends BaseModel {
     public value: string
 
     @column()
-    public type: string
+    public remark: string
+
+    @column()
+    public type: PropertyType
 }

+ 1 - 1
database/migrations/1740987471879_ocr_records.ts

@@ -10,8 +10,8 @@ export default class extends BaseSchema {
             table.text('record').notNullable()
             table.string('channel').notNullable()
             table.string('img').notNullable()
-            table.timestamp('created_at', { useTz: true })
             table.timestamp('updated_at', { useTz: true })
+            table.timestamp('created_at', { useTz: true }).defaultTo(this.now())
         })
     }
 

+ 1 - 1
database/migrations/1741164449535_ocr_channels.ts

@@ -10,8 +10,8 @@ export default class extends BaseSchema {
             table.integer('device_num').defaultTo(0).notNullable()
             table.integer('record_num').defaultTo(0).notNullable()
             table.integer('scan_num').defaultTo(0).notNullable()
-            table.timestamp('created_at', { useTz: true })
             table.timestamp('updated_at', { useTz: true })
+            table.timestamp('created_at', { useTz: true }).defaultTo(this.now())
         })
     }
 

+ 1 - 1
database/migrations/1741251032761_ocr_devices.ts

@@ -6,8 +6,8 @@ export default class extends BaseSchema {
     public async up() {
         this.schema.createTable(this.tableName, (table) => {
             table.string('id', 70).primary()
-            table.timestamp('created_at', { useTz: true })
             table.timestamp('updated_at', { useTz: true })
+            table.timestamp('created_at', { useTz: true }).defaultTo(this.now())
             table.string('platform').notNullable()
             table.string('device_info').nullable()
             table.string('channel').notNullable()

+ 26 - 0
database/migrations/1743410408089_properties.ts

@@ -0,0 +1,26 @@
+import BaseSchema from '@ioc:Adonis/Lucid/Schema'
+import { PropertyType } from 'App/Models/Property'
+
+export default class extends BaseSchema {
+    protected tableName = 'properties'
+
+    public async up() {
+        this.schema.alterTable(this.tableName, (table) => {
+            table.dropPrimary()
+        })
+
+        this.schema.alterTable(this.tableName, (table) => {
+            table.dropColumn('id')
+        })
+
+        this.schema.alterTable(this.tableName, (table) => {
+            table.increments('id').primary()
+            table.enum('type', Object.values(PropertyType)).alter()
+            table.string('remark').notNullable()
+        })
+    }
+
+    public async down() {
+        this.schema.dropTable(this.tableName)
+    }
+}