Faceteer 表达式构建器
Faceteer 表达式构建器是一组可用于创建的函数 过滤表达式 用于 Dynamo DB 查询,或 条件表达式 页:1 PutItem, (中文(简体) ), UpdateItem,以及 DeleteItem 操作.
安装
用 npm 安装 :
npm i @faceteer/expression-builder --save
用纱线安装 :
yarn add @faceteer/expression-builder
条件表达式
条件表达式用于要有条件地放置,更新,或从DynatomDB删除某个项目时.
条件表达式构建器将在代表条件表达式的数组中取出,并会用占位符生成属性名称和值的表达式.
条件表达式支持下列操作:
=, (中文(简体) ), <, (中文(简体) ), <=, (中文(简体) ), >, (中文(简体) ), >=, (中文(简体) ), <>
// The status is active
["status", "=", "active"];
// Age is greater than or equal to 65
["age", ">=", 65];
// The last updated time was before Jan 5, 2020
["lastUpdated", "<", new Date("2020-01-05")];
between
// Ids between 57242 and 99980
["id", "between", 57242, 99980];
// Last name starts with "m", "n", or "o"
["lastName", "between", "m", "O"];
begins_with
// The industry begins with marketing
["industry", "begins_with", "marketing"];
// Zip code begins with 11
["zipCode", "begins_with", "11"];
exists
// The attribute billingPlan exists
["billingPlan", "exists"];
not_exists
// The attribute deletedAt does not exist
["deletedAt", "not_exists"];
contains
// The company name contains the letters "LLC"
["companyName", "contains", "LLC"];
// The array of users contains the name "larry"
["users", "contains", "larry"];
size
// Users where the first name is greater than 20 characters
["firstName", "size", ">=", 20];
// Images where the size is less than 2048 bytes
["image", "size", "<", 2048];
// Products where there is only one review
["reviews", "size", "=", 1];
in
// Posts that have a status of draft or queued
["postStatus", "in", ["draft", "queued"]];
AND, (中文(简体) ), OR
// An active user with a billing plan
[["billingPlan", "exists"], "AND", ["status", "=", "active"]];
// Users with a first name longer than 20 characters or a missing first name
[["firstName", "size", ">=", 20], "OR", ["firstName", "not_exists"]];
过滤表达式
要查询 Dynamo DB 时会使用过滤表达式, 但只想要被询问结果的子集 .
滤波器表达式构建器将采用代表滤波器表达式的阵列,并生成属性名称和值的占位符表达式.
下列操作用于过滤表达式:
=<<=>=<>betweenbegins_with
除了所有操作 between 过滤表达式阵列的结构为 [{attribute}, {operand}, {value}]。 。 。 .
// Users that signed during or after 2020
const newUsersFilter = expressionBuilder.filter(["signedUpDate", ">=", "2020"]);
// Active users
const activeUsersFilter = expressionBuilder.filter(["status", "=", "active"]);
// Users who's industry begins with marketing
const industryFilter = expressionBuilder.filter([
"industry",
"begins_with",
"marketing",
]);
对于 between 过滤表达式阵列的结构为 [{attribute}, "between", {start}, {end}]。 。 。 .
// Users that signed up between 2015 and 2016
const signUpRangeFilter = expressionBuilder.filter([
"signedUpDate",
"between",
"2015",
"2016",
]);
您也可以使用 AND 财务报告和审定财务报表 OR 将多个语句连在一起的操作.
// Active users who signed up before 2010
const signUpFilter = builder.filter([
["signedUpDate", ">", "2010"],
"AND",
["status", "=", "active"],
]);
使用量
编译的表达式将返回具有三个属性的对象.
| 财产 | 说明 |
|---|---|
names | 包含由占位符索引的属性名的地图. |
values | 包含由他们的占位符索引的Dynamo DB格式中的属性值的地图. |
expression | 过滤表达式字符串 . |
这些可以直接与 DynamoDB 客户端在 aws-sdk
// Using a condition expression for a put
import * as builder from "./condition";
import { DynamoDB } from "aws-sdk";
import { Converter } from "@faceteer/converter";
const ddb = new DynamoDB();
const statusCondition = builder.condition([
"postStatus",
"in",
["Retry", "Pending"],
]);
ddb.putItem({
TableName: "Posts",
Item: Converter.marshall(post),
ConditionExpression: statusCondition.expression,
ExpressionAttributeNames: statusCondition.names,
ExpressionAttributeValues: statusCondition.values,
});
// Using a filter expression for a query
import * as expressionBuilder from "@faceteer/expression-builder";
import { DynamoDB } from "aws-sdk";
const ddb = new DynamoDB();
// Filter for users that signed during or after 2020
const signUpFilter = expressionBuilder.filter(["signedUpDate", ">=", "2020"]);
// Query the Dynamo DB table and add the placeholder
// names, values, and the filter expression
ddb.query({
TableName: "MyOrganizationTable",
KeyConditionExpression: `#PK = :partition AND #SK begins_with (:sort)`,
ExpressionAttributeNames: {
// The expression builder will generate bindings for
// expression attribute names
...signUpFilter.names,
"#PK": "PK",
"#SK": "SK",
},
ExpressionAttributeValues: {
// The expression builder will generate bindings for
// expression attribute values
...signUpFilter.values,
":partition": {
S: "#ORG_5753732",
},
":sort": {
S: "#USER",
},
},
FilterExpression: signUpFilter.expression,
});