Skip to content

Commit

Permalink
Add description to isolate instance per request
Browse files Browse the repository at this point in the history
  • Loading branch information
acomagu committed Apr 30, 2024
1 parent 590106b commit ac66ef5
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,53 @@ const options = {
const dynamodbDataLoader = new DynamodbDataLoader(tableSchemas, options); // All arguments are optional.
```

### Propagate with Express Context

Following best practices of DataLoader, a new instance of DynamodbDataLoader should be created per client request.

This example inserts the dataLoader to the Request object in express middleware.

```typescript
const app = express();

// Middleware to initialize DataLoader and store it in AsyncLocalStorage
app.use((req) => {
req.dataLoader = new DynamodbDataLoader();
});

app.get('/user/:id', async (req, res) => {
const item = await req.dataLoader.getter.load({
TableName: "Users",
Key: { userId: req.params.id },
});
res.send(item);
});
```

### Store to AsyncLocalStorage

The another way to isolate DataLoader per client request is using [AsyncLocalStorage](https://nodejs.org/api/async_context.html).

```typescript
const app = express();

const dynamodbDataLoaderStorage = new AsyncLocalStorage();

app.use((req, res, next) => {
dynamodbDataLoaderStorage.run(new DynamodbDataLoader(), next);
});

app.get('/user/:id', async (req, res) => {
const item = await dynamodbDataLoaderStorage.getStore()!.getter.load({
TableName: "Users",
Key: { userId: req.params.id },
});
res.send(item);
});
```

### Usage

## Fetching Data

### Get Operation
Expand Down

0 comments on commit ac66ef5

Please sign in to comment.