|
| 1 | +import { Socket } from '@supabase/realtime-js' |
| 2 | +import * as ChangeMapper from './utils/ChangeMapper' |
| 3 | + |
| 4 | +class Realtime { |
| 5 | + constructor( |
| 6 | + tableName, |
| 7 | + realtimeUrl, |
| 8 | + schema, |
| 9 | + apikey, |
| 10 | + uuid, |
| 11 | + eventType, |
| 12 | + callbackFunction, |
| 13 | + queryFilters |
| 14 | + ) { |
| 15 | + this.tableName = tableName |
| 16 | + this.realtimeUrl = realtimeUrl |
| 17 | + this.schema = schema |
| 18 | + this.apikey = apikey |
| 19 | + this.uuid = uuid |
| 20 | + |
| 21 | + this.socket = null |
| 22 | + this.channel = null |
| 23 | + this.listeners = {} |
| 24 | + |
| 25 | + this.queryFilters = queryFilters |
| 26 | + |
| 27 | + this.on(eventType, callbackFunction) |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * REALTIME FUNCTIONALITY |
| 32 | + */ |
| 33 | + |
| 34 | + createListener() { |
| 35 | + let socketUrl = `${this.realtimeUrl}` |
| 36 | + |
| 37 | + var filterString = '' |
| 38 | + this.queryFilters.forEach((queryFilter) => { |
| 39 | + switch (queryFilter.filter) { |
| 40 | + case 'filter': |
| 41 | + // temporary solution |
| 42 | + // this is the only thing we are supporting at the moment |
| 43 | + if (queryFilter.operator === 'eq') { |
| 44 | + // right now, the string is replaced instead of being stacked |
| 45 | + // the server does not support multiple eq. statements |
| 46 | + // as such, we will only process the very last eq. statement provided |
| 47 | + filterString = `:${queryFilter.columnName}=${queryFilter.operator}.${queryFilter.criteria}` |
| 48 | + } |
| 49 | + break |
| 50 | + default: |
| 51 | + break |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + let channel = |
| 56 | + this.tableName == '*' |
| 57 | + ? 'realtime:*' |
| 58 | + : `realtime:${this.schema}:${this.tableName}${filterString}` |
| 59 | + this.socket = new Socket(socketUrl, { params: { apikey: this.apikey } }) |
| 60 | + this.channel = this.socket.channel(channel) |
| 61 | + |
| 62 | + this.socket.onOpen(() => { |
| 63 | + console.debug(`${this.realtimeUrl}: REALTIME CONNECTED`) |
| 64 | + }) |
| 65 | + this.socket.onClose(() => { |
| 66 | + console.debug(`${this.realtimeUrl}: REALTIME DISCONNECTED`) |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + on(eventType, callbackFunction) { |
| 71 | + if (this.socket == null) this.createListener() |
| 72 | + |
| 73 | + this.channel.on(eventType, (payload) => { |
| 74 | + let payloadEnriched = { |
| 75 | + schema: payload.schema, |
| 76 | + table: payload.table, |
| 77 | + commit_timestamp: payload.commit_timestamp, |
| 78 | + } |
| 79 | + let newData = {} |
| 80 | + let oldData = {} |
| 81 | + let oldDataEnriched = {} |
| 82 | + |
| 83 | + switch (payload.type) { |
| 84 | + case 'INSERT': |
| 85 | + newData = ChangeMapper.convertChangeData(payload.columns, payload.record) |
| 86 | + payloadEnriched.eventType = 'INSERT' |
| 87 | + payloadEnriched.new = newData |
| 88 | + |
| 89 | + break |
| 90 | + |
| 91 | + case 'UPDATE': |
| 92 | + oldData = ChangeMapper.convertChangeData(payload.columns, payload.old_record) |
| 93 | + newData = ChangeMapper.convertChangeData(payload.columns, payload.record) |
| 94 | + |
| 95 | + Object.keys(oldData).forEach((key) => { |
| 96 | + if (oldData[key] != null) oldDataEnriched[key] = oldData[key] |
| 97 | + }) |
| 98 | + |
| 99 | + payloadEnriched.eventType = 'UPDATE' |
| 100 | + payloadEnriched.old = oldDataEnriched |
| 101 | + payloadEnriched.new = newData |
| 102 | + |
| 103 | + break |
| 104 | + |
| 105 | + case 'DELETE': |
| 106 | + oldData = ChangeMapper.convertChangeData(payload.columns, payload.old_record) |
| 107 | + |
| 108 | + Object.keys(oldData).forEach((key) => { |
| 109 | + if (oldData[key] != null) oldDataEnriched[key] = oldData[key] |
| 110 | + }) |
| 111 | + |
| 112 | + payloadEnriched.eventType = 'DELETE' |
| 113 | + payloadEnriched.old = oldDataEnriched |
| 114 | + |
| 115 | + break |
| 116 | + |
| 117 | + default: |
| 118 | + break |
| 119 | + } |
| 120 | + |
| 121 | + callbackFunction(payloadEnriched) |
| 122 | + }) |
| 123 | + |
| 124 | + this.listeners[eventType] = callbackFunction |
| 125 | + return this |
| 126 | + } |
| 127 | + |
| 128 | + subscribe() { |
| 129 | + if (this.socket == null) this.createListener() |
| 130 | + |
| 131 | + this.socket.connect() |
| 132 | + |
| 133 | + if (this.channel.state !== 'joined') { |
| 134 | + this.channel |
| 135 | + .join() |
| 136 | + .receive('ok', (resp) => |
| 137 | + console.debug(`${this.realtimeUrl}: Joined Realtime successfully `, resp) |
| 138 | + ) |
| 139 | + .receive('error', (resp) => console.debug(`${this.realtimeUrl}: Unable to join `, resp)) |
| 140 | + .receive('timeout', () => |
| 141 | + console.debug(`${this.realtimeUrl}: Network timeout. Still waiting...`) |
| 142 | + ) |
| 143 | + } |
| 144 | + |
| 145 | + return this |
| 146 | + } |
| 147 | + |
| 148 | + unsubscribe() { |
| 149 | + if (this.socket) this.socket.disconnect() |
| 150 | + |
| 151 | + return this |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +export default Realtime |
0 commit comments