corebasic 1.0.68 → 1.0.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -7,6 +7,7 @@ import * as Utils from './libs/utils.js'
7
7
  import * as Session from './libs/session.js'
8
8
  import * as Auth from './libs/auth.js'
9
9
  import * as Features from './libs/features.js'
10
+ import * as Messaging from './libs/messaging.js'
10
11
 
11
12
  export {
12
13
  Elabase,
@@ -15,5 +16,6 @@ export {
15
16
  Utils,
16
17
  Session,
17
18
  Auth,
18
- Features
19
+ Features,
20
+ Messaging
19
21
  }
package/libs/features.js CHANGED
@@ -1,6 +1,7 @@
1
- import * as Elabase from './elabase.js'
1
+ import * as Dip from './dip.js'
2
2
  import * as Kafka from './kafka.js'
3
3
  import * as Utils from './utils.js'
4
+ import * as Messaging from './messaging.js'
4
5
 
5
6
 
6
7
  let features = {}
@@ -69,8 +70,9 @@ export const start = async (app, url, file) => {
69
70
  topic = topic.replace('Features.','')
70
71
  while (true) {
71
72
  try {
73
+ await Dip.update(`users.txns.${message.user}`, {_id: message.txn}, { $setOnInsert: { user: message.user, feature: message.feature, date: message.date, created: message.date, updated: message.date, status: "Queued" } }, {upsert: true}) // Can always update the status later
72
74
  await features[message.feature].handler(topic, message)
73
- await Elabase.insert("Features.txns", {_id: message.txn, status: "Processed"})
75
+ await Dip.insert("Features.txns", {_id: message.txn, status: "Processed"})
74
76
  break
75
77
  } catch {
76
78
  console.warn(`Feature: ${message.feature}, error in executing handler during Kafka.receive(topic: ${topic})`)
@@ -83,7 +85,7 @@ export const start = async (app, url, file) => {
83
85
  // Transaction status api
84
86
  app.get('/transactions/:id', async (req, res) => {
85
87
  try {
86
- let items = await Elabase.query("Features.txns", {_id: req.params.id})
88
+ let items = await Dip.query("Features.txns", {_id: req.params.id})
87
89
  if (items.length)
88
90
  res.json({ data: { ...items[0], txn: items._id } })
89
91
  else
@@ -92,6 +94,15 @@ export const start = async (app, url, file) => {
92
94
  res.status(500).json({ message: "Failed to retreive data" })
93
95
  }
94
96
  })
97
+ app.get('/users/transactions', async (req, res) => {
98
+ try {
99
+ res.json({ data: await Dip.query(`users.txns.${message.user}`, {}, { $orderby: { date: -1 } }) })
100
+ } catch {
101
+ res.status(500).json({ message: "Failed to retreive data" })
102
+ }
103
+ })
104
+
105
+ try { await Messaging.start(app) } catch { console.log('Messaging failed to start. Maybe missing redis') }
95
106
  }
96
107
 
97
108
  const commandAction = async (req, res, topic) => {
package/libs/messaging.js CHANGED
@@ -3,12 +3,8 @@ import axios from 'axios'
3
3
  import { createClient } from 'redis';
4
4
 
5
5
  // url: 'redis://alice:foobared@localhost:6380'
6
- const consumer = createClient({ url: 'redis://localhost:6380' });
7
- await consumer.connect();
8
- const publisher = createClient({ url: 'redis://localhost:6380' });
9
- await publisher.connect();
6
+ let consumer, publisher
10
7
 
11
- consumer.on('error', err => console.log('Redis Client Error', err));
12
8
 
13
9
 
14
10
  let users_pool = {
@@ -27,7 +23,14 @@ async function connect({user,req,res}) {
27
23
  });
28
24
  }
29
25
 
30
- export function start(app) {
26
+ export async function start(app) {
27
+ consumer = createClient({ url: 'redis://localhost:6380' });
28
+ await consumer.connect();
29
+ publisher = createClient({ url: 'redis://localhost:6380' });
30
+ await publisher.connect();
31
+
32
+ consumer.on('error', err => console.log('Redis Client Error', err));
33
+
31
34
  app.get('/messages/:user', async (req, res) => {
32
35
  await connect({user: req.params.user, req, res})
33
36
  req.on("close", async () => await disconnect(req.params.user)) // request closed unexpectedly
@@ -37,7 +40,8 @@ export function start(app) {
37
40
 
38
41
 
39
42
  export async function publish(channel, message) {
40
- await publisher.publish(channel, typeof message === "object" ? JSON.stringify(message) : message);
43
+ if (publisher)
44
+ await publisher.publish(channel, typeof message === "object" ? JSON.stringify(message) : message);
41
45
  }
42
46
 
43
47
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "corebasic",
3
3
  "type": "module",
4
- "version": "1.0.68",
4
+ "version": "1.0.70",
5
5
  "description": "",
6
6
  "main": "index.js",
7
7
  "scripts": {