Skip to content

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

Solution for #24. Prevents CRUD instances from being considered as a subscription as well.

Angelicob7f5b0397e3a
diff --git a/libraries/supabase-js/src/Supabase.js b/libraries/supabase-js/src/Realtime.js
similarity index 58%
rename from libraries/supabase-js/src/Supabase.js
rename to libraries/supabase-js/src/Realtime.js
index 79b86012622..045849a9fd5 100644
--- a/libraries/supabase-js/src/Supabase.js
+++ b/libraries/supabase-js/src/Realtime.js
@@ -1,21 +1,30 @@
import { Socket } from '@supabase/realtime-js'
-import { PostgrestClient } from '@supabase/postgrest-js'
import * as ChangeMapper from './utils/ChangeMapper'
-class Supabase {
- constructor(tableName, restUrl, realtimeUrl, schema, apikey, uuid) {
+class Realtime {
+ constructor(
+ tableName,
+ realtimeUrl,
+ schema,
+ apikey,
+ uuid,
+ eventType,
+ callbackFunction,
+ queryFilters
+ ) {
this.tableName = tableName
- this.restUrl = restUrl
this.realtimeUrl = realtimeUrl
this.schema = schema
- this.uuid = uuid
this.apikey = apikey
+ this.uuid = uuid
this.socket = null
this.channel = null
this.listeners = {}
- this.queryFilters = []
+ this.queryFilters = queryFilters
+
+ this.on(eventType, callbackFunction)
}
/**
@@ -141,145 +150,6 @@ class Supabase {
return this
}
-
- /**
- * REST FUNCTIONALITIES
- */
-
- initClient() {
- 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) => {
- switch (queryFilter.filter) {
- case 'filter':
- api.filter(queryFilter.columnName, queryFilter.operator, queryFilter.criteria)
- break
-
- case 'match':
- api.match(queryFilter.query)
- break
-
- case 'order':
- api.order(queryFilter.property, queryFilter.ascending, queryFilter.nullsFirst)
- break
-
- case 'range':
- api.range(queryFilter.from, queryFilter.to)
- break
-
- case 'single':
- api.single()
- break
-
- default:
- break
- }
- })
-
- return api
- }
-
- select(columnQuery = '*', options = {}) {
- let api = this.initClient()
- return api.select(columnQuery, options)
- }
-
- insert(data, options = {}) {
- let api = this.initClient()
- return api.insert(data, options)
- }
-
- update(data, options = {}) {
- let api = this.initClient()
- return api.update(data, options)
- }
-
- delete(options = {}) {
- let api = this.initClient()
- return api.delete(options)
- }
-
- filter(columnName, operator, criteria) {
- this.queryFilters.push({
- filter: 'filter',
- columnName,
- operator,
- criteria,
- })
-
- return this
- }
-
- match(query) {
- this.queryFilters.push({
- filter: 'match',
- query,
- })
-
- return this
- }
-
- order(property, ascending = false, nullsFirst = false) {
- this.queryFilters.push({
- filter: 'order',
- property,
- ascending,
- nullsFirst,
- })
-
- return this
- }
-
- range(from, to) {
- this.queryFilters.push({
- filter: 'range',
- from,
- to,
- })
-
- return this
- }
-
- single() {
- this.queryFilters.push({ filter: 'single' })
-
- return this
- }
}
-// pre-empts if any of the filters are used before select
-const advancedFilters = [
- 'eq',
- 'neq',
- 'gt',
- 'lt',
- 'gte',
- 'lte',
- 'like',
- 'ilike',
- 'is',
- 'in',
- 'cs',
- 'cd',
- 'ova',
- 'ovr',
- 'sl',
- 'sr',
- 'nxr',
- 'nxl',
- 'adj',
-]
-
-advancedFilters.forEach(
- (operator) =>
- (Supabase.prototype[operator] = function filterValue(columnName, criteria) {
- return this.filter(columnName, operator, criteria)
- })
-)
-
-export default Supabase
+export default Realtime
diff --git a/libraries/supabase-js/src/index.js b/libraries/supabase-js/src/index.js
index 2ea57bef5ef..a953145de77 100644
--- a/libraries/supabase-js/src/index.js
+++ b/libraries/supabase-js/src/index.js
@@ -1,5 +1,5 @@
import { uuid } from './utils/Helpers'
-import Supabase from './Supabase'
+import Realtime from './Realtime'
import { PostgrestClient } from '@supabase/postgrest-js'
class SupabaseClient {
@@ -11,37 +11,55 @@ class SupabaseClient {
this.schema = 'public'
this.subscriptions = {}
- if(options.schema) this.schema = options.schema
+ this.tableName = null
+ this.queryFilters = []
+
+ if (options.schema) this.schema = options.schema
this.authenticate(supabaseUrl, supabaseKey)
}
+ /**
+ * General Functionalities
+ */
+
authenticate(supabaseUrl, supabaseKey) {
this.supabaseUrl = supabaseUrl
this.supabaseKey = supabaseKey
this.restUrl = `${supabaseUrl}/rest/v1`
this.realtimeUrl = `${supabaseUrl}/realtime/v1`.replace('http', 'ws')
-
+ }
+ clear() {
+ this.tableName = null
+ this.queryFilters = []
}
from(tableName) {
+ this.tableName = tableName
+ return this
+ }
+
+ /**
+ * Realtime Functionalities
+ */
+
+ on(eventType, callbackFunction) {
let identifier = uuid()
- this.subscriptions[identifier] = new Supabase(
- tableName,
- this.restUrl,
+ this.subscriptions[identifier] = new Realtime(
+ this.tableName,
this.realtimeUrl,
this.schema,
this.supabaseKey,
- identifier
+ identifier,
+ eventType,
+ callbackFunction,
+ this.queryFilters
)
- return this.subscriptions[identifier]
- }
- rpc(functionName, functionParameters = null) {
- let rest = new PostgrestClient(this.restUrl, { headers: { apikey: this.supabaseKey }, schema: this.schema })
- return rest.rpc(functionName, functionParameters)
+ this.clear()
+ return this.subscriptions[identifier]
}
getSubscriptions() {
@@ -52,8 +70,156 @@ class SupabaseClient {
mySubscription.unsubscribe()
delete this.subscriptions[mySubscription.uuid]
}
+
+ /**
+ * REST Functionalities
+ */
+
+ rpc(functionName, functionParameters = null) {
+ let rest = new PostgrestClient(this.restUrl, {
+ headers: { apikey: this.supabaseKey },
+ schema: this.schema,
+ })
+ return rest.rpc(functionName, functionParameters)
+ }
+
+ initClient() {
+ let rest = new PostgrestClient(this.restUrl, {
+ headers: { apikey: this.supabaseKey },
+ schema: this.schema,
+ })
+ let api = rest.from(this.tableName)
+
+ // go through queryFilters
+ this.queryFilters.forEach((queryFilter) => {
+ switch (queryFilter.filter) {
+ case 'filter':
+ api.filter(queryFilter.columnName, queryFilter.operator, queryFilter.criteria)
+ break
+
+ case 'match':
+ api.match(queryFilter.query)
+ break
+
+ case 'order':
+ api.order(queryFilter.property, queryFilter.ascending, queryFilter.nullsFirst)
+ break
+
+ case 'range':
+ api.range(queryFilter.from, queryFilter.to)
+ break
+
+ case 'single':
+ api.single()
+ break
+
+ default:
+ break
+ }
+ })
+
+ this.clear()
+ return api
+ }
+
+ select(columnQuery = '*', options = {}) {
+ let api = this.initClient()
+ return api.select(columnQuery, options)
+ }
+
+ insert(data, options = {}) {
+ let api = this.initClient()
+ return api.insert(data, options)
+ }
+
+ update(data, options = {}) {
+ let api = this.initClient()
+ return api.update(data, options)
+ }
+
+ delete(options = {}) {
+ let api = this.initClient()
+ return api.delete(options)
+ }
+
+ filter(columnName, operator, criteria) {
+ this.queryFilters.push({
+ filter: 'filter',
+ columnName,
+ operator,
+ criteria,
+ })
+
+ return this
+ }
+
+ match(query) {
+ this.queryFilters.push({
+ filter: 'match',
+ query,
+ })
+
+ return this
+ }
+
+ order(property, ascending = false, nullsFirst = false) {
+ this.queryFilters.push({
+ filter: 'order',
+ property,
+ ascending,
+ nullsFirst,
+ })
+
+ return this
+ }
+
+ range(from, to) {
+ this.queryFilters.push({
+ filter: 'range',
+ from,
+ to,
+ })
+
+ return this
+ }
+
+ single() {
+ this.queryFilters.push({ filter: 'single' })
+
+ return this
+ }
}
+// pre-empts if any of the filters are used before select
+const advancedFilters = [
+ 'eq',
+ 'neq',
+ 'gt',
+ 'lt',
+ 'gte',
+ 'lte',
+ 'like',
+ 'ilike',
+ 'is',
+ 'in',
+ 'cs',
+ 'cd',
+ 'ova',
+ 'ovr',
+ 'sl',
+ 'sr',
+ 'nxr',
+ 'nxl',
+ 'adj',
+]
+
+advancedFilters.forEach(
+ (operator) =>
+ (SupabaseClient.prototype[operator] = function filterValue(columnName, criteria) {
+ return this.filter(columnName, operator, criteria)
+ })
+)
+
const createClient = (supabaseUrl, supabaseKey, options = {}) => {
return new SupabaseClient(supabaseUrl, supabaseKey, options)
}
diff --git a/libraries/supabase-js/test/integration/testRealtime.js b/libraries/supabase-js/test/integration/testRealtime.js
index 6394097197c..109260c6535 100644
--- a/libraries/supabase-js/test/integration/testRealtime.js
+++ b/libraries/supabase-js/test/integration/testRealtime.js
@@ -1,11 +1,6 @@
import { assert } from 'chai'
import { createClient } from '../../src'
-// on()
-// subscribe()
-// unsubscribe()
-// removeSubscription()
-// getSubscriptions()
describe('test various subscriptions', function() {
const supabase = createClient('http://localhost:8000', 'examplekey')
diff --git a/libraries/supabase-js/test/integration/testRest.js b/libraries/supabase-js/test/integration/testRest.js
index 98a6b4d6cd0..8ef7e62ba0b 100644
--- a/libraries/supabase-js/test/integration/testRest.js
+++ b/libraries/supabase-js/test/integration/testRest.js
@@ -1,22 +1,6 @@
import { assert } from 'chai'
import { createClient } from '../../src'
-// from()
-// select()
-// order()
-// range()
-// single()
-// filter()
-// match()
-// gt()
-// lt()
-// gte()
-// lte()
-// like()
-// ilike()
-// is()
-// in()
-// not()
describe('test reading from the rest interface', () => {
const supabase = createClient('http://localhost:8000', 'examplekey')