All Projects → vapor-community → ferno

vapor-community / ferno

Licence: MIT license
Vapor Firebase Realtime database provider

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to ferno

VaporElasticsearch
A Vapor/Swift Elasticsearch client
Stars: ✭ 26 (-50%)
Mutual labels:  vapor, vapor-3
pagination
Simple Vapor 3 Pagination
Stars: ✭ 64 (+23.08%)
Mutual labels:  vapor, vapor-3
gatekeeper
Rate limiting middleware for Vapor 👮
Stars: ✭ 54 (+3.85%)
Mutual labels:  vapor, vapor-3
apns
Vapor APNS for iOS
Stars: ✭ 59 (+13.46%)
Mutual labels:  vapor, vapor-3
flash
Flash messages between views ⚡️
Stars: ✭ 34 (-34.62%)
Mutual labels:  vapor, vapor-3
submissions
Provides a common structure to deal with data based API requests
Stars: ✭ 15 (-71.15%)
Mutual labels:  vapor, vapor-3
paginator
Offset pagination for Vapor 🗂
Stars: ✭ 67 (+28.85%)
Mutual labels:  vapor
fluent-postgres-driver
🐘 PostgreSQL driver for Fluent.
Stars: ✭ 120 (+130.77%)
Mutual labels:  vapor
VaporCRUDRouter
A Rails-inspired extension to Vapor's routing system
Stars: ✭ 58 (+11.54%)
Mutual labels:  vapor
sqlite-nio
Non-blocking wrapper for libsqlite3-dev using SwiftNIO
Stars: ✭ 33 (-36.54%)
Mutual labels:  vapor
HomeKitty
A Vapor 3 website to easily browse HomeKit accessories.
Stars: ✭ 75 (+44.23%)
Mutual labels:  vapor
GraphQLRouteCollection
A GraphQL based RouteCollection for Vapor
Stars: ✭ 18 (-65.38%)
Mutual labels:  vapor
mongodb-vapor
MongoDB + Vapor integration
Stars: ✭ 31 (-40.38%)
Mutual labels:  vapor
Vapor-JWTAuthorization
Vapor JWT Authorization
Stars: ✭ 45 (-13.46%)
Mutual labels:  vapor
async
⏱ Promises and reactive-streams in Swift built for high-performance and scalability.
Stars: ✭ 35 (-32.69%)
Mutual labels:  vapor
KarmaAPI
Swift API using the Vapor web framework and Turnstile for authentication
Stars: ✭ 20 (-61.54%)
Mutual labels:  vapor
readme
Welcome to Vapor development at Nodes 📖
Stars: ✭ 47 (-9.62%)
Mutual labels:  vapor
sqlite-kit
Non-blocking SQLite client library with SQL builder built on SwiftNIO
Stars: ✭ 51 (-1.92%)
Mutual labels:  vapor
sendgrid
SendGrid-powered mail backend for Vapor
Stars: ✭ 66 (+26.92%)
Mutual labels:  vapor
OrderSystem
An independent micro-service that takes orders in and processes payments.
Stars: ✭ 16 (-69.23%)
Mutual labels:  vapor

Ferno 🔥

Ferno allows you to easily connect your Vapor project with your Firebase realtime database. It is built with the brand new Vapor 3. It gives you a nice and clean interface to interact with the Firebase Realtime REST API. It will automatically turn the response into your class/struct!

Prerequisites

You will need:

  • Vapor 3.0+

Installing

In your Package.swift file, add the line

.package(url: "https://github.com/vapor-community/ferno.git", from: "0.2.0")

Also make sure you add Ferno as a dependency

dependencies: ["Vapor", ..., "Ferno"]

Setup

  1. Ferno uses an access token to read and write to your database. First we will need to get your service account information.

    • Log into the Firebase console
    • Click the settings gear next to Project Overview
    • Select Project settings
    • Select the SERVICE ACCOUNTS tab
    • Click the button at the bottom that says GENERATE NEW PRIVATE KEY
    • This will download a .json file. You will now have access to the email and private key. You will pass those into the initialize during the next step.
  2. Register Ferno as a Provider and import Ferno. This is usually done in configure.swift

import Ferno

let fernoConfig = FernoConfig(basePath: "database-url", email: "service-account-email", privateKey: "private-key")
services.register(fernoConfig)
try services.register(FernoProvider())

Parameters

There are some custom parameters to pass into functions. I want to go over all the parameters you will need to know.

[FernoQuery]

In GET requests, you might want to query on your data. This is what [FernoQuery] is for.

FernoQuery is an enum with:

  1. case shallow(Bool)
  2. case orderBy(FernoValue)
  3. case limitToFirst(FernoValue)
  4. case limitToLast(FernoValue)
  5. case startAt(FernoValue)
  6. case endAt(FernoValue)
  7. case equalTo(FernoValue)

These are all the possible queries that are allowed on Firebase according to the docs

NOTES on [FernoQuery]

  • shallow(Bool) cannot be mixed with any other query parameters.
  • you usually use orderBy(FernoValue) in conjunction with enums 3-7
  • using orderBy(FernoValue) alone will just order the data returned

FernoValue

You will notice most cases in FernoQuery have a value of FernoValue. FernoValue is just a wrapper for Bool, String, Int, Double, Float. So you can just do .startAt(5) and everything will work.

Examples of [FernoQuery]

Just using shallow:

[.shallow(true)]

Filter data to only return data that matches "age": 21:

[.orderBy("age"), .equalTo(21)]

Just orderBy(returns data in ascending order):

[.orderBy("age")]

Usage

There are 6 functions that allow you to interact with your Firebase realtime database.

GET

There are two functions that allow you get your data.

client.ferno.retrieve(req: Request, queryItems: [FernoQuery], appendedPath: [String])
client.ferno.retrieveMany(req: Request, queryItems: [FernoQuery], appendedPath: [String])

The only difference between retrieve and retrieveMany is the return type.

  • retrieve returns -> F where F is of type Decodable
  • retrieveMany returns -> [String: F] where F is of type Decodable and String is the key

Example

  1. Define the value you want the data converted.
struct Developer: Content {
   var name: String
   var favLanguage: String
   var age: Int
}
  1. Make the request. Make sure you set the type of the response so Ferno knows what to convert.
let developers: Future<[String: Developer]> = try client.ferno.retrieveMany(req: request, queryItems: [], appendedPath: ["developers"])
let developer: Future<Developer> = try client.ferno.retrieve(req: request, queryItems: [], appendedPath: ["developers", "dev1"])

POST

Used to create a new entry in your database

client.ferno.create(req: Request, appendedPath: [String], body: T) -> Future<FernoChild>
  • body: T is of type Content.
  • FernoChild is a struct:
struct FernoChild: Content {
  var name: String
}
  • FernoChild is returned, because the API request returns the key from the newly created child.

Example

let newDeveloper = Developer(name: "Elon", favLanguage: "Python", age: 46) //conforms to Content
let newDeveloperKey: Future<FernoChild> = try client.ferno.create(req: request, appendedPath: ["developers"], body: newDeveloper)

DELETE

Used to delete an entry in your database

client.ferno.delete(req: Request, appendedPath: [String]) -> Future<Bool>
  • the delete method will return a boolean depending on if the delete was successful

Example

let successfulDelete: Future<Bool> = try client.ferno.delete(req: request, appendedPath: ["developers", "dev-1"])

PATCH

update values at a specific location, but omitted values won't get removed

client.ferno.update(req: Request, appendedPath: [String], body: T -> Future<T>
  • the update method will return the body

Example

struct UpdateDeveloperName: Content {
  var name: String
}

let newDeveloperName = UpdateDeveloperName(name: "Kimbal") //conforms to Content
let updatedDeveloperName: Future<UpdateDeveloperName> = try client.ferno.update(req: request, appendedPath: ["developers", newDeveloperKey.name], body: newDeveloper) //newDeveloperKey.name comes from the create method

PUT

overwrite the current location with data you are passing in

client.ferno.overwrite(req: Request, appendedPath: [String], body: T -> Future<T>

Example

struct LeadDeveloper: Content {
  var name: String
  var company: String
  var age: Int
}

let leadDeveloper = LeadDeveloper(name: "Ashley", company: "Bio-Fit", age: 20)
let leadDevResponse: Future<LeadDeveloper> = try client.ferno.overwrite(req: request, appendedPath: ["developers", newDeveloperKey.name], body: leadDeveloper)

Testing

Currently, tests were written using an actual dummy Firebase realtime database. If you want to run all of the tests, you will need to create a dummy Firebase realtime database.

Testing Setup

You need to go to Application+Testing.swift and fill in the missing values based on your Firebase service account. Then you will be able to run tests.

Authors

  • Austin Astorga - Main developer - My Github

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments

  • Vapor Discord (for helping me with all my issues <3)
  • Stripe Provider as a great template! stripe-provider
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].