Skip to content

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

refactor(backend:database): add JSON driver for SQL columns on MariaDB (TEXT storage)

johaven89bb4ee1c246
diff --git a/backend/src/applications/notifications/schemas/notifications.schema.ts b/backend/src/applications/notifications/schemas/notifications.schema.ts
index b78be4e9..f9e4cd52 100644
--- a/backend/src/applications/notifications/schemas/notifications.schema.ts
+++ b/backend/src/applications/notifications/schemas/notifications.schema.ts
@@ -5,7 +5,8 @@
*/
import { sql } from 'drizzle-orm'
-import { bigint, boolean, datetime, index, json, mysqlTable } from 'drizzle-orm/mysql-core'
+import { bigint, boolean, datetime, index, mysqlTable } from 'drizzle-orm/mysql-core'
+import { jsonColumn } from '../../../infrastructure/database/columns'
import { users } from '../../users/schemas/users.schema'
import type { NotificationContent } from '../interfaces/notification-properties.interface'
@@ -17,7 +18,7 @@ export const notifications = mysqlTable(
toUserId: bigint('toUserId', { mode: 'number', unsigned: true })
.references(() => users.id, { onDelete: 'cascade' })
.notNull(),
- content: json('content').$type<NotificationContent>(),
+ content: jsonColumn<NotificationContent>()('content'),
wasRead: boolean('wasRead').default(false).notNull(),
createdAt: datetime('createdAt', { mode: 'date' })
.default(sql`CURRENT_TIMESTAMP`)
diff --git a/backend/src/applications/notifications/services/notifications-queries.service.ts b/backend/src/applications/notifications/services/notifications-queries.service.ts
index a8206ead..4463d5b1 100644
--- a/backend/src/applications/notifications/services/notifications-queries.service.ts
+++ b/backend/src/applications/notifications/services/notifications-queries.service.ts
@@ -5,7 +5,7 @@
*/
import { Inject, Injectable } from '@nestjs/common'
-import { and, desc, eq, inArray, SelectedFields, sql, SQL } from 'drizzle-orm'
+import { and, desc, eq, inArray, SelectedFields, SQL } from 'drizzle-orm'
import { DB_TOKEN_PROVIDER } from '../../../infrastructure/database/constants'
import { DBSchema } from '../../../infrastructure/database/interfaces/database.interface'
import { dbCheckAffectedRows } from '../../../infrastructure/database/utils'
@@ -26,7 +26,7 @@ export class NotificationsQueries {
.select({
id: notifications.id,
fromUser: { id: users.id, login: users.login, email: users.email, fullName: userFullNameSQL(users) },
- content: sql`${notifications.content}`.mapWith(JSON.parse),
+ content: notifications.content,
wasRead: notifications.wasRead,
createdAt: notifications.createdAt
} satisfies NotificationFromUser | SelectedFields<any, any>)
diff --git a/backend/src/applications/sync/schemas/sync-clients.schema.ts b/backend/src/applications/sync/schemas/sync-clients.schema.ts
index 896c6965..d0726c48 100644
--- a/backend/src/applications/sync/schemas/sync-clients.schema.ts
+++ b/backend/src/applications/sync/schemas/sync-clients.schema.ts
@@ -5,7 +5,8 @@
*/
import { sql } from 'drizzle-orm'
-import { bigint, boolean, char, datetime, index, json, mysqlTable, varchar } from 'drizzle-orm/mysql-core'
+import { bigint, boolean, char, datetime, index, mysqlTable, varchar } from 'drizzle-orm/mysql-core'
+import { jsonColumn } from '../../../infrastructure/database/columns'
import { users } from '../../users/schemas/users.schema'
import type { SyncClientInfo } from '../interfaces/sync-client.interface'
@@ -18,7 +19,7 @@ export const syncClients = mysqlTable(
.notNull(),
token: char('token', { length: 36 }).notNull(),
tokenExpiration: bigint('tokenExpiration', { mode: 'number', unsigned: true }).notNull(),
- info: json('info').$type<SyncClientInfo>().notNull(),
+ info: jsonColumn<SyncClientInfo>()('info').notNull(),
enabled: boolean('enabled').default(true).notNull(),
currentIp: varchar('currentIp', { length: 45 }),
lastIp: varchar('lastIp', { length: 45 }),
diff --git a/backend/src/applications/sync/schemas/sync-paths.schema.ts b/backend/src/applications/sync/schemas/sync-paths.schema.ts
index e4df60a2..293b0f8a 100644
--- a/backend/src/applications/sync/schemas/sync-paths.schema.ts
+++ b/backend/src/applications/sync/schemas/sync-paths.schema.ts
@@ -5,7 +5,8 @@
*/
import { sql } from 'drizzle-orm'
-import { bigint, char, datetime, index, json, mysqlTable } from 'drizzle-orm/mysql-core'
+import { bigint, char, datetime, index, mysqlTable } from 'drizzle-orm/mysql-core'
+import { jsonColumn } from '../../../infrastructure/database/columns'
import { files } from '../../files/schemas/files.schema'
import { shares } from '../../shares/schemas/shares.schema'
import { spacesRoots } from '../../spaces/schemas/spaces-roots.schema'
@@ -34,7 +35,7 @@ export const syncPaths = mysqlTable(
spaceRootId: bigint('spaceRootId', { mode: 'number', unsigned: true }).references(() => spacesRoots.id, { onDelete: 'cascade' }),
shareId: bigint('shareId', { mode: 'number', unsigned: true }).references(() => shares.id, { onDelete: 'cascade' }),
fileId: bigint('fileId', { mode: 'number', unsigned: true }).references(() => files.id, { onDelete: 'cascade' }),
- settings: json('settings').$type<SyncPathSettings>().notNull(),
+ settings: jsonColumn<SyncPathSettings>()('settings').notNull(),
createdAt: datetime('createdAt', { mode: 'date' })
.default(sql`CURRENT_TIMESTAMP`)
.notNull()
diff --git a/backend/src/applications/sync/services/sync-queries.service.ts b/backend/src/applications/sync/services/sync-queries.service.ts
index 8419caa1..afb43143 100644
--- a/backend/src/applications/sync/services/sync-queries.service.ts
+++ b/backend/src/applications/sync/services/sync-queries.service.ts
@@ -97,7 +97,7 @@ export class SyncQueries {
ownerId: syncClients.ownerId,
token: syncClients.token,
tokenExpiration: syncClients.tokenExpiration,
- info: sql`${syncClients.info}`.mapWith(JSON.parse),
+ info: syncClients.info,
enabled: syncClients.enabled,
currentAccess: syncClients.currentAccess,
currentIp: syncClients.currentIp,
@@ -117,7 +117,7 @@ export class SyncQueries {
.select({
id: syncClients.id,
tokenExpiration: syncClients.tokenExpiration,
- info: sql`${syncClients.info}`.mapWith(JSON.parse),
+ info: syncClients.info,
enabled: syncClients.enabled,
currentAccess: syncClients.currentAccess,
currentIp: syncClients.currentIp,
@@ -127,7 +127,7 @@ export class SyncQueries {
isCurrentClient: sql<boolean>`IF (${clientId} IS NOT NULL AND ${syncClients.id} = ${clientId}, 1, 0)`.mapWith(Boolean),
paths: concatDistinctObjectsInArray(syncPaths.id, {
id: syncPaths.id,
- settings: sql`${syncPaths.settings}`.mapWith(JSON.parse),
+ settings: syncPaths.settings,
createdAt: syncPaths.createdAt
})
})
@@ -203,7 +203,7 @@ export class SyncQueries {
ELSE NULL
END
`.as('remotePath'),
- settings: sql`${syncPaths.settings}`.mapWith(JSON.parse)
+ settings: syncPaths.settings
})
.from(syncPaths)
.leftJoin(files, eq(files.id, syncPaths.fileId))
@@ -219,7 +219,7 @@ export class SyncQueries {
async getPathSettings(clientId: string, pathId: number): Promise<SyncPathSettings> {
const [path] = await this.db
.select({
- settings: sql`${syncPaths.settings}`.mapWith(JSON.parse)
+ settings: syncPaths.settings
})
.from(syncPaths)
.where(and(eq(syncPaths.clientId, clientId), eq(syncPaths.id, pathId)))
diff --git a/backend/src/infrastructure/cache/adapters/mysql-cache.adapter.ts b/backend/src/infrastructure/cache/adapters/mysql-cache.adapter.ts
index 8d755da0..2bf194d3 100644
--- a/backend/src/infrastructure/cache/adapters/mysql-cache.adapter.ts
+++ b/backend/src/infrastructure/cache/adapters/mysql-cache.adapter.ts
@@ -7,7 +7,7 @@
import { Inject, Injectable, Logger } from '@nestjs/common'
import { SchedulerRegistry } from '@nestjs/schedule'
import { CronJob } from 'cron'
-import { and, between, eq, exists, inArray, like, notBetween, sql, SQL } from 'drizzle-orm'
+import { and, between, eq, exists, inArray, like, notBetween, SQL } from 'drizzle-orm'
import cluster from 'node:cluster'
import { createSlug, currentTimeStamp } from '../../../common/shared'
import { configuration } from '../../../configuration/config.environment'
@@ -30,8 +30,6 @@ export class MysqlCacheAdapter implements Cache {
private readonly scheduledJobName = 'cache_expired_keys' as const
private readonly scheduledJobInterval = 5 // minutes
private readonly logger = new Logger(Cache.name.toUpperCase())
- private readonly whereNotExpired: () => SQL = () => notBetween(cache.expiration, 0, currentTimeStamp())
- private readonly whereExpired: () => SQL = () => between(cache.expiration, 0, currentTimeStamp())
constructor(
@Inject(DB_TOKEN_PROVIDER) private readonly db: DBSchema,
@@ -84,7 +82,7 @@ export class MysqlCacheAdapter implements Cache {
async get(key: string): Promise<any> {
const [v]: { value: any }[] = await this.db
- .select({ value: sql`${cache.value}`.mapWith(JSON.parse) })
+ .select({ value: cache.value })
.from(cache)
.where(and(eq(cache.key, key), this.whereNotExpired()))
.limit(1)
@@ -93,7 +91,7 @@ export class MysqlCacheAdapter implements Cache {
async mget(keys: string[]): Promise<any[]> {
const vs: { value: any }[] = await this.db
- .select({ value: sql`${cache.value}`.mapWith(JSON.parse) })
+ .select({ value: cache.value })
.from(cache)
.where(and(inArray(cache.key, keys), this.whereNotExpired()))
return vs.map((v: { value: any }) => v.value)
@@ -131,6 +129,14 @@ export class MysqlCacheAdapter implements Cache {
return createSlug(args.join(' '))
}
+ async quit(): Promise<void> {
+ this.logger.verbose(`${this.quit.name}`)
+ }
+
+ private readonly whereNotExpired: () => SQL = () => notBetween(cache.expiration, 0, currentTimeStamp())
+
+ private readonly whereExpired: () => SQL = () => between(cache.expiration, 0, currentTimeStamp())
+
private getTTL(ttl: number): number {
/* ttl (seconds):
- 0 : infinite expiration
@@ -139,10 +145,6 @@ export class MysqlCacheAdapter implements Cache {
return ttl ? currentTimeStamp() + ttl : ttl === 0 ? this.infiniteExpiration : currentTimeStamp() + this.defaultTTL
}
- async quit(): Promise<void> {
- this.logger.verbose(`${this.quit.name}`)
- }
-
private serialize(data: any) {
if (data === undefined) {
// undefined values are not handled by JSON serialization
diff --git a/backend/src/infrastructure/cache/schemas/mysql-cache.schema.ts b/backend/src/infrastructure/cache/schemas/mysql-cache.schema.ts
index c8297ee7..538914a0 100644
--- a/backend/src/infrastructure/cache/schemas/mysql-cache.schema.ts
+++ b/backend/src/infrastructure/cache/schemas/mysql-cache.schema.ts
@@ -4,13 +4,14 @@
* See the LICENSE file for licensing details
*/
-import { index, int, json, mysqlTable, varchar } from 'drizzle-orm/mysql-core'
+import { index, int, mysqlTable, varchar } from 'drizzle-orm/mysql-core'
+import { jsonColumn } from '../../database/columns'
export const cache = mysqlTable(
'cache',
{
key: varchar('key', { length: 768 }).primaryKey(),
- value: json('value').$type<any>(),
+ value: jsonColumn<any>()('value'),
expiration: int('expiration').default(-1).notNull()
},
(table) => [index('expiration_idx').on(table.expiration)]