-
-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving supabase/supabase-js to its own repository
- Loading branch information
0 parents
commit 64f53b2
Showing
15 changed files
with
1,263 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# MIT License | ||
|
||
Copyright (c) 2020 Supabase | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
REPO_DIR=$(shell pwd) | ||
|
||
help: | ||
@echo "\nDOCKER\n" | ||
@echo "make start # start backend " | ||
@echo "make remove # remove backend " | ||
@echo "make rebuild # rebuild backend - loses any changes to the database" | ||
@echo "make pull # pull all the latest docker images" | ||
|
||
######################### | ||
# Docker | ||
######################### | ||
|
||
|
||
start: | ||
docker-compose up | ||
|
||
remove: | ||
docker-compose down --remove-orphans | ||
|
||
rebuild: | ||
docker-compose down --remove-orphans | ||
docker-compose build | ||
docker-compose up --force-recreate | ||
|
||
pull: | ||
docker-compose pull |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Supabase JS | ||
|
||
## Commands | ||
- `npm run clean` - Remove `lib/` directory | ||
- `npm test` - Run tests with linting and coverage results. | ||
- `npm run test:integration` - Run the integration tests without docker (requires you to run the docker container separately) | ||
- `npm run test:integration:full` - Run the integration tests (requires docker to be running) | ||
- `npm test:only` - Run tests without linting or coverage. | ||
- `npm test:watch` - You can even re-run tests on file changes! | ||
- `npm test:prod` - Run tests with minified code. | ||
- `npm run test:examples` - Test written examples on pure JS for better understanding module usage. | ||
- `npm run lint` - Run ESlint with airbnb-config | ||
- `npm run cover` - Get coverage report for your code. | ||
- `npm run build` - Babel will transpile ES6 => ES5 and minify the code. | ||
- `npm run prepublish` - Hook for npm. Do all the checks before publishing your module. | ||
|
||
|
||
## Publishing | ||
|
||
Bump the semver. | ||
|
||
```sh | ||
npm publish --access=public | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
version: '3' | ||
|
||
services: | ||
supabase: | ||
image: supabase/supabase-dev:0.1.9 | ||
ports: | ||
- "8000:8000" | ||
environment: | ||
DB_HOST: db | ||
DB_NAME: postgres | ||
DB_USER: postgres | ||
DB_PASSWORD: postgres | ||
DB_PORT: 5432 | ||
DB_SCHEMA: public, personal | ||
depends_on: | ||
- db | ||
db: | ||
image: postgres:12 | ||
ports: | ||
- "6543:5432" | ||
volumes: | ||
- ./test/db:/docker-entrypoint-initdb.d/ | ||
command: | ||
- "postgres" | ||
- "-c" | ||
- "wal_level=logical" | ||
environment: | ||
POSTGRES_DB: postgres | ||
POSTGRES_USER: postgres | ||
POSTGRES_PASSWORD: postgres | ||
POSTGRES_PORT: 5432 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{ | ||
"name": "@supabase/supabase-js", | ||
"version": "0.34.0", | ||
"description": "Supabase Realtime API", | ||
"main": "./lib/index.js", | ||
"scripts": { | ||
"clean": "rimraf lib", | ||
"test": "mocha -r @babel/register -r babel-polyfill test/unit/**/*.js", | ||
"test:integration": "mocha -r @babel/register -r babel-polyfill test/integration/**/*.js", | ||
"test:integration:full": "docker-compose up -d && sleep 10 && mocha -r @babel/register -r babel-polyfill test/integration/**/*.js ; docker-compose down --remove-orphans", | ||
"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": "BABEL_ENV=production babel src --out-dir lib", | ||
"prepublish": "npm run clean && npm run build", | ||
"deploy:minor": "npm version minor && npm publish --access=public", | ||
"deploy:patch": "npm version patch && npm publish --access=public" | ||
}, | ||
"files": [ | ||
"lib", | ||
"src" | ||
], | ||
"repository": "https://github.com/supabase/supabase", | ||
"keywords": [ | ||
"supabase", | ||
"realtime", | ||
"postgrest", | ||
"javascript" | ||
], | ||
"author": "Supabase", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/supabase/supabase/issues" | ||
}, | ||
"homepage": "https://supabase.io", | ||
"devDependencies": { | ||
"@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-minify": "^0.5.1", | ||
"chai": "^4.1.2", | ||
"cross-env": "^7.0.2", | ||
"jest-websocket-mock": "^2.0.1", | ||
"mocha": "^7.1.0", | ||
"nyc": "^15.0.0", | ||
"rimraf": "^3.0.2" | ||
}, | ||
"dependencies": { | ||
"@supabase/doctest-js": "^0.1.0", | ||
"@supabase/postgrest-js": "^0.16.1", | ||
"@supabase/realtime-js": "^0.9.0", | ||
"superagent": "^5.2.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import { Socket } from '@supabase/realtime-js' | ||
import * as ChangeMapper from './utils/ChangeMapper' | ||
|
||
class Realtime { | ||
constructor( | ||
tableName, | ||
realtimeUrl, | ||
schema, | ||
apikey, | ||
uuid, | ||
eventType, | ||
callbackFunction, | ||
queryFilters | ||
) { | ||
this.tableName = tableName | ||
this.realtimeUrl = realtimeUrl | ||
this.schema = schema | ||
this.apikey = apikey | ||
this.uuid = uuid | ||
|
||
this.socket = null | ||
this.channel = null | ||
this.listeners = {} | ||
|
||
this.queryFilters = queryFilters | ||
|
||
this.on(eventType, callbackFunction) | ||
} | ||
|
||
/** | ||
* REALTIME FUNCTIONALITY | ||
*/ | ||
|
||
createListener() { | ||
let socketUrl = `${this.realtimeUrl}` | ||
|
||
var filterString = '' | ||
this.queryFilters.forEach((queryFilter) => { | ||
switch (queryFilter.filter) { | ||
case 'filter': | ||
// temporary solution | ||
// this is the only thing we are supporting at the moment | ||
if (queryFilter.operator === 'eq') { | ||
// right now, the string is replaced instead of being stacked | ||
// the server does not support multiple eq. statements | ||
// as such, we will only process the very last eq. statement provided | ||
filterString = `:${queryFilter.columnName}=${queryFilter.operator}.${queryFilter.criteria}` | ||
} | ||
break | ||
default: | ||
break | ||
} | ||
}) | ||
|
||
let channel = | ||
this.tableName == '*' | ||
? 'realtime:*' | ||
: `realtime:${this.schema}:${this.tableName}${filterString}` | ||
this.socket = new Socket(socketUrl, { params: { apikey: this.apikey } }) | ||
this.channel = this.socket.channel(channel) | ||
|
||
this.socket.onOpen(() => { | ||
console.debug(`${this.realtimeUrl}: REALTIME CONNECTED`) | ||
}) | ||
this.socket.onClose(() => { | ||
console.debug(`${this.realtimeUrl}: REALTIME DISCONNECTED`) | ||
}) | ||
} | ||
|
||
on(eventType, callbackFunction) { | ||
if (this.socket == null) this.createListener() | ||
|
||
this.channel.on(eventType, (payload) => { | ||
let payloadEnriched = { | ||
schema: payload.schema, | ||
table: payload.table, | ||
commit_timestamp: payload.commit_timestamp, | ||
} | ||
let newData = {} | ||
let oldData = {} | ||
let oldDataEnriched = {} | ||
|
||
switch (payload.type) { | ||
case 'INSERT': | ||
newData = ChangeMapper.convertChangeData(payload.columns, payload.record) | ||
payloadEnriched.eventType = 'INSERT' | ||
payloadEnriched.new = newData | ||
|
||
break | ||
|
||
case 'UPDATE': | ||
oldData = ChangeMapper.convertChangeData(payload.columns, payload.old_record) | ||
newData = ChangeMapper.convertChangeData(payload.columns, payload.record) | ||
|
||
Object.keys(oldData).forEach((key) => { | ||
if (oldData[key] != null) oldDataEnriched[key] = oldData[key] | ||
}) | ||
|
||
payloadEnriched.eventType = 'UPDATE' | ||
payloadEnriched.old = oldDataEnriched | ||
payloadEnriched.new = newData | ||
|
||
break | ||
|
||
case 'DELETE': | ||
oldData = ChangeMapper.convertChangeData(payload.columns, payload.old_record) | ||
|
||
Object.keys(oldData).forEach((key) => { | ||
if (oldData[key] != null) oldDataEnriched[key] = oldData[key] | ||
}) | ||
|
||
payloadEnriched.eventType = 'DELETE' | ||
payloadEnriched.old = oldDataEnriched | ||
|
||
break | ||
|
||
default: | ||
break | ||
} | ||
|
||
callbackFunction(payloadEnriched) | ||
}) | ||
|
||
this.listeners[eventType] = callbackFunction | ||
return this | ||
} | ||
|
||
subscribe() { | ||
if (this.socket == null) this.createListener() | ||
|
||
this.socket.connect() | ||
|
||
if (this.channel.state !== 'joined') { | ||
this.channel | ||
.join() | ||
.receive('ok', (resp) => | ||
console.debug(`${this.realtimeUrl}: Joined Realtime successfully `, resp) | ||
) | ||
.receive('error', (resp) => console.debug(`${this.realtimeUrl}: Unable to join `, resp)) | ||
.receive('timeout', () => | ||
console.debug(`${this.realtimeUrl}: Network timeout. Still waiting...`) | ||
) | ||
} | ||
|
||
return this | ||
} | ||
|
||
unsubscribe() { | ||
if (this.socket) this.socket.disconnect() | ||
|
||
return this | ||
} | ||
} | ||
|
||
export default Realtime |
Oops, something went wrong.