mirror of
https://github.com/supabase/supabase.git
synced 2026-07-05 04:14:46 +08:00
151 lines
3.0 KiB
Plaintext
151 lines
3.0 KiB
Plaintext
---
|
|
id: plv8
|
|
title: "plv8: Javascript Language"
|
|
description: Javascript language for PostgreSQL.
|
|
---
|
|
|
|
# plv8: Javascript Language
|
|
|
|
The `plv8` extension allows you use Javascript within Postgres.
|
|
|
|
## Overview
|
|
|
|
While Postgres natively runs SQL, it can also run other "procedural languages".
|
|
`plv8` allows you to run Javascript code - specifically any code that runs on the [V8 Javascript engine](https://v8.dev).
|
|
|
|
It can be used for database functions, triggers, queries and more.
|
|
|
|
## Usage
|
|
|
|
|
|
### Enabling
|
|
|
|
|
|
<Tabs
|
|
defaultActiveId="UI"
|
|
>
|
|
<TabPanel id="UI" label="Dashboard UI">
|
|
|
|
```sh
|
|
1. Go to the Database page.
|
|
2. Click on "Extensions" in the sidebar.
|
|
3. Search for "plv8".
|
|
4. Click the toggle.
|
|
```
|
|
|
|
</TabPanel>
|
|
<TabPanel id="SQL" label="SQL">
|
|
|
|
```sql
|
|
|
|
-- Example: enable the "plv8" extension
|
|
create extension plv8;
|
|
|
|
-- Example: disable the "plv8" extension
|
|
drop extension if exists plv8;
|
|
|
|
```
|
|
|
|
Even though the SQL code is `create extension`, this is the equivalent of "enabling the extension".
|
|
To disable an extension you can call `drop extension`.
|
|
|
|
Procedural languages are automatically installed within `pg_catalog`, so you don't need to specify a schema.
|
|
|
|
</TabPanel>
|
|
</Tabs>
|
|
|
|
### Creating `plv8` functions
|
|
|
|
Functions written in `plv8` are written just like any other PostgreSQL functions, only
|
|
with the `language` identifier set to `plv8`.
|
|
|
|
```sql
|
|
create or replace function function_name()
|
|
returns void as $$
|
|
// V8 Javascript
|
|
// code
|
|
// here
|
|
$$ language plv8;
|
|
```
|
|
|
|
You can call `plv8` functions like any other Postgres function:
|
|
|
|
|
|
|
|
<Tabs
|
|
defaultActiveId="SQL"
|
|
>
|
|
<TabPanel id="SQL" label="SQL">
|
|
|
|
|
|
```sql
|
|
select function_name();
|
|
```
|
|
|
|
</TabPanel>
|
|
<TabPanel id="JS" label="Javascript">
|
|
|
|
```js
|
|
|
|
const { data, error } = supabase.rpc('function_name')
|
|
|
|
```
|
|
|
|
</TabPanel>
|
|
</Tabs>
|
|
|
|
## Examples
|
|
|
|
### Scalar Functions
|
|
|
|
A [scalar function](https://plv8.github.io/#scalar-function-calls) is anything that takes in some user input and returns a single result.
|
|
|
|
```sql
|
|
create or replace function hello_world(name text)
|
|
returns text as $$
|
|
|
|
let output = `Hello, ${name}!`;
|
|
return output;
|
|
|
|
$$ language plv8;
|
|
```
|
|
|
|
### Executing SQL
|
|
|
|
You can execute SQL within `plv8` code using the [`plv8.execute` function](https://plv8.github.io/#plv8-execute).
|
|
|
|
```sql
|
|
create or replace function update_user(id bigint, first_name text)
|
|
returns smallint as $$
|
|
|
|
var num_affected = plv8.execute(
|
|
'update profiles set first_name = $1 where id = $2',
|
|
[first_name, id]
|
|
);
|
|
|
|
return num_affected;
|
|
$$ language plv8;
|
|
```
|
|
|
|
### Set-returning Functions
|
|
|
|
A [set-returning function](https://plv8.github.io/#set-returning-function-calls) is anything that returns a full set of results - for example, rows in a table.
|
|
|
|
```sql
|
|
create or replace function get_messages()
|
|
returns setof messages as $$
|
|
|
|
var json_result = plv8.execute(
|
|
'select * from messages'
|
|
);
|
|
|
|
return json_result;
|
|
$$ language plv8;
|
|
```
|
|
|
|
|
|
## Resources
|
|
|
|
- Official [`plv8` documentation](https://plv8.github.io/).
|
|
- [plv8 Github Repository](https://github.com/plv8/plv8).
|