{"id":20950135,"url":"https://github.com/richie765/pg-table-observer","last_synced_at":"2025-05-14T03:32:27.382Z","repository":{"id":57322654,"uuid":"70403003","full_name":"Richie765/pg-table-observer","owner":"Richie765","description":"Observe PostgreSQL table for changes","archived":false,"fork":false,"pushed_at":"2017-06-29T12:47:04.000Z","size":22,"stargazers_count":6,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-05T08:50:58.886Z","etag":null,"topics":["observer","postgresql"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Richie765.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-10-09T12:57:07.000Z","updated_at":"2022-04-25T13:03:12.000Z","dependencies_parsed_at":"2022-08-26T01:11:22.473Z","dependency_job_id":null,"html_url":"https://github.com/Richie765/pg-table-observer","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Richie765%2Fpg-table-observer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Richie765%2Fpg-table-observer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Richie765%2Fpg-table-observer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Richie765%2Fpg-table-observer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Richie765","download_url":"https://codeload.github.com/Richie765/pg-table-observer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225275436,"owners_count":17448386,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["observer","postgresql"],"created_at":"2024-11-19T00:45:46.622Z","updated_at":"2024-11-19T00:45:47.271Z","avatar_url":"https://github.com/Richie765.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pg-table-observer\nObserve PostgreSQL table for changes.\n\nRequires PostgresSQL version 9.3 or above.\n\n# Usage\n\n```javascript\nvar pgp = require('pg-promise')();\n\nimport PgTableObserver from 'pg-table-observer';\n\nconst connection = 'postgres://localhost/db';\n\nasync function start() {\n  try {\n    let db = await pgp(connection);\n\n    let table_observer = new PgTableObserver(db, 'myapp');\n\n    async function cleanupAndExit() {\n      await table_observer.cleanup();\n      pgp.end();\n      process.exit();\n    }\n\n    process.on('SIGTERM', cleanupAndExit);\n    process.on('SIGINT', cleanupAndExit);\n\n    // Multiple tables can be specified as an array\n\n    let handle = await table_observer.notify('test', change =\u003e {\n      console.log(change);\n    });\n\n    // Or trigger a callback when a condition is met\n\n    let handle = await table_observer.trigger(condition, () =\u003e {\n      console.log(\"condition was met\")\n    });\n\n    // ... when finished observing the table\n\n    await handle.stop();\n\n    // ... when finished observing altogether\n\n    await table_observer.cleanup();\n  }\n  catch(err) {\n    console.error(err);\n  }\n}\n\n// Show unhandled rejections\n\nprocess.on('unhandledRejection', (err, p) =\u003e console.log(err.stack));\n\nstart();\n```\n\n# constructor(db, channel)\n\nParameter | Description\n--------- | -----------\n`db` | PostgreSQL database connection\n`channel` | String, PostgreSQL LISTEN/NOTIFY channel to use, must be unique for every observer, database-wide.\n\n# let handle = async notify(tables, callback)\n\nUse this method if you want to observe a set of tables for any change.\n\nThe callback will be called for each INSERT, DELETE or UPDATE performed\non the tables, for each row individually.\n\nParameter | Description\n--------- | -----------\n`tables` | A string or array of tables to monitor. Table names will be converted to lowercase, and duplicates will be removed.\n`callback` | function(change). Will be called for any change to any of the tables that are being monitored. See below for its fields.\n\nFields for the `change` parameter of the `callback`:\n\nField | Description\n-------------- | -----------\n`table` | String, name of the table that changed. ***This will always be in lowercase.***\n`insert` | For INSERT, `true`\n`delete` | For DELETE, `true`\n`update` | For UPDATE, an object that contains the old and new values of each changed column. If a column `score` changed from 10 to 20, `change.update.score.from` would be 10 and `change.update.score.to` would be 20.\n`row` | The row values, for UPDATE, the NEW row values\n`old` | For UPDATE, the OLD row values\n\n## Return value\n\n`handle`: Object with the following fields:\n\nField | Description\n----- | -----------\n`async stop()` | async function(). Stop observing the tables.\n\n# let handle = async trigger(tables, triggers, callback, [options])\n\nUse this method if you want to take some action when triggered by some change to the tables.\n\nThe `triggers` callback will be called when there is a change to `tables`.\nWhen `triggers` returns `true`, the `callback` will be called.\n\nThe following logic prevents excessive callbacks when multiple rows are updated at once:\n* When `triggers` hits, `callback` is called and a timer is started.\n* When `triggers` hits again before the timer is finished, the `callback` will be called as soon as the timer finishes.\n* Until that happens, no `triggers` calls will happen.\n* Default behavior can be changed with `options`, see below.\n\n## Parameters\n\nParameter | Description\n--------- | -----------\n`tables` | A string or array of tables to monitor. Table names will be converted to lowercase, and duplicates will be removed.\n`triggers` | function(change). Will be called when a change to `tables` happens, with the same fields as described above. If this function returns `true`, the `callback` | function(). Will be called with `triggers` returns true, as described above.\n`options` | An optional object that may be used to change the default behavior as described above. See below for the possible options.\n\nOptions parameter:\n\nField | Description\n--------------- | -----------\n`trigger_first` | (default `true`): related to the 1st step above. When `true`, behaves as described above. When set to `false`, when `triggers` hit and the timer is not yet started, the `callback` will not be called immediately. Instead the timer is started and the `callback` will be called when the timer finishes. Use this if you expect many changes in short succession, or if the `callback` is relatively costly. On a heavy-load production system you may want to set this to `false`.\n`trigger_delay` | (default 200ms): the time the timer will be set to. You may want to increase this value on heavy-load production system, or when the `callback` is relatively costly.\n`reduce_triggers` | (default `true`): related to the 3rd step above. When `true`, behaves as described above. When `false`, `triggers` will be called for every change. The `callback` will still be called as described after the timer finishes. Use this if you want to keep track of which changes happen to which tables.\n\n\n## Return value\n\n`handle`: Object with the following fields.\n\nField | Description\n----- | -----------\n`async stop()` | async function(). Stop observing the tables.\n\n# async cleanup()\n\nStops observing all tables and frees up resources.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frichie765%2Fpg-table-observer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frichie765%2Fpg-table-observer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frichie765%2Fpg-table-observer/lists"}