pocketbase / pocketbase

Open Source realtime backend in 1 file
https://pocketbase.io
MIT License
40.76k stars 1.91k forks source link

Feature Request: Public Views #3096

Closed carlosblanco closed 1 year ago

carlosblanco commented 1 year ago

It would be awesome to be able to enable/disable public views for collections. Something similar to these feature in basedash. Public views can be very similar to the current ones in the admin panel. The admin panel views are pretty good already. View Rules can apply to them in a similar fashion to API rules.

ganigeorgiev commented 1 year ago

This was requested before, but at least for v1 (and until https://github.com/pocketbase/pocketbase/issues/898) there are no plans to allow access to the admin UI (or portion of it) to non-admin accounts because it will be limited and I don't think it will be very useful.

Users can always implement manually their own custom interfaces and branding for previewing the data if they want to, for example by creating html file(s) in pb_public.

RepComm commented 3 months ago

I have a use case for view collections as non-admin: I'm building a forms app and the frontend needs view access to collection schemas in order to generate the correct UI and show form fields properly. The forms map directly to one or multiple collections.

I think my go-to for now will be to 'hack' around the limitation, but I'd love a legit API rule for pb.collections.getOne for instance

Edit, my hack as of now:

./pb_hooks/view.pb.js

routerAdd("GET", "/api/schema/:collectionname", (c) => {
  const collectionName = c.pathParam("collectionname");

  const collection = $app.dao().findCollectionByNameOrId(collectionName);

  return c.json(200, {
    collection,
  });
});

Usage:

static async GetCollectionSchema(collectionId: string) {
    let result: CollectionModel;
    //only works for admin
    // return DB.ctx.collections.getOne(collectionId);

    const url = `${DB.ctx.baseUrl}/api/schema/${collectionId}`;
    const res = await fetch(url);
    const json = await res.json();

    result = json.collection;

    return result;
  }

or by URL localhost:8090/api/schema/users

ganigeorgiev commented 3 months ago

@RepComm I don't see how your comment is related to the initial post but in any case I don't recommend the above and it would be insecure since you'll expose the collection API rules to anyone.

If you really need access to the collection fields definition a better option would be to create a View collection with query like:

select id, schema from `_collections`
RepComm commented 3 months ago

It's possible I misinterpreted the original context. I didn't realize the solution you've suggested, that seems like a much better and easier way to do it. Thank you very much!

Edit, to reproduce what I had I also select 'name' for people looking in the future:

select id, schema, name from `_collections`
    return DB.ctx
      .collection("schemas")
      .getFirstListItem<CollectionModel>(`name="${collectionId}"`);