Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Support for postgREST v7.0.0 && multiple schema

Angelico25dbf3c4a80d
diff --git a/libraries/supabase-js/.babelrc b/libraries/supabase-js/.babelrc
index ef749a2d6c0..0ae1c324775 100644
--- a/libraries/supabase-js/.babelrc
+++ b/libraries/supabase-js/.babelrc
@@ -1,16 +1,3 @@
{
- "env": {
- "development": {
- "presets": ["env"],
- "plugins": [
- "add-module-exports"
- ]
- },
- "production": {
- "presets": ["env", "minify"],
- "plugins": [
- "add-module-exports"
- ]
- }
- }
-}
+ "presets": [["@babel/preset-env"], ["minify", { "removeUndefined": false }]]
+}
\ No newline at end of file
diff --git a/libraries/supabase-js/package.json b/libraries/supabase-js/package.json
index 6cd3b7a2c62..96852624ed1 100644
--- a/libraries/supabase-js/package.json
+++ b/libraries/supabase-js/package.json
@@ -7,12 +7,12 @@
"clean": "rimraf lib",
"test": "mocha --require babel-core/register --require babel-polyfill test/unit/**/*.js",
"test:integration:only": "mocha --require babel-core/register --require babel-polyfill test/integration/**/*.js",
- "test:integration": "docker-compose -f ./test/integration/docker-compose.yml up -d && sleep 10 && mocha --require babel-core/register --require babel-polyfill test/integration/**/*.js ; docker-compose -f ./test/integration/docker-compose.yml down",
+ "test:integration": "docker-compose -f ./test/integration/docker-compose.yml up -d && sleep 10 && mocha -r @babel/register -r babel-polyfill test/integration/**/*.js ; docker-compose -f ./test/integration/docker-compose.yml down",
"test:prod": "cross-env BABEL_ENV=production npm run test",
"test:watch": "npm test -- --watch",
"test:examples": "node examples/",
"cover": "nyc --check-coverage && npm test",
- "build": "cross-env BABEL_ENV=production babel src --out-dir lib",
+ "build": "BABEL_ENV=production babel src --out-dir lib",
"prepublish": "npm run clean && npm run build",
"deploy": "npm version patch && npm publish --access=public"
},
@@ -34,10 +34,12 @@
},
"homepage": "https://supabase.io",
"devDependencies": {
- "babel-cli": "^6.26.0",
+ "@babel/cli": "^7.8.3",
+ "@babel/core": "^7.8.3",
+ "@babel/preset-env": "^7.8.3",
+ "@babel/register": "^7.8.3",
"babel-plugin-add-module-exports": "^1.0.0",
"babel-polyfill": "^6.26.0",
- "babel-preset-env": "^1.6.1",
"babel-preset-minify": "^0.5.1",
"chai": "^4.1.2",
"cross-env": "^7.0.2",
@@ -48,7 +50,7 @@
},
"dependencies": {
"@supabase/doctest-js": "^0.1.0",
- "@supabase/postgrest-js": "^0.13.0",
+ "@supabase/postgrest-js": "^0.14.0",
"@supabase/realtime-js": "^0.1.7",
"superagent": "^5.2.1"
}
diff --git a/libraries/supabase-js/src/Supabase.js b/libraries/supabase-js/src/Supabase.js
index 1694c048d13..b85d1b92d7c 100644
--- a/libraries/supabase-js/src/Supabase.js
+++ b/libraries/supabase-js/src/Supabase.js
@@ -26,7 +26,7 @@ class Supabase {
let socketUrl = `${this.realtimeUrl}`
var filterString = ''
- this.queryFilters.forEach(queryFilter => {
+ this.queryFilters.forEach((queryFilter) => {
switch (queryFilter.filter) {
case 'filter':
// temporary solution
@@ -61,7 +61,7 @@ class Supabase {
on(eventType, callbackFunction) {
if (this.socket == null) this.createListener()
- this.channel.on(eventType, payload => {
+ this.channel.on(eventType, (payload) => {
let payloadEnriched = {
schema: payload.schema,
table: payload.table,
@@ -83,7 +83,7 @@ class Supabase {
oldData = ChangeMapper.convertChangeData(payload.columns, payload.old_record)
newData = ChangeMapper.convertChangeData(payload.columns, payload.record)
- Object.keys(oldData).forEach(key => {
+ Object.keys(oldData).forEach((key) => {
if (oldData[key] != null) oldDataEnriched[key] = oldData[key]
})
@@ -96,7 +96,7 @@ class Supabase {
case 'DELETE':
oldData = ChangeMapper.convertChangeData(payload.columns, payload.old_record)
- Object.keys(oldData).forEach(key => {
+ Object.keys(oldData).forEach((key) => {
if (oldData[key] != null) oldDataEnriched[key] = oldData[key]
})
@@ -124,10 +124,10 @@ class Supabase {
if (this.channel.state !== 'joined') {
this.channel
.join()
- .receive('ok', resp =>
+ .receive('ok', (resp) =>
console.debug(`${this.realtimeUrl}: Joined Realtime successfully `, resp)
)
- .receive('error', resp => console.debug(`${this.realtimeUrl}: Unable to join `, resp))
+ .receive('error', (resp) => console.debug(`${this.realtimeUrl}: Unable to join `, resp))
.receive('timeout', () =>
console.debug(`${this.realtimeUrl}: Network timeout. Still waiting...`)
)
@@ -147,11 +147,14 @@ class Supabase {
*/
initClient() {
- let rest = new PostgrestClient(this.restUrl, { headers: { apikey: this.apikey } })
+ let rest = new PostgrestClient(this.restUrl, {
+ headers: { apikey: this.apikey },
+ schema: this.schema,
+ })
let api = rest.from(this.tableName)
// go through queryFilters
- this.queryFilters.forEach(queryFilter => {
+ this.queryFilters.forEach((queryFilter) => {
switch (queryFilter.filter) {
case 'filter':
api.filter(queryFilter.columnName, queryFilter.operator, queryFilter.criteria)
@@ -253,7 +256,7 @@ class Supabase {
const advancedFilters = ['eq', 'gt', 'lt', 'gte', 'lte', 'like', 'ilike', 'is', 'in', 'not']
advancedFilters.forEach(
- operator =>
+ (operator) =>
(Supabase.prototype[operator] = function filterValue(columnName, criteria) {
return this.filter(columnName, operator, criteria)
})
diff --git a/libraries/supabase-js/src/index.js b/libraries/supabase-js/src/index.js
index 71bada773c4..2ea57bef5ef 100644
--- a/libraries/supabase-js/src/index.js
+++ b/libraries/supabase-js/src/index.js
@@ -8,9 +8,11 @@ class SupabaseClient {
this.supabaseKey = null
this.restUrl = null
this.realtimeUrl = null
- this.defaultSchema = 'public'
+ this.schema = 'public'
this.subscriptions = {}
+ if(options.schema) this.schema = options.schema
+
this.authenticate(supabaseUrl, supabaseKey)
}
@@ -19,6 +21,8 @@ class SupabaseClient {
this.supabaseKey = supabaseKey
this.restUrl = `${supabaseUrl}/rest/v1`
this.realtimeUrl = `${supabaseUrl}/realtime/v1`.replace('http', 'ws')
+
+
}
from(tableName) {
@@ -28,7 +32,7 @@ class SupabaseClient {
tableName,
this.restUrl,
this.realtimeUrl,
- this.defaultSchema,
+ this.schema,
this.supabaseKey,
identifier
)
@@ -36,7 +40,7 @@ class SupabaseClient {
}
rpc(functionName, functionParameters = null) {
- let rest = new PostgrestClient(this.restUrl, { headers: { apikey: this.supabaseKey } })
+ let rest = new PostgrestClient(this.restUrl, { headers: { apikey: this.supabaseKey }, schema: this.schema })
return rest.rpc(functionName, functionParameters)
}
diff --git a/libraries/supabase-js/test/integration/db/00-schema.sql b/libraries/supabase-js/test/integration/db/00-schema.sql
index fb13269db95..93a6cc52ced 100644
--- a/libraries/supabase-js/test/integration/db/00-schema.sql
+++ b/libraries/supabase-js/test/integration/db/00-schema.sql
@@ -2,6 +2,9 @@
-- Create the Replication publication
CREATE PUBLICATION supabase_realtime FOR ALL TABLES;
+-- Create a second schema
+CREATE SCHEMA personal;
+
-- USERS
CREATE TYPE public.user_status AS ENUM ('ONLINE', 'OFFLINE');
CREATE TABLE public.users (
@@ -39,19 +42,25 @@ CREATE TABLE public.messages (
ALTER TABLE public.messages REPLICA IDENTITY FULL; -- Send "previous data" to supabase
COMMENT ON COLUMN public.messages.data IS 'For unstructured data and prototyping.';
-CREATE OR REPLACE FUNCTION public.update_user_status(user_id bigint, new_user_status text)
-RETURNS jsonb AS $$
-BEGIN
- UPDATE users SET status = new_user_status::public.user_status
- WHERE id = user_id;
- return json_build_object(
- 'result',
- true,
- 'user_id',
- user_id,
- 'status',
- new_user_status
- );
-END;
-$$ LANGUAGE plpgsql;
+CREATE FUNCTION public.show_schema()
+RETURNS text AS $$
+ SELECT 'public';
+$$ LANGUAGE SQL IMMUTABLE;
+
+-- SECOND SCHEMA USERS
+CREATE TYPE personal.user_status AS ENUM ('ONLINE', 'OFFLINE');
+CREATE TABLE personal.users (
+ id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ inserted_at timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
+ updated_at timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
+ data jsonb DEFAULT null,
+ username text,
+ status user_status DEFAULT 'ONLINE'::user_status
+);
+ALTER TABLE personal.users REPLICA IDENTITY FULL; -- Send "previous data" to supabase
+COMMENT ON COLUMN personal.users.data IS 'For unstructured data and prototyping.';
+CREATE FUNCTION personal.show_schema()
+RETURNS text AS $$
+ SELECT 'personal';
+$$ LANGUAGE SQL IMMUTABLE;
\ No newline at end of file
diff --git a/libraries/supabase-js/test/integration/db/01-dummy-data.sql b/libraries/supabase-js/test/integration/db/01-dummy-data.sql
index 8d33f17700c..e616f4274b5 100644
--- a/libraries/supabase-js/test/integration/db/01-dummy-data.sql
+++ b/libraries/supabase-js/test/integration/db/01-dummy-data.sql
@@ -16,4 +16,13 @@ INSERT INTO
public.messages (message, channel_id, user_id)
VALUES
('Hello World 👋', 1, 1),
- ('Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.', 2, 1);
\ No newline at end of file
+ ('Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.', 2, 1);
+
+INSERT INTO
+ personal.users (username)
+VALUES
+ ('supabot'),
+ ('kiwicopple'),
+ ('awalias'),
+ ('dragarcia'),
+ ('leroyjenkins');
\ No newline at end of file
diff --git a/libraries/supabase-js/test/integration/docker-compose.yml b/libraries/supabase-js/test/integration/docker-compose.yml
index 069512d824a..6e85623b8a2 100644
--- a/libraries/supabase-js/test/integration/docker-compose.yml
+++ b/libraries/supabase-js/test/integration/docker-compose.yml
@@ -2,7 +2,7 @@ version: '3'
services:
supabase:
- image: supabase/supabase-dev:0.1.8
+ image: supabase/supabase-dev:0.1.9
ports:
- "8000:8000"
environment:
@@ -11,13 +11,13 @@ services:
DB_USER: postgres
DB_PASSWORD: postgres
DB_PORT: 5432
- DB_SCHEMA: public
+ DB_SCHEMA: public, personal
depends_on:
- db
db:
image: postgres:12
ports:
- - "5432:5432"
+ - "6543:5432"
volumes:
- ./db:/docker-entrypoint-initdb.d/
command:
diff --git a/libraries/supabase-js/test/integration/testRest.js b/libraries/supabase-js/test/integration/testRest.js
index 144a10f830e..98a6b4d6cd0 100644
--- a/libraries/supabase-js/test/integration/testRest.js
+++ b/libraries/supabase-js/test/integration/testRest.js
@@ -64,6 +64,15 @@ describe('test reading from the rest interface', () => {
assert(response.body.length === 2, 'should return two records, awalias, and supabot')
})
+ if('not()', async () => {
+ const response = await supabase
+ .from('users')
+ .not('id', 'lte', 3)
+ .select('*')
+ assert(response.body.length === 1, 'should return one record')
+ assert(response.body[0].username === 'dragarcia')
+ })
+
it('match()', async () => {
const response = await supabase
.from('users')
@@ -146,10 +155,10 @@ describe('test reading from the rest interface', () => {
assert(response.body[1].id === 3, 'last (and second) item should be id 3')
})
- it('not()', async () => {
+ it('neq()', async () => {
const response = await supabase
.from('users')
- .not('id', '3')
+ .neq('id', '3')
.select('*')
assert(response.body.length === 3, 'should be all except id 3')
})
@@ -159,11 +168,36 @@ describe('test rpc()', () => {
const supabase = createClient('http://localhost:8000', 'examplekey')
it('should return a value', async () => {
- const response = await supabase.rpc('update_user_status', {
- user_id: 2,
- new_user_status: 'OFFLINE',
- })
- assert(response.body.result === true)
- assert(response.body.status === 'OFFLINE')
+ const response = await supabase.rpc('show_schema')
+ assert(response.body === 'public')
+ })
+})
+
+describe('test multi schema', () => {
+ const supabase = createClient('http://localhost:8000', 'examplekey', {schema: 'personal'})
+
+ it('should return a value from the other schema', async () => {
+ const response = await supabase
+ .from('users')
+ .eq('username', 'leroyjenkins')
+ .select('*')
+
+ assert(response.body.length === 1)
+ assert(response.body[0].username === 'leroyjenkins')
+ })
+
+ it('should be able to update a value on the other schema', async () => {
+ const response = await supabase
+ .from('users')
+ .eq('username', 'leroyjenkins')
+ .update({username: 'jenkinsleroy'})
+
+ assert(response.body.length === 1)
+ assert(response.body[0].username === 'jenkinsleroy')
+ })
+
+ it('should return a value', async () => {
+ const response = await supabase.rpc('show_schema')
+ assert(response.body === 'personal')
})
})