Advertisement
Guest User

Untitled

a guest
May 24th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const Firestore = require('@google-cloud/firestore');
  4.  
  5. const PROJECTID = 'gifted-course-161615';
  6. const COLLECTION_NAME = 'gifted-course-161615';
  7.  
  8. const firestore = new Firestore({
  9. projectId: PROJECTID,
  10. timestampsInSnapshots: true,
  11. keyFilename: process.env.NODE_ENV !== 'production' ? './gifted-course-161615-ba326e832fff.json' : undefined
  12.  
  13. });
  14.  
  15. /**
  16. * Retrieve or Store a method into Firestore
  17. *
  18. * Responds to any HTTP request.
  19. *
  20. * GET = retrieve
  21. * POST = store (no update)
  22. *
  23. * success: returns the document content in JSON format & status=200
  24. * else: returns an error:<string> & status=404
  25. *
  26. * @param {!express:Request} req HTTP request context.
  27. * @param {!express:Response} res HTTP response context.
  28. */
  29. exports.testproxy = (req, res) => {
  30. if (req.method === 'POST') {
  31. // store/insert a new document
  32. const data = (req.body) || {};
  33. const ttl = Number.parseInt(data.ttl);
  34. const ciphertext = (data.ciphertext || '')
  35. .replace(/[^a-zA-Z0-9\-_!.,; ']*/g, '')
  36. .trim();
  37. const created = new Date().getTime();
  38.  
  39. // .add() will automatically assign an id
  40. return firestore.collection(COLLECTION_NAME).add({
  41. created,
  42. ttl,
  43. ciphertext
  44. }).then(doc => {
  45. console.info('stored new doc id#', doc.id);
  46. return res.status(200).send(doc);
  47. }).catch(err => {
  48. console.error(err);
  49. return res.status(404).send({
  50. error: 'unable to store',
  51. err
  52. });
  53. });
  54. }
  55.  
  56. // everything below this requires an id
  57. if (!(req.query && req.query.id)) {
  58. return res.status(404).send({
  59. error: 'No Id'
  60. });
  61. }
  62. const id = req.query.id;
  63. if (!(id && id.length)) {
  64. return res.status(404).send({
  65. error: 'Empty Id'
  66. });
  67. }
  68.  
  69. if (req.method === 'DELETE') {
  70. // delete an existing document by id
  71. return firestore.collection(COLLECTION_NAME)
  72. .doc(id)
  73. .delete()
  74. .then(() => {
  75. return res.status(200).send({ status: 'ok' });
  76. }).catch(err => {
  77. console.error(err);
  78. return res.status(404).send({
  79. error: 'unable to delete',
  80. err
  81. });
  82. });
  83. }
  84.  
  85. // read/retrieve an existing document by id
  86. return firestore.collection(COLLECTION_NAME)
  87. .doc(id)
  88. .get()
  89. .then(doc => {
  90. if (!(doc && doc.exists)) {
  91. return res.status(404).send({
  92. error: 'Unable to find the document'
  93. });
  94. }
  95. const data = doc.data();
  96. if (!data) {
  97. return res.status(404).send({
  98. error: 'Found document is empty'
  99. });
  100. }
  101. return res.status(200).send(data);
  102. }).catch(err => {
  103. console.error(err);
  104. return res.status(404).send({
  105. error: 'Unable to retrieve the document',
  106. err
  107. });
  108. });
  109. };
  110.  
  111. /*
  112.  
  113. const Firestore = require('@google-cloud/firestore');
  114.  
  115. const PROJECTID = 'gifted-course-161615';
  116. const COLLECTION_NAME = 'gifted-course-161615';
  117.  
  118. const firestore = new Firestore({
  119. projectId: PROJECTID,
  120. keyFilename: process.env.NODE_ENV !== 'production' ? './gifted-course-161615-ba326e832fff.json' : undefined
  121. });
  122.  
  123. const main = async () => {
  124. const collection = COLLECTION_NAME
  125. await firestore
  126. .collection(collection)
  127. //.where('timestamp', '>', '1551657600')
  128. //.where('timestamp', '<', '1551744000')
  129. .get()
  130. .then(querySnapshot => {
  131. querySnapshot.forEach(doc => {
  132. const data = doc.data()
  133. console.log(data)
  134. })
  135. })
  136. }
  137.  
  138. main()
  139.  
  140. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement