项目作者: neuledge

项目描述 :
🔒 Strongly typed library for querying and modeling DynamoDB documents in TypeScript.
高级语言: TypeScript
项目地址: git://github.com/neuledge/ddb-table.git
创建时间: 2020-04-25T12:43:13Z
项目社区:https://github.com/neuledge/ddb-table

开源协议:MIT License

下载


🔒 DDB-Table

Strongly typed library for querying and modeling DynamoDB documents in TypeScript.



View On NPM


Build Status


Dependency Status


Coverage Status


License



DDB-Table was built to provide strongly-typed data structures over DynamoDB tables. Using AWS
DocumentClient
& TypeScript you can easily fetch and store any JSON document and validate it’s
structure statically. Query secondary indexes and run complicated update expressions without any
error on runtime.

  1. await table
  2. .update('demo@example.com')
  3. .set('FullName', 'John Doe')
  4. // 🚨 TypeScript Error: 'fullName' is not assignable to 'Email' | 'FullName'
  5. .condition((cond) => cond.eq('fullName', 'Johnny Doe'))
  6. .exec();

Main Features

  • Strongly Typed - End-to-end TypeScript validation for your data.
  • Easy Query Expressions - Automatically escape name attributes and values.
  • Smart Projections - Make sure you only access the fields you project.
  • Query & Scan Indexes - Complete support for global or local indexes.
  • Pure JavaScript - Also works without TypeScript.

Sponsored by ❤️

If you like this project, please consider sponsoring us to help us continue to maintain and improve
this project.


Install

  1. npm i ddb-table


Usage

  1. import Table from 'ddb-table';
  2. import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
  3. import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb';
  4. interface MessageSchema {
  5. threadId: string;
  6. timestamp: number;
  7. senderId: string;
  8. message: string;
  9. status: 'sent' | 'received';
  10. tags?: Set<string>;
  11. attachments: {
  12. name: string;
  13. URL: string;
  14. }[];
  15. }
  16. const client = new DynamoDBClient({
  17. // settings...
  18. });
  19. // create the basic table definition
  20. const messages = new Table<MessageSchema, 'threadId', 'timestamp'>({
  21. tableName: 'Messages',
  22. primaryKey: 'threadId',
  23. sortKey: 'timestamp',
  24. documentClient: DynamoDBDocument.from(client);
  25. });
  26. const updateRes = await messages
  27. .update('john@gmail.com', 1588191225322)
  28. .set('message', 'Hello World!')
  29. .add('tags', new Set(['unread', 'important']))
  30. .set('attachments', (exp) =>
  31. exp.listAppend([{ name: 'Test', URL: 'demo.com' }]),
  32. )
  33. .return('ALL_NEW')
  34. .exec();
  35. console.log(updateRes.Attributes);

Working with indexes as well:

  1. // create a secondary index definition
  2. type SenderTimestampIndex = Pick<
  3. MessageSchema,
  4. 'threadId' | 'timestamp' | 'senderId'
  5. >;
  6. const outboxIndex = messages.index<
  7. SenderTimestampIndex,
  8. 'senderId',
  9. 'timestamp'
  10. >('senderId-timestamp-index', 'senderId', 'timestamp');
  11. const it = outboxIndex
  12. .query()
  13. .keyCondition((cond) => cond.eq('senderId', 'john@gmail.com'))
  14. .keyCondition((cond) =>
  15. cond.between('timestamp', Date.now() - 3600e3, Date.now()),
  16. )
  17. .project({ threadId: 1, message: 1 })
  18. .reverseIndex()
  19. .entries();
  20. for await (const item of it) {
  21. console.log(item);
  22. }

Error Handling

  1. import { DynamoDBExceptionName } from 'ddb-table';
  2. try {
  3. await table.put(...).exec();
  4. } catch (err) {
  5. if ((err as DynamoDBServiceException)?.name === DynamoDBExceptionName.ConditionalCheckFailedException) {
  6. // handle exception
  7. }
  8. }


License

MIT license © 2022 Neuledge