Code Monkey home page Code Monkey logo

supabase-swift's Introduction

supabase-swift

Supabase client for swift. Mirrors the design of supabase-js

Installation

Swift Package Manager:

Add the following lines to your Package.swift file:

let package = Package(
    ...
    dependencies: [
        ...
        .package(name: "Supabase", url: "https://github.com/supabase/supabase-swift.git", .exact("0.0.2")), // Add the package
    ],
    targets: [
        .target(
            name: "YourTargetName",
            dependencies: ["Supabase"] // Add as a dependency
        )
    ]
)

If you're using Xcode, use this guide to add supabase-swift to your project. Use https://github.com/supabase/supabase-swift.git for the url when Xcode asks.

Usage

For all requests made for supabase, you will need to initialize a SupabaseClient object.

let client = SupabaseClient(supabaseUrl: "{ Supabase URL }", supabaseKey: "{ Supabase anonymous Key }")

This client object will be used for all the following examples.

Database

Query todo table for all completed todos.

struct Todo: Codable {
    var id: String = UUID().uuidString
    var label: String
    var isDone: Bool = false
}
let query = try client.database.from("todos")
    .select()
    .eq(column: "isDone", value: "true")
                                
query.execute { [weak self] results in
    guard let self = self else { return }

    switch results {
    case let .success(response):
        let todos = try? response.decoded(to: [Todo].self)
        print(todos)
    case let .failure(error):
        print(error.localizedDescription)
    }
}

Insert a todo into the database.

let todo = Todo(label: "Example todo!")

let jsonData: Data = try JSONEncoder().encode(todo)
let jsonDict: [String: Any] = try JSONSerialization.jsonObject(with: jsonData, options: .allowFragments))

client.database.from("todos")    
    .insert(values: jsonDict)
    .execute { results in
        // Handle response
    }

For more query examples visit the Javascript docs to learn more. The API design is a near 1:1 match.

Execute an RPC

do {
    try client.database.rpc(fn: "testFunction", parameters: nil).execute { result in
        // Handle result
    }
} catch {
   print("Error executing the RPC: \(error)")
}

Realtime

Realtime docs coming soon

Auth

Sign up with email and password

client.auth.signUp(email: "[email protected]", password: "password") { result in
    switch result {
    case let .success(session, user): print(user)
    case let .failure(error): print(error.localizedDescription)
    }
}

Login up with email and password

client.auth.signIn(email: "[email protected]", password: "password") { result in
    switch result {
    case let .success(session): print(session.accessToken, session.user)
    case let .failure(error): print(error.localizedDescription)
    }
}

Storage

Storage docs coming soon

Contributing

  • Fork the repo on GitHub
  • Clone the project to your own machine
  • Commit changes to your own branch
  • Push your work back up to your fork
  • Submit a Pull request so that we can review your changes and merge

Sponsors

We are building the features of Firebase using enterprise-grade, open source products. We support existing communities wherever possible, and if the products don’t exist we build them and open source them ourselves. Thanks to these sponsors who are making the OSS ecosystem better for everyone.

New Sponsor

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.