Query MariaDB from NodeJS without Caching from MariaDB - javascript

I try to do a query from MariaDB with my NodeJS without using the MariaDB caching. I use Sequelize to query the database.
Everything works fine but I recognized that there is definitly some issue with caching. The select query I use always reports a long time that a new user is not available. But another MariaDB insert generated the data before.
To check the user I use:
isUserExists: async function ( inUser, inChat, inLevel ) {
const iskunde = await kunden.findAll({
where: {
kunde: inUser,
chat: inChat,
level: inLevel,
created_at: {
[Op.gte]: Sequelize.literal("DATE_SUB(NOW(), INTERVAL 15 MINUTE)"),
},
},
raw: true,
});
//var currentdate = new Date();
//console.log(currentdate);
//console.log(iskunde);
if(Array.isArray(iskunde) && iskunde.length) {
return true;
}else{
return false;
}
},
This becomes a big problem with my app. So I try to find a way to get the "realtime" data from database. From what I read there is a SELECT possible with SQL_NO_CACHE but I do not find any information how to manage this with Sequelize. Any idea?

Related

Using JOINS with Supabase when no FK is present

I am running into issues with my querying when using supabase. I have this query which I can use successfully in DataGrip
SELECT
sja.audience_id,
sja.segment,
relation,
sjac.constraint_id,
sjac.constraint_value,
sjac.targeting
FROM signal_journey_audience_constraint_relations
JOIN signal_journey_audiences sja ON signal_journey_audience_constraint_relations.audience_id = sja.audience_id
JOIN signal_journey_audience_constraints sjac ON signal_journey_audience_constraint_relations.constraint_id = sjac.constraint_id
But when using supbase I can an error
async function getTableData() {
const { data, error } = await supabase.from(
'signal_journey_audience_constraint_relations'
).select(`
audience_id:signal_journey_audiences(audience_id),
segment:signal_journey_audiences(segment),
relation,
constraint_id:signal_journey_audience_constraints(constraint_id),
constraint_value:signal_journey_audience_constraints(constraint_value),
targeting:signal_journey_audience_constraints(targeting)
),
`);
if (data) {
console.log(data);
setTableData(data);
} else {
console.log(error);
}
}
Error is
hint: Verify that 'signal_journey_audience_constraint_re…ship was created, try reloading the schema cache.
message: Could not find a relationship between 'signal_journey_audience_constraint_relations' and 'signal_journey_audience_constraints' in the schema cache
I am getting confused to why I can run the query in DataGrip but not in Supbase. I'm 90% sure I just have some syntax issue but can't figure it out.

Pipeline test fails because of time difference of local and pipeline environments

I have problem surrounding the difference between pipeline execution and my local environment.
I'm intercepting a request using the following command:
cy.intercept(
{
method: "GET",
pathname: env.path,
query: {
dateFrom: "2020-12-31T22:00:00.000Z",
dateTo: "2022-01-01T21:59:59.000Z",
},
},
(req) => {
expect(req.url).to.include(
"&dateFrom=2020-12-31T22%3A00%3A00.000Z",
"Date From included"
);
expect(req.url).to.include(
"&dateTo=2022-01-01T21%3A59%3A59.000Z",
"Date to included"
);
}
).as("filterByDates");
Executing it on my local environment is fine, but when I run it in the pipeline there is a problem, because server time is UTC by default, the test always fails, because sent time is not as expected.
Now I'm thinking how to approach this, because the time input is not by me, but by the plugin "datePicker", which always inputs the machine time (server/environment), so the question is what is a good approach towards this issue? (i'm using day.js)
Do I convert each time input to UTC ?
Do I intercept the request and make it UTC -2 for the request ?
Or should I just ignore everything after "T" including hours/minutes/seconds, which I'm highly against.
I'll really be glad if i get responses, thanks in advance.
Yes you have to convert expected values to TZ in common with test environment.
But the test also has to know what environment it runs in.
var utc = require('dayjs/plugin/utc')
dayjs.extend(utc)
...
cy.intercept(
...
(req) => {
let expectedDateFrom = "2020-12-31T22:00:00.000Z";
const isCI = Cypress.env('CI');
if (isCI) {
expectedDateFrom = dayjs().utc().format()
}
const encodedDateFrom = encodeURI(expectedDateFrom)
expect(req.url).to.include(`&dateFrom=${encodedDateFrom}`,"Date From included");
Since you have two dates, maybe a conversion function?
const toEnvFormat = (expected) => {
const isCI = Cypress.env('CI');
if (isCI) {
expected = dayjs().utc().format()
}
return expected;
}
cy.intercept(
...
(req) => {
const expectedDateFrom = toEnvFormat("2020-12-31T22:00:00.000Z");
const encodedDateFrom = encodeURI(expectedDateFrom)
expect(req.url).to.include(`&dateFrom=${encodedDateFrom}`,"Date From included");
Ref dayjs UTC
I've found solution to just add the time zone to the package.json in the root folder as so:
"scripts": {
"start": "TZ=Europe/Sofia npx cypress open",
"run": "TZ=Europe/Sofia npx cypress run --browser chrome"
},

How to add my subscription data to update my previous query

I have one query and one subscription, what I am trying to do is add my data to previous query so that it shows the full list.
I have one query which is returning me list of students and I am rendering that on UI like below
function Test(props) {
const { loading, data: dta } = useQuery(GETSTUDENTS);
const { data: d } = useSubscription(GETSUBSTUDENTS, {
onSubscriptionData: ({ subscriptionData: { data } }) => {
let fname = data.getSubStudent.fname;
let lname = data.getSubStudent.lname;
dta.getStudents.push({ fname, lname });
},
});
return (
<div className="">
{dta &&
dta.getStudents.map((li) => {
<div>
<p>{li.fname}</p>
<p>{li.lname}</p>
</div>;
})}
</div>
);
}
export default Test;
But the main issue is the above one is not updating the cache so when I change the routes and come bqack again it takes the previous data only.
So What I wnat to know na what is the best way to do this, I have check subscribeToMore also but did not get idea How to implement that and how it works with hooks.
I am getting some data from subscription and on that basis I want to change some other part so can I use refetchQueries I did not found any good tutorial which uses hooks (react-apollo-hooks) using qraphql
First, you can just use the pooling option of the useQuery instead of subscription,
I suggest you check it.
From Apollo docs:
"In the majority of cases, your client should not use subscriptions to
stay up to date with your backend. Instead, you should poll
intermittently with queries, or re-execute queries on demand when a
user performs a relevant action."
Apollo subscription
If you still want to use the subscription I think you should use the subscribeToMore and to update your cache policy inside the apollo cache file:
const cache = new InMemoryCache({
typePolicies: {
Agenda: {
fields: {
tasks: {
merge(existing = [], incoming: any[]) {
return [...existing, ...incoming];
},
},
},
},
},
});
You can read more about it here: merge cahce
And check that video: youtube apollo cache

Sailsjs fetch data from two different mysql databases

I'm building a rest api using sails js v 1.x
I need to connect two mysql database so I have defined them in config/datastores.js file life this:
module.exports.datastores = {
default: {
adapter: require('sails-mysql'),
url: 'mysql://root:12345#192.168.0.5:3306/test',
},
mysqldb: {
adapter: require('sails-mysql'),
url: 'mysql://root:12345#192.168.0.5:3306/test2',
},
};
In my controller, I have this function which needs to get data by joining the database test as well as test2
module.exports = {
index: function (req, res) {
User.getDatastore().sendNativeQuery("SELECT * from test.users u INNER JOIN test2.users t ON u.id=t.id limit 10",function(err, rawResult) {
res.send(rawResult);
})
},
};
But this gives me an error :
{
"code": "ER_NO_SUCH_TABLE",
"errno": 1146,
"sqlMessage": "Table 'test.users' doesn't exist",
}
Also I have a blank User model and execution of raw sql queries work perfecty when the query is like select * from users (it uses the default database i.e test)
How do I achieve this kind of query by connecting more than one MySQL database in sails js?
I was able to solve the issue this way :
my config.datastore file :
module.exports.datastores = {
default: {
adapter: require('sails-mysql'),
url: 'mysql://root:12345#192.168.0.5:3306/',
},
};
My controller :
module.exports = {
index: function (req, res) {
User.getDatastore().sendNativeQuery("SELECT * from test.users u INNER JOIN test2.users t ON u.id=t.id limit 10",function(err, rawResult) {
res.send(rawResult);
})
},
};
Was having trouble figuring it out at first as there is no such example showing a join on multiple tables from different databases when using raw MySQL queries.
The only change I did is I just removed the database name from the default connection URL. Now I'm able to access all the databases on this particular server and also able to join multiple databases.

Setting a server variable to data held in a mongo collection Meteor.js

For my application, I have to make an HTTP.call to get a session ID. I need to store this session ID into a mongo collection, which I am doing like this.
if (Meteor.isServer) {
sessionDB = new Mongo.Collection("sessionID");
HTTP.call( 'GET', 'http://mycalltosomeapi.svc/json/whatever', {
}, function( error, response ) {
if ( error ) {
console.log(error);
} else {
sessionDB.insert({
sessionId: response.data.session_id,
date_inserted: new Date()
});
}
}
how do I grab the most recent entry of my mongo stored 'sessionId' and put that into a variable so I can make other calls? I tried doing it like this but had no luck.
var sessionId = sessionDB.findOne({}, {sessionId:1}).sort({"date_inserted":-1}).limit(1);
Been researching for a couple hours now, can't find anything. Thanks in advance
sort and limit are options to find and findOne. Give this a try:
var options = {
fields: {sessionId: 1},
sort: {date_inserted: -1}
};
var sessionId = sessionDB.findOne({}, options);

Categories

Resources