Git Product home page Git Product logo

refine-pocketbase's Introduction

refine-pocketbase

pb

PocketBase providers for Refine.

Installation

yarn add refine-pocketbase
# or
npm install refine-pocketbase

Data Provider

Basic Usage

import PocketBase from "pocketbase";
import { authProvider, dataProvider, liveProvider } from "refine-pocketbase";

const pb = new PocketBase(POCKETBASE_URL);

<Refine
  authProvider={authProvider(pb)}
  dataProvider={dataProvider(pb)}
  liveProvider={liveProvider(pb)}
  ...
>
  ...
</Refine>

The Meta Properties fields and expand

The code below uses useList to fetch a list of users. The resulting list contains user records with the id, name, avatar and the name of the organisation a user is assigned to. The meta properties fields and expand are used to customize the server response to the requirements of the user interface.

const users = useList({
  resource: "users",
  meta: {
    fields: ["id", "name", "avatar", "expand.org.name"],
    expand: ["org"],
  }
});

Here fields is an array of strings limiting the fields to return in the server's response. expand is an array with names of the related records that will be included in the response. Pocketbase supports up to 6-level depth nested relations expansion. See https://pocketbase.io/docs/api-records for more examples.

A couple of other refine hooks and components like useOne, useTable, <Show/>, <List/>, etc. will support the meta props fields and expand if used with the refine-pocketbase data provider.

Auth Provider

A number of configuration properties are supported by the auth provider, primarily for controlling redirection following specific auth events. Please take a look at the self-explanatory names in the AuthOptions typescript interface to find the available options.

import { authProvider, AuthOptions } from "refine-pocketbase";

const authOptions: AuthOptions = {
  loginRedirectTo: "/dashboard",
};

<Refine
  authProvider={authProvider(pb, authOptions)}
  ...
>
  ...
</Refine>

Auth Collection

users is the default auth collection in Pocketbase. Several auth collections can be supported in a single Pocketbase instance. You can use a different collection with the authProvider by using the collection property:

const authOptions: AuthOptions = {
  collection: "superusers",
};

Features

  • auth provider
    • register
    • login with password
    • login with provider
    • forgot password
    • update password
  • data provider
    • filters
    • sorters
    • pagination
    • expand
    • filter
  • live provider
    • subscribe
    • unsubscribe
  • audit log provider

Tasks: PRs Welcome!

  • auditLogProvider implementation
  • happy path test specs
    • authProvider
    • dataProvider (except for deleteOne)
    • liveProvider
    • auditLogProvider
  • test specs for authProvider error conditions
    • register
    • forgotPassword
    • updatePassword
    • login
  • test specs for dataProvider error conditions
    • getList
    • create
    • update
    • getOne
    • deleteOne
  • test specs for deleteOne
  • test specs with expand
    • getList
    • getOne
  • test specs with fields
    • getList
    • getOne
  • test specs for auditLogProvider errors
  • Setup Github Actions
    • test environment
    • build & publish

Contribute

Buy Me A Coffee

refine-pocketbase's People

Contributors

kruschid avatar necatiozmen avatar

Stargazers

Nikolay Gerzhan avatar Zhang Yang avatar Sarmad avatar  avatar Roberto Salas avatar Geoffrey Dagley avatar Chris Smith avatar santosh avatar Riza Nafis avatar Reksa Ma'mur avatar  avatar H avatar Hub avatar  avatar Amirreza salimi avatar karim avatar Fernando Bold avatar footearth avatar Furkan Balkaç avatar Julien Tanay avatar  avatar  avatar Href avatar  avatar Nasyarobby Putra avatar Mohamed Khennoussi avatar Ricardo Sawir avatar Jens Neuber avatar Joda Stößer avatar Mirus avatar Valentin Ganchev avatar Youssef Siam avatar Enrique Enciso avatar Gyula Kerezsi avatar HungHcc avatar A. S. M. Muhiminul Hasan avatar Eren Erkalkan avatar Batuhan Wilhelm avatar Recep Kütük avatar Alican Erdurmaz avatar  avatar Omer Aplak avatar  avatar

Watchers

 avatar Jason Lei avatar

refine-pocketbase's Issues

support for expand via meta props

Sample code provided by @fspijkerman via discord:

getList: async ({ resource, pagination, filters, meta, sorters }) => {
  const { current = 1, pageSize = 10 } = pagination ?? {};
  const generatedFilter = generateFilter(filters);
  const result = await pocketBaseClient.collection(resource).getList(current, pageSize, {
    filter: generatedFilter,
    expand: meta?.expand,
    sort: sorters?.flatMap( (x) => `${x.order == "asc" ? "+" : "-"}${x.field.replace("expand.", "")}`).join(",") ?? ""
  });

  if (result.items) {
    return {
      data: result.items,
      total: result.totalItems,
    } as any;
  }

  return {
    data: [],
    total: 0,
  } as any;
},
getOne: async ({ resource, id, meta }) => {
  if (id == undefined) {
    return { data: undefined }
  }

  try {
    const result = await pocketBaseClient.collection(resource).getOne(id as string, {
      expand: meta?.expand ?? null,
      fields: meta?.fields?.join(",") ?? "*",
    });

    if (result) {
      return {
        data: result as any,
      };
    }

    return {
      data: undefined
    }
  } catch (e: any) {
    const error: HttpError = {
      message: e.response.message,
      statusCode: e.status as number,
    };
    return Promise.reject(error);
  }
},

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.