Skip to content

Commit

Permalink
feat: migrate from cls-hooked to AsyncLocalStorage
Browse files Browse the repository at this point in the history
BREAKING CHANGE: cls-hooked is no longer supported
  • Loading branch information
yujiosaka committed Jun 12, 2024
1 parent e1ab3d3 commit 585c804
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 241 deletions.
113 changes: 88 additions & 25 deletions README.md

Large diffs are not rendered by default.

27 changes: 12 additions & 15 deletions docs/TIPS.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ There are 2 steps to enable user tracking, ie, recording the user who created a
const sequelizeRevision = new SequelizeRevision(sequelize, { userModel: "user" });
```

2. Pass the id of the user who is responsible for the database operation to revisions either by sequelize options or by using [cls-hooked](https://www.npmjs.com/package/cls-hooked).
2. Pass the id of the user who is responsible for the database operation to revisions either by sequelize options or by using [AsyncLocalStorage](https://nodejs.org/api/async_context.html#class-asynclocalstorage).

```typescript
await Model.update({ /* ... */ }, { userId: user.id });
Expand All @@ -95,17 +95,15 @@ await Model.update({ /* ... */ }, { userId: user.id });
OR

```typescript
import { createNamespace } = from "cls-hooked";
import { AsyncLocalStorage } from "async_hooks";

const session = createNamespace("my session");
session.set("userId", user.id);
const asyncLocalStorage = new AsyncLocalStorage();

await Model.update({ /* ... */ });
await asyncLocalStorage.run(user.id, async () => {
await Model.update({ /* ... */ });
});
```

To enable cls-hooked set `continuationNamespace` in initialization options.
Additionally, you may also have to call `.run()` or `.bind()` on your cls namespace, as described in the [docs](https://www.npmjs.com/package/cls-hooked).

## Disable logging for a single call

To not log a specific change to a revisioned object, just pass a `noRevision` with `true` value.
Expand All @@ -124,7 +122,7 @@ You can save meta data to revisions table in 2 steps Whne revisions table alread
const sequelizeRevision = new SequelizeRevision(sequelize, { metaDataFields: { userRole: false } });
```

2. Pass the metadata to revisions either by sequelize options or by using [cls-hooked](https://www.npmjs.com/package/cls-hooked).
2. Pass the metadata to revisions either by sequelize options or by using [AsyncLocalStorage](https://nodejs.org/api/async_context.html#class-asynclocalstorage).

```typescript
await Model.update({ /* ... */ }, { revisionMetaData: { userRole: "admin" } });
Expand All @@ -133,16 +131,15 @@ await Model.update({ /* ... */ }, { revisionMetaData: { userRole: "admin" } });
OR

```typescript
import { createNamespace } = from "cls-hooked";
import { AsyncLocalStorage } from "async_hooks";

const session = createNamespace("my session");
session.set("metaData", { userRole: "admin" });
const metaDataAsyncLocalStorage = new AsyncLocalStorage();

await Model.update({ /* ... */ });
await metaDataAsyncLocalStorage.run({ userRole: "admin" }, async () => {
await Model.update({ /* ... */ });
});
```

To enable cls-hooked set continuationNamespace in initialization options. Additionally, you may also have to call .run() or .bind() on your cls namespace, as described in the docs.

## Exclude attributes

There are 2 ways to avoid logging revisions for updating specific attributes.
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"sequelize": ">=6.1.0"
},
"dependencies": {
"cls-hooked": "4.2.2",
"debug": "4.3.4",
"deep-diff": "1.0.2",
"diff": "5.2.0",
Expand All @@ -60,7 +59,6 @@
"@semantic-release/git": "10.0.1",
"@semantic-release/npm": "12.0.1",
"@semantic-release/release-notes-generator": "13.0.0",
"@types/cls-hooked": "4.3.8",
"@types/debug": "4.1.12",
"@types/deep-diff": "1.0.5",
"@types/diff": "5.2.1",
Expand Down
54 changes: 0 additions & 54 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 585c804

Please sign in to comment.