myclubapp / backend

Backend for my-club App
https://my-club.ap
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

feat: create firebase singleton #1

Closed sansan88 closed 2 years ago

sansan88 commented 2 years ago

1

i am use typescript, a pattner singleton, because admin.initializeApp(); needs a only instance

import admin from 'firebase-admin'; export default class firebaseDAO {

private static _intance: firebaseDAO;
db: any;
private constructor() {

    admin.initializeApp();
    this.db = admin.firestore();
}

public static get instance() {
    return this._intance || (this._intance = new this());
}

// iniciar un metodo
start() {
}

}

create a DAO

import firebaseDAO from '../database/firebase.dao';

constructor(){ this.db = firebaseDAO.instance.db; }

async getPacientes() { try { const userQuerySnapshot = await this.db.collection('pacientes').get(); const users: any[] = []; userQuerySnapshot.forEach( (doc: any) => { users.push({ id: doc.id, data: doc.data() }); } ); this.response.resultado=users; return this.response; } catch (error) { this.response.resultado=null; return this.response; } }

sansan88 commented 2 years ago

https://stackoverflow.com/questions/57397608/the-default-firebase-app-does-not-exist-make-sure-you-call-initializeapp-befo