Git Product home page Git Product logo

pg_extism's Introduction

pg_extism

Note: This is an experimental SDK.

Getting started

Make sure plv8 is installed on your server. Then enable it for your database:

create extension plv8;

Now run the script in dist/index.sql and you'll get two SQL functions: extism_create_plugin and extism_call.

Use pg_extism

Assume you have a table called plugins:

CREATE TABLE public.plugins (
	id int4 GENERATED ALWAYS AS IDENTITY( INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START 1 CACHE 1 NO CYCLE) NOT NULL,
	"data" bytea NULL,
	"name" varchar NULL,
	CONSTRAINT plugins_pk PRIMARY KEY (id)
);

Insert a few plugins into the table. And then call:

select extism_call(data, 'count_vowels', 'Hello World!') from plugins where id = 2;

Note: this assumes the plugin with id 2 has a function called count_vowels. You can find the plugin here.

If you want more control over your plugin, you can use extism_create_plugin:

-- DROP FUNCTION public.count_vowels_kv(varchar);

CREATE OR REPLACE FUNCTION public.count_vowels_kv(input character varying)
 RETURNS character varying
 LANGUAGE plv8
AS $function$
	const createPlugin = plv8.find_function("extism_create_plugin");
	const wasm = plv8.execute("select data from plugins where id = 3")[0];
	const opts = {
		useWasi: true,
		
		functions: {
			"extism:host/user": {
				kv_read(cp, offs) {
	                const key = cp.read(offs).text();
				    let result = plv8.execute("SELECT value FROM kv WHERE key = $1", [key]);
				    let value = result.length > 0 ? result[0].value : new Uint8Array([0, 0, 0, 0]);
				    return cp.store(value);
	            },
	            kv_write(cp, kOffs, vOffs) {
					const key = cp.read(kOffs).text();
				    const value = cp.read(vOffs).bytes();
				    let result = plv8.execute("SELECT value FROM kv WHERE key = $1", [key]);
				    if (result.length > 0) {
				        plv8.execute("UPDATE kv SET value = $1 WHERE key = $2", [value, key]);
				    } else {
				        plv8.execute("INSERT INTO kv (key, value) VALUES ($1, $2)", [key, value]);
				    }
	            }
			}
		}
	};

	const plugin = createPlugin(wasm, opts);
	return plugin.call("count_vowels", input).text()
$function$
;

The 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.

pg_extism's People

Contributors

mhmd-azeez avatar nilslice avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

wasm-forks

pg_extism's Issues

Can't run haskell plugins

Uncomment the haskell test and then run npm run test:

PS D:\dylibso\pg_extism> npm run test

> @extism/[email protected] test
> cross-env NODE_OPTIONS='--experimental-wasi-unstable-preview1' jest --coverage --config jest.config.js

  console.debug
    Haskell (reactor) runtime detected.

      at debug (src/index.ts:631:11)

  console.log
    newBoundTask: RTS is not initialised; call hs_init() first

      at log (src/index.ts:423:17)

  console.log


      at log (src/index.ts:423:17)

 FAIL  tests/index.test.ts
  test extism
    √ fails on hash mismatch (32 ms)
    √ can create and call a plugin (8 ms)
    √ can detect if function exists or not (4 ms)
    √ errors when function is not known (8 ms)
    √ plugin can allocate memory (268 ms)
    √ plugin can fail gracefuly (4 ms)
    √ host functions works (7 ms)
    √ can deny http requests (10 ms)
    × can initialize haskell runtime (60 ms)

  ● test extism › can initialize haskell runtime

    proc_exit: 1

      489 |       },
      490 |       proc_exit(rval: number): void {
    > 491 |         throw new Error(`proc_exit: ${rval}`);
          |               ^
      492 |       },
      493 |       clock_time_get(id: number, precision: bigint, time: number): number {
      494 |         const buffer = new DataView(memory().buffer);

      at proc_exit (src/index.ts:491:15)
      at __wasi_proc_exit (wasm:/wasm/00459472:1:27082)
      at _Exit (wasm:/wasm/00459472:1:45622)
      at exit (wasm:/wasm/00459472:1:90271)
      at stg_exit (wasm:/wasm/00459472:1:695506)
      at newBoundTask (wasm:/wasm/00459472:1:82416)
      at rts_lock (wasm:/wasm/00459472:1:572802)
      at testing (wasm:/wasm/00459472:1:790932)
      at Plugin.func [as callRaw] (src/index.ts:101:5)
      at Plugin.callRaw [as call] (src/index.ts:107:25)
      at Object.<anonymous> (tests/index.test.ts:124:35)

----------|---------|----------|---------|---------|--------------------------------------------------------------------------------------------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|--------------------------------------------------------------------------------------------------------
All files |   64.28 |    48.61 |   59.09 |   64.72 |
 index.ts |   64.28 |    48.61 |   59.09 |   64.72 | ...399,430-488,494-525,533,539-550,561,586,588-592,598,627,647,650,655-657,669,707-732,775,796,814,824
----------|---------|----------|---------|---------|--------------------------------------------------------------------------------------------------------
Test Suites: 1 failed, 1 total
Tests:       1 failed, 8 passed, 9 total
Snapshots:   0 total
Time:        1.681 s, estimated 2 s
Ran all test suites.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.