[英]AWS DynamoDB conditional get
我剛開始使用 DynamoDB,我想不出只有在SSID
有效時才能最有效地檢索用戶數據的方法。
當前方法:
import {DynamoDB} from "aws-sdk"
import * as cookie from "cookie"
import {forObject} from "../../scripts/Common";
import StatusError from "../../classes/StatusError";
export default async function ({headers}) {
const {UNAME, SSID} = cookie.parse(headers.Cookie)
const res = (await new DynamoDB.DocumentClient().get({
TableName: 'WB360-USERS',
Key: {
UNAME
},
AttributesToGet: ["ARTICLES", "SSID"]
}).promise()).Item;
if(res.SSID !== SSID)
throw new StatusError("Invalid SSID",401);
delete res.SSID;
return forObject({},res)
}
在上述方法中,我正在檢索給定用戶的ARTICLES
和SSID
的完整列表,然后將獲取的SSID
與 cookies 中的 SSID 進行比較。 這種方法非常低效。
只有當條件有效時,是否有更好的方法來檢索數據
帶 TS 的ENV NodeJS
閱讀@Rafael Almeida
的回答后,我發現查詢是解決此問題的最佳方法。
const res = (await new DynamoDB.DocumentClient().query({
TableName: 'WB360-USERS',
KeyConditionExpression: 'UNAME = :U ',
FilterExpression: 'SSID = :S',
ProjectionExpression: "ARTICLES",
ExpressionAttributeValues: {
':S': SSID,
':U': UNAME
}
}).promise()).Items[0];
if (!res)
return forError(new StatusError("Invalid SSID or UNAME", 401));
return forObject({}, res)
如果res
未定義,則SSID
無效,反之亦然。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.