@faceteer/expression-builder

中文文档
2.0.1发布于 2022/01/11MITTypeScriptCommonJS近 30 天下载 3344

An expression builder for Dynamo DB

此软件包已被标记为弃用

Merged into @faceteer/facet 6.1.0. Install @faceteer/facet instead.

原始使用文档
文档目录13 节
  1. Installation
  2. Condition Expression
  3. = , < , <= , > , >= , <>
  4. between
  5. begins_with
  6. exists
  7. not_exists
  8. contains
  9. size
  10. in
  11. AND , OR
  12. Filter Expression
  13. Usage

Faceteer Expression Builder

The Faceteer Expression Builder is a set of functions that can be used to create filter expressions for Dynamo DB queries, or condition expressions for PutItem, UpdateItem, and DeleteItem operations.

Installation

Install with npm:

npm i @faceteer/expression-builder --save

Install with yarn:

yarn add @faceteer/expression-builder

Condition Expression

Condition expressions are used when you want to conditionally put, update, or delete an item from DynamoDB.

The condition expression builder will take in an array representing a condition expression and generate the expression with placeholders for the attribute names and values.

The following operands are supported for condition expressions:

=, <, <=, >, >=, <>

// 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"]];

Filter Expression

Filter expressions are used when you want to query Dynamo DB, but only want a subset of the queried results.

The filter expression builder will take in an array representing a filter expression and generate the expression with placeholders for the attribute names and values.

The following operands are supported for filter expressions:

  • =
  • <
  • <=
  • >=
  • <>
  • between
  • begins_with

For all operands besides between the structure of a filter expression array is [{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",
]);

For between the structure of a filter expression array is [{attribute}, "between", {start}, {end}].

// Users that signed up between 2015 and 2016
const signUpRangeFilter = expressionBuilder.filter([
  "signedUpDate",
  "between",
  "2015",
  "2016",
]);

You may also use the AND and OR operands to chain together multiple statements.

// Active users who signed up before 2010
const signUpFilter = builder.filter([
  ["signedUpDate", ">", "2010"],
  "AND",
  ["status", "=", "active"],
]);

Usage

The compiled expression will return an object with three properties.

PropertyDescription
namesA map containing attribute names indexed by their placeholders.
valuesA map containing attribute values in the Dynamo DB format indexed by their placeholders.
expressionThe filter expression string.

These can be used direct with the DynamoDB client in the 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,
});

版本与发布记录

@faceteer/expression-builder 共发布 6 个版本,首次发布于 2021/07/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.1latest2022/01/114 年前
2.0.02022/01/114 年前
1.0.32021/07/215 年前
1.0.22021/07/105 年前
1.0.12021/07/105 年前
1.0.02021/07/105 年前

关于 @faceteer/expression-builder 的常见问题

@faceteer/expression-builder 是什么?

An expression builder for Dynamo DB注意:该包已被作者标记为弃用——Merged into @faceteer/facet 6.1.0. Install @faceteer/facet instead.

如何安装 @faceteer/expression-builder?

在项目目录运行 npm install @faceteer/expression-builder,或使用 pnpm add / yarn add / bun add。

@faceteer/expression-builder 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@faceteer/expression-builder 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@faceteer/expression-builder 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=12.4.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@faceteer/expression-builder 可以商用吗?

@faceteer/expression-builder 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@faceteer/expression-builder 的下载量有多少?

根据 npm 官方下载统计,@faceteer/expression-builder 在 2026/10/14 – 2026/11/12 这 30 天内被下载 3,344 次,日均约 111 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@faceteer/expression-builder 还在维护吗?

最新版本 2.0.1 发布于 2022/01/11(4 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/07/10。作者已将其标记为弃用,新项目建议寻找替代方案。