Skip to content

Commit

Permalink
refactor: use inversify for DI, clean up code (divyenduz#16)
Browse files Browse the repository at this point in the history
* chore: run it for trackfootball but ignore the sources

* refactor: introduce inversify for bundler aka esbuild

* refactor: remove unused dep to bundler

* refactor: rename build to bundle

* refactor: add a simple example and a start_proc example

* refactor: edit start_proc example to include multiple functions

* refactor: introduce inversify to ts compiler aka ts-morph

* chores: refactor, change public api

* chores: refactor, remove a bunch of fixmes

* refactor: make bundler independent of plv8 mode

* refactor: fix tests

* refactor: clean up

* refactor: clean up

* refactor: fix tests

* refactor: move start_proc and mode code to plv8ify cli

* chore: comments

* fix: readme, actual args sync

* fix: use tsconfig.build.json to have tests be ts checked in dev but not included in prod

* fix: types check

* ci: run on push to main
  • Loading branch information
divyenduz authored Dec 11, 2022
1 parent 7f51bba commit 8f7e4f0
Show file tree
Hide file tree
Showing 42 changed files with 1,457 additions and 1,223 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test-and-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: Run Tests and Release
# This workflow is triggered on pushes to the repository.
on:
push:
branches:
- main
pull_request:
branches:
- main
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_modules
/plv8ify-dist/
dist/

.nyc_output
.nyc_output
examples/trackfootball
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ See all examples in the [examples folder](/examples). Use `yarn examples` to app

## CLI Arguments

| Generate Command Flags | Type | Description | Default |
| ----------------------- | ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------- |
| --debug | Boolean | Print additional debug information | `false` |
| --write-esbuild-output | Boolean | Write the intermediate bundled Javascript output from ESBuild | `false` |
| --input-file | String | Specify an input file path (only Typescript supported at the moment) | `input.ts` |
| --output-folder | String | Specify an output folder | `plv8ify-dist` |
| --scope-prefix | String | Specify a scope prefix, by default `plv8ify`, adds `plv8ify_` as prefix for exported typescript functions | `plv8ify` |
| --pg-function-delimiter | String | Specify a delimiter for the generated Postgres function | `$plv8ify$` |
| --fallback-type | String | Specify a fallback type when `plv8ify` fails to map a detected Typescript type to a Postges type | `JSONB` |
| --mode | 'inline' or 'start_proc' | Bundle the library inline in each function or bundle the libary to be used with plv8.start_proc | `inline` |
| Generate Command Flags | Type | Description | Default |
| ----------------------- | ------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------- |
| --debug | Boolean | Print additional debug information | `false` |
| --write-bundler-output | Boolean | Write the intermediate bundled Javascript output from bundler (currently, only ESBuild interface exists) | `false` |
| --input-file | String | Specify an input file path (only Typescript supported at the moment) | `input.ts` |
| --output-folder | String | Specify an output folder | `plv8ify-dist` |
| --scope-prefix | String | Specify a scope prefix, by default `plv8ify`, adds `plv8ify_` as prefix for exported typescript functions | `plv8ify` |
| --pg-function-delimiter | String | Specify a delimiter for the generated Postgres function | `$plv8ify$` |
| --fallback-type | String | Specify a fallback type when `plv8ify` fails to map a detected Typescript type to a Postges type | `JSONB` |
| --mode | 'inline' or 'start_proc' | Bundle the library inline in each function or bundle the libary to be used with plv8.start_proc | `inline` |
| --volatility | 'IMMUTABLE' or 'STABLE' or 'VOLATILE' | Change the volatility of all the generated functions. To change volatility of a specific function use the comment format `//@plv8ify-volatility-STABLE` in the input typescript file (see `examples/turf-js/input.ts`). Note that for now only single-line comment syntax is supported. | `IMMUTABLE` |

## Caveats
Expand Down
7 changes: 7 additions & 0 deletions examples/hello-start_proc/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function hello() {
return 'world'
}

export function world() {
return 'hello'
}
6 changes: 6 additions & 0 deletions examples/hello-start_proc/plv8ify-dist/plv8ify_hello.plv8.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DROP FUNCTION IF EXISTS plv8ify_hello();
CREATE OR REPLACE FUNCTION plv8ify_hello() RETURNS text AS $plv8ify$

return plv8ify.hello()

$plv8ify$ LANGUAGE plv8 IMMUTABLE STRICT;
42 changes: 42 additions & 0 deletions examples/hello-start_proc/plv8ify-dist/plv8ify_init.plv8.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
DROP FUNCTION IF EXISTS plv8ify_init();
CREATE OR REPLACE FUNCTION plv8ify_init() RETURNS VOID AS $$
this.plv8ify = (() => {
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __reExport = (target, module, copyDefault, desc) => {
if (module && typeof module === "object" || typeof module === "function") {
for (let key of __getOwnPropNames(module))
if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
__defProp(target, key, { get: () => module[key], enumerable: !(desc = __getOwnPropDesc(module, key)) || desc.enumerable });
}
return target;
};
var __toCommonJS = /* @__PURE__ */ ((cache) => {
return (module, temp) => {
return cache && cache.get(module) || (temp = __reExport(__markAsModule({}), module, 1), cache && cache.set(module, temp), temp);
};
})(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);

// examples/hello-start_proc/input.ts
var input_exports = {};
__export(input_exports, {
hello: () => hello,
world: () => world
});
function hello() {
return "world";
}
function world() {
return "hello";
}
return __toCommonJS(input_exports);
})();

$$ LANGUAGE plv8 IMMUTABLE STRICT;
3 changes: 3 additions & 0 deletions examples/hello-start_proc/plv8ify-dist/plv8ify_start.plv8.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

SET plv8.start_proc = plv8ify_init;
SELECT plv8_reset();
6 changes: 6 additions & 0 deletions examples/hello-start_proc/plv8ify-dist/plv8ify_world.plv8.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DROP FUNCTION IF EXISTS plv8ify_world();
CREATE OR REPLACE FUNCTION plv8ify_world() RETURNS text AS $plv8ify$

return plv8ify.world()

$plv8ify$ LANGUAGE plv8 IMMUTABLE STRICT;
3 changes: 3 additions & 0 deletions examples/hello/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function hello() {
return 'world'
}
40 changes: 40 additions & 0 deletions examples/hello/plv8ify-dist/plv8ify_hello.plv8.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
DROP FUNCTION IF EXISTS plv8ify_hello();
CREATE OR REPLACE FUNCTION plv8ify_hello() RETURNS text AS $plv8ify$
var plv8ify = (() => {
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __reExport = (target, module, copyDefault, desc) => {
if (module && typeof module === "object" || typeof module === "function") {
for (let key of __getOwnPropNames(module))
if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
__defProp(target, key, { get: () => module[key], enumerable: !(desc = __getOwnPropDesc(module, key)) || desc.enumerable });
}
return target;
};
var __toCommonJS = /* @__PURE__ */ ((cache) => {
return (module, temp) => {
return cache && cache.get(module) || (temp = __reExport(__markAsModule({}), module, 1), cache && cache.set(module, temp), temp);
};
})(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);

// examples/hello/input.ts
var input_exports = {};
__export(input_exports, {
hello: () => hello
});
function hello() {
return "world";
}
return __toCommonJS(input_exports);
})();

return plv8ify.hello()

$plv8ify$ LANGUAGE plv8 IMMUTABLE STRICT;
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"dependencies": {
"arg": "5.0.1",
"esbuild": "0.14.18",
"inversify": "6.0.1",
"reflect-metadata": "0.1.13",
"ts-morph": "13.0.3",
"ts-pattern": "3.3.5",
"webpack-node-externals": "3.0.0"
Expand All @@ -22,12 +24,12 @@
"@turf/invariant": "6.5.0",
"@turf/polygon-to-linestring": "5.0.0",
"@turf/turf": "6.5.0",
"@types/node": "17.0.14",
"@types/node": "18.11.7",
"@types/tap": "15.0.5",
"date-fns": "2.28.0",
"esbuild-register": "3.3.2",
"esbuild-runner": "2.2.1",
"object-hash": "^3.0.0",
"object-hash": "3.0.0",
"prettier": "2.5.1",
"react": "17.0.2",
"react-dom": "17.0.2",
Expand All @@ -36,14 +38,16 @@
"typescript": "4.5.5"
},
"scripts": {
"build": "rm -rf dist; tsc",
"build": "rm -rf dist; tsc --project tsconfig.build.json",
"prepublish": "yarn build",
"test": "tap --node-arg=--require=esbuild-register --no-check-coverage",
"dev": "yarn example:turf-js",
"example:hello": "yarn esr src/index.ts generate --input-file examples/hello/input.ts --output-folder examples/hello/plv8ify-dist",
"example:hello-start_proc": "yarn esr src/index.ts generate --input-file examples/hello-start_proc/input.ts --output-folder examples/hello-start_proc/plv8ify-dist --mode start_proc",
"example:turf-js": "yarn esr src/index.ts generate --input-file examples/turf-js/input.ts --output-folder examples/turf-js/plv8ify-dist",
"example:turf-js-distance": "yarn esr src/index.ts generate --input-file examples/turf-js-distance/input.ts --output-folder examples/turf-js-distance/plv8ify-dist",
"example:react-js": "yarn esr src/index.ts generate --input-file examples/react-js/input.tsx --output-folder examples/react-js/plv8ify-dist",
"example:object-hash": "yarn esr src/index.ts generate --input-file examples/object-hash/input.ts --output-folder examples/object-hash/plv8ify-dist",
"examples": "yarn example:turf-js; yarn example:react-js; yarn example:object-hash; yarn example:turf-js-distance"
"examples": "yarn example:hello; yarn example:hello-start_proc; yarn example:turf-js; yarn example:react-js; yarn example:object-hash; yarn example:turf-js-distance"
}
}
53 changes: 0 additions & 53 deletions src/fns/esbuild/bundle.test.ts

This file was deleted.

47 changes: 0 additions & 47 deletions src/fns/esbuild/bundle.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/fns/plv8/startProc/client.test.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/fns/plv8/startProc/client.ts

This file was deleted.

31 changes: 0 additions & 31 deletions src/fns/plv8/startProc/init.test.ts

This file was deleted.

23 changes: 0 additions & 23 deletions src/fns/plv8/startProc/init.ts

This file was deleted.

Loading

0 comments on commit 8f7e4f0

Please sign in to comment.