File size: 6,812 Bytes
0bcc252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import _ from 'lodash';
import { AutoCastable, Prop, RPC_MARSHAL } from 'civkit/civ-rpc';
import {
    Firestore, FieldValue, DocumentReference,
    Query, Timestamp, SetOptions, DocumentSnapshot,
} from '@google-cloud/firestore';

// Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the \"new\" operator)
function patchFireStoreArrogance(func: Function) {
    return function (this: unknown) {
        const origObjectGetPrototype = Object.getPrototypeOf;
        Object.getPrototypeOf = function (x) {
            const r = origObjectGetPrototype.call(this, x);
            if (!r) {
                return r;
            }
            return Object.prototype;
        };
        try {
            return func.call(this, ...arguments);
        } finally {
            Object.getPrototypeOf = origObjectGetPrototype;
        }
    };
}

Reflect.set(DocumentReference.prototype, 'set', patchFireStoreArrogance(Reflect.get(DocumentReference.prototype, 'set')));
Reflect.set(DocumentSnapshot, 'fromObject', patchFireStoreArrogance(Reflect.get(DocumentSnapshot, 'fromObject')));

function mapValuesDeep(v: any, fn: (i: any) => any): any {
    if (_.isPlainObject(v)) {
        return _.mapValues(v, (i) => mapValuesDeep(i, fn));
    } else if (_.isArray(v)) {
        return v.map((i) => mapValuesDeep(i, fn));
    } else {
        return fn(v);
    }
}

export type Constructor<T> = { new(...args: any[]): T; };
export type Constructed<T> = T extends Partial<infer U> ? U : T extends object ? T : object;

export function fromFirestore<T extends FirestoreRecord>(
    this: Constructor<T>, id: string, overrideCollection?: string
): Promise<T | undefined>;
export async function fromFirestore(
    this: any, id: string, overrideCollection?: string
) {
    const collection = overrideCollection || this.collectionName;
    if (!collection) {
        throw new Error(`Missing collection name to construct ${this.name}`);
    }

    const ref = this.DB.collection(overrideCollection || this.collectionName).doc(id);

    const ptr = await ref.get();

    if (!ptr.exists) {
        return undefined;
    }

    const doc = this.from(
        // Fixes non-native firebase types
        mapValuesDeep(ptr.data(), (i: any) => {
            if (i instanceof Timestamp) {
                return i.toDate();
            }

            return i;
        })
    );

    Object.defineProperty(doc, '_ref', { value: ref, enumerable: false });
    Object.defineProperty(doc, '_id', { value: ptr.id, enumerable: true });

    return doc;
}

export function fromFirestoreQuery<T extends FirestoreRecord>(
    this: Constructor<T>, query: Query
): Promise<T[]>;
export async function fromFirestoreQuery(this: any, query: Query) {
    const ptr = await query.get();

    if (ptr.docs.length) {
        return ptr.docs.map(doc => {
            const r = this.from(
                mapValuesDeep(doc.data(), (i: any) => {
                    if (i instanceof Timestamp) {
                        return i.toDate();
                    }

                    return i;
                })
            );
            Object.defineProperty(r, '_ref', { value: doc.ref, enumerable: false });
            Object.defineProperty(r, '_id', { value: doc.id, enumerable: true });

            return r;
        });
    }

    return [];
}

export function setToFirestore<T extends FirestoreRecord>(
    this: Constructor<T>, doc: T, overrideCollection?: string, setOptions?: SetOptions
): Promise<T>;
export async function setToFirestore(
    this: any, doc: any, overrideCollection?: string, setOptions?: SetOptions
) {
    let ref: DocumentReference<any> = doc._ref;
    if (!ref) {
        const collection = overrideCollection || this.collectionName;
        if (!collection) {
            throw new Error(`Missing collection name to construct ${this.name}`);
        }

        const predefinedId = doc._id || undefined;
        const hdl = this.DB.collection(overrideCollection || this.collectionName);
        ref = predefinedId ? hdl.doc(predefinedId) : hdl.doc();

        Object.defineProperty(doc, '_ref', { value: ref, enumerable: false });
        Object.defineProperty(doc, '_id', { value: ref.id, enumerable: true });
    }

    await ref.set(doc, { merge: true, ...setOptions });

    return doc;
}

export function deleteQueryBatch<T extends FirestoreRecord>(
    this: Constructor<T>, query: Query
): Promise<T>;
export async function deleteQueryBatch(this: any, query: Query) {
    const snapshot = await query.get();

    const batchSize = snapshot.size;
    if (batchSize === 0) {
        return;
    }

    // Delete documents in a batch
    const batch = this.DB.batch();
    snapshot.docs.forEach((doc) => {
        batch.delete(doc.ref);
    });
    await batch.commit();

    process.nextTick(() => {
        this.deleteQueryBatch(query);
    });
};

export function fromFirestoreDoc<T extends FirestoreRecord>(
    this: Constructor<T>, snapshot: DocumentSnapshot,
): T | undefined;
export function fromFirestoreDoc(
    this: any, snapshot: DocumentSnapshot,
) {
    const doc = this.from(
        // Fixes non-native firebase types
        mapValuesDeep(snapshot.data(), (i: any) => {
            if (i instanceof Timestamp) {
                return i.toDate();
            }

            return i;
        })
    );

    Object.defineProperty(doc, '_ref', { value: snapshot.ref, enumerable: false });
    Object.defineProperty(doc, '_id', { value: snapshot.id, enumerable: true });

    return doc;
}
const defaultFireStore = new Firestore({
    projectId: process.env.GCLOUD_PROJECT,
});
export class FirestoreRecord extends AutoCastable {
    static collectionName?: string;
    static OPS = FieldValue;
    static DB = defaultFireStore;
    static get COLLECTION() {
        if (!this.collectionName) {
            throw new Error('Not implemented');
        }

        return this.DB.collection(this.collectionName);
    }

    @Prop()
    _id?: string;
    _ref?: DocumentReference<Partial<Omit<this, '_ref' | '_id'>>>;

    static fromFirestore = fromFirestore;
    static fromFirestoreDoc = fromFirestoreDoc;
    static fromFirestoreQuery = fromFirestoreQuery;

    static save = setToFirestore;
    static deleteQueryBatch = deleteQueryBatch;

    [RPC_MARSHAL]() {
        return {
            ...this,
            _id: this._id,
            _ref: this._ref?.path
        };
    }

    degradeForFireStore(): this {
        return JSON.parse(JSON.stringify(this, function (k, v) {
            if (k === '') {
                return v;
            }
            if (typeof v === 'object' && v && (typeof v.degradeForFireStore === 'function')) {
                return v.degradeForFireStore();
            }

            return v;
        }));
    }
}