nekostore
v0.6.0
Published
Real-time synced data-store like Firestore with type
Downloads
6
Maintainers
Readme
Nekostore
Real-time synchronized data store like Firestore for TypeScript.
Requirements
- Node.js >= 10.0.0
Usage
Install
$ npm i -S nekostore
Import
import Nekostore from 'nekostore';
import BasicDriver from 'nekostore/src/driver/basic';
Create Nekostore instance
const driver: Driver = new BasicDriver();
const nekostore: Nekostore = new Nekostore(driver);
Get reference
See also
interface Data {
foo: string;
bar?: number;
}
interface ChildData {
pyo: boolean;
}
const colRef: CollectionReference<Data> = nekostore.collection<Date>('c1');
const docRef: DocumentReference<Data> = colRef.doc('d1');
const childColRef: CollectionReference<ChildData> = docRef.collection<ChildData>('child');
Add document
const docRef: DocumentReference<Data> = await colRef.add({ foo: 'a', bar: 0 });
Get document
const snapshot: DocumentSnapshot<Data> = await docRef.get();
if (snapshot.exists()) {
console.log(snapshot.data, snapshot.createTime, snapshot.updateTime);
}
Update document
await docRef.update({ bar: 1 });
Set document
const docRef: DocumentReference<Data> = colRef.('d2');
const s1: DocumentSnapshot<Data> = await docRef.get();
console.log(s1.exists()); // false
await docRef.set({ foo: 'b' });
const s2: DocumentSnapshot<Data> = await docRef.get();
console.log(s2.exists()); // true
console.log(snapshot.data); // { foo: 'b' }
Delete document
await docRef.delete();
Query
function printDocumentsData(snapshot: QuerySnapshot<Data>): void {
snapshot.docs.forEach((doc: DocumentChange<Data>): void => {
console.log(doc.ref.id, doc.type, doc.exists(), doc.data);
});
}
await colRef.add({ foo: 'a', bar: 0 }); // d1
await colRef.add({ foo: 'b', bar: 1 }); // d2
await colRef.add({ foo: 'c', bar: 2 }); // d3
Get all documents
const snapshot: QuerySnapshot<Data> = await colRef.get();
printDocumentsData(snapshot); // random order
Sort
const asc: QuerySnapshot<Data> = await colRef.orderBy('bar').get();
printDocumentsData(asc); // d1, d2, d3
const desc: QuerySnapshot<Data> = await colRef.orderBy('bar').get();
printDocumentsData(desc); // d3, d2, d1
Limit
const snapshot: QuerySnapshot<Data> = await colRef.orderBy('foo').limit(2).get();
printDocumentsData(snapshot); // d1, d2
EndAt EndBefore StartAfter StartAt
const s1: QuerySnapshot<Data> = await colRef.orderBy('bar').endAt(1).get();
printDocumentsData(s1); // d1, d2
const s2: QuerySnapshot<Data> = await colRef.orderBy('bar').endBefore(1).get();
printDocumentsData(s2); // d1
const s3: QuerySnapshot<Data> = await colRef.orderBy('bar').startAfter(1).get();
printDocumentsData(s3); // d3
const s4: QuerySnapshot<Data> = await colRef.orderBy('bar').startAt(1).get();
printDocumentsData(s4); // d2, d3
Where
const s1: QuerySnapshot<Data> = await colRef.where('foo', '==', 'c').get();
printDocumentsData(s1); // d3
const s2: QuerySnapshot<Data> = await colRef.orderBy('bar').where('foo', '>=', 'b').get();
printDocumentsData(s2); // d2, d3
const s3: QuerySnapshot<Data> = await colRef.orderBy('bar').where('foo', '<', 'b').get();
printDocumentsData(s3); // d1
Vue integration
import { Component, Vue, Prop } from 'vue-property-decorator';
import DocumentReference from 'nekostore/lib/DocumentReference';
import { Doc } from 'nekostore/lib/decorators';
@Component
class MyComponent extends Vue {
@Prop({ type: Object, required: false, default: null }) ref!: DocumentReference<Data> | null;
@Doc<Data, MyComponent>('ref') doc!: Data | null;
}
import { Component, Vue, Prop } from 'vue-property-decorator';
import { NonEmptyDocumentSnapshot } from 'nekostore/lib/DocumentSnapshot';
import Query from 'nekostore/lib/Query';
import { Collection } from 'nekostore/lib/decorators';
@Component
class MyComponent extends Vue {
@Prop({ type: Object, required: false, default: null }) query!: Query<Data> | null;
@Collection<Data, MyComponent>('query') docs!: NonEmptyDocumentSnapshot[] | null;
}