Hi All,
I have a process to be automated . I have a table created in HubDB and it will have few values like code , name fields and date and time field. I cannot use hubDB ID for specific reason. The code column value will need to be sequential. The code column will have a value like ALPHA1900 (one sample value) . My flow is as follows
Look up for the last value for the code column.Increment the value of this code column and then insert the new record into hubDB. In the end publish the table. Am following the promise pattern here i.e. in thennable invoking subsequent calls like insertion and thennable of insertion doing the publish.
I published the workflow what i can see when bulk enrollments are happening there seems to be race condition and some times two records are getting the same code column value . How can i avoid this ?
Thanks,
Prat
Hi, @PAlapati
Thanks for the fantastic question. I’d love to know if there is a native solution, but I don’t see anything like row-level locking or support for transactions.
Here’s a thought (I’ve been upskilling Node and Express lately):
- create single ‘code generator’ function
- all processes must call to get a new code
- you’d need to make sure your function guarantees only one code is generated at a time and that each code is unique
My basic example is based on a personal example I made, but it is not intended to be a production version.
const express = require('express');
const app = express();
const port = 3000;
let lastCode = 1900;
app.get('/generateCode', (req, res) => {
lastCode++;
res.send('ALPHA' + lastCode);
});
app.listen(port, () => {
console.log(`Code generator app listening at http://localhost:${port}`)
});
I’d love to hear about any workarounds you’ve tried out.
Have fun building! — Jaycee
Hi Jaycee,
I had this thought. I am worried that even here there might be race condition. See for example if 7 enrollments are trying to invoke this there might be chances again they might be seeing the same number ? Also as mentioned int the original post last code is being calculated from one of the column in hubdb itself. https://api.hubapi.com/cms/v3/hubdb/tables/tablename/rows/draft?sort=-column_name&limit=1&properties=column_name.
Thanks,
Prathima