{"_id":"@acomagu/dynamodb-dataloader","_rev":"1-e574ec28e9957597c199b41a0253168c","name":"@acomagu/dynamodb-dataloader","dist-tags":{"latest":"1.0.1"},"versions":{"1.0.0":{"name":"@acomagu/dynamodb-dataloader","version":"1.0.0","type":"module","private":false,"publishConfig":{"access":"public"},"main":"./dynamodb-dataloader.js","types":"./dynamodb-dataloader.d.ts","scripts":{"test":"node --loader ts-node/esm --test ./dynamodb-dataloader.test.ts"},"devDependencies":{"@aws-sdk/client-dynamodb":"^3.564.0","@aws-sdk/lib-dynamodb":"^3.564.0","@aws-sdk/util-dynamodb":"^3.564.0","@types/node":"^20.12.7","dataloader":"^2.2.2","ts-node":"^10.9.2","typedoc":"^0.25.13","typescript":"^5.4.5","ulid":"^2.3.0"},"_id":"@acomagu/dynamodb-dataloader@1.0.0","gitHead":"0f9016b674f41e6f6ab98b8f29cb7d047ae5d005","description":"This library provides a [DataLoader](https://github.com/graphql/dataloader) layer for efficient fetching from DynamoDB by caching and batching.","_nodeVersion":"21.7.2","_npmVersion":"10.5.2","dist":{"integrity":"sha512-UFjdTE9cRfefbhtX7k8mSoUOWQ+TNphhG4Q5fi4Co0J5wzLMCWVKS60as8Goviz/Mw3trqlXmsj1K5Iu2jUCXA==","shasum":"e120cdc7d2238031a31fc941d5b1c1632ea0aeb4","tarball":"https://registry.npmjs.org/@acomagu/dynamodb-dataloader/-/dynamodb-dataloader-1.0.0.tgz","fileCount":11,"unpackedSize":143654,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQD/5frs+A2tLATIeoiIDV14LZeaicYuPlAnBtZhFfy6uAIgIIkXd1gH9wOWkufgjFm0l1d+L3OUhtGMWMV/K6oGgx0="}]},"_npmUser":{"name":"acomagu","email":"acomagu@gmail.com"},"directories":{},"maintainers":[{"name":"acomagu","email":"acomagu@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/dynamodb-dataloader_1.0.0_1714468395472_0.06540347606957586"},"_hasShrinkwrap":false},"1.0.1":{"name":"@acomagu/dynamodb-dataloader","version":"1.0.1","type":"module","private":false,"publishConfig":{"access":"public"},"main":"./dynamodb-dataloader.js","types":"./dynamodb-dataloader.d.ts","scripts":{"test":"node --loader ts-node/esm --test ./dynamodb-dataloader.test.ts"},"devDependencies":{"@aws-sdk/client-dynamodb":"^3.564.0","@aws-sdk/lib-dynamodb":"^3.564.0","@aws-sdk/util-dynamodb":"^3.564.0","@types/node":"^20.12.7","dataloader":"^2.2.2","ts-node":"^10.9.2","typedoc":"^0.25.13","typescript":"^5.4.5"},"_id":"@acomagu/dynamodb-dataloader@1.0.1","gitHead":"ac66ef514c7ffe633870cdceb0716e115e42f4af","description":"This library provides a [DataLoader](https://github.com/graphql/dataloader) layer for efficient fetching from DynamoDB by caching and batching.","_nodeVersion":"21.7.2","_npmVersion":"10.5.2","dist":{"integrity":"sha512-vnED8vl9jMGh3cS9BXqVF77nwSelbBcBTTRDALbb9hqltSV7TudlykVckkZV0b9nkfqSAE37NW2qukktm+T3ww==","shasum":"d399ca2799bb87320b9365b022c31eee4874bc77","tarball":"https://registry.npmjs.org/@acomagu/dynamodb-dataloader/-/dynamodb-dataloader-1.0.1.tgz","fileCount":11,"unpackedSize":145617,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIH4XEDavKfD7FnxM2soP/X+mVj+cKSUsJsYgNpvqLFLXAiEAwCXyIVqeY/x0nszEqHwvh1p1Ju3mELtsChk3yNxzr7g="}]},"_npmUser":{"name":"acomagu","email":"acomagu@gmail.com"},"directories":{},"maintainers":[{"name":"acomagu","email":"acomagu@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/dynamodb-dataloader_1.0.1_1717672943514_0.07002092113009128"},"_hasShrinkwrap":false}},"time":{"created":"2024-04-30T09:13:15.375Z","1.0.0":"2024-04-30T09:13:15.647Z","modified":"2024-06-06T11:22:23.831Z","1.0.1":"2024-06-06T11:22:23.676Z"},"maintainers":[{"name":"acomagu","email":"acomagu@gmail.com"}],"description":"This library provides a [DataLoader](https://github.com/graphql/dataloader) layer for efficient fetching from DynamoDB by caching and batching.","readme":"# DynamoDB DataLoader\n\nThis library provides a [DataLoader](https://github.com/graphql/dataloader) layer for efficient fetching from DynamoDB by caching and batching.\n\n## Features\n\n- Batch Loading: Combines multiple queries into fewer network requests to DynamoDB(only for `get` operation).\n- Unified Caching: Caches are shared across get, query, and scan operations.\n    - But this shared caching is effective only in limited scenarios, such as when entries previously fetched using query or scan are accessed again using get. Also, the feature does not function when only parts of records are retrieved.\n\n## Initializing the DataLoader\n\nDefine the schema for your tables, specifying each table's name and the attribute names that form the keys used in caching.\n\n```typescript\nimport { DynamodbDataLoader, TableSchema } from '@acomagu/dynamodb-dataloader';\n\nconst tableSchemas: TableSchema[] = [\n  { tableName: \"Users\", keyAttributeNames: [\"userId\"] },\n  { tableName: \"Posts\", keyAttributeNames: [\"userId\", \"postId\"] }, // PK and SK\n]; // Used to enable cache sharing across query, scan, and get operations.\n\nconst options = {\n  dynamodbClient: new DynamoDBClient({ /* AWS SDK configuration options */ }),\n  getOptions: { /* BatchGet options */ },\n};\n\nconst dynamodbDataLoader = new DynamodbDataLoader(tableSchemas, options); // All arguments are optional.\n```\n\n### Propagate with Express Context\n\nFollowing best practices of DataLoader, a new instance of DynamodbDataLoader should be created per client request.\n\nThis example inserts the dataLoader to the Request object in express middleware.\n\n```typescript\nconst app = express();\n\n// Middleware to initialize DataLoader and store it in AsyncLocalStorage\napp.use((req) => {\n  req.dataLoader = new DynamodbDataLoader();\n});\n\napp.get('/user/:id', async (req, res) => {\n  const item = await req.dataLoader.getter.load({\n    TableName: \"Users\",\n    Key: { userId: req.params.id },\n  });\n  res.send(item);\n});\n```\n\n### Store to AsyncLocalStorage\n\nThe another way to isolate DataLoader per client request is using [AsyncLocalStorage](https://nodejs.org/api/async_context.html).\n\n```typescript\nconst app = express();\n\nconst dynamodbDataLoaderStorage = new AsyncLocalStorage();\n\napp.use((req, res, next) => {\n  dynamodbDataLoaderStorage.run(new DynamodbDataLoader(), next);\n});\n\napp.get('/user/:id', async (req, res) => {\n  const item = await dynamodbDataLoaderStorage.getStore()!.getter.load({\n    TableName: \"Users\",\n    Key: { userId: req.params.id },\n  });\n  res.send(item);\n});\n```\n\n### Usage\n\n## Fetching Data\n\n### Get Operation\n\nFetch data for a specific user ID from the \"Users\" table using the getter DataLoader:\n\n```typescript\nconst getUserRequest = {\n  TableName: \"Users\",\n  Key: { userId: \"12345\" }\n};\nconst item = await dynamodbDataLoader.getter.load(getUserRequest);\n```\n\n### Query Operation\n\nExample of querying posts for a specific user:\n\n```typescript\nconst queryPostsRequest = {\n  TableName: \"Posts\",\n  KeyConditionExpression: \"userId = :userId\",\n  ExpressionAttributeValues: {\n    \":userId\": \"12345\",\n  },\n};\nconst items = await dynamodbDataLoader.querier.load(queryPostsRequest);\n```\n\n### Scan Operation\n\nScanning for items with a specific filter:\n\n```typescript\nconst scanRequest = {\n  TableName: \"Posts\",\n  FilterExpression: \"contains(content, :content)\",\n  ExpressionAttributeValues: {\n    \":content\": \"DynamoDB\",\n  },\n};\nconst items = await dynamodbDataLoader.scanner.load(scanRequest);\n```\n\n### API Documentation\n\n[Documentation](https://acomagu.github.io/dynamodb-dataloader)\n","readmeFilename":"README.md"}