4.0.3 • Published 11 months ago

@daveyplate/supabase-swr-entities v4.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
11 months 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

12 months ago

1.2.3

12 months ago

1.2.2

12 months ago

2.0.3

12 months ago

2.0.2

12 months ago

2.0.5

12 months ago

2.0.4

12 months ago

2.0.7

12 months ago

2.0.6

12 months ago

2.0.1

12 months ago

2.0.0

12 months ago

3.0.12

12 months ago

3.0.13

12 months ago

3.0.10

12 months ago

3.0.11

12 months ago

3.0.16

12 months ago

3.0.17

12 months ago

3.0.14

12 months ago

3.0.15

12 months ago

4.0.1

11 months ago

4.0.0

11 months ago

4.0.3

11 months ago

4.0.2

11 months ago

3.0.60

11 months ago

3.0.61

11 months ago

3.0.62

11 months ago

3.0.45

11 months ago

3.0.46

11 months ago

3.0.43

11 months ago

3.0.44

11 months ago

3.0.47

11 months ago

3.0.48

11 months ago

3.0.42

11 months ago

3.0.40

11 months ago

3.0.56

11 months ago

3.0.4

12 months ago

3.0.57

11 months ago

3.0.3

12 months ago

3.0.54

11 months ago

3.0.2

12 months ago

3.0.55

11 months ago

3.0.1

12 months ago

3.0.8

12 months ago

3.0.7

12 months ago

3.0.58

11 months ago

3.0.6

12 months ago

3.0.59

11 months ago

3.0.5

12 months ago

3.0.52

11 months ago

3.0.0

12 months ago

3.0.53

11 months ago

3.0.50

11 months ago

3.0.51

11 months ago

3.0.23

11 months ago

3.0.24

11 months ago

3.0.21

11 months ago

3.0.22

11 months ago

3.0.27

11 months ago

3.0.28

11 months ago

3.0.25

11 months ago

3.0.26

11 months ago

3.0.20

12 months ago

3.0.18

12 months ago

3.0.19

12 months ago

3.0.9

12 months ago

3.0.34

11 months ago

3.0.35

11 months ago

3.0.32

11 months ago

3.0.33

11 months ago

3.0.38

11 months ago

3.0.39

11 months ago

3.0.36

11 months ago

3.0.37

11 months ago

3.0.30

11 months ago

3.0.31

11 months ago

3.0.29

11 months 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