Skip to content

Commit

Permalink
performance boost
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Nov 23, 2023
1 parent 746172b commit 5f8282d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions database/getAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ export async function getAssetByAssetAlias(assetAlias, aliasTypeId, connectedEmi
sql += ' and aliasTypeId = ?';
sqlParameters.push(aliasTypeId);
}
const assetId = emileDB.prepare(sql).get(sqlParameters);
const assetId = emileDB.prepare(sql).pluck().get(sqlParameters);
if (assetId !== undefined) {
asset = getAsset(assetId.assetId, emileDB);
asset = getAsset(assetId, emileDB);
}
if (connectedEmileDB === undefined) {
emileDB.close();
Expand Down
6 changes: 3 additions & 3 deletions database/getAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ export async function getAssetByAssetAlias(
sqlParameters.push(aliasTypeId)
}

const assetId = emileDB.prepare(sql).get(sqlParameters) as
| { assetId: number }
const assetId = emileDB.prepare(sql).pluck().get(sqlParameters) as
| number
| undefined

if (assetId !== undefined) {
asset = getAsset(assetId.assetId, emileDB)
asset = getAsset(assetId, emileDB)
}

if (connectedEmileDB === undefined) {
Expand Down
5 changes: 3 additions & 2 deletions database/isValidUserReportKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const accessMillis = getConfigProperty('settings.reportKeyAccessDays') * 86400 *
export function isValidUserReportKey(reportKey, requestIp) {
const emileDB = sqlite(databasePath, { readonly: true });
const ipAddress = isLocal(requestIp) ? 'localhost' : requestIp;
const result = emileDB
const userName = emileDB
.prepare(`select u.userName
from UserAccessLog l
left join Users u on l.userName = u.userName
Expand All @@ -15,7 +15,8 @@ export function isValidUserReportKey(reportKey, requestIp) {
and u.recordDelete_timeMillis is null
and u.canLogin = 1
and ? - l.accessTimeMillis <= ?`)
.pluck()
.get(ipAddress, reportKey, Date.now(), accessMillis);
emileDB.close();
return (result?.userName ?? '') !== '';
return (userName ?? '') !== '';
}
9 changes: 4 additions & 5 deletions database/isValidUserReportKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function isValidUserReportKey(

const ipAddress = isLocal(requestIp) ? 'localhost' : requestIp

const result = emileDB
const userName = emileDB
.prepare(
`select u.userName
from UserAccessLog l
Expand All @@ -26,11 +26,10 @@ export function isValidUserReportKey(
and u.canLogin = 1
and ? - l.accessTimeMillis <= ?`
)
.get(ipAddress, reportKey, Date.now(), accessMillis) as
| { userName: string }
| undefined
.pluck()
.get(ipAddress, reportKey, Date.now(), accessMillis) as string | undefined

emileDB.close()

return (result?.userName ?? '') !== ''
return (userName ?? '') !== ''
}

0 comments on commit 5f8282d

Please sign in to comment.