Loystore
Make your firestore quickly and easily.
Example
import firebase from 'firebase';
import 'firebase/firestore';
import Loystore from 'loystore';
const db = firebase.firestore();
type Post = {
title: string;
description: string;
rating: number;
author: string;
};
const post = new Loystore(db, 'posts');
async function get(documentID: string) {
try {
const data = await post.get(documentID);
return data; /**
* {
* id: documentID,
* data: {
* title: 'Hello',
* description: 'Hello Loystore',
* rating: 10,
* author: 'Kim'
* }
* }
*/
} catch (err) {
console.log(err);
}
}
async function set(documentID: string, data: Post, merge: boolean) {
try {
const data = await post.set(documentID, data, merge);
return data; // { id: documentID, data: data }
} catch (err) {
console.log(err);
}
}
async function add(data: Post) {
try {
const data = await post.add(data);
return data; // { id: 1rcI0EZrzQ0zoWNwWbUY, data: data }
} catch (err) {
console.log(err);
}
}
async function update(documentID: string, data: Post) {
try {
const data = await post.update(documentID, data);
return data; // { id: documentID, data: data }
} catch (err) {
console.log(err);
}
}
async function delete(documentID) {
try {
const data = await post.delete(documentID);
return data; // true
} catch (err) {
console.log(err);
}
}
async function getAll(limit: number) {
try {
// const data = await post.getAll(); // limit default 50.
const data = await post.getAll(limit);
return data; /**
* [
* {
* id: '1rcI0EZrzQ0zoWNwWbUY',
* data: {
* title: 'Hello',
* description: 'Hello loystore.',
* rating: 10,
* author: 'Kim'
* }
* },
* {
* id: '3OlEQ0Md1fgpv2gznGg3',
* data: {
* title: 'Good morning',
* description: 'Good morning everyone!',
* rating: 0,
* author: 'John'
* }
* },
* {
* id: '8qVKhEcULd6U9Dcfm0ae',
* data: {
* title: 'Good night',
* description: 'Good night everyone!',
* rating: 5,
* author: 'Jack'
* }
* }
* ];
*/
} catch (err) {
console.log(err);
}
}
const query1 = post.collection.where('rating', '<=', 8).orderBy('author', 'desc');
async function getFilter(query, limit: number) {
// const data = await post.getFilter(query); // limit default 50.
const data = await post.getFilter(query, limit);
return data; /**
* [
* {
* id: '3OlEQ0Md1fgpv2gznGg3',
* data: {
* title: 'Good morning',
* description: 'Good morning everyone!',
* rating: 0,
* author: 'John'
* }
* },
* {
* id: '8qVKhEcULd6U9Dcfm0ae',
* data: {
* title: 'Good night',
* description: 'Good night everyone!',
* rating: 5,
* author: 'Jack'
* }
* }
* ];
*/
}
// getFilter(query1, 50); // example
const query2 = post.collection.where('rating', '<=', 8).orderBy('author', 'asc');
async function getPage(query, limit: number, offset: number) { // next is lastDoc
try {
const data1 = await post.getPage(query, 100);
// Get 1 ~ 100 documents.
const data2 = await post.getPage(query, 100, 101);
// Get 101 ~ 200 documents.
const next = data2.next;
const data3 = await post.getPage(query, 10, 10, next);
// Get 10 ~ 20 documents starting with next(Doc).
return [...data1, ...data2, ...data3, ...data4]; // 1 ~ 200 + 210 ~ 220 documents.
} catch (err) {
console.log(err);
}
}
// getPage(query2, 20); // example
// Only working on firebase-admin instance.
async function getCollections() {
try {
const data = await post.getCollections();
return data; // posts collections.
} catch (err) {
console.log(err);
}
}
0.2.5
- Modify getPage parameters
// before
getPage(query, startAt, limit, next);
// after
getPage(query, limit, offset = 1, next);