Code Monkey home page Code Monkey logo

pocketbase-ts's Introduction

pocketbase-ts

Note: This is a work in progress, this should not be used until it close to release.

A simplified PocketBase SDK to ease the developer experience.

Features

  • Modular API
  • Strongly typed schemas

Roadmap/Todo

  • Finish rest of the PocketBase API
  • Add authentication
  • Clean up code significantly
  • Add realtime records
  • Add base schema for Users & Admins
  • Add Settings collection
  • Add Logs collection
  • Add import collection
  • Add good error handling
  • Finish schema validation
  • Potentially change from a singleton to instances?

Usage

This is using the demo on the pocketbase.io site.

import { init, collection, list, Schema, Type, authPassword, Admins } from "pocketbase-ts";

async function main() {
    init("https://pocketbase.io");
    await authPassword(Admins, "[email protected]", "123456");

    const PostSchema = new Schema({
        title: Type.text({ required: true }),
        description: Type.text(),
        active: Type.bool(),
        options: Type.select(),
        featuredImages: Type.file()
    });

    const Posts = collection("posts", PostSchema);
    const posts = await list(Posts, { filter: "active=true" });

    console.log(posts);
}

main();

Understanding the API

The API is designed like action (...arguments). This is uniform across the rest of the api.

CRUD methods, aka Create, Read, Update, Delete

To get a record by ID you use the get function.

await get(Users, "id of record");

To find one record you use the find function.

await find(Users, { filter: "likes>10" });

To create a record you use the create function.

await create(Users, { name: "Solaris9" });

To list paginated records you use the list function.

await list(Users, { sort: "created" });

Authentication

Authenticating is similar using the authentications methods.

await authPassword(Users, "[email protected]", "123456");
await authPassword(Admins, "[email protected]", "123456");

await requestEmailChange(Admins, "[email protected]");
await confirmEmailChange(Admins, token, password);

Realtime

To subscribe to a Collection you use the subscribe method.

// subscribe to the Collection for all create, delete, and update actions
subscribe(Posts, (action, record) => {
    // code
});

// subscribe to the specific record for delete and update actions
subscribe(Posts, "record-id", (action, record) => {
    // code
});

To unsubscribe from a subscription you can use unsubscribe or the method returned from subscribe

// subscribe to the Collection for all create, delete, and update actions
const unsubscribe = await subscribe(Posts, (action, record) => {
    // code
});

unsubscribe();

pocketbase-ts's People

Contributors

solaris9 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

nicosql

pocketbase-ts's Issues

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.