4.0.3 • Published 1 year ago

@daveyplate/supabase-swr-entities v4.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

supabase-swr-entities

WIP - Full Readme coming soon (by end of 2024)

Installation

npm install @daveyplate/supabase-swr-entities

Usage

Entity Schema

Create schema JSON file in root directory:

`entity.schemas.json'

[
    {
        "table": "profiles",
        "authenticate": false,
        "disableList": false,
        "select": ["id", "full_name", "avatar_url", "created_at", "updated_at"],
        "defaultOrder": "-created_at",
        "defaultParams": {
            "full_name_neq": null
        },
        "requiredParams": {
            "deactivated": false
        }
    }
]

Pages Router Example

/pages/api/[entities]/[entity_id].js

import { entityRoute } from '@daveyplate/supabase-swr-entities/server'
import { createClient } from '@/utils/supabase/api'

export default async (req, res) => {
    const supabase = createClient(req, res)

    const response = await entityRoute({
        supabase,
        ...req
    })

    res.status(response.status).json(response.body)
}

/pages/api/[entities]/index.js

import { entitiesRoute } from '@daveyplate/supabase-swr-entities/server'
import { createClient } from '@/utils/supabase/api'

export default async (req, res) => {
    const supabase = createClient(req, res)

    const response = await entitiesRoute({
        supabase,
        ...req
    })

    res.status(response.status).json(response.body)
}

App Router Example

/app/api/[entities]/[entity_id]/route.js

import { entityRoute } from '@daveyplate/supabase-swr-entities/server'
import { createClient } from '@/utils/supabase/server'

async function handler(request, context) {
    const params = await context.params

    const { nextUrl: { search } } = request
    const urlSearchParams = new URLSearchParams(search)
    const query = Object.fromEntries(urlSearchParams.entries())

    const body = request.method == "POST" || request.method == "PATCH" ? await request.json() : null
    const supabase = createClient()

    const response = await entityRoute({
        supabase,
        method: request.method,
        headers: request.headers,
        query: { ...params, ...query },
        body
    })

    return Response.json(response.body, {
        status: response.status
    })
}

export async function GET(request, context) {
    return await handler(request, context)
}

export async function PATCH(request, context) {
    return await handler(request, context)
}

export async function DELETE(request, context) {
    return await handler(request, context)
}

/app/api/[entities]/route.js

import { entitiesRoute } from '@daveyplate/supabase-swr-entities/server'
import { createClient } from '@/utils/supabase/server'

async function handler(request, context) {
    const params = await context.params

    const { nextUrl: { search } } = request
    const urlSearchParams = new URLSearchParams(search)
    const query = Object.fromEntries(urlSearchParams.entries())

    const body = request.method == "POST" || request.method == "PATCH" ? await request.json() : null
    const supabase = createClient()

    const response = await entitiesRoute({
        supabase,
        method: request.method,
        headers: request.headers,
        query: { ...params, ...query },
        body
    })

    return Response.json(response.body, {
        status: response.status
    })
}

export async function GET(request, context) {
    return await handler(request, context)
}

export async function PATCH(request, context) {
    return await handler(request, context)
}

export async function POST(request, context) {
    return await handler(request, context)
}

export async function DELETE(request, context) {
    return await handler(request, context)
}
1.2.4

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.5

1 year ago

2.0.4

1 year ago

2.0.7

1 year ago

2.0.6

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

3.0.12

1 year ago

3.0.13

1 year ago

3.0.10

1 year ago

3.0.11

1 year ago

3.0.16

1 year ago

3.0.17

1 year ago

3.0.14

1 year ago

3.0.15

1 year ago

4.0.1

1 year ago

4.0.0

1 year ago

4.0.3

1 year ago

4.0.2

1 year ago

3.0.60

1 year ago

3.0.61

1 year ago

3.0.62

1 year ago

3.0.45

1 year ago

3.0.46

1 year ago

3.0.43

1 year ago

3.0.44

1 year ago

3.0.47

1 year ago

3.0.48

1 year ago

3.0.42

1 year ago

3.0.40

1 year ago

3.0.56

1 year ago

3.0.4

1 year ago

3.0.57

1 year ago

3.0.3

1 year ago

3.0.54

1 year ago

3.0.2

1 year ago

3.0.55

1 year ago

3.0.1

1 year ago

3.0.8

1 year ago

3.0.7

1 year ago

3.0.58

1 year ago

3.0.6

1 year ago

3.0.59

1 year ago

3.0.5

1 year ago

3.0.52

1 year ago

3.0.0

1 year ago

3.0.53

1 year ago

3.0.50

1 year ago

3.0.51

1 year ago

3.0.23

1 year ago

3.0.24

1 year ago

3.0.21

1 year ago

3.0.22

1 year ago

3.0.27

1 year ago

3.0.28

1 year ago

3.0.25

1 year ago

3.0.26

1 year ago

3.0.20

1 year ago

3.0.18

1 year ago

3.0.19

1 year ago

3.0.9

1 year ago

3.0.34

1 year ago

3.0.35

1 year ago

3.0.32

1 year ago

3.0.33

1 year ago

3.0.38

1 year ago

3.0.39

1 year ago

3.0.36

1 year ago

3.0.37

1 year ago

3.0.30

1 year ago

3.0.31

1 year ago

3.0.29

1 year ago

1.2.0

1 year ago

1.2.1

1 year ago

1.1.9

1 year ago

1.1.8

1 year ago

1.1.7

1 year ago

1.1.6

1 year ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.12

1 year ago

1.1.11

1 year ago

1.1.10

1 year ago

1.1.16

1 year ago

1.1.15

1 year ago

1.1.14

1 year ago

1.1.13

1 year ago

1.1.19

1 year ago

1.1.18

1 year ago

1.1.17

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.22

1 year ago

1.0.21

1 year ago

1.0.20

1 year ago

1.0.26

1 year ago

1.0.25

1 year ago

1.0.24

1 year ago

1.0.23

1 year ago

1.0.29

1 year ago

1.0.28

1 year ago

1.0.27

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago