Code Monkey home page Code Monkey logo

pocketbase-sdk-rust's Introduction

Pocketbase SDK

A Rust SDK for Pocketbase Clients. Pocketbase is an open source backend for your SaaS & Mobile Applications. The Goal of this project is to create a wrapper around the APIs that Pocketbase exposes to abstract away unnecessary details of implementation, so that you can focus on building your app and not worry about integration with pocketbase.

Pocketbase SDK currently only known to work on x86 targets. So essentially only CLI/Native applications can be built using Pocketbase SDK. But WebAsm support is on the Roadmap

Currently Compatible with Pocketbase Version 0.10.3

Installation

  $ cargo add pocketbase-sdk

or add the following to your Cargo.toml

[dependencies]
pocketbase-sdk = "0.0.8"
tokio = { version = "1", features = ["full"] }
serde = { version = "1.0.145", features = ["derive"] }

Usage

Note: You need to have a table created called "posts" inside of your repository for this code to work which must contain the files "title, content, author, created, updated". To use this in your own script what you want to do is modify the struct to have the data that you have, modify the variable name which here is named post to the variable your passing in our creating.

use serde::{Serialize, Deserialize};
use pocketbase_sdk::client::Client;
use pocketbase_sdk::user::UserTypes;
use pocketbase_sdk::records::operations::{
  list, view, delete, create
};

#[derive(Serialize, Deserialize, Debug)]
struct Post {
  title: String,
  content: String,
  created: String,
  updated: String,
  author: String,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /* new client + authentication */
    let mut client = Client::new("http://localhost:8090/api/").unwrap();
    let auth = client.auth_via_email(
        String::from("[email protected]"),
        String::from("Admin@123"),
        UserTypes::User /* use UserTypes::Admin for admin Authentication */
    ).await;
    assert!(auth.is_ok());

    /* create record */
    let post = Post {
      title: "Sample title".to_string(),
      content: "Sample Content".to_string(),
      author: "Sample Author".to_string(),
      created: "".to_string(),
      updated: "".to_string()
    };

    let repsonse = create::record::<Post>("posts", &post, &client).await.unwrap();
    match repsonse {
        create::CreateResponse::SuccessResponse(res) => {
            assert_eq!(res.title, String::from("Sample title"))
        },
        create::CreateResponse::FailureResponse(_err) => panic!("Failed!")
    }

    /* view record */
    let repsonse = view::record::<Post>("posts", "9bbl183t7ioqrea", &client).await.unwrap();
    match repsonse {
        view::ViewResponse::SuccessResponse(res) => println!("9bbl183t7ioqrea"),
        view::ViewResponse::ErrorResponse(_err) => panic!("Failed!")
    }

    /* list paginated records */
    let response = list::records::<Post>("posts", &client).await.unwrap();
    match response {
      list::ListResponse::SuccessResponse(paginated_record_list) => {
        assert_ne!(paginated_record_list.total_items, 0)
      },
      list::ListResponse::ErrorResponse(_e) => panic!("could not retrieve resource.")
    }

    /* delete a record */
    let response = delete::record("posts", "9bbl183t7ioqrea", &client).await;
    assert!(response.is_ok());

    Ok(())
}

Roadmap

  1. WebAsm Support (v0.1.7)
  2. Support Record File Attachments Upload #11
  3. Admin Tools: Logs
  4. Admin Tools: Settings
  5. Support Pocketbase Realtime APIs

pocketbase-sdk-rust's People

Contributors

leqitdev avatar miguelgargallo avatar sreedevk avatar stetsed avatar

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.