Skip to content

Commit

Permalink
Merge pull request #42 from mattiarossi/feature/41-schema-name-support
Browse files Browse the repository at this point in the history
Support generating functions on custom schemas, Refs #41
  • Loading branch information
divyenduz authored Jan 18, 2024
2 parents 2080206 + 258e894 commit 05613e9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,33 @@ export function point(lat, long) {

See all examples in the [examples folder](/examples). Use `yarn examples` to apply any changes to all the examples.

## Deploy on custom schema

To generate a function to be deployed on a schema different than the default one (usually: public) decorate the function with a `//@plv8ify-schema-name <schemaname>` comment

```
//@plv8ify-schema-name testschema
export function hello() {
return 'world'
}
```

will generate

```
DROP FUNCTION IF EXISTS testschema.plv8ify_hello();
CREATE OR REPLACE FUNCTION testschema.plv8ify_hello() RETURNS text AS $plv8ify$
// input.ts
function hello() {
return "world";
}
return hello()
$plv8ify$ LANGUAGE plv8 IMMUTABLE STRICT;
```

## Trigger functions

To write a trigger function, decorate the function with the `//@plv8ify-trigger` comment, and have the function return a `testRow` type where `testRow` defines the type of the row for the trigger. You can also add a NEW parameter for insert and update triggers, and OLD for update and delete triggers.
Expand Down
22 changes: 22 additions & 0 deletions src/impl/PLV8ifyCLI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,28 @@ function test(NEW, OLD) {
NEW.id = 102;
return NEW;
}
}
`,
pgFunctionDelimiter: '$plv8ify$',
fallbackReturnType: 'JSONB',
})
expect(sql).toMatchSnapshot()
})

it('getSQLFunction with custom-schema', async () => {
const plv8ify = new PLV8ifyCLI()
const sql = plv8ify.getPLV8SQLFunction({
fn: {
name: 'test',
parameters: [],
comments: ['//@plv8ify-schema-name testschema'],
} as TSFunction,
scopePrefix: 'plv8ify',
mode: 'inline',
defaultVolatility: 'IMMUTABLE',
bundledJs: `
function test() {
return "hello";
}
`,
pgFunctionDelimiter: '$plv8ify$',
Expand Down
13 changes: 12 additions & 1 deletion src/impl/PLV8ifyCLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ export class PLV8ifyCLI implements PLV8ify {
return trigger
}

private getFunctionCustomSchema(fn: TSFunction) {
const schemaStr = '//@plv8ify-schema-name '
const comments = fn.comments
const schema = comments
.filter((comment) => comment.includes(schemaStr))
.map((comment) => comment.replace(schemaStr, ''))[0]
return schema
}

// Input: parsed parameters, output of FunctionDeclaratioin.getParameters()
// Output: SQL string of bind params
private getSQLParametersString(
Expand Down Expand Up @@ -246,7 +255,9 @@ export class PLV8ifyCLI implements PLV8ify {
fallbackReturnType,
defaultVolatility,
}: GetPLV8SQLFunctionArgs) {
const scopedName = scopePrefix + '_' + fn.name
const customSchema = this.getFunctionCustomSchema(fn)
const scopedName =
(customSchema ? customSchema + '.' : '') + scopePrefix + '_' + fn.name
if (this.getFunctionTrigger(fn)) {
fn.returnType = 'TRIGGER'
}
Expand Down
13 changes: 13 additions & 0 deletions src/impl/__snapshots__/PLV8ifyCLI.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,16 @@ return test(NEW,OLD)
$plv8ify$ LANGUAGE plv8 IMMUTABLE STRICT;"
`;

exports[`PLV8ifyCLI tests getSQLFunction with custom-schema 1`] = `
"DROP FUNCTION IF EXISTS testschema.plv8ify_test();
CREATE OR REPLACE FUNCTION testschema.plv8ify_test() RETURNS JSONB AS $plv8ify$
function test() {
return "hello";
}
return test()
$plv8ify$ LANGUAGE plv8 IMMUTABLE STRICT;"
`;

0 comments on commit 05613e9

Please sign in to comment.