cosmic-database
Version:
A database library for cosmic.new. Designed to be used and deployed on cosmic.new
153 lines • 4.87 kB
TypeScript
export type WhereFilterOp = '==' | '!=' | '<' | '<=' | '>' | '>=' | 'array-contains' | 'array-contains-any' | 'in' | 'not-in';
export type OrderByDirection = 'asc' | 'desc';
export type DocumentData = {
[field: string]: any;
};
type QueryState = {
whereFields: Set<string>;
orderByFields: Set<string>;
hasArrayContains: boolean;
hasInequality: boolean;
inequalityField?: string;
};
type QueryParams = {
where: Array<{
field: string;
op: WhereFilterOp;
value: any;
}>;
orderBy: Array<{
field: string;
direction?: OrderByDirection;
}>;
limit?: number;
offset?: number;
};
export declare class DocumentSnapshot<T = DocumentData> {
private _id;
private _ref;
private _data?;
private _exists;
constructor(id: string, ref: DocumentReference<T>, data?: T);
get id(): string;
get ref(): DocumentReference<T>;
get exists(): boolean;
data(): T | undefined;
get(fieldPath: string): any;
}
export declare class QuerySnapshot<T = DocumentData> {
private _docs;
private _size;
constructor(docs: DocumentSnapshot<T>[]);
get docs(): DocumentSnapshot<T>[];
get size(): number;
get empty(): boolean;
forEach(callback: (doc: DocumentSnapshot<T>) => void): void;
}
export declare class DocumentReference<T = DocumentData> {
private _path;
private _id;
private _parent;
constructor(path: string, parent: CosmicCollectionReference<T>);
get id(): string;
get path(): string;
get parent(): CosmicCollectionReference<T>;
get(): Promise<DocumentSnapshot<T>>;
set(data: T, options?: {
merge?: boolean;
}): Promise<void>;
update(data: Partial<T> | {
[field: string]: any;
}): Promise<void>;
delete(): Promise<void>;
collection(collectionPath: string): CosmicCollectionReference;
}
export declare class WriteBatch {
private operations;
set(ref: DocumentReference, data: DocumentData, options?: {
merge?: boolean;
}): WriteBatch;
update(ref: DocumentReference, data: DocumentData): WriteBatch;
delete(ref: DocumentReference): WriteBatch;
commit(): Promise<void>;
}
export declare class CosmicQuery<T = DocumentData> {
private collectionPath;
private params;
private state;
constructor(collectionPath: string, params?: QueryParams, state?: QueryState);
private validateWhere;
private validateOrderBy;
where(fieldPath: string, opStr: WhereFilterOp, value: any): CosmicQuery<T>;
orderBy(fieldPath: string, directionStr?: OrderByDirection): CosmicQuery<T>;
limit(limit: number): CosmicQuery<T>;
offset(offset: number): CosmicQuery<T>;
get(): Promise<QuerySnapshot<T>>;
}
export declare class CosmicCollectionReference<T = DocumentData> {
private _path;
constructor(path: string);
get id(): string;
get path(): string;
get parent(): DocumentReference | null;
doc(documentPath?: string): DocumentReference<T>;
add(data: T): Promise<DocumentReference<T>>;
where(fieldPath: string, opStr: WhereFilterOp, value: any): CosmicQuery<T>;
orderBy(fieldPath: string, directionStr?: OrderByDirection): CosmicQuery<T>;
limit(limit: number): CosmicQuery<T>;
offset(offset: number): CosmicQuery<T>;
get(): Promise<QuerySnapshot<T>>;
private generateId;
}
export declare class FieldValue {
static serverTimestamp(): {
_type: 'serverTimestamp';
};
static increment(n: number): {
_type: 'increment';
value: number;
};
static arrayUnion(...elements: any[]): {
_type: 'arrayUnion';
elements: any[];
};
static arrayRemove(...elements: any[]): {
_type: 'arrayRemove';
elements: any[];
};
static delete(): {
_type: 'delete';
};
}
export declare class Timestamp {
seconds: number;
nanoseconds: number;
constructor(seconds: number, nanoseconds: number);
static now(): Timestamp;
static fromDate(date: Date): Timestamp;
static fromMillis(millis: number): Timestamp;
toDate(): Date;
toMillis(): number;
}
export declare class GeoPoint {
latitude: number;
longitude: number;
constructor(latitude: number, longitude: number);
isEqual(other: GeoPoint): boolean;
}
export declare class CosmicDatabase {
private getClientId;
private buildTenantPath;
collection<T = DocumentData>(collectionPath: string): CosmicCollectionReference<T>;
doc(documentPath: string): DocumentReference;
collectionGroup(): CosmicQuery;
getTenantPrefix(): string;
getCurrentClientId(): string;
batch(): WriteBatch;
get FieldValue(): typeof FieldValue;
get Timestamp(): typeof Timestamp;
get GeoPoint(): typeof GeoPoint;
}
export declare const db: CosmicDatabase;
export {};
//# sourceMappingURL=index.d.ts.map