tethys.backend/app/models/activity.ts
Arno Kaimbacher 7e2f320b4f
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 44s
feat: implement activity logging for user actions and create activities table
2026-06-24 15:03:17 +02:00

57 lines
1.7 KiB
TypeScript

// app/models/activity.ts
import { DateTime } from 'luxon';
import { belongsTo, column } from '@adonisjs/lucid/orm';
import BaseModel from './base_model.js';
import type { BelongsTo } from '@adonisjs/lucid/types/relations';
import User from '#models/user';
import { SnakeCaseNamingStrategy } from '@adonisjs/lucid/orm';
export default class Activity extends BaseModel {
public static namingStrategy = new SnakeCaseNamingStrategy();
public static primaryKey = 'id';
public static table = 'activities';
@column({ isPrimary: true })
declare id: number;
@column()
declare type: string;
@column()
declare userId: number | null;
@column()
declare subjectType: string | null;
@column()
declare subjectId: number | null;
@column()
declare description: string;
// Manual JSON (de)serialization keeps this working on SQLite/MySQL.
// On Postgres json/jsonb the driver already parses — drop the `consume`
// JSON.parse there to avoid double-handling.
// @column({
// prepare: (value: Record<string, any> | null) => (value ? JSON.stringify(value) : null),
// consume: (value: string | null) => (value ? JSON.parse(value) : null),
// })
// declare properties: Record<string, any> | null;
@column()
declare properties: Record<string, any> | null;
@column.dateTime({ autoCreate: true })
declare createdAt: DateTime;
@column.dateTime({ autoCreate: true, autoUpdate: true })
declare updatedAt: DateTime;
// @belongsTo(() => User)
// declare user: BelongsTo<typeof User>;
@belongsTo(() => User, {
foreignKey: 'userId',
})
declare user: BelongsTo<typeof User>;
}