{"id":13512571,"url":"https://github.com/tuplo/dynoexpr","last_synced_at":"2025-03-31T00:30:38.461Z","repository":{"id":41896794,"uuid":"251724553","full_name":"tuplo/dynoexpr","owner":"tuplo","description":"Expression builder for AWS.DynamoDB.DocumentClient","archived":false,"fork":false,"pushed_at":"2024-06-14T09:04:21.000Z","size":1361,"stargazers_count":128,"open_issues_count":1,"forks_count":10,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-03-03T10:46:29.118Z","etag":null,"topics":["aws","aws-dynamodb","dynamodb","expression-builders"],"latest_commit_sha":null,"homepage":"https://npm.im/@tuplo/dynoexpr","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tuplo.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-31T20:33:07.000Z","updated_at":"2025-02-21T22:04:22.000Z","dependencies_parsed_at":"2023-02-08T05:30:47.889Z","dependency_job_id":"c35409d7-2130-463e-832d-42cab488cb38","html_url":"https://github.com/tuplo/dynoexpr","commit_stats":null,"previous_names":[],"tags_count":62,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuplo%2Fdynoexpr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuplo%2Fdynoexpr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuplo%2Fdynoexpr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuplo%2Fdynoexpr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tuplo","download_url":"https://codeload.github.com/tuplo/dynoexpr/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246399816,"owners_count":20770907,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","aws-dynamodb","dynamodb","expression-builders"],"created_at":"2024-08-01T04:00:21.028Z","updated_at":"2025-03-31T00:30:38.085Z","avatar_url":"https://github.com/tuplo.png","language":"TypeScript","funding_links":[],"categories":["Tools"],"sub_categories":[],"readme":"\u003cbr /\u003e\n\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"logo.png\" alt=\"Logo\" width=\"120\" height=\"120\"\u003e\n\n  \u003ch1 align=\"center\"\u003edynoexpr\u003c/h3\u003e\n\n  \u003cp align=\"center\"\u003e\n    Expression builder for \u003ci\u003eAWS.DynamoDB.DocumentClient\u003c/i\u003e\n  \u003c/p\u003e\n  \u003cp align=\"center\"\u003e\n  \t\u003cimg src=\"https://img.shields.io/npm/v/@tuplo/dynoexpr\"\u003e\n  \t\u003ca href=\"https://codeclimate.com/github/tuplo/dynoexpr/test_coverage\"\u003e\n    \u003cimg src=\"https://api.codeclimate.com/v1/badges/3564497cf991d094e2eb/test_coverage\" /\u003e\n  \t\u003c/a\u003e\n  \u003c/p\u003e\n\n\u003c/div\u003e\n\n## Introduction\n\nConverts a plain object to a DynamoDB expression with all variables and names\nreplaced with safe placeholders. It supports `Condition`, `KeyCondition`, `Filter`, `Projection` and `Update` expressions. The resulting expressions can then be used with `AWS.DynamoDB.DocumentClient` requests.\n\n```typescript\nimport dynoexpr from '@tuplo/dynoexpr';\n\nconst params = dynoexpr({\n  KeyCondition: { id: '567' },\n  Condition: { rating: '\u003e 4.5' },\n  Filter: { color: 'blue' },\n  Projection: ['weight', 'size'],\n});\n\n/*\n{\n  KeyConditionExpression: '(#nca40fdf5 = :v8dcca6b2)',\n  ExpressionAttributeValues: {\n    ':v8dcca6b2': '567',\n    ':vc95fafc8': 4.5,\n    ':v792aabee': 'blue'\n  },\n  ConditionExpression: '(#n0f1c2905 \u003e :vc95fafc8)',\n  FilterExpression: '(#n2d334799 = :v792aabee)',\n  ProjectionExpression: '#neb86488e,#n0367c420',\n  ExpressionAttributeNames: {\n    '#nca40fdf5': 'id',\n    '#n0f1c2905': 'rating',\n    '#n2d334799': 'color',\n    '#neb86488e': 'weight',\n    '#n0367c420': 'size'\n  }\n}\n*/\n```\n\n## Install\n\n```bash\n$ npm install @tuplo/dynoexpr\n\n# or with yarn\n$ yarn add @tuplo/dynoexpr\n```\n\n## Usage\n\n### Passing parameters to DocumentClient\n\n```typescript\nconst docClient = new AWS.DynamoDB.DocumentClient();\n\nconst params = dynoexpr({\n  KeyCondition: {\n    HashKey: 'key',\n    RangeKey: 'between 2015 and 2019',\n  },\n});\n\nconst results = await docClient\n  .query({ TableName: 'table', ...params })\n  .promise();\n```\n\n### Using multiple expressions on the same field\n\nYou can use multiple expressions on the same field, by packing them into an array and assigning it to the key with the field's name.\n\n```typescript\nconst params = dynoexpr({\n  Condition: {\n    color: ['attribute_not_exists', 'yellow', 'blue'],\n  },\n  ConditionLogicalOperator: 'OR',\n});\n\n/*\n{\n  ConditionExpression: '(attribute_not_exists(#n2d334799)) OR (#n2d334799 = :v0d81c8cd) OR (#n2d334799 = :v792aabee)',\n  ExpressionAttributeNames: {\n    '#n2d334799': 'color'\n  },\n  ExpressionAttributeValues: {\n    ':v0d81c8cd': 'yellow',\n    ':v792aabee': 'blue'\n  }\n}\n*/\n```\n\n### Using functions\n\n`DynamoDB` supports a number of [functions](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Functions) to be evaluated when parsing expressions. You don't need to reference the `path` argument because that's identified by the object's key.\n\n```typescript\nconst params = dynoexpr({\n  Condition: {\n    docs: 'attribute_exists',\n    brand: 'attribute_not_exists',\n    extra: 'attribute_type(NULL)',\n    color: 'begins_with dark',\n    address: 'contains(Seattle)',\n    description: 'size \u003c 20',\n  },\n});\n\n/*\n{\n  ConditionExpression: '(attribute_exists(#nd0a55266)) AND (attribute_not_exists(#n4e5f8507)) AND (attribute_type(#n4a177797,:v64b0a475)) AND (begins_with(#n2d334799,:v1fdc3f67)) AND (contains(#n3af77f77,:v26425a2a)) AND (size(#nb6c8f268) \u003c :vde9019e3)',\n  ExpressionAttributeNames: {\n    '#nd0a55266': 'docs',\n    '#n4e5f8507': 'brand',\n    '#n4a177797': 'extra',\n    '#n2d334799': 'color',\n    '#n3af77f77': 'address',\n    '#nb6c8f268': 'description'\n  },\n  ExpressionAttributeValues: {\n    ':v64b0a475': 'NULL',\n    ':v1fdc3f67': 'dark',\n    ':v26425a2a': 'Seattle',\n    ':vde9019e3': 20\n  }\n}\n*/\n```\n\n### Using multiple expressions on the same request\n\n```typescript\nconst params = dynoexpr({\n  Update: { Sum: 'Sum + 20' },\n  Condition: { Sum: `\u003c 100` },\n});\n\n/*\n{\n  ConditionExpression: '(#n5af617ef \u003c :va88c83b0)',\n  ExpressionAttributeNames: {\n    '#n5af617ef': 'Sum'\n  },\n  ExpressionAttributeValues: {\n    ':va88c83b0': 100,\n    ':vde9019e3': 20\n  },\n  UpdateExpression: 'SET #n5af617ef = #n5af617ef + :vde9019e3'\n}\n*/\n```\n\n### Working with Sets\n\nIf a value is provided as a Set, it will be converted to `DocumentClient.DynamoDbSet`. But `dynoexpr` doesn't include `DocumentClient` so you need to provide it.\n\n```typescript\nimport { DocumentClient } from \"aws-sdk/clients/dynamodb\";\n\nconst params = dynoexpr({\n  DocumentClient,\n  Update: {\n    Color: new Set(['Orange', 'Purple'])\n  },\n})\n\n/*\n{\n  UpdateExpression: 'SET #n8979552b = :v3add0a80',\n  ExpressionAttributeNames: {\n    '#n8979552b': 'Color'\n  },\n  ExpressionAttributeValues: {\n    ':v3add0a80': Set { wrapperName: 'Set', values: [Array], type: 'String' }\n  }\n}\n*/\n```\n\n#### When using UpdateAdd or UpdateDelete, arrays are converted to DynamoDbSet\n\n```typescript\nimport { DocumentClient } from \"aws-sdk/clients/dynamodb\";\n\nconst params = dynoexpr({\n  DocumentClient,\n  UpdateAdd: {\n    Color: ['Orange', 'Purple']\n  }\n})\n\n/*\n{\n  UpdateExpression: 'ADD #ndc9f7295 :v3add0a80',\n  ExpressionAttributeNames: { \n    '#ndc9f7295': 'Color'\n  },\n  ExpressionAttributeValues: {\n    ':v3add0a80': Set { wrapperName: 'Set', values: [Array], type: 'String' }\n  }\n}\n*/\n```\n\n### Keep existing Expressions, AttributeNames and AttributeValues\n\n```typescript\nconst params = dynoexpr({\n  Filter: { color: 'blue' },\n  ProjectionExpression: '#year',\n  ExpressionAttributeNames: {\n    '#year': 'year',\n  },\n});\n\n/*\n{\n  ProjectionExpression: '#year',\n  ExpressionAttributeNames: {\n    '#year': 'year',\n    '#n2d334799': 'color'\n  },\n  FilterExpression: '(#n2d334799 = :v792aabee)',\n  ExpressionAttributeValues: {\n    ':v792aabee': 'blue'\n  }\n}\n*/\n```\n\n### Using object paths on expressions\n\nYou can provide a path to an attribute on a deep object, each node will be escaped.\n\n```typescript\nconst params = dynoexpr({\n  Update: {\n    'foo.bar.baz': 'foo.bar.baz + 1'\n  }\n});\n\n/*\n{\n  ExpressionAttributeNames: {\n    \"#n22f4f0ae\": \"bar\",\n    \"#n5f0025bb\": \"foo\",\n    \"#n82504b33\": \"baz\",\n  },\n  ExpressionAttributeValues: {\n    \":vc823bd86\": 1,\n  },\n  UpdateExpression:\n    \"SET #n5f0025bb.#n22f4f0ae.#n82504b33 = #n5f0025bb.#n22f4f0ae.#n82504b33 + :vc823bd86\",\n}\n*/\n\n```\n\nIf one of the nodes needs to escape some of its characters, use double quotes around it, like this:\n\n```typescript\nconst params = dynoexpr({\n  Update: {\n    'foo.\"bar-cuz\".baz': 'foo.\"bar-cuz\".baz + 1'\n  }\n});\n```\n\n\n### Parsing atomic requests, only expressions will be replaced\n\nYou can pass the whole request parameters to `dynoexpr` - only the expression builders will be replaced.\n\n```typescript\nconst params = dynoexpr({\n  TableName: 'Table',\n  Key: { HashKey: 'key' },\n  ReturnConsumedCapacity: 'TOTAL',\n  KeyCondition: {\n    color: 'begins_with dark',\n  },\n});\n\n/*\n{\n  TableName: 'Table',\n  Key: { HashKey: 'key' },\n  ReturnConsumedCapacity: 'TOTAL',\n  KeyConditionExpression: '(begins_with(#n2d334799,:v1fdc3f67))',\n  ExpressionAttributeNames: {\n    '#n2d334799': 'color'\n  },\n  ExpressionAttributeValues: {\n    ':v1fdc3f67': 'dark'\n  }\n}\n*/\n```\n\n### Parsing Batch requests\n\n```typescript\nconst params = dynoexpr({\n  RequestItems: {\n    'Table-1': {\n      Keys: [{ foo: 'bar' }],\n      Projection: ['a', 'b'],\n    },\n  },\n  ReturnConsumedCapacity: 'TOTAL',\n});\n\n/*\n{\n  \"RequestItems\":{\n    \"Table-1\":{\n      \"Keys\": [{\"foo\":\"bar\"}],\n      \"ProjectionExpression\": \"#na0f0d7ff,#ne4645342\",\n      \"ExpressionAttributeNames\":{\n        \"#na0f0d7ff\": \"a\",\n        \"#ne4645342\": \"b\"\n      }\n    }\n  },\n  \"ReturnConsumedCapacity\": \"TOTAL\"\n}\n*/\n```\n\n### Parsing Transact requests\n\n```typescript\nconst params = dynoexpr({\n  TransactItems: [{\n    Get: {\n      TableName: 'Table-1',\n      Key: { id: 'foo' },\n      Projection: ['a', 'b'],\n    },\n  }],\n  ReturnConsumedCapacity: 'INDEXES',\n});\n\n/*\n{\n  \"TransactItems\": [\n    { \n      \"Get\": {\n        \"TableName\": \"Table-1\",\n        \"Key\": { \"id\": \"foo\" },\n        \"ProjectionExpression\": \"#na0f0d7ff,#ne4645342\",\n        \"ExpressionAttributeNames\": {\n          \"#na0f0d7ff\":\"a\",\n          \"#ne4645342\":\"b\"\n        }\n      }\n    }\n  ],\n  \"ReturnConsumedCapacity\": \"INDEXES\"\n}\n*/\n```\n\n### Type the resulting parameters\n\nThe resulting object is compatible with all `DocumentClient` requests, but if you want to be type-safe, `dynoexpr` accepts a generic type to be applied to the return value.\n\n```typescript\nconst params = dynoexpr\u003cAWS.DocumentClient.UpdateItemInput\u003e({\n  TableName: 'Table',\n  Key: 1,\n  UpdateSet: { color: 'pink' },\n});\n```\n\n## API\n\n### dynoexpr\u0026lt;T\u0026gt;(params)\n\n#### `params`\n\nExpression builder parameters\n\n```typescript\ntype DynamoDbPrimitive = string | number | boolean | object;\ntype DynamoDbValue =\n  | DynamoDbPrimitive\n  | DynamoDbPrimitive[]\n  | Set\u003cDynamoDbPrimitive\u003e;\n\n// all attributes are optional, depending on what expression(s) are to be built\n{\n  Condition: { [key: string]: DynamoDbValue },\n  ConditionLogicalOperator: 'AND' | 'OR',\n\n  KeyCondition: { [key: string]: DynamoDbValue },\n  KeyConditionLogicalOperator: 'AND' | 'OR',\n\n  FilterCondition: { [key: string]: DynamoDbValue },\n  FilterLogicalOperator: 'AND' | 'OR',\n\n  Projection: string[],\n\n  Update: { [key: string]: DynamoDbValue },\n  UpdateAction: 'SET' | 'ADD' | 'DELETE' | 'REMOVE',\n\n  UpdateSet: { [key: string]: DynamoDbValue },\n  UpdateAdd: { [key: string]: DynamoDbValue },\n  UpdateDelete: { [key: string]: DynamoDbValue },\n  UpdateRemove: { [key: string]: DynamoDbValue },\n  \n  DocumentClient: AWS.DynamoDB.DocumentClient\n}\n```\n\n#### Return value\n\nParameters accepted by `AWS.DynamoDB.DocumentClient`\n\n```typescript\n// all attributes are optional depending on the expression(s) being built\n{\n  ConditionExpression: string,\n\n  KeyConditionExpression: string,\n\n  FilterConditionExpression: string,\n\n  ProjectionExpression: string,\n\n  UpdateExpression: string,\n\n  ExpressionAttributeNames: { [key: string]: string },\n  ExpressionAttributeValues: { [key: string]: string },\n}\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftuplo%2Fdynoexpr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftuplo%2Fdynoexpr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftuplo%2Fdynoexpr/lists"}