Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 6x 6x 1x 1x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | export {}
const { Firestore, FieldValue, Filter } = require('@google-cloud/firestore')
const crypto = require('crypto')
const GoogleCloudAdapter = require('../GoogleCloudAdapter')
const { ErrorUtils } = require('../../../utils/error')
const $error = new ErrorUtils()
type TFirestorePropsObject = {
projectId?: string | boolean
databaseId?: string | boolean
}
type TFirestoreProps = TFirestorePropsObject | string
type TFirestoreCollection = {
id: string
}
type TFirestoreDocData = {}
type TFirestoreDocRef = {
parent: {}
path: string
}
type TFirestoreDoc = {
id: string
data: () => TFirestoreDocData[]
ref: TFirestoreDocRef
}
type TFirestoreGetDocProps = {
collection: string
id: string
subcollection?: string
subid?: string
}
type WhereClause = [string, string, string | number | boolean | any[]]
type TFirestoreGetDocsProps = {
collection: string
id?: string
subcollection?: string
subid?: string
where?: WhereClause[]
orderBy?: string
limit?: number
startAfter?: {}
}
type TFirestoreGetGroupDocsProps = {
collection: string
where?: WhereClause[]
orderBy?: string
limit?: number
startAfter?: {}
}
type TFirestoreAddDocProps = {
collection: string
id?: string
subcollection?: string
subid?: string
data: TFirestoreDocData
}
type TFirestoreUpdateDocProps = {
collection: string
id: string
subcollection?: string
subid?: string
data: TFirestoreDocData
}
type TFirestoreSetDocProps = {
docRef: TFirestoreDocRef
data: TFirestoreDocData
}
type TFirestoreDeleteDocProps = {
collection: string
id: string
subcollection?: string
subid?: string
}
type TFirestoreDeleteDocsProps = {
collection: string
id?: string
subcollection?: string
where?: WhereClause[]
}
type TFirestoreDeleteDocFieldProps = {
collection: string
id: string
subcollection?: string
subid?: string
field: string
}
type TFirestoreDocsResponse = {
count: number
startAfter?: {}
docs: Record<string, unknown>[]
}
export class FirestoreAdapter extends GoogleCloudAdapter {
constructor(props?: TFirestoreProps) {
super()
this.firestore
if (props && typeof props === 'string') {
this.firestore = new Firestore({ projectId: props })
} else if (props && typeof props === 'object') {
this.firestore = new Firestore(props)
} else {
this.firestore = new Firestore()
}
}
/**
* List collections
* @param {string} collection
* @param {string} id
* @returns {Promise<string[]>}
* @memberof FirestoreAdapter
* @example
* const collections = await firestore.listCollections()
* const collections = await firestore.listCollections('users', '1234567890')
*/
async listCollections(collection?: string, id?: string) {
let result
try {
if (collection && id) {
result = await this.firestore
.collection(collection)
.doc(id)
.listCollections()
} else {
result = await this.firestore.listCollections()
}
return result.map((c: TFirestoreCollection) => c.id)
} catch (error) {
throw $error.errorHandler({ error })
}
}
/**
* Get document by id
* @param {object} props
* @param {string} props.collection
* @param {string} props.id
* @param {string} props.subcollection
* @param {string} props.subid
* @returns {Promise<string[]>}
* @memberof FirestoreAdapter
* @example
* const documents = await firestore.getDoc('users', '1234567890')
* const documents = await firestore.getDoc('users', '1234567890', 'posts', '0987654321')
*/
async getDoc(props: TFirestoreGetDocProps) {
try {
let ref = this.firestore.collection(props.collection).doc(props.id)
if (props.subcollection && props.subid) {
ref = ref.collection(props.subcollection).doc(props.subid)
}
const result = await ref.get()
if (result.exists) {
return { id: props.id, ...result.data() }
}
return false
} catch (error) {
throw $error.errorHandler({ error })
}
}
async getDocRef(props: TFirestoreGetDocProps) {
return this.firestore.collection(props.collection).doc(props.id)
}
/**
* Get documents by collection
* @typedef {Array<string>} OrderBy
* @param {Object} props
* @param {string} props.collection
* @param {string} props.id
* @param {string} props.subcollection
* @param {string} props.subid
* @param {string[]} props.where
* @param {Array<OrderBy>} props.orderBy
* @param {number} props.limit
* @param {Object} props.startAfter
* @returns {Promise<string[]>}
* @memberof FirestoreAdapter
* @example
* const documents = await firestore.getDocs({
* collection: 'users'
* })
* const documents = await firestore.getDocs({
* collection: 'users',
* id: '1234567890',
* subcollection: 'posts'
* })
* const documents = await firestore.getDocs({
* collection: 'users',
* where: [['gender', '==', 'F']],
* orderBy: [['sortOrder', 'asc']['name', 'desc']],
* limit: 10,
* })
*/
async getDocs(props: TFirestoreGetDocsProps) {
try {
let ref = this.firestore.collection(props.collection)
if (props.id && props.subcollection) {
ref = ref.doc(props.id).collection(props.subcollection)
}
if (props.where) {
for (let w of props.where) {
if (w[0] === 'or' && Array.isArray(w[1])) {
let filterRef = Filter.or(
...w[1].map((f: string[]) => {
return Filter.where(f[0], f[1], f[2])
})
)
ref = ref.where(filterRef)
} else {
ref = ref.where(w[0], w[1], w[2])
}
}
}
// Require an orderBy for pagination
let orderBy
if (props.orderBy) {
orderBy = props.orderBy
for (let o of orderBy) {
ref = ref.orderBy(o[0], o[1] || 'asc')
}
}
// Require a limit for pagination
const limit = props.limit ?? 1000
ref = ref.limit(limit)
// Add startAfter for pagination when provided
if (props.startAfter) {
ref = ref.startAfter(props.startAfter)
}
const snapshot = await ref.get()
const docs = snapshot.docs.map((doc: TFirestoreDoc) => {
return {
id: doc.id,
...doc.data(),
}
})
let result: TFirestoreDocsResponse = {
count: docs.length,
docs,
}
if (docs.length === limit) {
result.startAfter = snapshot.docs[snapshot.docs.length - 1]
}
return result
} catch (error) {
throw $error.errorHandler({ error })
}
}
async getGroupDocs(props: TFirestoreGetGroupDocsProps) {
try {
let ref = this.firestore.collectionGroup(props.collection)
if (props.where) {
for (let w of props.where) {
if (w[0] === 'or' && Array.isArray(w[1])) {
let filterRef = Filter.or(
...w[1].map((f: string[]) => {
return Filter.where(f[0], f[1], f[2])
})
)
ref = ref.where(filterRef)
} else {
ref = ref.where(w[0], w[1], w[2])
}
}
}
// Require an orderBy for pagination
const orderBy = props.orderBy ?? 'created_at'
ref = ref.orderBy(orderBy)
// Require a limit for pagination
const limit = props.limit ?? 1000
ref = ref.limit(limit)
// Add startAfter for pagination when provided
if (props.startAfter) {
ref = ref.startAfter(props.startAfter)
}
const snapshot = await ref.get()
const docs = snapshot.docs.map((doc: TFirestoreDoc) => {
const path = doc.ref.path.split('/')
return {
id: doc.id,
parent_collection: path[0],
parent_doc: path[1],
...doc.data(),
}
})
let result: TFirestoreDocsResponse = {
count: docs.length,
docs,
}
if (docs.length === limit) {
result.startAfter = snapshot.docs[snapshot.docs.length - 1]
}
return result
} catch (error) {
throw $error.errorHandler({ error })
}
}
async getDocCount(props: TFirestoreGetDocsProps) {
try {
let ref = this.firestore.collection(props.collection)
if (props.id && props.subcollection) {
ref = ref.doc(props.id).collection(props.subcollection)
}
if (props.where) {
for (let w of props.where) {
if (w[0] === 'or' && Array.isArray(w[1])) {
let filterRef = Filter.or(
...w[1].map((f: string[]) => {
return Filter.where(f[0], f[1], f[2])
})
)
ref = ref.where(filterRef)
} else {
ref = ref.where(w[0], w[1], w[2])
}
}
}
const snapshot = await ref.count().get()
const count = snapshot.data().count
return count
} catch (error) {
throw $error.errorHandler({ error })
}
}
async addDoc(props: TFirestoreAddDocProps) {
try {
let id
let ref = this.firestore.collection(props.collection)
if (props.id && props.subcollection) {
id = props.subid ? props.subid : crypto.randomUUID()
ref = ref.doc(props.id).collection(props.subcollection).doc(id)
} else {
id = props.id ? props.id : crypto.randomUUID()
ref = ref.doc(id)
}
await ref.set({
created_at: new Date().valueOf(),
id,
...props.data,
})
return id
} catch (error) {
throw $error.errorHandler({ error })
}
}
async replaceDoc(props: TFirestoreUpdateDocProps) {
try {
let ref = this.firestore.collection(props.collection).doc(props.id)
if (props.subcollection && props.subid) {
ref = ref.collection(props.subcollection).doc(props.subid)
}
await ref.set({
updated_at: new Date().valueOf(),
id: props.id ?? crypto.randomUUID(),
...props.data,
})
return true
} catch (error) {
throw $error.errorHandler({ error })
}
}
async setDoc(props: TFirestoreSetDocProps) {
try {
let ref: any = props.docRef
await ref.set({
updated_at: new Date().valueOf(),
...props.data,
}, { merge: true })
return true
} catch (error) {
throw $error.errorHandler({ error })
}
}
async updateDoc(props: TFirestoreUpdateDocProps) {
try {
let ref = this.firestore.collection(props.collection).doc(props.id)
if (props.subcollection && props.subid) {
ref = ref.collection(props.subcollection).doc(props.subid)
}
await ref.update({
updated_at: new Date().valueOf(),
...props.data,
})
return true
} catch (error) {
throw $error.errorHandler({ error })
}
}
async deleteDoc(props: TFirestoreDeleteDocProps) {
try {
let ref = this.firestore.collection(props.collection).doc(props.id)
if (props.subcollection && props.subid) {
ref = ref.collection(props.subcollection).doc(props.subid)
}
await ref.delete()
return true
} catch (error) {
throw $error.errorHandler({ error })
}
}
async deleteDocs(props: TFirestoreDeleteDocsProps) {
try {
const exists = await this.getDocs(props)
if (!exists.docs.length) return true
const docs = exists.docs
const deleteBatch = this.firestore.batch()
docs.forEach((doc) => {
if (props.subcollection) {
deleteBatch.delete(
this.firestore
.collection(props.collection)
.doc(doc.id)
.collection(props.subcollection)
.doc(doc.id)
)
} else {
deleteBatch.delete(
this.firestore.collection(props.collection).doc(doc.id)
)
}
})
await deleteBatch.commit()
return true
} catch (error) {
throw $error.errorHandler({ error })
}
}
async deleteDocField(props: TFirestoreDeleteDocFieldProps) {
try {
let ref = this.firestore.collection(props.collection).doc(props.id)
if (props.subcollection && props.subid) {
ref = ref.collection(props.subcollection).doc(props.subid)
}
await ref.update({
updated_at: new Date().valueOf(),
[props.field]: FieldValue.delete(),
})
return true
} catch (error) {
throw $error.errorHandler({ error })
}
}
}
|