├── .eslintrc.js ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmignore ├── .prettierrc.js ├── LICENSE ├── docker-compose.yml ├── index.d.ts ├── jest.config.js ├── package.json ├── readme.md ├── renovate.json ├── scripts ├── docs.js └── generate-queries.ts ├── src ├── cli.ts ├── fs.ts ├── git.ts ├── index.ts ├── memoize.ts ├── pg-log.ts ├── pg-types.ts ├── serializer.ts └── sync-promise.ts ├── test ├── exports.test.ts ├── memoize.test.ts ├── result-printer.ts └── walkthrough.test.ts ├── tsconfig.json ├── tsconfig.lib.json ├── webpack.config.js ├── webpack ├── async-lock-shim.js └── globals.js └── yarn.lock /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | parserOptions: { 4 | ecmaVersion: 2018, 5 | sourceType: 'module', 6 | }, 7 | ignorePatterns: [ 8 | // break 9 | 'index.cjs', 10 | '**/node_modules/**', 11 | '**/dist/**', 12 | '**/generated/**', 13 | ], 14 | plugins: [ 15 | // break 16 | 'prettier', 17 | 'codegen', 18 | ], 19 | rules: { 20 | 'prettier/prettier': ['warn', require('./.prettierrc')], 21 | 'codegen/codegen': 'error', 22 | }, 23 | overrides: [ 24 | { 25 | files: ['**/*.md'], 26 | rules: { 27 | 'prettier/prettier': 'off', 28 | }, 29 | }, 30 | ], 31 | } 32 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | push: {} 4 | pull_request: {} 5 | 6 | jobs: 7 | run: 8 | if: github.event_name == 'push' || github.event.pull_request.base.repo.url != github.event.pull_request.head.repo.url 9 | runs-on: ubuntu-latest 10 | services: 11 | postgres: 12 | # can't update to 12 until https://github.com/clkao/docker-postgres-plv8/issues/36 is fixed 13 | image: clkao/postgres-plv8:11-2 14 | env: 15 | POSTGRES_USER: postgres 16 | POSTGRES_PASSWORD: postgres 17 | POSTGRES_DB: postgres 18 | options: >- 19 | --health-cmd pg_isready 20 | --health-interval 10s 21 | --health-timeout 5s 22 | --health-retries 5 23 | ports: 24 | - 5435:5432 25 | steps: 26 | - uses: actions/checkout@v2 27 | - run: yarn 28 | - run: yarn ci 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | */* 2 | .* 3 | *.tgz 4 | !.*rc.js 5 | !.*ignore 6 | !.git* 7 | !.github/** 8 | !src/** 9 | !test/** 10 | !scripts/** 11 | !patches/** 12 | !webpack/** 13 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | */* 2 | * 3 | !dist/* 4 | !src/* 5 | !webpack.config.js 6 | !queries/* 7 | -------------------------------------------------------------------------------- /.prettierrc.js: -------------------------------------------------------------------------------- 1 | /** @type {import('prettier').Options} */ 2 | module.exports = { 3 | singleQuote: true, 4 | semi: false, 5 | arrowParens: 'avoid', 6 | tabWidth: 2, 7 | trailingComma: 'all', 8 | bracketSpacing: false, 9 | endOfLine: 'auto', 10 | printWidth: 120, 11 | } 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | postgres: 4 | # can't update to 12 until https://github.com/clkao/docker-postgres-plv8/issues/36 is fixed 5 | image: clkao/postgres-plv8:11-2 6 | restart: always 7 | ports: 8 | - "5435:5432" 9 | environment: 10 | POSTGRES_USER: postgres 11 | POSTGRES_PASSWORD: postgres 12 | POSTGRES_DB: postgres 13 | volumes: 14 | - postgres:/var/lib/postgresql/data 15 | volumes: 16 | postgres: 17 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export type GitRepo = Record; 2 | 3 | export const gitLog: (repo: GitRepo, depth?: number) => 1 -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | preset: 'ts-jest', 3 | globals: { 4 | 'ts-jest': { 5 | diagnostics: false, 6 | }, 7 | }, 8 | collectCoverageFrom: ['**/*.ts', '!**/*.d.ts'], 9 | } 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "plv8-git", 3 | "version": "0.1.2", 4 | "description": "Tracks history of rows in postgresql database tables, using in-memory git operations", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/mmkal/plv8-git.git" 8 | }, 9 | "homepage": "https://github.com/mmkal/plv8-git#readme", 10 | "bugs": { 11 | "url": "https://github.com/mmkal/plv8-git/issues" 12 | }, 13 | "keywords": [ 14 | "postgresql", 15 | "git", 16 | "plv8", 17 | "postgres", 18 | "psql", 19 | "sql", 20 | "isomorphic-git", 21 | "memfs", 22 | "event-sourcing" 23 | ], 24 | "license": "Apache-2.0", 25 | "main": "dist/bundle.js", 26 | "types": "dist/index.d.ts", 27 | "bin": "dist/cli.js", 28 | "scripts": { 29 | "prebuild": "rm -rf dist && rm -rf queries", 30 | "build": "yarn webpack && yarn generate && yarn compile", 31 | "ci": "yarn build && yarn test && yarn lint", 32 | "compile": "tsc -p tsconfig.lib.json", 33 | "predocker-apply": "yarn docker-copy-query", 34 | "docker-apply": "yarn psql -f /queries/create-git-functions.sql", 35 | "docker-bash": "docker-compose exec postgres bash", 36 | "docker-logs": "docker-compose logs --follow --tail 100", 37 | "docker-psql": "docker-compose exec postgres psql -h localhost -U postgres postgres", 38 | "predocker-copy-query": "yarn docker-exec mkdir -p /queries", 39 | "docker-copy-query": "docker cp queries/create-git-functions.sql plv8-git_postgres_1:/queries", 40 | "docker-exec": "docker exec plv8-git_postgres_1", 41 | "eslint": "eslint --max-warnings 0", 42 | "generate": "yarn tsn scripts/generate-queries", 43 | "lint": "tsc -p . && yarn eslint . ", 44 | "prepare": "patch-package", 45 | "psql": "yarn --silent docker-exec psql -h localhost -U postgres postgres", 46 | "test": "jest", 47 | "tsn": "ts-node --transpile-only", 48 | "webpack": "webpack" 49 | }, 50 | "dependencies": { 51 | "@rushstack/ts-command-line": "^4.7.3" 52 | }, 53 | "devDependencies": { 54 | "@babel/core": "7.13.8", 55 | "@typescript-eslint/parser": "4.15.2", 56 | "assert": "2.0.0", 57 | "babel-loader": "8.2.2", 58 | "babel-plugin-transform-async-to-promises": "0.8.15", 59 | "buffer": "6.0.3", 60 | "eslint": "7.21.0", 61 | "eslint-plugin-codegen": "0.15.0", 62 | "eslint-plugin-prettier": "3.3.1", 63 | "isomorphic-git": "1.9.2", 64 | "jest": "26.6.3", 65 | "memfs": "3.2.2", 66 | "patch-package": "6.4.0", 67 | "path-browserify": "1.0.1", 68 | "prettier": "2.2.1", 69 | "process": "0.11.10", 70 | "slonik": "23.8.5", 71 | "stream-browserify": "3.0.0", 72 | "ts-jest": "26.5.2", 73 | "ts-loader": "9.1.1", 74 | "ts-node": "9.1.1", 75 | "typescript": "4.2.2", 76 | "url": "0.11.0", 77 | "util": "0.12.3", 78 | "webpack": "5.24.2", 79 | "webpack-cli": "4.5.0" 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # plv8-git 2 | 3 | An experimental helper which tracks the modification history of rows in postgres database tables, using git, based on the idea in [this tweet](https://twitter.com/mayfer/status/1308606131426582528). 4 | 5 | The implementation uses [plv8](https://github.com/plv8/plv8) to run JavaScript in postgres, with [isomorphic-git](https://npmjs.com/package/isomorphic-git) and [memfs](https://npmjs.com/package) to perform git operations in-memory. 6 | 7 | 8 | - [Motivation](#motivation) 9 | - [Usage](#usage) 10 | - [Tracking history](#tracking-history) 11 | - [Deletions](#deletions) 12 | - [Options](#options) 13 | - [Commit messages](#commit-messages) 14 | - [Git config](#git-config) 15 | - [Log depth](#log-depth) 16 | - [Tags](#tags) 17 | - [Restoring previous versions](#restoring-previous-versions) 18 | - [Column name clashes](#column-name-clashes) 19 | - [Caveat](#caveat) 20 | - [Implementation](#implementation) 21 | 22 | 23 | ## Motivation 24 | 25 | To paraphrase [@mayfer's twitter thread](https://twitter.com/mayfer/status/1308606131426582528): 26 | 27 | 28 | - never have to worry about building edit/delete/undo/backup/recover type features, one generic git-backed [column] is enough 29 | 30 | - removes the need to keep additional SQL tables which keep logs of all edit histories. 31 | 32 | - makes event sourcing a lot more modular. instead of tons of tables storing custom events, every SQL update on a column also updates its git bundle, saved into a separate binary column 33 | 34 | - with just 1 extra column, you 35 | can add multiuser versioning to *any* indexed column! 36 | 37 | - how cool this will be for large JSON or other text blob c get overwritten a lot duringall commits are controlled by the main app, it's trivial to integrate commit authors directly into any regular application's user auth system 38 | 39 | - due to the git standard, this repo then can easily be fed into any generic git UI for all sorts of diffing, logging & visualizing 40 | 41 | ## Usage 42 | 43 | The easiest way to get started is to use the pre-baked sql files exported with the package: 44 | 45 | ```bash 46 | npm install plv8-git 47 | 48 | psql -c " 49 | create extension if not exists plv8; 50 | select plv8_version(); 51 | " 52 | psql -f node_modules/plv8-git/queries/create-git-functions.sql 53 | ``` 54 | 55 | Or from javascript: 56 | 57 | ```js 58 | const sqlClient = getSqlClient() 59 | 60 | const sql = require('plv8-git/queries').getGitFunctionsSql() 61 | await sqlClient.runRawSql(sql) 62 | ``` 63 | 64 | Note: for `create extension plv8` to work the plv8.control file must exist on your database system. You can use [the postgres-plv8 docker image](https://github.com/clkao/docker-postgres-plv8/tree/master/11-2) for development (or production, if you really want to deploy a containerised database to production). Amazon RDS instances [have the extension available](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_PostgreSQL.html), as does [Azure Postgres 11](https://docs.microsoft.com/en-us/azure/postgresql/concepts-extensions#postgres-11-extensions). 65 | 66 | This will have created three postgres functions: `git_track`, `git_log` and `git_resolve`. 67 | 68 | 69 | ### Tracking history 70 | 71 | `git_track` is a trigger function that can be added to any table, with a `json` column, default-named `git`: 72 | 73 | ```sql 74 | create table test_table( 75 | id int, 76 | text text, 77 | git json 78 | ); 79 | 80 | create trigger test_table_git_track_trigger 81 | before insert or update 82 | on test_table for each row 83 | execute procedure git_track(); 84 | ``` 85 | 86 | Now, whenever rows are inserted or updated into the `test_table` table, the `git` column will automatically be managed as a serialisation of the `.git` folder of an ephemeral git repo. All you need to do is `insert`/`update` as normal: 87 | 88 | ```sql 89 | insert into test_table(id, text) 90 | values(1, 'item 1 old content'); 91 | 92 | update test_table 93 | set text = 'item 1 new content' 94 | where id = 1; 95 | ``` 96 | 97 | There's still just a single row in the `test_table` table, but the full history of it is tracked in the `git` column. The `git_log` function can be used to access the change history: 98 | 99 | ```sql 100 | select git_log(git) 101 | from test_table 102 | where id = 1 103 | ``` 104 | 105 | This query will return: 106 | 107 | ```json 108 | { 109 | "git_log": [ 110 | { 111 | "message": "test_table_git_track_trigger: BEFORE UPDATE ROW on public.test_table", 112 | "author": "pguser (pguser@pg.com)", 113 | "timestamp": "2000-12-25T12:00:00.000Z", 114 | "oid": "[oid]", 115 | "tags": [], 116 | "changes": [ 117 | { 118 | "field": "text", 119 | "new": "item 1 new content", 120 | "old": "item 1 old content" 121 | } 122 | ] 123 | }, 124 | { 125 | "message": "test_table_git_track_trigger: BEFORE INSERT ROW on public.test_table", 126 | "author": "pguser (pguser@pg.com)", 127 | "timestamp": "2000-12-25T12:00:00.000Z", 128 | "oid": "[oid]", 129 | "tags": [], 130 | "changes": [ 131 | { 132 | "field": "id", 133 | "new": 1 134 | }, 135 | { 136 | "field": "text", 137 | "new": "item 1 old content" 138 | } 139 | ] 140 | } 141 | ] 142 | } 143 | ``` 144 | 145 | i.e. you can see the row's full history, in human- and machine-readable form, straight from the table. 146 | 147 | To use existing git clients to get rich visual diffs, etc., you can simply pull the `git` field for a given row, and convert it into real files: 148 | 149 | ```sql 150 | select git from test_table where id = 1 151 | ``` 152 | 153 | ```json 154 | { 155 | "git": { 156 | "/repo/.git/objects/8a/ed642bf5118b9d3c859bd4be35ecac75b6e873": "[byte array]", 157 | "/repo/.git/objects/d0/ff5974b6aa52cf562bea5921840c032a860a91": "[byte array]", 158 | "/repo/.git/objects/d8/4bdb34d4eeef4034d77e5403f850e35bc4a51b": "[byte array]", 159 | "/repo/.git/objects/a4/16ea84421fa7e1351582da48235bac88380a33": "[byte array]", 160 | "/repo/.git/objects/fb/d04e1aae9ce0b11a8946e2c9ac2619f7428a64": "[byte array]", 161 | "/repo/.git/objects/a1/9a1584344c1f3783bff51524a5a4b86f2cc093": "[byte array]", 162 | "/repo/.git/objects/8a/b31b5afaea56114427e1f01b81d001b079a0f5": "[byte array]", 163 | "/repo/.git/refs/heads/main": "[byte array]", 164 | "/repo/.git/config": "[byte array]", 165 | "/repo/.git/HEAD": "[byte array]", 166 | "/repo/.git/index": "[byte array]" 167 | } 168 | } 169 | ``` 170 | 171 | This will return a json-formatted object, with keys corresponding to file system paths, and byte-array values as contents. Write them to disk using the CLI tool provided with this package: 172 | 173 | ```bash 174 | GIT=$(psql -qAt -c "select git from test_table where id = 1") 175 | node_modules/.bin/plv8-git write --input "$GIT" --output path/to/git/dir 176 | ``` 177 | 178 | `path/to/git/dir` will now be a valid git repository, with one file corresponding to each column in `test_table`. You can `cd` into it, and run commands like `git log`, or use your favourite git UI to inspect the history in as much detail as you'd like. 179 | 180 | ### Deletions 181 | 182 | You can also take advantage of the `git` column to track deletions, by adding a delete hook: 183 | 184 | ```sql 185 | create table deleted_history( 186 | schemaname name, 187 | tablename name, 188 | identifier jsonb, 189 | deleted_at timestamptz, 190 | git json 191 | ); 192 | 193 | create function test_table_track_deletion() returns trigger as 194 | $$ 195 | begin 196 | insert into deleted_history(schemaname, tablename, identifier, deleted_at, git) 197 | values ('public', 'test_table', jsonb_build_object('id', OLD.id), now(), OLD.git); 198 | 199 | return OLD; 200 | end 201 | $$ 202 | language plpgsql; 203 | 204 | create trigger test_table_track_deletion_trigger 205 | before delete 206 | on test_table for each row 207 | execute procedure test_table_track_deletion(); 208 | ``` 209 | 210 | You can now perform deletions as normal and they'll be automatically tracked in `deleted_history`: 211 | 212 | ```sql 213 | delete from test_table 214 | where id = 1 215 | ``` 216 | 217 | The `deleted_history` table can be queried in the same was as the other tables: 218 | 219 | ```sql 220 | select * 221 | from deleted_history 222 | where identifier->>'id' = '1' 223 | ``` 224 | 225 | This will return something like: 226 | 227 | ```json 228 | { 229 | "schemaname": "public", 230 | "tablename": "test_table", 231 | "identifier": { 232 | "id": 1 233 | }, 234 | "deleted_at": "2000-12-25T12:00:00.000Z", 235 | "git": { 236 | "/repo/.git/objects/8a/ed642bf5118b9d3c859bd4be35ecac75b6e873": "[byte array]", 237 | "/repo/.git/objects/d0/ff5974b6aa52cf562bea5921840c032a860a91": "[byte array]", 238 | "/repo/.git/objects/d8/4bdb34d4eeef4034d77e5403f850e35bc4a51b": "[byte array]", 239 | "/repo/.git/objects/a4/16ea84421fa7e1351582da48235bac88380a33": "[byte array]", 240 | "/repo/.git/objects/fb/d04e1aae9ce0b11a8946e2c9ac2619f7428a64": "[byte array]", 241 | "/repo/.git/objects/a1/9a1584344c1f3783bff51524a5a4b86f2cc093": "[byte array]", 242 | "/repo/.git/objects/8a/b31b5afaea56114427e1f01b81d001b079a0f5": "[byte array]", 243 | "/repo/.git/refs/heads/main": "[byte array]", 244 | "/repo/.git/config": "[byte array]", 245 | "/repo/.git/HEAD": "[byte array]", 246 | "/repo/.git/index": "[byte array]" 247 | } 248 | } 249 | ``` 250 | 251 | You can use `git_log` again to get a readable history: 252 | 253 | ```sql 254 | select git_log(git) 255 | from deleted_history 256 | where identifier->>'id' = '1' 257 | ``` 258 | 259 | ```json 260 | { 261 | "git_log": [ 262 | { 263 | "message": "test_table_git_track_trigger: BEFORE UPDATE ROW on public.test_table", 264 | "author": "pguser (pguser@pg.com)", 265 | "timestamp": "2000-12-25T12:00:00.000Z", 266 | "oid": "[oid]", 267 | "tags": [], 268 | "changes": [ 269 | { 270 | "field": "text", 271 | "new": "item 1 new content", 272 | "old": "item 1 old content" 273 | } 274 | ] 275 | }, 276 | { 277 | "message": "test_table_git_track_trigger: BEFORE INSERT ROW on public.test_table", 278 | "author": "pguser (pguser@pg.com)", 279 | "timestamp": "2000-12-25T12:00:00.000Z", 280 | "oid": "[oid]", 281 | "tags": [], 282 | "changes": [ 283 | { 284 | "field": "id", 285 | "new": 1 286 | }, 287 | { 288 | "field": "text", 289 | "new": "item 1 old content" 290 | } 291 | ] 292 | } 293 | ] 294 | } 295 | ``` 296 | 297 | In this example, `deleted_history` is generic enough that it could be the "history" table for several other relations, since it uses columns `schemaname` and `tablename`, and `identifier` as the flexible `JSONB` data type to allow for different types of primary key. This avoids the overhead of needing a new `_history` table for every relation created - all the data, including history, is captured in the `git` column. The `identifier` column is only used for lookups. 298 | 299 | ### Options 300 | 301 | #### Commit messages 302 | 303 | You can pass a custom commit message and author by pre-loading the `git` property with `commit` details, which can include a commit message and user info: 304 | 305 | ```sql 306 | insert into test_table( 307 | id, 308 | text, 309 | git 310 | ) 311 | values( 312 | 2, 313 | 'original value set by alice', 314 | '{ "commit": { "message": "some custom message", "author": { "name": "Alice", "email": "alice@gmail.com" } } }' 315 | ) 316 | ``` 317 | 318 | ```sql 319 | select git_log(git) 320 | from test_table 321 | where id = 2 322 | ``` 323 | 324 | ```json 325 | { 326 | "git_log": [ 327 | { 328 | "message": "some custom message\\n\\ntest_table_git_track_trigger: BEFORE INSERT ROW on public.test_table", 329 | "author": "Alice (alice@gmail.com)", 330 | "timestamp": "2000-12-25T12:00:00.000Z", 331 | "oid": "[oid]", 332 | "tags": [], 333 | "changes": [ 334 | { 335 | "field": "id", 336 | "new": 2 337 | }, 338 | { 339 | "field": "text", 340 | "new": "original value set by alice" 341 | } 342 | ] 343 | } 344 | ] 345 | } 346 | ``` 347 | 348 | #### Git config 349 | 350 | You can configure git using `git_set_local_config` or `git_set_global_config`: 351 | 352 | ```sql 353 | select git_set_local_config('user.name', 'Bob'); 354 | select git_set_local_config('user.email', 'bobby@company.com'); 355 | 356 | insert into test_table(id, text) 357 | values(201, 'value set by bob') 358 | ``` 359 | 360 | ```sql 361 | select git_log(git) 362 | from test_table 363 | where id = 201 364 | ``` 365 | 366 | ```json 367 | { 368 | "git_log": [ 369 | { 370 | "message": "test_table_git_track_trigger: BEFORE INSERT ROW on public.test_table", 371 | "author": "Bob (bobby@company.com)", 372 | "timestamp": "2000-12-25T12:00:00.000Z", 373 | "oid": "[oid]", 374 | "tags": [], 375 | "changes": [ 376 | { 377 | "field": "id", 378 | "new": 201 379 | }, 380 | { 381 | "field": "text", 382 | "new": "value set by bob" 383 | } 384 | ] 385 | } 386 | ] 387 | } 388 | ``` 389 | 390 | Under the hood these use `set_config` with the `is_local` parameter respectively true/false for the local/global variants. 391 | 392 | #### Log depth 393 | 394 | `git_log` also accepts a `depth` parameter to limit the amount of history that is fetched: 395 | 396 | ```sql 397 | update test_table 398 | set text = 'a new value set by admin', 399 | git = '{ "commit": { "message": "Changed because the previous value was out-of-date" } }' 400 | where id = 2 401 | ``` 402 | 403 | ```sql 404 | select git_log(git, depth := 1) 405 | from test_table 406 | where id = 2 407 | ``` 408 | 409 | ```json 410 | { 411 | "git_log": [ 412 | { 413 | "message": "Changed because the previous value was out-of-date\\n\\ntest_table_git_track_trigger: BEFORE UPDATE ROW on public.test_table", 414 | "author": "pguser (pguser@pg.com)", 415 | "timestamp": "2000-12-25T12:00:00.000Z", 416 | "oid": "[oid]", 417 | "tags": [], 418 | "changes": [ 419 | { 420 | "field": "text", 421 | "new": "a new value set by admin", 422 | "old": "original value set by alice" 423 | } 424 | ] 425 | } 426 | ] 427 | } 428 | ``` 429 | 430 | By setting `depth := 1`, only the most recent change is returned. 431 | 432 | #### Tags 433 | 434 | You can pass `tags` to the git object. The below example uses a convention of tagging with the day, month, and year so it will later be easy to restore to previous versions: 435 | 436 | ```sql 437 | insert into test_table(id, text, git) 438 | values (3, 'item 3 xmas day value', '{ "git": { "tags": ["2000-12-25", "2000-12", "2000"] } }'); 439 | 440 | update test_table 441 | set 442 | text = 'item 3 boxing day value', 443 | git = '{ "tags": ["2000-12-26", "2000-12", "2000"] }' 444 | where id = 3; 445 | 446 | update test_table 447 | set 448 | text = 'item 3 new year value', 449 | git = '{ "tags": ["2001-01-01", "2001-01", "2001"] }' 450 | where id = 3; 451 | ``` 452 | 453 | Or, set them in git config as a colon-separated list: 454 | 455 | ```sql 456 | select git_set_local_config('tags', 'your_app_request_id=1234:your_app_trace_id=5678'); 457 | 458 | update test_table 459 | set text = 'item 3 yet another value' 460 | where id = 3; 461 | ``` 462 | 463 | ### Restoring previous versions 464 | 465 | `git_resolve` gives you a json representation of a prior version of a row, which can be used for backup and restore. The first argument is a `git` json value, the second value is a valid git ref string (e.g. a git oid returned by `git_log`, or `HEAD`, or `main`. Note that an issue with [isomorphic-git](https://github.com/isomorphic-git/isomorphic-git/issues/1238) means that you can't currently pass values like `HEAD~1` here). 466 | 467 | Combine it with `git_log` to get a previous version - the below query uses `->1->>'oid'` to get the oid from the second item in the log array: 468 | 469 | ```sql 470 | select git_resolve(git, ref := git_log(git)->1->>'oid') 471 | from test_table 472 | where id = 2 473 | ``` 474 | 475 | ```json 476 | { 477 | "git_resolve": { 478 | "id": 2, 479 | "text": "original value set by alice" 480 | } 481 | } 482 | ``` 483 | 484 | This can be used in an update query to revert a change: 485 | 486 | ```sql 487 | update test_table set (id, text) = 488 | ( 489 | select id, text 490 | from json_populate_record( 491 | null::test_table, 492 | git_resolve(git, ref := git_log(git)->1->>'oid') 493 | ) 494 | ) 495 | where id = 2 496 | returning id, text 497 | ``` 498 | 499 | ```json 500 | { 501 | "id": 2, 502 | "text": "original value set by alice" 503 | } 504 | ``` 505 | 506 | If you used `tags` as described above, you can take advantage of them to restore to a known-good state easily: 507 | 508 | ```sql 509 | select git_log(git) 510 | from test_table 511 | where id = 3 512 | ``` 513 | 514 | ```json 515 | [ 516 | { 517 | "git_log": [ 518 | { 519 | "message": "test_table_git_track_trigger: BEFORE UPDATE ROW on public.test_table", 520 | "author": "pguser (pguser@pg.com)", 521 | "timestamp": "2000-12-25T12:00:00.000Z", 522 | "oid": "[oid]", 523 | "tags": [ 524 | "your_app_request_id=1234", 525 | "your_app_trace_id=5678" 526 | ], 527 | "changes": [ 528 | { 529 | "field": "text", 530 | "new": "item 3 yet another value", 531 | "old": "item 3 new year value" 532 | } 533 | ] 534 | }, 535 | { 536 | "message": "test_table_git_track_trigger: BEFORE UPDATE ROW on public.test_table", 537 | "author": "pguser (pguser@pg.com)", 538 | "timestamp": "2000-12-25T12:00:00.000Z", 539 | "oid": "[oid]", 540 | "tags": [ 541 | "2001", 542 | "2001-01", 543 | "2001-01-01" 544 | ], 545 | "changes": [ 546 | { 547 | "field": "text", 548 | "new": "item 3 new year value", 549 | "old": "item 3 boxing day value" 550 | } 551 | ] 552 | }, 553 | { 554 | "message": "test_table_git_track_trigger: BEFORE UPDATE ROW on public.test_table", 555 | "author": "pguser (pguser@pg.com)", 556 | "timestamp": "2000-12-25T12:00:00.000Z", 557 | "oid": "[oid]", 558 | "tags": [ 559 | "2000", 560 | "2000-12", 561 | "2000-12-26" 562 | ], 563 | "changes": [ 564 | { 565 | "field": "text", 566 | "new": "item 3 boxing day value", 567 | "old": "item 3 xmas day value" 568 | } 569 | ] 570 | }, 571 | { 572 | "message": "test_table_git_track_trigger: BEFORE INSERT ROW on public.test_table", 573 | "author": "pguser (pguser@pg.com)", 574 | "timestamp": "2000-12-25T12:00:00.000Z", 575 | "oid": "[oid]", 576 | "tags": [], 577 | "changes": [ 578 | { 579 | "field": "id", 580 | "new": 3 581 | }, 582 | { 583 | "field": "text", 584 | "new": "item 3 xmas day value" 585 | } 586 | ] 587 | } 588 | ] 589 | } 590 | ] 591 | ``` 592 | 593 | ```sql 594 | update test_table set (id, text) = 595 | ( 596 | select id, text 597 | from json_populate_record( 598 | null::test_table, 599 | git_resolve(git, ref := '2000-12') 600 | ) 601 | ) 602 | where id = 3 603 | returning id, text 604 | ``` 605 | 606 | ```json 607 | { 608 | "id": 3, 609 | "text": "item 3 boxing day value" 610 | } 611 | ``` 612 | 613 | ```sql 614 | update test_table set (id, text) = 615 | ( 616 | select id, text 617 | from json_populate_record( 618 | null::test_table, 619 | git_resolve(git, ref := 'your_app_request_id=1234') 620 | ) 621 | ) 622 | where id = 3 623 | returning id, text 624 | ``` 625 | 626 | ```json 627 | { 628 | "id": 3, 629 | "text": "item 3 yet another value" 630 | } 631 | ``` 632 | 633 | A similar technique can restore a deleted item: 634 | 635 | ```sql 636 | insert into test_table 637 | select * from json_populate_record( 638 | null::test_table, 639 | ( 640 | select git_resolve(git, ref := 'HEAD') 641 | from deleted_history 642 | where tablename = 'test_table' and identifier->>'id' = '1' 643 | ) 644 | ) 645 | returning id, text 646 | ``` 647 | 648 | ```json 649 | { 650 | "id": 1, 651 | "text": "item 1 new content" 652 | } 653 | ``` 654 | 655 | ### Column name clashes 656 | 657 | History can be tracked even on pre-existing tables which already have a `git` column used for something else: 658 | 659 | ```sql 660 | create table repos( 661 | id int, 662 | name text, 663 | git text -- the repo clone url 664 | ); 665 | ``` 666 | 667 | Any column with type `json` can be used, by passing the column name when creating a trigger: 668 | 669 | ```sql 670 | alter table repos 671 | add column my_custom_plv8_git_column json; 672 | 673 | create trigger repos_git_track_trigger 674 | before insert or update 675 | on repos for each row 676 | execute procedure git_track('my_custom_plv8_git_column'); 677 | 678 | insert into repos(id, name, git) 679 | values (1, 'plv8-git', 'https://github.com/mmkal/plv8-git.git'); 680 | ``` 681 | 682 | ```sql 683 | select git_log(my_custom_plv8_git_column) 684 | from repos 685 | where git = 'https://github.com/mmkal/plv8-git.git' 686 | ``` 687 | 688 | ```json 689 | { 690 | "git_log": [ 691 | { 692 | "message": "repos_git_track_trigger: BEFORE INSERT ROW on public.repos", 693 | "author": "pguser (pguser@pg.com)", 694 | "timestamp": "2000-12-25T12:00:00.000Z", 695 | "oid": "[oid]", 696 | "tags": [], 697 | "changes": [ 698 | { 699 | "field": "git", 700 | "new": "https://github.com/mmkal/plv8-git.git" 701 | }, 702 | { 703 | "field": "id", 704 | "new": 1 705 | }, 706 | { 707 | "field": "name", 708 | "new": "plv8-git" 709 | } 710 | ] 711 | } 712 | ] 713 | } 714 | ``` 715 | 716 | 717 | ## Caveat 718 | 719 | - This library is experimental, and hasn't been pressure-tested. There may well be edge-cases where it falls down. 720 | - It hasn't been performance-tested yet. It works well for rows with small, easily-json-stringifiable data. Large, frequently updated rows may hit issues. 721 | - It currently uses the `JSON` data type to store a serialised copy of the `.git` repo folder. This can likely be optimised to use `BYTEA` or another data type. 722 | - It uses several tools that were _not_ built with each other in mind (although each is well-designed and flexible enough for them to play nice without too many problems). See the [implementation section](#implementation) 723 | - It's still in v0, so breaking changes may occur. 724 | 725 | ## Implementation 726 | 727 | At its core, this library bundles [isomorphic-git](https://npmjs.com/package/isomorphic-git) and [memfs](https://npmjs.com/package/memfs) to produce an entirely in-memory, synchronous git implementation which can run inside postgres's plv8 engine. A few modifications are applied to each: 728 | 729 | Since plv8 triggers need to return values synchronously, but isomorphic-git uses promises extensively, a shim of the global `Promise` object was created called [`SyncPromise`](./src/sync-promise.ts). This has the same API as `Promise`, but its callbacks are executed immediately. 730 | 731 | To avoid the event-loop, all async-await code in isomorphic-git is transformed to `.then`, `.catch` etc. by [babel-plugin-transform-async-to-promises](https://npmjs.com/package/babel-plugin-transform-async-to-promises). `async-lock`, which is a dependency of isomorphic-git, is also [shimmed](./webpack/async-lock-shim.js) to bypass its locking mechanism which relies on timers - it's not necessary anyway, since all git operations take place on an ephemeral, in-memory, synchronous filesystem. 732 | 733 | `memfs` is also shimmed before being passed to isomorphic-git to [replace its promise-based operations with sync ones](./src/fs.ts). 734 | 735 | These libraries are bundled using webpack into a standalone module with no dependencies. The source code for this bundle is copied into a sql file by [generate-queries](./scripts/generate-queries.ts), so that it can be used to define a postgres function with plv8. 736 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json", 3 | "extends": [ 4 | "config:base", 5 | ":semanticCommits", 6 | "schedule:monthly" 7 | ], 8 | "prConcurrentLimit": 4, 9 | "dependencyDashboard": true, 10 | "dependencyDashboardAutoclose": true, 11 | "packageRules": [ 12 | { 13 | "depTypeList": ["devDependencies"], 14 | "excludePackageNames": [ 15 | "isomorphic-git", 16 | "memfs" 17 | ], 18 | "groupName": "devDependencies", 19 | "automerge": true 20 | } 21 | ] 22 | } 23 | -------------------------------------------------------------------------------- /scripts/docs.js: -------------------------------------------------------------------------------- 1 | const path = require('path') 2 | const fs = require('fs') 3 | 4 | /** @type {import('eslint-plugin-codegen').Preset<{}>} */ 5 | module.exports = params => { 6 | const testfile = path.join(path.dirname(params.meta.filename), 'test/walkthrough.test.ts') 7 | const content = fs.readFileSync(testfile).toString() 8 | const lines = content.split('\n') 9 | const testName = 'walkthrough' 10 | const start = lines.findIndex(line => line.startsWith(`test('${testName}'`)) + 1 11 | if (start === 0) { 12 | throw new Error(`Couldn't find test ${testName} in ${testfile}`) 13 | } 14 | const end = lines.findIndex((line, i) => i > start && line.startsWith('})')) 15 | 16 | let codeBlockLinePrefix = 'CODE_BLOCK_LINE:' 17 | 18 | return lines 19 | .slice(start, end) 20 | .map(line => line.replace(/^ /, '')) 21 | .join('\n') 22 | .split('`') 23 | .map((section, i) => { 24 | return i % 2 === 0 25 | ? section 26 | : section 27 | .split('\n') 28 | .map(line => line.replace(/^ /, codeBlockLinePrefix)) 29 | .join('\n') 30 | }) 31 | .join('`') 32 | .split('\n') 33 | .filter(line => !line.startsWith('// todo') && !line.startsWith('// TODO')) 34 | .map((line, i) => { 35 | if (line.endsWith('=> {')) { 36 | codeBlockLinePrefix += ' ' 37 | return null 38 | } 39 | if (line.endsWith('})')) { 40 | codeBlockLinePrefix = codeBlockLinePrefix.replace(/ $/, '') 41 | return null 42 | } 43 | if (line.includes('sql`')) return '```sql' 44 | if (line.includes('.toMatchInlineSnapshot(`')) return '```json' 45 | if (line.trim().endsWith('`)')) return '```' 46 | if (line.startsWith('// ')) return line.replace('// ', '') 47 | if (line.startsWith(codeBlockLinePrefix)) return line.replace(codeBlockLinePrefix, '') 48 | if (line.replace(/\r/, '') !== '') { 49 | throw new Error( 50 | ` 51 | Unexpected content in test ${testName} from ${testfile}:${i} 52 | The test is used to generate documentation, so should only contain sql queries and inline snapshots. 53 | Use a different test for anything else! 54 | Found content: 55 | 56 | ${JSON.stringify(line)} 57 | `.replace(/^ +/g, ''), 58 | ) 59 | } 60 | return line 61 | }) 62 | .filter(line => line !== null) 63 | .join('\n') 64 | } 65 | -------------------------------------------------------------------------------- /scripts/generate-queries.ts: -------------------------------------------------------------------------------- 1 | import * as crypto from 'crypto' 2 | import * as fs from 'fs' 3 | import * as path from 'path' 4 | 5 | export const getQuery = (js: string) => { 6 | const hash = crypto.createHash('sha256').update(js).digest('hex') 7 | const quotes = `$_${hash}$` 8 | if (js.includes(quotes)) { 9 | throw new Error(`Failed to generate quote markers to properly escape js code`) 10 | } 11 | 12 | return ` 13 | create or replace function git_call_sync(name text, args json) returns json as 14 | ${quotes} 15 | var module = {}; // fake module; webpack does 'module.exports.blah = ...' 16 | ${js}; // <-- bundled code 17 | return module.exports.git_call_sync(name, args); 18 | ${quotes} 19 | language plv8; 20 | 21 | create or replace function git_track() returns trigger as 22 | $$ 23 | 24 | var git_call_sync = plv8.find_function('public.git_call_sync'); 25 | const newData = git_call_sync( 26 | 'rowToRepo', 27 | [{OLD, NEW, TG_NAME, TG_WHEN, TG_LEVEL, TG_OP, TG_RELID, TG_TABLE_NAME, TG_TABLE_SCHEMA, TG_ARGV}] 28 | ); 29 | return {...NEW, ...newData}; 30 | 31 | $$ 32 | 33 | language plv8; 34 | 35 | create or replace function git_resolve(git_json json, ref text) returns json as 36 | $$ 37 | 38 | declare 39 | result json; 40 | begin 41 | select git_call_sync('gitResolve', json_build_array(git_json, ref)) 42 | into result; 43 | 44 | return result; 45 | end; 46 | 47 | $$ 48 | language plpgsql; 49 | 50 | 51 | create or replace function git_log(git_json json, depth int) returns json as 52 | $$ 53 | 54 | declare 55 | result json; 56 | begin 57 | select git_call_sync('gitLog', json_build_array(git_json, depth)) 58 | into result; 59 | 60 | return result; 61 | end; 62 | 63 | $$ 64 | language plpgsql; 65 | 66 | -- overload for getting full depth 67 | create or replace function git_log(git_json json) returns json as 68 | $$ 69 | 70 | declare 71 | result json; 72 | begin 73 | select git_log(git_json, 0) 74 | into result; 75 | 76 | return result; 77 | end; 78 | 79 | $$ 80 | language plpgsql; 81 | 82 | create or replace function git_set_local_config(name text, value text) returns text as 83 | $$ 84 | select set_config('git.' || name, value, /* is_local */ true); 85 | $$ 86 | language sql; 87 | 88 | create or replace function git_set_global_config(name text, value text) returns text as 89 | $$ 90 | select set_config('git.' || name, value, /* is_local */ false); 91 | $$ 92 | language sql; 93 | 94 | create or replace function git_get_config(name text) returns text as 95 | $$ 96 | select current_setting('git.' || name, /* missing_ok */ true); 97 | $$ 98 | language sql; 99 | ` 100 | } 101 | 102 | export const write = (filesystem = fs) => { 103 | const sql = getQuery(filesystem.readFileSync(require.resolve('..')).toString()) 104 | const queriesDir = path.join(__dirname, '../queries') 105 | 106 | filesystem.mkdirSync(queriesDir, {recursive: true}) 107 | filesystem.writeFileSync(path.join(queriesDir, 'create-git-functions.sql'), sql, 'utf8') 108 | filesystem.writeFileSync( 109 | path.join(queriesDir, 'index.js'), 110 | `const path = require('path')\n` + // 111 | `const fs = require('fs')\n\n` + 112 | `exports.gitFunctionsPath = path.join(__dirname, 'create-git-functions.sql')\n\n` + 113 | `exports.getGitFunctionsSql = () => fs.readFileSync(exports.gitFunctionsPath, 'utf8')\n\n` + 114 | `exports.getGitFunctionsSqlAsync = () => fs.promises.readFile(exports.gitFunctionsPath, 'utf8')\n`, 115 | 'utf8', 116 | ) 117 | filesystem.writeFileSync( 118 | path.join(queriesDir, 'index.d.ts'), 119 | `/** Path on filesystem to file containing git tracking SQL functions */\n` + // 120 | `export const gitFunctionsPath: string\n\n` + 121 | `/** Synchronously read the file system to return git tracking SQL functions as a string */\n` + 122 | `export const getGitFunctionsSql: () => string\n\n` + 123 | `/** Asynchronously read the file system to return git tracking SQL functions as a string */\n` + 124 | `export const getGitFunctionsSqlAsync: () => Promise\n`, 125 | 'utf8', 126 | ) 127 | } 128 | 129 | if (require.main === module) write() 130 | -------------------------------------------------------------------------------- /src/cli.ts: -------------------------------------------------------------------------------- 1 | import * as cli from '@rushstack/ts-command-line' 2 | import * as fs from 'fs' 3 | import * as path from 'path' 4 | import * as childProcess from 'child_process' 5 | 6 | export class Plv8WriteAction extends cli.CommandLineAction { 7 | private _input!: cli.CommandLineStringParameter 8 | private _output!: cli.CommandLineStringParameter 9 | 10 | constructor() { 11 | super({ 12 | actionName: 'write', 13 | summary: 'Write a git repo to disk', 14 | documentation: 15 | 'Pass a json-formatted git repo as input, and a directory path as output. A fully functional git repo representing that row will be written to disk.', 16 | }) 17 | } 18 | 19 | onDefineParameters() { 20 | this._input = this.defineStringParameter({ 21 | parameterLongName: '--input', 22 | description: 'JSON-formatted git repo. Usually retrieved via "select git from some_table"', 23 | argumentName: 'GIT_JSON', 24 | }) 25 | 26 | this._output = this.defineStringParameter({ 27 | parameterLongName: '--output', 28 | description: 'Directory git repo should be written to', 29 | argumentName: 'PATH', 30 | }) 31 | } 32 | 33 | async onExecute() { 34 | const [input, output] = [this._input.value, this._output.value] 35 | if (!input) { 36 | throw new Error(`Missing input.\n` + this.renderHelpText()) 37 | } 38 | if (!output) { 39 | throw new Error(`Missing output.\n` + this.renderHelpText()) 40 | } 41 | const repo = JSON.parse(input) 42 | fs.mkdirSync(output, {recursive: true}) 43 | if (fs.readdirSync(output).length > 0) { 44 | throw new Error(`driectory ${output} is not empty`) 45 | } 46 | Object.entries(repo).forEach(([relativePath, byteArray]) => { 47 | const filepath = path.join(output, relativePath.replace('/repo/', '')) 48 | fs.mkdirSync(path.dirname(filepath), {recursive: true}) 49 | fs.writeFileSync(filepath, Buffer.from(byteArray)) 50 | }) 51 | childProcess.execSync('git checkout .', {cwd: output, stdio: 'inherit'}) 52 | console.log('git repo written to', output) 53 | } 54 | } 55 | 56 | export class Plv8CommmandLine extends cli.CommandLineParser { 57 | constructor() { 58 | super({ 59 | toolFilename: 'plv8-git', 60 | toolDescription: 'Write a git repo to disk', 61 | }) 62 | 63 | this.addAction(new Plv8WriteAction()) 64 | } 65 | 66 | onDefineParameters() {} 67 | 68 | onExecute() { 69 | return super.onExecute() 70 | } 71 | } 72 | 73 | if (require.main === module) { 74 | new Plv8CommmandLine().execute() 75 | } 76 | -------------------------------------------------------------------------------- /src/fs.ts: -------------------------------------------------------------------------------- 1 | import * as memfs from 'memfs' 2 | 3 | /** 4 | * isomorphic-git calls the async versions of file system APIs. But they _can_ all execute synchronously. 5 | * So shim all the promisified functions to just call the sync equivalents. 6 | */ 7 | export const setupMemfs = () => { 8 | const vol = new memfs.Volume() 9 | const fs = memfs.createFsFromVolume(vol) 10 | Object.assign(fs, { 11 | promises: { 12 | // shim the "promises" to be actually sync! 13 | readFile: fs.readFileSync.bind(fs), 14 | writeFile: fs.writeFileSync.bind(fs), 15 | mkdir: fs.mkdirSync.bind(fs), 16 | rmdir: fs.rmdirSync.bind(fs), 17 | unlink: fs.unlinkSync.bind(fs), 18 | stat: fs.statSync.bind(fs), 19 | lstat: fs.lstatSync.bind(fs), 20 | readdir: fs.readdirSync.bind(fs), 21 | readlink: fs.readlinkSync.bind(fs), 22 | symlink: fs.symlinkSync.bind(fs), 23 | }, 24 | }) 25 | return {fs, vol} 26 | } 27 | -------------------------------------------------------------------------------- /src/git.ts: -------------------------------------------------------------------------------- 1 | import * as memfs from 'memfs' 2 | import * as path from 'path' 3 | import * as git from 'isomorphic-git' 4 | import * as serializer from './serializer' 5 | import {PG_Vars} from './pg-types' 6 | import {setupMemfs} from './fs' 7 | import {memoizeAsync} from './memoize' 8 | 9 | function writeGitFiles(gitFiles: any, fs: memfs.IFs) { 10 | if (!gitFiles) { 11 | throw new Error(`Expected gitFiles as object, got ${gitFiles}`) 12 | } 13 | Object.keys(gitFiles).map(filepath => { 14 | fs.mkdirSync(path.dirname(filepath), {recursive: true}) 15 | fs.writeFileSync(filepath, Buffer.from(gitFiles[filepath])) 16 | }) 17 | } 18 | 19 | /** 20 | * Implementation for a trigger function which takes a row as a hashmap, and returns 21 | * a new value with a `git` json property, representing the `.git` folder of a repo. 22 | * Note - a different column name can be passed to `TG_ARGV`. 23 | */ 24 | export const rowToRepo = ({OLD, NEW, ...pg}: PG_Vars) => { 25 | const {fs, vol} = setupMemfs() 26 | const repo = {fs, dir: '/repo'} 27 | const repoColumn = pg.TG_ARGV[0] || 'git' 28 | if (pg.TG_ARGV[0] && repoColumn.match(/\W/)) { 29 | throw new Error(`Invalid column name ${repoColumn}`) 30 | } 31 | 32 | const setupGitFolder = () => { 33 | if (pg.TG_OP === 'INSERT') { 34 | return git.init({...repo, defaultBranch: 'main'}) 35 | } 36 | 37 | if (!OLD![repoColumn]) { 38 | throw new Error(`expected ${repoColumn} column on ${pg.TG_OP} old value: ${JSON.stringify(OLD, null, 2)}.`) 39 | } 40 | 41 | return writeGitFiles(OLD![repoColumn], fs) 42 | } 43 | 44 | const gitParams = NEW?.[repoColumn] || {} 45 | 46 | const defaultCommitMessage = `${pg.TG_NAME}: ${pg.TG_WHEN} ${pg.TG_OP} ${pg.TG_LEVEL} on ${pg.TG_TABLE_SCHEMA}.${pg.TG_TABLE_NAME}`.trim() 47 | 48 | return Promise.resolve() 49 | .then(setupGitFolder) 50 | .then(() => { 51 | if (!NEW) return 52 | Object.entries(NEW) 53 | .filter(([k]) => k !== repoColumn) 54 | .forEach(([k, val]) => { 55 | const content = serializer.stringify(val) 56 | const filepath = `${repo.dir}/${k}` 57 | fs.writeFileSync(filepath, content, {encoding: 'utf8'}) 58 | }) 59 | return Promise.resolve() 60 | .then(() => git.add({...repo, filepath: '.'})) 61 | .then(() => 62 | git.commit({ 63 | ...repo, 64 | message: [ 65 | gitParams.commit?.message, 66 | getSetting('commit.message'), 67 | defaultCommitMessage, 68 | getSetting('commit.message.signature'), 69 | ] 70 | .filter(Boolean) 71 | .join('\n\n'), 72 | author: { 73 | name: gitParams.commit?.author?.name || getSetting('user.name') || 'pguser', 74 | email: gitParams.commit?.author?.email || getSetting('user.email') || 'pguser@pg.com', 75 | }, 76 | }), 77 | ) 78 | .then(commit => { 79 | const allTags: string[] = [ 80 | ...(getSetting('tags')?.split(':') || []), // colon separated tags from config 81 | ...(gitParams.tags || []), 82 | ].filter(Boolean) 83 | return Promise.all( 84 | allTags.map((tag: string) => { 85 | return git.tag({...repo, ref: tag, object: commit}) 86 | }), 87 | ) 88 | }) 89 | }) 90 | .then(() => { 91 | const files: Record = {} 92 | // memfs has a toJSON method, but we can't use it directly because it tries to coerce all files into utf8 strings. Take advantage of its directory-walking though. 93 | const volJson = vol.toJSON(repo.dir + '/.git') 94 | const paths = Object.keys(volJson).filter(p => volJson[p] !== null) 95 | paths.forEach(p => { 96 | files[p] = Array.from(fs.readFileSync(p) as Buffer) 97 | }) 98 | 99 | return files 100 | }) 101 | .then(repo => ({ 102 | ...NEW, 103 | [repoColumn]: repo, 104 | })) 105 | } 106 | 107 | declare const plv8: { 108 | execute(sql: string, args?: unknown[]): Record[] 109 | } 110 | const getSetting = (name: string) => { 111 | // https://www.postgresql.org/docs/9.4/functions-admin.html 112 | const [{git_get_config}] = plv8.execute('select git_get_config($1)', [name]) 113 | return git_get_config as string | null 114 | } 115 | 116 | type TreeInfo = {type: string; content: string; oid: string} 117 | type WalkResult = {filepath: string; ChildInfo: TreeInfo; ParentInfo?: TreeInfo} 118 | 119 | /** 120 | * When passed a json object representing the `.git` folder of a repo, returns a list 121 | * of changes made to corresponding row. Optionally, pass `depth` to limit how far back 122 | * in time to fetch history for. 123 | */ 124 | export const gitLog = (gitRepoJson: object, depth?: number) => { 125 | const {fs} = setupMemfs() 126 | const repo = {fs, dir: '/repo'} 127 | 128 | // `listTags` lists all tags for the repo. so we need to use resolveRef to check that each tags is pointing at a given id 129 | // this can mean a lot of repeated calls. 130 | const resolveTagRef = memoizeAsync(git.resolveRef) 131 | 132 | return Promise.resolve() 133 | .then(() => writeGitFiles(gitRepoJson, fs)) 134 | .then(() => git.log({...repo, depth})) 135 | .then(log => { 136 | return Promise.all( 137 | log.map(e => { 138 | return git 139 | .walk({ 140 | ...repo, 141 | trees: [e.oid, e.commit.parent[0]].filter(Boolean).map(ref => git.TREE({ref})), 142 | map: (filepath, entries) => { 143 | const [Child, Parent] = entries || [] 144 | return Promise.all([resolveTree(Child), Parent && resolveTree(Parent)]).then( 145 | ([ChildInfo, ParentInfo]): WalkResult => ({filepath, ChildInfo, ParentInfo} as WalkResult), 146 | ) 147 | }, 148 | }) 149 | .then((results: WalkResult[]) => { 150 | return git.listTags({...repo}).then(tags => { 151 | return Promise.all(tags.map(t => resolveTagRef({...repo, ref: t}))).then(resolvedTags => { 152 | const filteredTags = tags.filter((t, i) => resolvedTags[i] === e.oid) 153 | return {results, tags: filteredTags} 154 | }) 155 | }) 156 | }) 157 | .then(({results, tags}) => ({ 158 | message: e.commit.message.trim(), 159 | author: `${e.commit.author.name} (${e.commit.author.email})`, 160 | timestamp: new Date(e.commit.author.timestamp * 1000).toISOString(), 161 | oid: e.oid, 162 | tags, 163 | changes: results 164 | .filter( 165 | r => r.ChildInfo?.type === 'blob' && r.filepath !== '.' && r.ChildInfo.oid !== r.ParentInfo?.oid, 166 | ) 167 | .map(r => ({ 168 | field: r.filepath, 169 | new: serializer.parse(r.ChildInfo.content), 170 | old: r.ParentInfo && serializer.parse(r.ParentInfo.content), 171 | })), 172 | })) 173 | }), 174 | ) 175 | }) 176 | } 177 | 178 | /** 179 | * Resolves a git ref into a dictionary of values, which can be passed to `INSERT` or `UPDATE` operations 180 | * @param gitRepoJson a json object representing the `.git` folder of a repo 181 | * @param ref a git ref string 182 | */ 183 | export const gitResolve = (gitRepoJson: object, ref: string) => { 184 | const {fs} = setupMemfs() 185 | const repo = {fs, dir: '/repo'} 186 | 187 | return Promise.resolve() 188 | .then(() => writeGitFiles(gitRepoJson, fs)) 189 | .then(() => 190 | git.walk({ 191 | ...repo, 192 | trees: [git.TREE({ref})], 193 | map: (filepath, entries) => resolveTree(entries![0])!.then(tree => ({filepath, tree})), 194 | }), 195 | ) 196 | .then((results: Array<{filepath: string; tree: ResolvedTree}>) => 197 | results 198 | .filter(r => r.tree.type === 'blob' && r.filepath !== '.') 199 | .reduce( 200 | (dict, next) => Object.assign(dict, {[next.filepath]: serializer.parse(next.tree.content)}), 201 | {} as Record, 202 | ), 203 | ) 204 | } 205 | 206 | /** 207 | * for some reason A.content() converts from a buffer to {"0": 100, "1": 101} format. 208 | * Object.values(...) converts back to a number array. Wasteful, but works for now. 209 | */ 210 | const btos = (obj: any) => Buffer.from(Object.values(obj || {})).toString() 211 | 212 | type PromiseResult

= P extends Promise ? X : never 213 | type ResolvedTree = PromiseResult> 214 | /** gets the type, content and oid for a `WalkerEntry` */ 215 | const resolveTree = (tree: git.WalkerEntry | null) => { 216 | const promises = tree && [tree.type(), tree.content().then(btos), tree.oid()] 217 | return promises && Promise.all(promises).then(([type, content, oid]) => ({type, content, oid})) 218 | } 219 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import * as git from './git' 2 | import {plog} from './pg-log' 3 | import {SyncPromise} from './sync-promise' 4 | 5 | export {SyncPromise} from './sync-promise' 6 | 7 | export * from './git' 8 | 9 | /** 10 | * Calls a function from the `./git` module. 11 | * Turn a promise-returning function into a synchronous one. Only works if the function uses 12 | * `.then` rather than `async`/`await`, and doesn't use timers/the event loop. 13 | * @param name the name of the function from the `./git` module 14 | * @param args args that will be passed directly to the function 15 | */ 16 | export const git_call_sync = (name: keyof typeof git, args: any[]) => { 17 | Object.assign(Promise, SyncPromise) 18 | Object.assign(console, {log: plog}) 19 | const operation: (...args: any[]) => Promise = git[name] 20 | let result 21 | operation(...args).then(r => (result = r)) 22 | return result 23 | } 24 | -------------------------------------------------------------------------------- /src/memoize.ts: -------------------------------------------------------------------------------- 1 | export const memoizeAsync = (fn: (...args: A) => Promise): ((...args: A) => Promise) => { 2 | const cache = new Map() 3 | return (...args: A) => { 4 | const key = JSON.stringify(args) 5 | if (cache.has(key)) { 6 | return Promise.resolve(cache.get(key)!) 7 | } 8 | 9 | return fn(...args).then(result => { 10 | cache.set(key, result) 11 | return result 12 | }) 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/pg-log.ts: -------------------------------------------------------------------------------- 1 | import {inspect} from 'util' 2 | 3 | declare const plv8: any, WARNING: any 4 | 5 | /** 6 | * Wrapper for `plv8.elog` which outputs to a `docker-compose up` console. 7 | */ 8 | export const plog = (...args: any[]): undefined => { 9 | if (typeof plv8 === 'undefined') return 10 | // inspect is an easy way to pretty-print any value 11 | const s = inspect(args).slice(1, -1).trim() 12 | // if it's on multiple lines, start it on its own line 13 | const formatted = s.includes('\n') ? `|||\n ${s}` : s 14 | 15 | plv8.elog(WARNING, formatted) 16 | } 17 | 18 | /** 19 | * stupid function that lets you do `debugPromise('some message').set = _someComplicatedFunctionCall` 20 | * which is useful when you want to inspect a giant expression without wrapping it parens 21 | * (some babel-webpack loaders wrap async calls in crazy expressions which makes them hard to read) 22 | */ 23 | export const debugPromise = (m: any, dbg: any) => { 24 | dbg && plog('about to kick off', m) 25 | const o = {} 26 | Object.defineProperty(o, 'set', { 27 | // prettier-ignore 28 | // pseudoset val 29 | set: val => { 30 | const promiseType = val instanceof Promise ? 'EVIL promise' : val.syncPromise ? 'nice promise' : 'some kinda thing' 31 | const plogArgs = [`${m} ${val} is ${promiseType}`] 32 | if (dbg) { 33 | plogArgs.push('. It will resolve to', val.val) 34 | } 35 | plog(...plogArgs) 36 | return val 37 | }, 38 | }) 39 | return o 40 | } 41 | 42 | const start = Date.now() 43 | let current = start 44 | 45 | /** useful for timing. just call `checkpoint('foo')` for timing info to be printed */ 46 | export const checkpoint = (name: string) => { 47 | const previous = current 48 | current = Date.now() 49 | plog(['checkpoint', name, current - previous, 'since previous and ', current - start, 'since start'].join(' ')) 50 | } 51 | -------------------------------------------------------------------------------- /src/pg-types.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * The variables available in plv8 triggers: https://plv8.github.io/#trigger-function-calls 3 | */ 4 | export type PG_Vars = { 5 | OLD?: Record 6 | NEW?: Record 7 | TG_NAME: unknown 8 | TG_WHEN: unknown 9 | TG_LEVEL: unknown 10 | TG_OP: unknown 11 | TG_RELID: unknown 12 | TG_TABLE_NAME: unknown 13 | TG_TABLE_SCHEMA: unknown 14 | TG_ARGV: string[] 15 | } 16 | -------------------------------------------------------------------------------- /src/serializer.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * A custom serializer to unambiguously serialize json-able content to files. 3 | * It would also be possible to just use JSON.stringify, but this way string values 4 | * are a bit more human readable when using git diff tools. 5 | */ 6 | 7 | const prefixes = { 8 | string: `// type:string\n`, 9 | json: `// type:json\n`, 10 | } 11 | 12 | export const stringify = (val: unknown) => { 13 | if (typeof val === 'string') { 14 | return prefixes.string + val 15 | } 16 | if (Array.isArray(val) && typeof val[0] === 'number') { 17 | return prefixes.json + JSON.stringify(val) 18 | } 19 | return prefixes.json + JSON.stringify(val, null, 2) 20 | } 21 | 22 | export const parse = (str: string) => { 23 | if (str.startsWith(prefixes.string)) { 24 | return str.slice(prefixes.string.length) 25 | } 26 | if (str.startsWith(prefixes.json)) { 27 | return JSON.parse(str.slice(prefixes.json.length)) 28 | } 29 | return str 30 | } 31 | -------------------------------------------------------------------------------- /src/sync-promise.ts: -------------------------------------------------------------------------------- 1 | const rethrow = (e: unknown) => { 2 | throw e 3 | } 4 | 5 | const createSyncPromise = (val: any): any => { 6 | const self = { 7 | syncPromise: true, 8 | val, 9 | then: (onok = (x: any) => x, onerr = rethrow) => { 10 | let next 11 | try { 12 | next = onok(val) 13 | } catch (e) { 14 | next = onerr(e) 15 | } 16 | return SyncPromise.resolve(next) 17 | }, 18 | catch: () => { 19 | throw Error(`catch not supported by sync promises`) 20 | }, 21 | } 22 | return self 23 | } 24 | 25 | /** 26 | * A partial replacement implementation of `Promise` which _doesn't_ use the event loop. plv8 triggers 27 | * require return values synchronously, so this executes the `.then` callbacks immediately. It doesn't 28 | * support `.catch` because errors are thrown synchronously too. 29 | */ 30 | export const SyncPromise: Pick = { 31 | resolve: ((val?: any): Promise => 32 | val && typeof val.then === 'function' ? val : createSyncPromise(val)) as typeof Promise.resolve, 33 | reject: rethrow, 34 | all: (((promises: any[]) => 35 | SyncPromise.resolve( 36 | promises.map(p => { 37 | let result: any = null 38 | SyncPromise.resolve(p).then((value: unknown) => (result = {value})) 39 | return result.value 40 | }), 41 | )) as unknown) as typeof Promise.all, 42 | } 43 | -------------------------------------------------------------------------------- /test/exports.test.ts: -------------------------------------------------------------------------------- 1 | test('queries exports', async () => { 2 | expect(require('../queries').gitFunctionsPath).toEqual(expect.any(String)) 3 | expect(require('../queries').getGitFunctionsSql()).toEqual(expect.any(String)) 4 | expect(await require('../queries').getGitFunctionsSqlAsync()).toEqual(expect.any(String)) 5 | }) 6 | -------------------------------------------------------------------------------- /test/memoize.test.ts: -------------------------------------------------------------------------------- 1 | import {memoizeAsync} from '../src/memoize' 2 | 3 | test('memoize', async () => { 4 | const mock = jest.fn(async () => Math.random()) 5 | 6 | const memoized = memoizeAsync(mock) 7 | 8 | const first = await memoized() 9 | const second = await memoized() 10 | 11 | expect([first, second]).toEqual([expect.any(Number), expect.any(Number)]) 12 | expect(mock).toHaveBeenCalledTimes(1) 13 | expect(second).toEqual(first) 14 | }) 15 | -------------------------------------------------------------------------------- /test/result-printer.ts: -------------------------------------------------------------------------------- 1 | import {createHash} from 'crypto' 2 | 3 | const start = new Date() 4 | /** stupid way of getting stable date results */ 5 | export const fuzzifyDate = (s: string) => { 6 | const real = new Date(s) 7 | return real.getTime() - start.getTime() < 5000 ? new Date('2000-12-25T12:00Z') : real 8 | } 9 | 10 | const gitRepoHashes: string[] = [] 11 | /** 12 | * JSON.stringify with a replacer that returns stable values for byte arrays, oids, git repo json representations and timestamps. 13 | * Useful for jest snapshot testing - the result is pretty human readable and stays the same across runs. 14 | */ 15 | export const readableJson = (o: unknown) => { 16 | /** 17 | * very advanced algorithm for determining if a key-value pair is worth pretty-printing. if not, 18 | * we're better off putting it on a single line so it doesn't take up too much space 19 | */ 20 | const isByteArray = (k: string, v: unknown) => Array.isArray(v) && v.length > 0 && v.every(x => typeof x === 'number') 21 | 22 | const isGitRepoJson = (k: string, v: unknown): v is Record => 23 | k === 'git' && Boolean(v) && typeof v === 'object' 24 | 25 | const markers: any = {} 26 | const replacer = (k: string, v: unknown): any => { 27 | if (isByteArray(k, v)) { 28 | return '[byte array]' 29 | } 30 | if (isGitRepoJson(k, v)) { 31 | const actualJson = JSON.stringify(v) 32 | gitRepoHashes.push(actualJson) 33 | const copy: typeof v = {} 34 | Object.keys(v).forEach((oldKey, i) => { 35 | // replace the actual git object paths with fake ones. hashing based on index 36 | const newKey = oldKey.replace(/\/objects\/(.*)/, () => { 37 | const hash = createHash('sha256') 38 | .update(`${i}.${gitRepoHashes.indexOf(actualJson)}`) 39 | .digest('hex') 40 | return `/objects/${hash.slice(0, 2)}/${hash.slice(2, 40)}` 41 | }) 42 | copy[newKey] = '[byte array]' 43 | }) 44 | return copy 45 | } 46 | if (k === 'oid' && typeof v === 'string') { 47 | return '[oid]' 48 | } 49 | if (k === 'timestamp' && typeof v === 'string') { 50 | return fuzzifyDate(v).toISOString() 51 | } 52 | return v 53 | } 54 | 55 | let json = JSON.stringify(o, replacer, 2) 56 | Object.keys(markers).forEach(id => { 57 | json = json.replace(id, markers[id]) 58 | }) 59 | 60 | return json 61 | } 62 | -------------------------------------------------------------------------------- /test/walkthrough.test.ts: -------------------------------------------------------------------------------- 1 | import {createPool, sql} from 'slonik' 2 | import {readFileSync} from 'fs' 3 | import * as path from 'path' 4 | import {fuzzifyDate, readableJson} from './result-printer' 5 | 6 | // NOTE! This file is used to auto-generate the readme. 7 | // Tests that shouldn't be part of the walkthrough documentation should go elsewhere. 8 | 9 | const connectionString = `postgresql://postgres:postgres@localhost:5435/postgres` 10 | 11 | const client = createPool(connectionString, { 12 | idleTimeout: 1, 13 | typeParsers: [{name: 'timestamptz', parse: v => fuzzifyDate(v).toISOString()}], 14 | }) 15 | let result: any 16 | 17 | beforeAll(async () => { 18 | // todo: use a different schema than public, then just drop and recreate the whole schema 19 | // set search path doesn't seem to do the trick, may require swapping out the pool or something 20 | await client.query(sql` 21 | drop trigger if exists test_table_track_deletion_trigger on test_table; 22 | drop function if exists test_table_track_deletion; 23 | drop table if exists deleted_history; 24 | drop trigger if exists test_table_git_track_trigger on test_table; 25 | drop table if exists test_table; 26 | drop trigger if exists repos_git_track_trigger on repos; 27 | drop table if exists repos; 28 | 29 | create extension if not exists plv8; 30 | 31 | drop function if exists git_track cascade; 32 | drop function if exists git_log(json, int) cascade; 33 | drop function if exists git_log cascade; 34 | drop function if exists git_resolve cascade; 35 | drop function if exists git_call_sync cascade; 36 | drop function if exists git_set_config cascade; 37 | drop function if exists git_set_local_config cascade; 38 | drop function if exists git_set_global_config cascade; 39 | drop function if exists git_get_config cascade; 40 | drop function if exists set_local_git_config; 41 | drop function if exists set_global_git_config; 42 | drop function if exists get_git_config; 43 | drop function if exists git_get_config; 44 | `) 45 | 46 | await client.query({ 47 | type: 'SLONIK_TOKEN_SQL', 48 | sql: readFileSync(path.join(__dirname, '../queries/create-git-functions.sql')).toString(), 49 | values: [], 50 | }) 51 | 52 | console.log('plv8 version', await client.oneFirst(sql`select plv8_version()`)) 53 | }) 54 | 55 | afterAll(async () => { 56 | await client.end() 57 | }) 58 | 59 | expect.addSnapshotSerializer({ 60 | test: () => true, 61 | print: val => readableJson(val), 62 | }) 63 | 64 | test('walkthrough', async () => { 65 | // ### Tracking history 66 | 67 | // `git_track` is a trigger function that can be added to any table, with a `json` column, default-named `git`: 68 | 69 | await client.query(sql` 70 | create table test_table( 71 | id int, 72 | text text, 73 | git json 74 | ); 75 | 76 | create trigger test_table_git_track_trigger 77 | before insert or update 78 | on test_table for each row 79 | execute procedure git_track(); 80 | `) 81 | 82 | // Now, whenever rows are inserted or updated into the `test_table` table, the `git` column will automatically be managed as a serialisation of the `.git` folder of an ephemeral git repo. All you need to do is `insert`/`update` as normal: 83 | 84 | await client.query(sql` 85 | insert into test_table(id, text) 86 | values(1, 'item 1 old content'); 87 | 88 | update test_table 89 | set text = 'item 1 new content' 90 | where id = 1; 91 | `) 92 | 93 | // There's still just a single row in the `test_table` table, but the full history of it is tracked in the `git` column. The `git_log` function can be used to access the change history: 94 | 95 | result = await client.one(sql` 96 | select git_log(git) 97 | from test_table 98 | where id = 1 99 | `) 100 | 101 | // This query will return: 102 | 103 | expect(result).toMatchInlineSnapshot(` 104 | { 105 | "git_log": [ 106 | { 107 | "message": "test_table_git_track_trigger: BEFORE UPDATE ROW on public.test_table", 108 | "author": "pguser (pguser@pg.com)", 109 | "timestamp": "2000-12-25T12:00:00.000Z", 110 | "oid": "[oid]", 111 | "tags": [], 112 | "changes": [ 113 | { 114 | "field": "text", 115 | "new": "item 1 new content", 116 | "old": "item 1 old content" 117 | } 118 | ] 119 | }, 120 | { 121 | "message": "test_table_git_track_trigger: BEFORE INSERT ROW on public.test_table", 122 | "author": "pguser (pguser@pg.com)", 123 | "timestamp": "2000-12-25T12:00:00.000Z", 124 | "oid": "[oid]", 125 | "tags": [], 126 | "changes": [ 127 | { 128 | "field": "id", 129 | "new": 1 130 | }, 131 | { 132 | "field": "text", 133 | "new": "item 1 old content" 134 | } 135 | ] 136 | } 137 | ] 138 | } 139 | `) 140 | 141 | // i.e. you can see the row's full history, in human- and machine-readable form, straight from the table. 142 | 143 | // To use existing git clients to get rich visual diffs, etc., you can simply pull the `git` field for a given row, and convert it into real files: 144 | 145 | result = await client.one(sql` 146 | select git from test_table where id = 1 147 | `) 148 | 149 | expect(result).toMatchInlineSnapshot(` 150 | { 151 | "git": { 152 | "/repo/.git/objects/8a/ed642bf5118b9d3c859bd4be35ecac75b6e873": "[byte array]", 153 | "/repo/.git/objects/d0/ff5974b6aa52cf562bea5921840c032a860a91": "[byte array]", 154 | "/repo/.git/objects/d8/4bdb34d4eeef4034d77e5403f850e35bc4a51b": "[byte array]", 155 | "/repo/.git/objects/a4/16ea84421fa7e1351582da48235bac88380a33": "[byte array]", 156 | "/repo/.git/objects/fb/d04e1aae9ce0b11a8946e2c9ac2619f7428a64": "[byte array]", 157 | "/repo/.git/objects/a1/9a1584344c1f3783bff51524a5a4b86f2cc093": "[byte array]", 158 | "/repo/.git/objects/8a/b31b5afaea56114427e1f01b81d001b079a0f5": "[byte array]", 159 | "/repo/.git/refs/heads/main": "[byte array]", 160 | "/repo/.git/config": "[byte array]", 161 | "/repo/.git/HEAD": "[byte array]", 162 | "/repo/.git/index": "[byte array]" 163 | } 164 | } 165 | `) 166 | 167 | // This will return a json-formatted object, with keys corresponding to file system paths, and byte-array values as contents. Write them to disk using the CLI tool provided with this package: 168 | 169 | // ```bash 170 | // GIT=$(psql -qAt -c "select git from test_table where id = 1") 171 | // node_modules/.bin/plv8-git write --input "$GIT" --output path/to/git/dir 172 | // ``` 173 | 174 | // `path/to/git/dir` will now be a valid git repository, with one file corresponding to each column in `test_table`. You can `cd` into it, and run commands like `git log`, or use your favourite git UI to inspect the history in as much detail as you'd like. 175 | 176 | // ### Deletions 177 | 178 | // You can also take advantage of the `git` column to track deletions, by adding a delete hook: 179 | 180 | await client.query(sql` 181 | create table deleted_history( 182 | schemaname name, 183 | tablename name, 184 | identifier jsonb, 185 | deleted_at timestamptz, 186 | git json 187 | ); 188 | 189 | create function test_table_track_deletion() returns trigger as 190 | $$ 191 | begin 192 | insert into deleted_history(schemaname, tablename, identifier, deleted_at, git) 193 | values ('public', 'test_table', jsonb_build_object('id', OLD.id), now(), OLD.git); 194 | 195 | return OLD; 196 | end 197 | $$ 198 | language plpgsql; 199 | 200 | create trigger test_table_track_deletion_trigger 201 | before delete 202 | on test_table for each row 203 | execute procedure test_table_track_deletion(); 204 | `) 205 | 206 | // You can now perform deletions as normal and they'll be automatically tracked in `deleted_history`: 207 | 208 | await client.query(sql` 209 | delete from test_table 210 | where id = 1 211 | `) 212 | 213 | // The `deleted_history` table can be queried in the same was as the other tables: 214 | 215 | result = await client.one(sql` 216 | select * 217 | from deleted_history 218 | where identifier->>'id' = '1' 219 | `) 220 | 221 | // This will return something like: 222 | 223 | expect(result).toMatchInlineSnapshot(` 224 | { 225 | "schemaname": "public", 226 | "tablename": "test_table", 227 | "identifier": { 228 | "id": 1 229 | }, 230 | "deleted_at": "2000-12-25T12:00:00.000Z", 231 | "git": { 232 | "/repo/.git/objects/8a/ed642bf5118b9d3c859bd4be35ecac75b6e873": "[byte array]", 233 | "/repo/.git/objects/d0/ff5974b6aa52cf562bea5921840c032a860a91": "[byte array]", 234 | "/repo/.git/objects/d8/4bdb34d4eeef4034d77e5403f850e35bc4a51b": "[byte array]", 235 | "/repo/.git/objects/a4/16ea84421fa7e1351582da48235bac88380a33": "[byte array]", 236 | "/repo/.git/objects/fb/d04e1aae9ce0b11a8946e2c9ac2619f7428a64": "[byte array]", 237 | "/repo/.git/objects/a1/9a1584344c1f3783bff51524a5a4b86f2cc093": "[byte array]", 238 | "/repo/.git/objects/8a/b31b5afaea56114427e1f01b81d001b079a0f5": "[byte array]", 239 | "/repo/.git/refs/heads/main": "[byte array]", 240 | "/repo/.git/config": "[byte array]", 241 | "/repo/.git/HEAD": "[byte array]", 242 | "/repo/.git/index": "[byte array]" 243 | } 244 | } 245 | `) 246 | 247 | // You can use `git_log` again to get a readable history: 248 | 249 | result = await client.one(sql` 250 | select git_log(git) 251 | from deleted_history 252 | where identifier->>'id' = '1' 253 | `) 254 | 255 | expect(result).toMatchInlineSnapshot(` 256 | { 257 | "git_log": [ 258 | { 259 | "message": "test_table_git_track_trigger: BEFORE UPDATE ROW on public.test_table", 260 | "author": "pguser (pguser@pg.com)", 261 | "timestamp": "2000-12-25T12:00:00.000Z", 262 | "oid": "[oid]", 263 | "tags": [], 264 | "changes": [ 265 | { 266 | "field": "text", 267 | "new": "item 1 new content", 268 | "old": "item 1 old content" 269 | } 270 | ] 271 | }, 272 | { 273 | "message": "test_table_git_track_trigger: BEFORE INSERT ROW on public.test_table", 274 | "author": "pguser (pguser@pg.com)", 275 | "timestamp": "2000-12-25T12:00:00.000Z", 276 | "oid": "[oid]", 277 | "tags": [], 278 | "changes": [ 279 | { 280 | "field": "id", 281 | "new": 1 282 | }, 283 | { 284 | "field": "text", 285 | "new": "item 1 old content" 286 | } 287 | ] 288 | } 289 | ] 290 | } 291 | `) 292 | 293 | // In this example, `deleted_history` is generic enough that it could be the "history" table for several other relations, since it uses columns `schemaname` and `tablename`, and `identifier` as the flexible `JSONB` data type to allow for different types of primary key. This avoids the overhead of needing a new `_history` table for every relation created - all the data, including history, is captured in the `git` column. The `identifier` column is only used for lookups. 294 | 295 | // ### Options 296 | 297 | // #### Commit messages 298 | 299 | // You can pass a custom commit message and author by pre-loading the `git` property with `commit` details, which can include a commit message and user info: 300 | 301 | await client.query(sql` 302 | insert into test_table( 303 | id, 304 | text, 305 | git 306 | ) 307 | values( 308 | 2, 309 | 'original value set by alice', 310 | '{ "commit": { "message": "some custom message", "author": { "name": "Alice", "email": "alice@gmail.com" } } }' 311 | ) 312 | `) 313 | 314 | result = await client.one(sql` 315 | select git_log(git) 316 | from test_table 317 | where id = 2 318 | `) 319 | 320 | expect(result).toMatchInlineSnapshot(` 321 | { 322 | "git_log": [ 323 | { 324 | "message": "some custom message\\n\\ntest_table_git_track_trigger: BEFORE INSERT ROW on public.test_table", 325 | "author": "Alice (alice@gmail.com)", 326 | "timestamp": "2000-12-25T12:00:00.000Z", 327 | "oid": "[oid]", 328 | "tags": [], 329 | "changes": [ 330 | { 331 | "field": "id", 332 | "new": 2 333 | }, 334 | { 335 | "field": "text", 336 | "new": "original value set by alice" 337 | } 338 | ] 339 | } 340 | ] 341 | } 342 | `) 343 | 344 | // #### Git config 345 | 346 | // You can configure git using `git_set_local_config` or `git_set_global_config`: 347 | 348 | result = await client.transaction(async transaction => { 349 | await transaction.query(sql` 350 | select git_set_local_config('user.name', 'Bob'); 351 | select git_set_local_config('user.email', 'bobby@company.com'); 352 | 353 | insert into test_table(id, text) 354 | values(201, 'value set by bob') 355 | `) 356 | 357 | return transaction.one(sql` 358 | select git_log(git) 359 | from test_table 360 | where id = 201 361 | `) 362 | }) 363 | 364 | expect(result).toMatchInlineSnapshot(` 365 | { 366 | "git_log": [ 367 | { 368 | "message": "test_table_git_track_trigger: BEFORE INSERT ROW on public.test_table", 369 | "author": "Bob (bobby@company.com)", 370 | "timestamp": "2000-12-25T12:00:00.000Z", 371 | "oid": "[oid]", 372 | "tags": [], 373 | "changes": [ 374 | { 375 | "field": "id", 376 | "new": 201 377 | }, 378 | { 379 | "field": "text", 380 | "new": "value set by bob" 381 | } 382 | ] 383 | } 384 | ] 385 | } 386 | `) 387 | 388 | // Under the hood these use `set_config` with the `is_local` parameter respectively true/false for the local/global variants. 389 | 390 | // #### Log depth 391 | 392 | // `git_log` also accepts a `depth` parameter to limit the amount of history that is fetched: 393 | 394 | await client.query(sql` 395 | update test_table 396 | set text = 'a new value set by admin', 397 | git = '{ "commit": { "message": "Changed because the previous value was out-of-date" } }' 398 | where id = 2 399 | `) 400 | 401 | result = await client.one(sql` 402 | select git_log(git, depth := 1) 403 | from test_table 404 | where id = 2 405 | `) 406 | 407 | expect(result).toMatchInlineSnapshot(` 408 | { 409 | "git_log": [ 410 | { 411 | "message": "Changed because the previous value was out-of-date\\n\\ntest_table_git_track_trigger: BEFORE UPDATE ROW on public.test_table", 412 | "author": "pguser (pguser@pg.com)", 413 | "timestamp": "2000-12-25T12:00:00.000Z", 414 | "oid": "[oid]", 415 | "tags": [], 416 | "changes": [ 417 | { 418 | "field": "text", 419 | "new": "a new value set by admin", 420 | "old": "original value set by alice" 421 | } 422 | ] 423 | } 424 | ] 425 | } 426 | `) 427 | 428 | // By setting `depth := 1`, only the most recent change is returned. 429 | 430 | // #### Tags 431 | 432 | // You can pass `tags` to the git object. The below example uses a convention of tagging with the day, month, and year so it will later be easy to restore to previous versions: 433 | 434 | await client.query(sql` 435 | insert into test_table(id, text, git) 436 | values (3, 'item 3 xmas day value', '{ "git": { "tags": ["2000-12-25", "2000-12", "2000"] } }'); 437 | 438 | update test_table 439 | set 440 | text = 'item 3 boxing day value', 441 | git = '{ "tags": ["2000-12-26", "2000-12", "2000"] }' 442 | where id = 3; 443 | 444 | update test_table 445 | set 446 | text = 'item 3 new year value', 447 | git = '{ "tags": ["2001-01-01", "2001-01", "2001"] }' 448 | where id = 3; 449 | `) 450 | 451 | // Or, set them in git config as a colon-separated list: 452 | 453 | await client.transaction(async transaction => { 454 | await transaction.query(sql` 455 | select git_set_local_config('tags', 'your_app_request_id=1234:your_app_trace_id=5678'); 456 | 457 | update test_table 458 | set text = 'item 3 yet another value' 459 | where id = 3; 460 | `) 461 | }) 462 | 463 | // ### Restoring previous versions 464 | 465 | // `git_resolve` gives you a json representation of a prior version of a row, which can be used for backup and restore. The first argument is a `git` json value, the second value is a valid git ref string (e.g. a git oid returned by `git_log`, or `HEAD`, or `main`. Note that an issue with [isomorphic-git](https://github.com/isomorphic-git/isomorphic-git/issues/1238) means that you can't currently pass values like `HEAD~1` here). 466 | 467 | // Combine it with `git_log` to get a previous version - the below query uses `->1->>'oid'` to get the oid from the second item in the log array: 468 | 469 | result = await client.one(sql` 470 | select git_resolve(git, ref := git_log(git)->1->>'oid') 471 | from test_table 472 | where id = 2 473 | `) 474 | 475 | expect(result).toMatchInlineSnapshot(` 476 | { 477 | "git_resolve": { 478 | "id": 2, 479 | "text": "original value set by alice" 480 | } 481 | } 482 | `) 483 | 484 | // This can be used in an update query to revert a change: 485 | 486 | result = await client.one(sql` 487 | update test_table set (id, text) = 488 | ( 489 | select id, text 490 | from json_populate_record( 491 | null::test_table, 492 | git_resolve(git, ref := git_log(git)->1->>'oid') 493 | ) 494 | ) 495 | where id = 2 496 | returning id, text 497 | `) 498 | 499 | expect(result).toMatchInlineSnapshot(` 500 | { 501 | "id": 2, 502 | "text": "original value set by alice" 503 | } 504 | `) 505 | 506 | // If you used `tags` as described above, you can take advantage of them to restore to a known-good state easily: 507 | 508 | result = await client.any(sql` 509 | select git_log(git) 510 | from test_table 511 | where id = 3 512 | `) 513 | 514 | expect(result).toMatchInlineSnapshot(` 515 | [ 516 | { 517 | "git_log": [ 518 | { 519 | "message": "test_table_git_track_trigger: BEFORE UPDATE ROW on public.test_table", 520 | "author": "pguser (pguser@pg.com)", 521 | "timestamp": "2000-12-25T12:00:00.000Z", 522 | "oid": "[oid]", 523 | "tags": [ 524 | "your_app_request_id=1234", 525 | "your_app_trace_id=5678" 526 | ], 527 | "changes": [ 528 | { 529 | "field": "text", 530 | "new": "item 3 yet another value", 531 | "old": "item 3 new year value" 532 | } 533 | ] 534 | }, 535 | { 536 | "message": "test_table_git_track_trigger: BEFORE UPDATE ROW on public.test_table", 537 | "author": "pguser (pguser@pg.com)", 538 | "timestamp": "2000-12-25T12:00:00.000Z", 539 | "oid": "[oid]", 540 | "tags": [ 541 | "2001", 542 | "2001-01", 543 | "2001-01-01" 544 | ], 545 | "changes": [ 546 | { 547 | "field": "text", 548 | "new": "item 3 new year value", 549 | "old": "item 3 boxing day value" 550 | } 551 | ] 552 | }, 553 | { 554 | "message": "test_table_git_track_trigger: BEFORE UPDATE ROW on public.test_table", 555 | "author": "pguser (pguser@pg.com)", 556 | "timestamp": "2000-12-25T12:00:00.000Z", 557 | "oid": "[oid]", 558 | "tags": [ 559 | "2000", 560 | "2000-12", 561 | "2000-12-26" 562 | ], 563 | "changes": [ 564 | { 565 | "field": "text", 566 | "new": "item 3 boxing day value", 567 | "old": "item 3 xmas day value" 568 | } 569 | ] 570 | }, 571 | { 572 | "message": "test_table_git_track_trigger: BEFORE INSERT ROW on public.test_table", 573 | "author": "pguser (pguser@pg.com)", 574 | "timestamp": "2000-12-25T12:00:00.000Z", 575 | "oid": "[oid]", 576 | "tags": [], 577 | "changes": [ 578 | { 579 | "field": "id", 580 | "new": 3 581 | }, 582 | { 583 | "field": "text", 584 | "new": "item 3 xmas day value" 585 | } 586 | ] 587 | } 588 | ] 589 | } 590 | ] 591 | `) 592 | 593 | result = await client.one(sql` 594 | update test_table set (id, text) = 595 | ( 596 | select id, text 597 | from json_populate_record( 598 | null::test_table, 599 | git_resolve(git, ref := '2000-12') 600 | ) 601 | ) 602 | where id = 3 603 | returning id, text 604 | `) 605 | 606 | expect(result).toMatchInlineSnapshot(` 607 | { 608 | "id": 3, 609 | "text": "item 3 boxing day value" 610 | } 611 | `) 612 | 613 | result = await client.one(sql` 614 | update test_table set (id, text) = 615 | ( 616 | select id, text 617 | from json_populate_record( 618 | null::test_table, 619 | git_resolve(git, ref := 'your_app_request_id=1234') 620 | ) 621 | ) 622 | where id = 3 623 | returning id, text 624 | `) 625 | 626 | expect(result).toMatchInlineSnapshot(` 627 | { 628 | "id": 3, 629 | "text": "item 3 yet another value" 630 | } 631 | `) 632 | 633 | // A similar technique can restore a deleted item: 634 | 635 | result = await client.one(sql` 636 | insert into test_table 637 | select * from json_populate_record( 638 | null::test_table, 639 | ( 640 | select git_resolve(git, ref := 'HEAD') 641 | from deleted_history 642 | where tablename = 'test_table' and identifier->>'id' = '1' 643 | ) 644 | ) 645 | returning id, text 646 | `) 647 | 648 | expect(result).toMatchInlineSnapshot(` 649 | { 650 | "id": 1, 651 | "text": "item 1 new content" 652 | } 653 | `) 654 | 655 | // ### Column name clashes 656 | 657 | // History can be tracked even on pre-existing tables which already have a `git` column used for something else: 658 | 659 | await client.query(sql` 660 | create table repos( 661 | id int, 662 | name text, 663 | git text -- the repo clone url 664 | ); 665 | `) 666 | 667 | // Any column with type `json` can be used, by passing the column name when creating a trigger: 668 | 669 | await client.query(sql` 670 | alter table repos 671 | add column my_custom_plv8_git_column json; 672 | 673 | create trigger repos_git_track_trigger 674 | before insert or update 675 | on repos for each row 676 | execute procedure git_track('my_custom_plv8_git_column'); 677 | 678 | insert into repos(id, name, git) 679 | values (1, 'plv8-git', 'https://github.com/mmkal/plv8-git.git'); 680 | `) 681 | 682 | result = await client.one(sql` 683 | select git_log(my_custom_plv8_git_column) 684 | from repos 685 | where git = 'https://github.com/mmkal/plv8-git.git' 686 | `) 687 | 688 | expect(result).toMatchInlineSnapshot(` 689 | { 690 | "git_log": [ 691 | { 692 | "message": "repos_git_track_trigger: BEFORE INSERT ROW on public.repos", 693 | "author": "pguser (pguser@pg.com)", 694 | "timestamp": "2000-12-25T12:00:00.000Z", 695 | "oid": "[oid]", 696 | "tags": [], 697 | "changes": [ 698 | { 699 | "field": "git", 700 | "new": "https://github.com/mmkal/plv8-git.git" 701 | }, 702 | { 703 | "field": "id", 704 | "new": 1 705 | }, 706 | { 707 | "field": "name", 708 | "new": "plv8-git" 709 | } 710 | ] 711 | } 712 | ] 713 | } 714 | `) 715 | }) 716 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "lib": [ 4 | "es2019" 5 | ], 6 | "module": "commonjs", 7 | "target": "es2019", 8 | "strict": true, 9 | "sourceMap": true, 10 | "noEmit": true 11 | }, 12 | "include": [ 13 | "src", 14 | "test", 15 | "scripts" 16 | ] 17 | } 18 | -------------------------------------------------------------------------------- /tsconfig.lib.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "rootDir": "src", 5 | "outDir": "dist", 6 | "noEmit": false, 7 | "declaration": true 8 | }, 9 | "include": ["src"] 10 | } 11 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path') 2 | const webpack = require('webpack') 3 | 4 | /** @type {webpack.Configuration} */ 5 | module.exports = { 6 | entry: path.join(__dirname, './src/index.ts'), 7 | mode: 'none', 8 | output: { 9 | libraryTarget: 'commonjs2', 10 | path: path.resolve(__dirname, 'dist'), 11 | filename: 'bundle.js', 12 | }, 13 | optimization: { 14 | minimize: false, 15 | }, 16 | target: 'web', 17 | module: { 18 | rules: [ 19 | { 20 | test: /\.tsx?$/, 21 | use: [{loader: 'ts-loader', options: {transpileOnly: true}}], 22 | exclude: /node_modules/, 23 | }, 24 | { 25 | test: /isomorphic-git/, 26 | use: [ 27 | { 28 | loader: 'babel-loader', 29 | options: { 30 | babelrc: false, 31 | plugins: [ 32 | // isomorphic git uses promises. but we want to avoid the event loop to allow using 33 | // filesystem operations that have synchronous fallbacks. Everything's done in memory 34 | // anyway, so no need for async operations. 35 | // This does _not_ allow doing things like cloning remote repos, but you'd never want to 36 | // do that in a database trigger anyway 37 | 'babel-plugin-transform-async-to-promises', 38 | ], 39 | }, 40 | }, 41 | ], 42 | }, 43 | { 44 | test: /async-lock/, 45 | use: [ 46 | { 47 | loader: require.resolve('./webpack/async-lock-shim'), 48 | }, 49 | ], 50 | }, 51 | ], 52 | }, 53 | resolve: { 54 | extensions: ['.tsx', '.ts', '.js'], 55 | fallback: { 56 | path: require.resolve('path-browserify'), 57 | stream: require.resolve('stream-browserify'), 58 | }, 59 | }, 60 | plugins: [ 61 | new webpack.ProvidePlugin({ 62 | Buffer: ['buffer', 'Buffer'], 63 | process: [require.resolve('./webpack/globals'), 'process'], 64 | setTimeout: [require.resolve('./webpack/globals'), 'setTimeout'], 65 | setInterval: [require.resolve('./webpack/globals'), 'setInterval'], 66 | }), 67 | ], 68 | } 69 | -------------------------------------------------------------------------------- /webpack/async-lock-shim.js: -------------------------------------------------------------------------------- 1 | /** 2 | * A dumb webpack loader that bypasses everything the `async-lock` module does. 3 | * async-lock uses clever timers to ensure operations don't conflict with each other. 4 | * That's not a concern in this project's use-case, because all operations happen synchronously on 5 | * an ephemeral in-memory filesystem, so conflicts aren't possible. And postgres doesn't have an 6 | * event loop, so async function won't work. 7 | * @type {(source: string) => string} 8 | */ 9 | module.exports = function (source) { 10 | return source.replace( 11 | /AsyncLock.prototype.acquire = function/, 12 | ` 13 | AsyncLock.prototype.acquire = function (key, fn) { 14 | return fn(); // no locking! Not needed because everything's synchronous; not possible because everything's synchronous 15 | } 16 | 17 | AsyncLock.prototype.acquire_original = function 18 | `, 19 | ) 20 | } 21 | -------------------------------------------------------------------------------- /webpack/globals.js: -------------------------------------------------------------------------------- 1 | module.exports.process = { 2 | env: {}, 3 | } 4 | module.exports.setTimeout = () => { 5 | throw new Error(`Can't use setTimeout in postgres!`) 6 | } 7 | module.exports.setInterval = () => { 8 | throw new Error(`Can't use setInterval in postgres!`) 9 | } 10 | --------------------------------------------------------------------------------