1.0.10 • Published 18 days ago

@momsfriendlydevco/supabase-reactive v1.0.10

Weekly downloads
-
License
MIT
Repository
github
Last release
18 days ago

@MomsFriendlyDevCo/Supabase-Reactive

Supabase plugin for reactive read/write against local objects.

Extends the existing Supabase JavaScript functionality by adding a bi-directional, bound object which syncs with the server when its state changes. Changes on the server (or from another client) similarly update local state across all clients.

import Reactive from '@momsfriendlydevco/supabase-reactive';
import {createClient} from '@supabase/supabase-js'

// Create a Supabase client
let supabase = creatClient('https://MY-SUPABASE-DOMAIN.supabase.co', 'big-long-key');

// Create a reactive
let state = Reactive('my-table/id-to-sync', {supabase});

// Changes to state are now synced bi-directionally
state.foo = 1;
state.bar = [1, 2, 3];
state.baz = {key1: {subkey1: [4, 5, 6]}};
delete state.bar;

API

Supabase Table Structure

Ideally the data structure within Supabase should be made up of these columns:

  • id - a UUID is recommended
  • created_at - optional timestamp to indicate when the row was created
  • edited_at - timestamp to track changes
  • version - optional numeric to indicate the version offset of the row
  • data - the main JSONB data entity storage

An example Postgres data command is:

create table
  public.test (
    id uuid not null default gen_random_uuid (),
    created_at timestamp with time zone not null default now(),
    edited_at timestamp with time zone null,
    data jsonb null,
    version bigint null,
    constraint test_pkey primary key (id),
    constraint test_id_key unique (id)
  ) tablespace pg_default;

SupabaseReactive(path, options)

The main exported function which returns a Reactive object.

The resulting reactive object also has a series of non-enumerable utility functions which all start with a single dollar sign. See below for their purpose and documentation.

This can take an optional shorthand path and/or an options structure.

Valid options are:

OptionTypeDefaultDescription
supbaseSupabaseSupabase instance to use
tableStringSupabase table to store data within if path is not specified
idStringID of the table row to sync data with
isArrayBooleanfalseSpecifies if the data entity is an Array rather than an object
read=trueBooleantrueAllow reading from the remote Supabase server, disabling this makes the data transfer transmit only
watch=trueBooleantrueAllow watching for local changes and write them to the remote server if enabled
write=trueBooleantrueAllow writing back local changes to the Supabase server
attachReactivesBooleantrueExpose all utility functions as '$' prefixed functions to control the local state
throttleObjectLodash debounce options + wait key used to throttle all writes, set to falsy to disable
idColumn='id'String'id'Row ID column to sync with
filterObjectQuery filter to use when accessing multiple rows
dataColumnString'data'Data / JSONB column to sync data with
timestampColumnString'edited_at'Timezone+TZ column to use when syncing data
versionColumnStringOptional version column, this increments on each write and is only really useful for debugging purposes
reactiveCreateFunctionAsync function used to create an observable / reactive data entity from its input. Defaults to Vue's reactive function
reactiveWatchFunctionAsync function used to create a watch on the created reactive. Defaults to Vue's watch function
onInitFunctionAsync function when first populating data from the remote. Called as (data)
onReadFunctionAsync function called on subsequent reads when populating data from the remote. Called as (data)
onChangeFunctionAsync function called when a detected local write is about to be sent to the remote. Called as (dataPayload)
onDestroyFunctionAsync function called when destroying state. Called as (data:Reactive)
debugFunction / BooleanOptional debugging function callback. Called as (...msg:Any)
splitPathFunctionPath parser, expected to decorate the settings object. Called as (path: String, settings: Object) and expected to mutate the settings state

defaults

Storage object for all defaults used by SupabaseReactive.

Reactive.$meta

Meta information about the current row. This only really exists because we can't assign scalars in Javascript without it resetting the pointer later.

This object is made up of:

KeyTypeDescription
idStringThe ID of the current row
tableStringThe active table for the current row
timestampNull / DateThe last known timestamp of data from the server (or NULL if no data has been pulled yet)
IfNull / Numbera versioning column is enabled this represents the last known version of the data, similar to $meta.timestamp
WhetherBooleanthe state is being updated locally - indicates that local watchers should ignore incoming change detection

Reactive.$set(state, options)

Sets the content of the current reactive.

Valid options are:

OptionTypeDefaultDescription
markUpdatingBooleantrueMark the object as within an update to prevent recursion + disable local observers
removeKeysBooleantrueClean out dead reactive keys if the new state doesn't also contain them
timestampDateSet the reactive timestamp if provided
versionNumberSet the reactive version if provided

Reactive.$toObject()

Tidy JSON field data so that is safe from private methods (anything starting with '$' or '_', proxies or other non POJO slush. Returns a POJO.

Reactive.$refresh()

Alias of Reactive.$read().

Reactive.$getQuery()

Generate a Supabase object representing a query for the current configuration. Returns a Supabase promise which resolves when the operation has completed

Reactive.$init()

Initial operaton to wait on data from service + return reactable This function is the default response when calling the outer SupabaseReactive() function.

Reactive.$read()

Fetch the current data state from the server and update the reactive. Returns a promise.

Reactive.$fetch()

Fetch the current data state from the server but don't update the local state. This function is only really useful for snapshotting server state. Returns a promise which resolves with the snapshot data.

Reactive.$watch(isWatching=true)

Watch local data for changes and push to the server as needed. Returns a promise.

Reactive.$flush()

Wait for all local writes to complete. NOTE: This only promises that local writes complete, not that a subsequent read is required. Returns a promise.

Reactive.$subscribe(isSubscribed=true)

Toggle subscription to the realtime datafeed. Returns a promise.

Reactive.$destroy()

Release all watchers and subscriptions, local and remote. Returns a promise.

1.0.10

18 days ago

1.0.9

29 days ago

1.0.8

1 month ago

1.0.7

3 months ago

1.0.6

3 months ago

1.0.4

5 months ago

1.0.3

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago