All Projects → poisondminds → firebase-db-wrapper-swift

poisondminds / firebase-db-wrapper-swift

Licence: other
An easy-to-use wrapper for Firebase's Realtime Database

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to firebase-db-wrapper-swift

Rxfirebase
Rxjava 2.0 wrapper on Google's Android Firebase library.
Stars: ✭ 509 (+3081.25%)
Mutual labels:  wrapper, firebase-realtime-database
pygmentize
Pygmentize is a wrapper to `pygmentize`, the command line interface provided by Pygments, a python syntax highlighter.
Stars: ✭ 25 (+56.25%)
Mutual labels:  wrapper
steampak
Nicely packed tools to work with Steam APIs
Stars: ✭ 21 (+31.25%)
Mutual labels:  wrapper
laravel-firebase
Laravel Firebase API implementation
Stars: ✭ 22 (+37.5%)
Mutual labels:  firebase-realtime-database
koishi
Python wrapper for the unofficial scraped API of the satori testing system.
Stars: ✭ 13 (-18.75%)
Mutual labels:  wrapper
pyGroff
laTEX is awesome but we are lazy -> groff with markdown syntax and inline code execution
Stars: ✭ 25 (+56.25%)
Mutual labels:  wrapper
python-makefun
Dynamically create python functions with a proper signature.
Stars: ✭ 62 (+287.5%)
Mutual labels:  wrapper
dotty dict
Dictionary wrapper for quick access to deeply nested keys.
Stars: ✭ 67 (+318.75%)
Mutual labels:  wrapper
popyt
A very easy to use Youtube Data v3 API wrapper.
Stars: ✭ 42 (+162.5%)
Mutual labels:  wrapper
pwnscripts
Very simple script(s) to hasten binary exploit creation
Stars: ✭ 66 (+312.5%)
Mutual labels:  wrapper
caller-lookup
Reverse Caller Id using TrueCaller
Stars: ✭ 55 (+243.75%)
Mutual labels:  wrapper
Lavalink.py
A wrapper for Lavalink in Python, which aims to be powerful and intuitive.
Stars: ✭ 162 (+912.5%)
Mutual labels:  wrapper
hlsjs-upimg-wrapper
The upimg decoder module for hls.js
Stars: ✭ 19 (+18.75%)
Mutual labels:  wrapper
hibpwned
Python API wrapper for haveibeenpwned.com (API v3)
Stars: ✭ 21 (+31.25%)
Mutual labels:  wrapper
Nodeactyl
A NodeJS API for Pterodactyl panel, this was originally designed for discord.js (Discord bots)
Stars: ✭ 107 (+568.75%)
Mutual labels:  wrapper
SoapHttpClient
HttpClient wrapper for sending SOAP messages.
Stars: ✭ 80 (+400%)
Mutual labels:  wrapper
ZetaHtmlEditControl
A small wrapper class around the Windows Forms 2.0 WebBrowser control.
Stars: ✭ 72 (+350%)
Mutual labels:  wrapper
benjamincarlson.io
My personal website built with Next.js, Chakra UI, Firebase, and next-mdx-remeote.
Stars: ✭ 102 (+537.5%)
Mutual labels:  firebase-realtime-database
restique
A wrapper around restic with profiles
Stars: ✭ 43 (+168.75%)
Mutual labels:  wrapper
ssh2.nim
Async SSH, SCP and SFTP client for Nim, using libssh2 wrapper [WIP]
Stars: ✭ 17 (+6.25%)
Mutual labels:  wrapper

firebase-db-wrapper-swift

An easy-to-use object wrapper for Firebase's Realtime Database

Dependencies: FirebaseDatabase, FirebaseStorage

For demonstration purposes, we'll use the database structure defined below, comprised of murals & artists:

{
  "murals" : 
  {
    "-KaQYfs3kbt4XgDY0ftb" : 
    {
      "artists" : 
      {
        "-KbJbPknFNECn07m1yzy" : true,
        "-KbJXK4aoXc6NZ6VwD7W" : true
      },
      "description" : "A beautiful mural in Orange, CA",
      "images" : 
      {
        "m1" : 
        {
          "location" : "/murals/m1.jpg"
        },
        "m2" : 
        {
          "location" : "/murals/m2.jpg"
        }
      },
      "name" : "A really great mural"
    }
  },
  "artists" : 
  {
    "-KbJXK4aoXc6NZ6VwD7W" : 
    {
      "country" : "US",
      "firstName" : "Mary",
      "lastName" : "Smith"
    },
    "-KbJbPknFNECn07m1yzy" : 
    {
      "country" : "US",
      "firstName" : "Kerry",
      "lastName" : "Winston"
    }
}

FIRModel Usage

Subclass FIRModel to make your models serialize & deserialize from objects returned directly from your Firebase Realtime Database. The structure of a simple read-only FIRModel representing a mural may look like this:

class MuralModel: FIRModel
{	
	static var FIELD_NAME = "name"
	static var FIELD_DESCRIPTION = "description"
	static var FIELD_IMAGES = "images"
	static var FIELD_ARTISTS = "artists"
	
	var name: String? { return self.get(MuralModel.FIELD_NAME) }
	var desc: String? { return self.get(MuralModel.FIELD_DESCRIPTION) }
	
	var images: [ImageModel] { return self.get(MuralModel.FIELD_IMAGES) }
	var artists: [ArtistModel] { return self.get(MuralModel.FIELD_ARTISTS) }
}

Artist:

class ArtistModel: FIRModel
{	
	static var FIELD_FIRSTNAME = "firstName"
	static var FIELD_LASTNAME = "lastName"
	static var FIELD_COUNTRY = "country"

	var firstName: String? { return self.get(ArtistModel.FIELD_FIRSTNAME) }
    	var lastName: String? { return self.get(ArtistModel.FIELD_LASTNAME) }
    	var country: String? { return self.get(ArtistModel.FIELD_COUNTRY) }
}

Image:

class ImageModel: FIRModel
{
	static var FIELD_LOCATION = "location"
	var location: String? { return self.get(ImageModel.FIELD_LOCATION) }
}

FIRModel mirrors the functionality of Firebase's FIRDataSnapshot, and is therefore constructed using one:

let mural = MuralModel(snapshot: muralSnapshot)

Properties can be as nested as necessary. Notice that images and artists in MuralModel are of complex object types. These are too subclasses of FIRModel. Look back at the database structure. As recommended in Firebase's database structure guidelines, in our database, the artists node consists only of keys. Because of this, the artists node, for example, will consist of a number of ArtistModel, but only the key property will be populated. This is where FIRQueryable comes in.

FIRQueryable Usage

FIRQueryable is a protocol that can be adopted by any FIRModel that belongs to a top level collection in the Firebase database. By adopting this protocol, you'll simply need to define which collection your model belongs to. See example below of ArtistModel.

class ArtistModel: FIRModel, FIRQueryable
{	
	static var COLLECTION_NAME = "artists"
	
	...
}

When FIRQueryable is adopted, you may use getExternal(completion: () -> ()) to retrieve a partially populated model. See example below.

let firstArtist = self.mural.artists[0]
firstArtist.getExternal {
    self.artistLabel.text = firstArtist.firstName
}

FIRQueryable additionally contains several static query-convenience functions. FIRQueryable.Where() is demonstrated below.

MuralModel.Where(child: MuralModel.FIELD_NAME, equals: "Some value", limit: 1000) { (murals: [MuralModel]) in
            
    // Do something
}

FIRPropertyWritable Usage

By nature, FIRModel is a read-only model. FIRPropertyWritable can be adopted to allow modifying properties in a FIRModel. A more sophisticated MuralModel is demonstrated below.

class MuralModel: FIRModel, FIRPropertyWritable
{	
	static var FIELD_NAME = "name"
	static var FIELD_DESCRIPTION = "description"
    	static var FIELD_IMAGES = "images"
    	static var FIELD_ARTISTS = "artists"
	
	var name: String? {
		get { return self.get(MuralModel.FIELD_NAME) }
		set { self.set(value: newValue, for: MuralModel.FIELD_NAME) }
	}
	var desc: String? {
		get { return self.get(MuralModel.FIELD_DESCRIPTION) }
		set { self.set(value: newValue, for: MuralModel.FIELD_DESCRIPTION) }
	}
	
	var images: [ImageModel] { return self.get(MuralModel.FIELD_IMAGES) }
	var artists: [ArtistModel] { return self.get(MuralModel.FIELD_ARTISTS) }
}

FIRInsertable Usage

FIRInsertable can be adopted by a FIRModel that belongs to a database collection that can be written to.

class ArtistModel: FIRModel, FIRInsertable
{
    static var COLLECTION_NAME = "artists"
	
	static var FIELD_FIRSTNAME = "firstName"
	static var FIELD_LASTNAME = "lastName"
	static var FIELD_COUNTRY = "country"

	var firstName: String? { return self.get(ArtistModel.FIELD_FIRSTNAME) }
	var lastName: String? { return self.get(ArtistModel.FIELD_LASTNAME) }
	var country: String? { return self.get(ArtistModel.FIELD_COUNTRY) }

    class func Create(firstName: String, lastName: String, country: String, completion: @escaping (ArtistModel) -> Void)
    {
        let data = [
            FIELD_FIRSTNAME: firstName,
            FIELD_LASTNAME: lastName,
            FIELD_COUNTRY: country
        ]
        
        self.Insert(data: data, completion: completion)
    }
}

FIRInsertable.Insert() can also be used statically outside of a class.

let data = [
    FIELD_FIRSTNAME: firstName,
    FIELD_LASTNAME: lastName,
    FIELD_COUNTRY: country
]

ArtistModel.Insert(data: data) { (createdArtist: ArtistModel) in
    
    // Handle completion
}

FIRDeletable Usage

FIRDeletable can be adopted by a FIRModel that belongs to a database collection that can be written to. A class that adopts FIRDeletable has the ability to call the function delete()

FIRStorageDownloadable Usage

Any FIRModel that has a location: String pointing to a location in Firebase Storage can instead adopt FIRStorageDownloadable to provide a seamless integration. In our case:

class ImageModel: FIRModel, FIRStorageDownloadable { }

Then from here,

let image: ImageModel = mural.images[0]
image.getData(withMaxSize: 1 * 1024 * 1024, completion: { (d: Data?, e: Error?) in
    
    if let error = e
    {
        print("Woops: \(error)")
    }
    else if let data = d
    {
        self.imageView.image = UIImage(data: data)
    }
})

Enjoy!

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].