Title: Extensibility / Plugin Support · Issue #288 · sqlc-dev/sqlc · GitHub
Open Graph Title: Extensibility / Plugin Support · Issue #288 · sqlc-dev/sqlc
X Title: Extensibility / Plugin Support · Issue #288 · sqlc-dev/sqlc
Description: Add support for calling into plugins during code generation. One specific use case I have is to support automatic encryption/decryption of columns. To illustrate, here's very naive proposal: CREATE TABLE authors ( id BIGSERIAL PRIMARY KE...
Open Graph Description: Add support for calling into plugins during code generation. One specific use case I have is to support automatic encryption/decryption of columns. To illustrate, here's very naive proposal: CREATE...
X Description: Add support for calling into plugins during code generation. One specific use case I have is to support automatic encryption/decryption of columns. To illustrate, here's very naive proposal: CR...
Opengraph URL: https://github.com/sqlc-dev/sqlc/issues/288
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Extensibility / Plugin Support","articleBody":"Add support for calling into plugins during code generation. One specific use case I have is to support automatic encryption/decryption of columns. To illustrate, here's very naive proposal:\r\n\r\n```sql\r\nCREATE TABLE authors (\r\n id BIGSERIAL PRIMARY KEY,\r\n name text NOT NULL, -- :encrypted\r\n bio text -- :encrypted\r\n);\r\n```\r\n\r\nAdding the `:encrypted` annotation and having the `encryption` plugin enabled would cause sqlc to augment the generated code to look like the below:\r\n\r\n```go\r\n// db.go\r\ninterface Cipher {\r\n Encrypt(s string) (string, error)\r\n Decrypt(s string) (string, error)\r\n}\r\n\r\nfunc New(db DBTX) *Queries {\r\n\treturn &Queries{db: db, enc: &NoopCipher{}}\r\n}\r\n\r\nfunc NewWithEncryptor(db DBTX, ciph Cipher) *Queries {\r\n\treturn &Queries{db: db, ciph: ciph}\r\n}\r\n\r\ntype Queries struct {\r\n\tdb DBTX\r\n\tciph Cipher\r\n}\r\n\r\n// query.sql.go\r\nconst createAuthor = `-- name: CreateAuthor :one\r\nINSERT INTO authors (\r\n name, bio\r\n) VALUES (\r\n $1, $2\r\n)\r\nRETURNING id, name, bio\r\n`\r\n\r\ntype CreateAuthorParams struct {\r\n\tName string\r\n\tBio sql.NullString\r\n}\r\n\r\nfunc (q *Queries) CreateAuthor(ctx context.Context, arg CreateAuthorParams) (Author, error) {\r\n\tvar err error\r\n\tvar i Author\r\n\r\n\targ.Name, err = q.ciph.Encrypt(arg.Bio)\r\n\tif err != nil {\r\n\t\treturn i, err\r\n\t}\r\n\targ.Bio, err = q.ciph.Encrypt(arg.Bio)\r\n\tif err != nil {\r\n\t\treturn i, err\r\n\t}\r\n\trow := q.db.QueryRowContext(ctx, createAuthor, arg.Name, arg.Bio)\r\n\terr := row.Scan(&i.ID, &i.Name, &i.Bio)\r\n\tif err != nil {\r\n\t\treturn i, err\r\n\t}\r\n\ti.Name, err = q.ciph.Decrypt(i.Name)\r\n\tif err != nil {\r\n\t\treturn i, err\r\n\t}\r\n\ti.Bio, err = q.ciph.Decrypt(i.Bio)\r\n\tif err != nil {\r\n\t\treturn i, err\r\n\t}\r\n\r\n\treturn i, err\r\n}\r\n```\r\n\r\nThe `Cipher` is be an interface dictated by the plugin that the user would have to provide.\r\n\r\nThis proposal leaves a lot to answer and is just an example of what we can do with plugins. `sqlc` having an understanding of the schema and being able to parse annotations (comments) makes it a great place to hook into this automated code for translating. I don't think this is achievable simply with a custom type, since the `Cipher` needs to be initialized with secret key material to perform the encryption/decryption.\r\n\r\nI don't know how exactly you would do a plugin with sqlc. Maybe make it easy to create a custom build of the `sqlc` CLI, and adding support to the library for code generation hooks. Or, maybe invoking external binaries that communicate over grpc (a pattern in the Go community nowadays), but that could result in a complex protocol and you'd have to describe the AST and in-memory models in protobuf.\r\n\r\nI think this could be a really powerful feature though. Curious what your thoughts are.","author":{"url":"https://github.com/mightyguava","@type":"Person","name":"mightyguava"},"datePublished":"2020-01-22T19:32:13Z","url":"https://github.com/sqlc-dev/sqlc/issues/288"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:96d90b59-59da-ae67-fd83-93afac707c4d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C520:B1B84:1C6C45B:2419825:6B0195E2 |
| html-safe-nonce | 95170c359bf4a76ea09aff50ea08753d9cbaa1cd768f6bb2438242533d60fd09 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNTIwOkIxQjg0OjFDNkM0NUI6MjQxOTgyNTo2QjAxOTVFMiIsInZpc2l0b3JfaWQiOiI4ODQ3Mjc1ODE0MjYyMjQ4OTMwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | a661ecbd11075109f3393cbef82b4c19ab25d15c0db23f25c660cd16c66edcc2 |
| hovercard-subject-tag | issue:553747752 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/sqlc-dev/sqlc/288/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c1bfc675d0a3bd73bd1875f9922834da9184d83efa0335b23cab0022442757f4/sqlc-dev/sqlc/issues/288 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c1bfc675d0a3bd73bd1875f9922834da9184d83efa0335b23cab0022442757f4/sqlc-dev/sqlc/issues/288 |
| og:image:alt | Add support for calling into plugins during code generation. One specific use case I have is to support automatic encryption/decryption of columns. To illustrate, here's very naive proposal: CREATE... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mightyguava |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/sqlc-dev/sqlc git https://github.com/sqlc-dev/sqlc.git |
| octolytics-dimension-user_id | 136738596 |
| octolytics-dimension-user_login | sqlc-dev |
| octolytics-dimension-repository_id | 193160679 |
| octolytics-dimension-repository_nwo | sqlc-dev/sqlc |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 193160679 |
| octolytics-dimension-repository_network_root_nwo | sqlc-dev/sqlc |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width