Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ability to disable automatic bookkeeping #11

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Airtable Change Detector
# Airtable Change Detector

[![npm version](https://badge.fury.io/js/airtable-change-detector.svg)](https://badge.fury.io/js/airtable-change-detector)

Expand Down Expand Up @@ -53,7 +53,8 @@ const myTableDetector = new ChangeDetector(base("MyTableName"), {
metaFieldName: "MyMetaField", // Defaults to `Meta`
lastModifiedFieldName: "My Last Modified", // Defaults to `Last Modified`
lastProcessedFieldName: "Last Processed", // If not included, detector will not write this field
sensitiveFields: ["Address", "Birthdate"] // Fields not to include in `Meta`s previous state. Useful for keeping data deletion easy.
sensitiveFields: ["Address", "Birthdate"], // Fields not to include in `Meta`s previous state. Useful for keeping data deletion easy.
autoUpdateEnabled: true // Defaults to true. If disabled you need to, when ready, pass an array of records to the function myTableDetector.updateRecords() to mark the record(s) as having been processed. It is recommended that you 'await' the return of the function to ensure a successful update.
});
myTableDetector.pollWithInterval(
"pollingNameForLogging",
Expand All @@ -69,7 +70,7 @@ myTableDetector.pollWithInterval(
// record.didChange(field) returns true if the field changed (or is new) between the last observation and now
// record.getPrior(field) returns the prior value for `field` or undefined
// See `enrichRecord` for more added methods

if (record.didChange(colorFieldName)) {
// Ex: send new color value to webhook
promises.push(axios.post(myWebhookUrl, { newColor: record.get(colorFieldName) }));
Expand All @@ -91,10 +92,11 @@ myTableDetector.pollWithInterval(
## Caveats

- All comparison for the sake of change detecting is done with lodash's [isEqual](https://lodash.com/docs/4.17.15#isEqual)
- Changes are only detected ONCE. If your change logic fails, you could:
- By default, changes are only detected ONCE. If your change logic fails, you could:
- Retry it yourself
- Modify a field that is watched by `Last Modified` field. The change detector will fire again for the field you changed.
- Remove the watched field that you were reacting to in the `lastValues` Meta property. The prior value would be lost. (this is complicated and we may provide an API if there is interest)
- Optionally disable the auto update capability and invoke the updateRecords function yourself. See the option autoUpdateEnabled above

## Info

Expand All @@ -106,9 +108,9 @@ for [mutual-aid-app](https://github.com/crownheightsma/mutual-aid-app).
```sh
npm i // Initial install

npm run lint // Find lint issues
npm run fix // Attempt to fix lint issues
npm run test // Run mocha tests
npm run lint // Find lint issues
npm run fix // Attempt to fix lint issues
npm run test // Run mocha tests
```

## Contributing
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"release-it": "13.5.8",
"sinon": "9.0.2"
},
"version": "1.1.0",
"version": "1.2.0",
"repository": {
"type": "git",
"url": "git+https://github.com/crownheightsaid/airtable-change-detector.git"
Expand Down
5 changes: 4 additions & 1 deletion src/ChangeDetector.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class ChangeDetector {
this.lastProcessedFieldName = options.lastProcessedFieldName || "";
this.writeDelayMs = options.writeDelayMs || 0;
this.sensitiveFields = options.sensitiveFields || [];
this.autoUpdateEnabled = options.autoUpdateEnabled || true;
}

/**
Expand All @@ -91,7 +92,9 @@ class ChangeDetector {
if (results.length === 0) {
return results;
}
await this.updateRecords(recordsWithFieldChanges);
if (this.autoUpdateEnabled) {
await this.updateRecords(recordsWithFieldChanges);
}
this.updateLastModified(toExamine);
return results;
}
Expand Down