|
| 1 | +import common from "../common/base.mjs"; |
| 2 | + |
| 3 | +export default { |
| 4 | + ...common, |
| 5 | + key: "firebase_admin_sdk-get-document", |
| 6 | + name: "Get Document", |
| 7 | + description: "Retrieves a document from a Firestore collection. [See the documentation](https://firebase.google.com/docs/firestore/query-data/get-data#get_a_document)", |
| 8 | + version: "0.0.1", |
| 9 | + type: "action", |
| 10 | + props: { |
| 11 | + ...common.props, |
| 12 | + collection: { |
| 13 | + propDefinition: [ |
| 14 | + common.props.firebase, |
| 15 | + "collection", |
| 16 | + (c) => ({ |
| 17 | + region: c.databaseRegion, |
| 18 | + }), |
| 19 | + ], |
| 20 | + }, |
| 21 | + document: { |
| 22 | + propDefinition: [ |
| 23 | + common.props.firebase, |
| 24 | + "document", |
| 25 | + (c) => ({ |
| 26 | + collection: c.collection, |
| 27 | + region: c.databaseRegion, |
| 28 | + }), |
| 29 | + ], |
| 30 | + }, |
| 31 | + }, |
| 32 | + methods: { |
| 33 | + ...common.methods, |
| 34 | + async getResponse() { |
| 35 | + const docRef = this.firebase.getDocument({ |
| 36 | + collection: this.collection, |
| 37 | + document: this.document, |
| 38 | + }); |
| 39 | + const snapshot = await docRef.get(); |
| 40 | + |
| 41 | + if (!snapshot.exists) { |
| 42 | + return { |
| 43 | + exists: false, |
| 44 | + id: this.document, |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + return { |
| 49 | + exists: true, |
| 50 | + id: snapshot.id, |
| 51 | + data: snapshot.data(), |
| 52 | + createTime: snapshot.createTime?.toDate(), |
| 53 | + updateTime: snapshot.updateTime?.toDate(), |
| 54 | + readTime: snapshot.readTime?.toDate(), |
| 55 | + ref: snapshot.ref.path, |
| 56 | + }; |
| 57 | + }, |
| 58 | + emitSummary($, response) { |
| 59 | + if (!response.exists) { |
| 60 | + $.export("$summary", `Document ${this.document} not found in collection ${this.collection}`); |
| 61 | + return; |
| 62 | + } |
| 63 | + $.export("$summary", `Successfully retrieved document ${this.document} from collection ${this.collection}`); |
| 64 | + }, |
| 65 | + }, |
| 66 | +}; |
0 commit comments