Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

fix(backend:database): support pre-parsed JSON results from mysql2

johaven98072f0757dc
diff --git a/backend/src/applications/shares/services/shares-queries.service.ts b/backend/src/applications/shares/services/shares-queries.service.ts
index 0c0850fd..cc236b5d 100644
--- a/backend/src/applications/shares/services/shares-queries.service.ts
+++ b/backend/src/applications/shares/services/shares-queries.service.ts
@@ -14,7 +14,8 @@ import {
convertToWhere,
dateTimeUTC,
dbCheckAffectedRows,
- dbGetInsertedId
+ dbGetInsertedId,
+ dbParseJson
} from '../../../infrastructure/database/utils'
import { fileHasCommentsSubquerySQL } from '../../comments/schemas/comments.schema'
import type { FileProps } from '../../files/interfaces/file-props.interface'
@@ -772,12 +773,12 @@ export class SharesQueries {
alias: unionAlias.childShareAlias,
name: unionAlias.childShareName,
type: unionAlias.childShareType
- })}, '[]')`.mapWith(JSON.parse),
+ })}, '[]')`.mapWith(dbParseJson),
syncs: sql`IF (${sql.placeholder('withSyncs')}, ${concatDistinctObjectsInArray(unionAlias.syncPathId, {
id: unionAlias.syncPathId,
clientId: unionAlias.syncPathClientId,
clientName: unionAlias.syncPathClientName
- })}, '[]')`.mapWith(JSON.parse),
+ })}, '[]')`.mapWith(dbParseJson),
hasComments: sql<boolean>`IF (${sql.placeholder('withHasComments')}, ${fileHasCommentsSubquerySQL(unionAlias.id)}, 0)`.mapWith(Boolean)
}
this.shareRootFilesQuery = this.db.select(select).from(unionAlias).groupBy(unionAlias.rootId).prepare()
diff --git a/backend/src/applications/spaces/services/spaces-queries.service.ts b/backend/src/applications/spaces/services/spaces-queries.service.ts
index fee71749..04d2b1fb 100644
--- a/backend/src/applications/spaces/services/spaces-queries.service.ts
+++ b/backend/src/applications/spaces/services/spaces-queries.service.ts
@@ -14,7 +14,8 @@ import {
convertToWhere,
dateTimeUTC,
dbCheckAffectedRows,
- dbGetInsertedId
+ dbGetInsertedId,
+ dbParseJson
} from '../../../infrastructure/database/utils'
import { fileHasCommentsSubquerySQL } from '../../comments/schemas/comments.schema'
import { FileDBProps } from '../../files/interfaces/file-db-props.interface'
@@ -91,7 +92,7 @@ export class SpacesQueries {
alias: spaces.alias,
storageUsage: spaces.storageUsage,
storageQuota: spaces.storageQuota,
- externalPaths: sql<string[]>`JSON_ARRAYAGG(${spacesRoots.externalPath})`.mapWith(JSON.parse).as('externalPaths')
+ externalPaths: sql<string[]>`JSON_ARRAYAGG(${spacesRoots.externalPath})`.mapWith(dbParseJson).as('externalPaths')
})
.from(spaces)
.leftJoin(spacesRoots, and(eq(spacesRoots.spaceId, spaces.id), isNotNull(spacesRoots.externalPath)))
@@ -271,12 +272,12 @@ export class SpacesQueries {
alias: shares.alias,
name: shares.name,
type: shares.type
- })}, '[]')`.mapWith(JSON.parse),
+ })}, '[]')`.mapWith(dbParseJson),
syncs: sql`IF (${sql.placeholder('withSyncs')}, ${concatDistinctObjectsInArray(syncPaths.id, {
id: syncPaths.id,
clientId: syncClients.id,
clientName: sql`JSON_VALUE(${syncClients.info}, '$.node')`
- })}, '[]')`.mapWith(JSON.parse),
+ })}, '[]')`.mapWith(dbParseJson),
hasComments: sql<boolean>`IF (${sql.placeholder('withHasComments')}, ${fileHasCommentsSubquerySQL(files.id)}, 0)`.mapWith(Boolean)
}
this.spaceRootFilesQuery = this.db
diff --git a/backend/src/applications/users/services/users-queries.service.ts b/backend/src/applications/users/services/users-queries.service.ts
index eba80d11..32876f84 100644
--- a/backend/src/applications/users/services/users-queries.service.ts
+++ b/backend/src/applications/users/services/users-queries.service.ts
@@ -13,7 +13,8 @@ import {
convertToSelect,
dateTimeUTC,
dbCheckAffectedRows,
- dbGetInsertedId
+ dbGetInsertedId,
+ dbParseJson
} from '../../../infrastructure/database/utils'
import { GROUP_TYPE, GROUP_VISIBILITY } from '../constants/group'
import { MEMBER_TYPE } from '../constants/member'
@@ -589,7 +590,7 @@ export class UsersQueries {
) AS usersUnion
`
const [r] = await this.db.execute(userIds)
- return JSON.parse(r[0].ids) || []
+ return dbParseJson(r[0].ids) || []
}
@CacheDecorator(1800)
@@ -620,7 +621,7 @@ export class UsersQueries {
)
)
const [r] = await this.db.execute(q)
- return JSON.parse(r[0].ids) || []
+ return dbParseJson(r[0].ids) || []
}
async clearWhiteListCaches(userIds: number[] | '*'): Promise<void> {
diff --git a/backend/src/infrastructure/database/utils.ts b/backend/src/infrastructure/database/utils.ts
index 8b7400e2..42dd5407 100644
--- a/backend/src/infrastructure/database/utils.ts
+++ b/backend/src/infrastructure/database/utils.ts
@@ -63,6 +63,10 @@ export function convertToWhere<T>(table: T, filters: Partial<Record<keyof T, any
export const dateTimeUTC = (dateField: Column): SQL<string> => sql`CONCAT(${sql`${dateField}`}, 'Z')`
+export function dbParseJson(value: unknown): any {
+ return typeof value === 'string' ? JSON.parse(value) : value
+}
+
export function concatDistinctObjectsInArray(mustBeNotNull: Column, object: Record<string, Column | object>): SQL<any> {
/* Concat json objects in array (only one nesting level is supported) */
const expr: SQL<any> = sql`IFNULL(CONCAT('[', GROUP_CONCAT(DISTINCT IF(${mustBeNotNull} IS NULL, NULL, JSON_OBJECT(`
@@ -82,7 +86,7 @@ export function concatDistinctObjectsInArray(mustBeNotNull: Column, object: Reco
}
})
expr.append(sql`))), ']'), JSON_ARRAY())`)
- expr.mapWith(JSON.parse)
+ expr.mapWith(dbParseJson)
return expr
}