Skip to content
This repository has been archived by the owner on Apr 8, 2019. It is now read-only.
/ firebase-odm Public archive

An Object Relational Mapping for Angular Firestore.

Notifications You must be signed in to change notification settings

lsoaresesilva/firebase-odm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Object document mapper for Angular Firestore by Google's Firebase

Saving, reading and updating on Google Firebase with AngularFirestore is a tedious and repetitive task. This project aims to minimize this problem.

At the time, insert, read, list all, delete, delete all and count are working. TODO: working to save relationships recursively and to update an entity.

Installation

  1. Clone the repository to your project as a submodule: git add submodule https://github.com/lsoaresesilva/firebase-odm.git

Usage

  1. Configure AngularFirebase in your project: https://github.com/angular/angularfire2
  2. Run npm test, and verify if everything works as expected (it will run the unit tests and create some documents in your firestore)
  3. Create a simple class and extend from Document (we may call it a Model from MVC)
  4. Inside a component, inject AngularFireStore as usual
  5. Pass this injected instance to the constructor of your class which extended from Document
  6. Enjoy!

Example

\\file: person.ts

@Collection("personCollection")
export class Person extends Document{
  name:string;
}

\\file: your-component.ts

export class AppComponent {
  
  constructor(public db: AngularFirestore){
    let person = new Person(this.db);
    person.name = "Leonardo"
    person.add().subscribe(result=>{
      console.log(result.id) // optional
    });
    

    Person.get(this.db, 'your-entity-id').subscribe(
      document=>{ // its a person!},
      err=>{// ops, an error, maybe a document with this id does not exists.});
    Person.getAll(this.db).subscribe(result=>{ // list of persons}); //
    Person.count(this.db).subscribe(result=>{ // number of documents});

    person.delete();
    Person.deleteAll(this.db);
  }
}
\\file: animal.ts

@Collection("animalsCollection")
export class Animal extends Document{
  name:string;
}

\\file: person.ts
@Collection("peopleCollection")
export class Person extends Document{
  name:string;
  @oneToMany({document:Animal})
  animal
}


\\file: your-component.ts

export class AppComponent {
  
  constructor(public db: AngularFirestore){
    let animal = new Animal(thid.db)
    animal.name = "Scooby doo";
    let person = new Person(this.db);
    person.name = "Leonardo"
    person.animal = animal
    person.add().subscribe(result=>{
         // Will also save animal :)
    });
   
  }
}

About

An Object Relational Mapping for Angular Firestore.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published