mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-12 00:49:14 +08:00
refactor(MessageService): enhance rowToMessage function to handle JSON parsing
- Updated rowToMessage to include a parseJson utility for handling both parsed objects and JSON strings from raw SQL queries. - Ensured that data fields such as data, assistantMeta, modelMeta, and stats are correctly parsed when necessary. - Added documentation to clarify the handling of JSON columns in Drizzle ORM.
This commit is contained in:
parent
d50149dccb
commit
096259cf27
@ -43,23 +43,35 @@ const DEFAULT_LIMIT = 20
|
||||
|
||||
/**
|
||||
* Convert database row to Message entity
|
||||
*
|
||||
* Note: When using raw SQL queries (db.all with sql``), Drizzle ORM does NOT
|
||||
* automatically parse JSON columns. This function handles both parsed objects
|
||||
* (from ORM queries) and JSON strings (from raw SQL queries).
|
||||
*/
|
||||
function rowToMessage(row: typeof messageTable.$inferSelect): Message {
|
||||
// Handle JSON strings from raw SQL queries (db.all with sql``)
|
||||
// ORM queries (.select().from()) return already-parsed objects
|
||||
const parseJson = <T>(value: T | string | null | undefined): T | null => {
|
||||
if (value == null) return null
|
||||
if (typeof value === 'string') return JSON.parse(value)
|
||||
return value as T
|
||||
}
|
||||
|
||||
return {
|
||||
id: row.id,
|
||||
topicId: row.topicId,
|
||||
parentId: row.parentId,
|
||||
role: row.role as Message['role'],
|
||||
data: row.data,
|
||||
data: parseJson(row.data)!,
|
||||
searchableText: row.searchableText,
|
||||
status: row.status as Message['status'],
|
||||
siblingsGroupId: row.siblingsGroupId ?? 0,
|
||||
assistantId: row.assistantId,
|
||||
assistantMeta: row.assistantMeta,
|
||||
assistantMeta: parseJson(row.assistantMeta),
|
||||
modelId: row.modelId,
|
||||
modelMeta: row.modelMeta,
|
||||
modelMeta: parseJson(row.modelMeta),
|
||||
traceId: row.traceId,
|
||||
stats: row.stats,
|
||||
stats: parseJson(row.stats),
|
||||
createdAt: row.createdAt ? new Date(row.createdAt).toISOString() : new Date().toISOString(),
|
||||
updatedAt: row.updatedAt ? new Date(row.updatedAt).toISOString() : new Date().toISOString()
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user