1
0

db.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Import modules. */
  2. import moment from 'moment'
  3. import initFusions from '../../handlers/initFusions.ts'
  4. console.info('Initializing Ephemeral Database Manager...')
  5. /* Initialize locals. */
  6. let response
  7. let status
  8. /* Initialize DB handlers. */
  9. let fusionsDb
  10. let profilesDb
  11. // let sessionsDb
  12. let systemDb
  13. /**
  14. * CashFusions Datastore
  15. *
  16. * An collection of fusion details.
  17. */
  18. fusionsDb = {}
  19. /**
  20. * Profiles Datastore
  21. *
  22. * An collection of profiles (public keys).
  23. */
  24. profilesDb = {}
  25. /**
  26. * Sessions Datastore
  27. *
  28. * An collection of sessions.
  29. */
  30. // sessionsDb = {}
  31. /**
  32. * System Datastore
  33. *
  34. * A system management object.
  35. */
  36. systemDb = {}
  37. /**
  38. * Put (Data)
  39. */
  40. const put = async (_dbname, _key, _value) => {
  41. try {
  42. switch(_dbname) {
  43. case 'fusions':
  44. fusionsDb[_key] = _value
  45. break
  46. case 'profiles':
  47. profilesDb[_key] = _value
  48. break
  49. // case 'sessions':
  50. // sessionsDb[_key] = _value
  51. // break
  52. case 'system':
  53. systemDb[_key] = _value
  54. break
  55. }
  56. } catch (err) {
  57. console.error(err)
  58. return err
  59. }
  60. /* Return status. */
  61. return `[ ${_dbname} ] data saved successfully!`
  62. }
  63. /**
  64. * Initialize
  65. */
  66. const init = async () => {
  67. /* Validate (initial) data. */
  68. if (Object.keys(systemDb).length === 0) {
  69. status = {
  70. createdAt: moment().unix(),
  71. updatedAt: moment().unix(),
  72. },
  73. response = await put('system', 'status', status)
  74. .catch(err => console.error(err))
  75. // console.log('RESPONSE (system)', response)
  76. } else {
  77. /* Set status. */
  78. status = systemDb.status
  79. /* Update timestamp. */
  80. status.updatedAt = moment().unix()
  81. /* Save data to store. */
  82. response = await put('system', 'status', status)
  83. .catch(err => console.error(err))
  84. // console.log('RESPONSE (status)', response)
  85. }
  86. // console.log('SYSTEM (DB) STATUS', systemDb.status)
  87. }
  88. init()
  89. initFusions(fusionsDb)
  90. export default defineEventHandler((event) => {
  91. // console.log('Ephemeral Db Request: ' + getRequestURL(event))
  92. event.context.Db = {
  93. /* Getters */
  94. fusions: fusionsDb,
  95. profiles: profilesDb,
  96. // sessions: sessionsDb,
  97. system: systemDb,
  98. /* Setters */
  99. put,
  100. }
  101. })