@nichoth/replicache-supabase
TypeScript icon, indicating that this package has built-in type declarations

0.0.9 • Public • Published

replicache supabase

tests Socket Badge types module license

Use replicache with supabase.

This is some glue code for replicache and supabase. Use it in your lambda functions.

This is using the global version strategy.

install

npm i -S @nichoth/replicache-supabase

globals

env vars

We need several environment variables for the DB:

SUPABASE_ANON_KEY="eyJhbGc..."
SUPABASE_DATABASE_PASSWORD="123abc"
SUPABASE_URL="https://my-url.supabase.co"

example

push

Use this in a lambda function.

// netlify/functions/replicache-push/replicache-push.ts
import 'dotenv/config'
import { Handler, HandlerEvent } from '@netlify/functions'
import { processPush, PushRequestSchema } from '@nichoth/replicache-supabase/push'
// mutators are specific to your application
import { Mutators } from '../../../example/mutators.js'

export const handler:Handler = async function (ev:HandlerEvent) {
    if (!ev.body) return { statusCode: 400 }

    const userID = (ev.headers.cookie && ev.headers.cookie['userID']) || 'anon'
    const body = JSON.parse(ev.body)
    const push = PushRequestSchema.parse(body)

    try {
        await processPush(push, userID, Mutators())
    } catch (err) {
        return { statusCode: 500 }
    }

    return { statusCode: 200, body: 'OK' }
}

pull

Use this in a lambda function.

// netlify/functions/replicache-pull/replicache-pull.ts
import { Handler, HandlerEvent } from '@netlify/functions'
import {
    authError,
    processPull,
    PullRequestSchema
} from '@nichoth/replicache-supabase/pull'

export const handler:Handler = async function (ev:HandlerEvent) {
    if (!ev.body) return { statusCode: 400 }
    const userID = (ev.headers.cookie && ev.headers.cookie['userID']) || 'anon'
    const body = JSON.parse(ev.body)

    const pullRequest = PullRequestSchema.parse(body)

    try {
        const pullResponse = await processPull(pullRequest, userID)
        return { statusCode: 200, body: JSON.stringify(pullResponse) }
    } catch (err) {
        if (err === authError) {
            return { statusCode: 401, body: 'Unauthorized' }
        } else {
            return {
                statusCode: 500,
                body: 'Error processing pull: ' + err.toString()
            }
        }
    }
}

test

Run the test like this

npx esbuild test/index.ts --format=cjs --platform=node --bundle | node

Readme

Keywords

Package Sidebar

Install

npm i @nichoth/replicache-supabase

Weekly Downloads

4

Version

0.0.9

License

MIT

Unpacked Size

59.4 kB

Total Files

37

Last publish

Collaborators

  • nichoth