Use upsert for the app list with mysql

This commit is contained in:
Jonas Lochmann 2022-09-12 02:00:00 +02:00
parent d808661ebe
commit 1414e41672
No known key found for this signature in database
GPG key ID: 8B8C9AEE10FA5B36

View file

@ -26,41 +26,6 @@ export async function dispatchUpdateInstalledApps ({ deviceId, action, cache }:
cache: Cache cache: Cache
}) { }) {
async function upsert({ type, data }: { type: number, data: Buffer }) { async function upsert({ type, data }: { type: number, data: Buffer }) {
const dialect = cache.database.dialect
const isMysql = dialect === 'mysql' || dialect === 'mariadb'
if (isMysql) {
const counter = await cache.database.encryptedAppList.count({
where: {
familyId: cache.familyId,
deviceId,
type
},
transaction: cache.transaction
})
if (counter === 0) {
await cache.database.encryptedAppList.create({
familyId: cache.familyId,
deviceId,
type,
version: generateVersionId(),
data
}, { transaction: cache.transaction })
} else {
await cache.database.encryptedAppList.update({
data,
version: generateVersionId()
}, {
where: {
familyId: cache.familyId,
deviceId,
type
},
transaction: cache.transaction
})
}
} else {
await cache.database.encryptedAppList.upsert({ await cache.database.encryptedAppList.upsert({
familyId: cache.familyId, familyId: cache.familyId,
deviceId, deviceId,
@ -69,7 +34,6 @@ export async function dispatchUpdateInstalledApps ({ deviceId, action, cache }:
data data
}, { transaction: cache.transaction }) }, { transaction: cache.transaction })
} }
}
if (action.base) { if (action.base) {
await upsert({ type: types.base, data: action.base }) await upsert({ type: types.base, data: action.base })