{"id":22862383,"url":"https://github.com/dylibso/pg_extism","last_synced_at":"2025-04-30T22:02:27.727Z","repository":{"id":189093311,"uuid":"680030284","full_name":"dylibso/pg_extism","owner":"dylibso","description":"Wasm UDFs for Postgres, implemented as a TLE on plv8 using Extism","archived":false,"fork":false,"pushed_at":"2025-01-26T14:26:22.000Z","size":3979,"stargazers_count":31,"open_issues_count":1,"forks_count":1,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-04-25T18:54:01.887Z","etag":null,"topics":["extism","postgresql","udf","wasm"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dylibso.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-18T07:11:18.000Z","updated_at":"2025-04-24T16:02:28.000Z","dependencies_parsed_at":"2024-04-02T16:27:23.845Z","dependency_job_id":"5c24edd3-c71a-453a-9d7d-1d887e0f8166","html_url":"https://github.com/dylibso/pg_extism","commit_stats":null,"previous_names":["dylibso/pg_extism"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dylibso%2Fpg_extism","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dylibso%2Fpg_extism/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dylibso%2Fpg_extism/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dylibso%2Fpg_extism/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dylibso","download_url":"https://codeload.github.com/dylibso/pg_extism/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251789605,"owners_count":21644085,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["extism","postgresql","udf","wasm"],"created_at":"2024-12-13T10:13:13.314Z","updated_at":"2025-04-30T22:02:27.689Z","avatar_url":"https://github.com/dylibso.png","language":"TypeScript","readme":"# pg_extism\n\n**Note**: This is an experimental SDK.\n\n## Getting started\nMake sure [`plv8`](https://github.com/plv8/plv8) is installed on your server. Then enable it for your database:\n```sql\ncreate extension plv8;\n```\n\nNow run the script in [dist/index.sql](./dist/index.sql) and you'll get two SQL functions: `extism_create_plugin` and `extism_call`.\n\n## Use pg_extism\n\nAssume you have a table called `plugins`:\n```sql\nCREATE TABLE public.plugins (\n\tid int4 GENERATED ALWAYS AS IDENTITY( INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START 1 CACHE 1 NO CYCLE) NOT NULL,\n\t\"data\" bytea NULL,\n\t\"name\" varchar NULL,\n\tCONSTRAINT plugins_pk PRIMARY KEY (id)\n);\n```\n\nInsert a few plugins into the table. And then call:\n\n```sql\nselect extism_call(data, 'count_vowels', 'Hello World!') from plugins where id = 2;\n```\n\n**Note:** this assumes the plugin with id `2` has a function called `count_vowels`. You can find the plugin [here](https://github.com/extism/plugins/releases).\n\nIf you want more control over your plugin, you can use `extism_create_plugin`:\n\n```sql\n-- DROP FUNCTION public.count_vowels_kv(varchar);\n\nCREATE OR REPLACE FUNCTION public.count_vowels_kv(input character varying)\n RETURNS character varying\n LANGUAGE plv8\nAS $function$\n\tconst createPlugin = plv8.find_function(\"extism_create_plugin\");\n\tconst wasm = plv8.execute(\"select data from plugins where id = 3\")[0];\n\tconst opts = {\n\t\tuseWasi: true,\n\t\t\n\t\tfunctions: {\n\t\t\t\"extism:host/user\": {\n\t\t\t\tkv_read(cp, offs) {\n\t                const key = cp.read(offs).text();\n\t\t\t\t    let result = plv8.execute(\"SELECT value FROM kv WHERE key = $1\", [key]);\n\t\t\t\t    let value = result.length \u003e 0 ? result[0].value : new Uint8Array([0, 0, 0, 0]);\n\t\t\t\t    return cp.store(value);\n\t            },\n\t            kv_write(cp, kOffs, vOffs) {\n\t\t\t\t\tconst key = cp.read(kOffs).text();\n\t\t\t\t    const value = cp.read(vOffs).bytes();\n\t\t\t\t    let result = plv8.execute(\"SELECT value FROM kv WHERE key = $1\", [key]);\n\t\t\t\t    if (result.length \u003e 0) {\n\t\t\t\t        plv8.execute(\"UPDATE kv SET value = $1 WHERE key = $2\", [value, key]);\n\t\t\t\t    } else {\n\t\t\t\t        plv8.execute(\"INSERT INTO kv (key, value) VALUES ($1, $2)\", [key, value]);\n\t\t\t\t    }\n\t            }\n\t\t\t}\n\t\t}\n\t};\n\n\tconst plugin = createPlugin(wasm, opts);\n\treturn plugin.call(\"count_vowels\", input).text()\n$function$\n;\n```\nThe above example shows how you can use `extism_create_plugin` to supply your own host functions to the plugin. You can find th source code for the plugin [here](https://github.com/extism/plugins/tree/main/count_vowels_kvstore).\n","funding_links":[],"categories":["\u003ca name=\"TypeScript\"\u003e\u003c/a\u003eTypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdylibso%2Fpg_extism","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdylibso%2Fpg_extism","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdylibso%2Fpg_extism/lists"}