Usage
- Install the npm package:
yarn add fireorm reflect-metadata #or npm install fireorm reflect-metadata
# note: the reflect-metadata shim is required
- Initialize your Firestore application:
import * as admin from 'firebase-admin';
import * as fireorm from 'fireorm';
const serviceAccount = require('../firestore.creds.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: `https://${serviceAccount.project_id}.firebaseio.com`,
});
const firestore = admin.firestore();
fireorm.initialize(firestore);
- Create your Firestore models:
import { Collection } from 'fireorm';
@Collection()
class Todo {
id: string;
text: string;
done: Boolean;
}
- Do cool stuff with fireorm!
import { Collection, getRepository } from 'fireorm';
@Collection()
class Todo {
id: string;
text: string;
done: Boolean;
}
const todoRepository = getRepository(Todo);
const todo = new Todo();
todo.text = "Check fireorm's Github Repository";
todo.done = false;
const todoDocument = await todoRepository.create(todo); // Create todo
const mySuperTodoDocument = await todoRepository.findById(todoDocument.id); // Read todo
await todoRepository.update(mySuperTodoDocument); // Update todo
await todoRepository.delete(mySuperTodoDocument.id); // Delete todo
Firebase Complex Data Types
Firestore has support for complex data types such as GeoPoint and Reference. Full handling of complex data types is being handled in this issue. Temporarily, fireorm will export Class Transformer's @Type decorator. It receives a lamda where you return the type you want to cast to. See GeoPoint Example.
Limitations
If you want to cast GeoPoints to your custom class, it must have latitude: number
and longitude: number
as public class fields. Hopefully this won't be a limitation in v1.