Orientdb query approach to get properties of vertices and edges - node.js

Im a newbie to orient db .I have a vertex user which has properties adress,name and another vertex Images with properties imagename,date.Both are connected by and edge postedby.Now I want to write a query to select all images posted by a user with all the properties of both the vertices.How can I write the query to get this.I use orientjs in my project

Try this:
var OrientDB = require('orientjs');
var db = OrientDB({
host: 'localhost',
port: 2424,
});
var db = db.use({
name: '<db name>',
username: '<username>',
password: '<pwd>'
});
db.query('select address, name, out("postedby").imagename as imagename, out("postedby").date as date from User where name = "<insert a name>" unwind imagename,date')
.then(function (response) {
console.log(response);
});
db.close();
Hope it helps
Regards

Related

postegreSQL inserting query using node js

I have this code on index.js in which i connect to a postgreSQL database and execute a query but it keeps telling me: column "Jhonny" does not exist.
Here is my code:
const {Client} = require ('pg');
const client = new Client({
host: 'localhost',
user: 'postgres',
database: 'React',
password: '01021997',
port: 5432,
})
client.connect();
client.query('INSERT INTO movies (moviename, review) VALUES ("Jhonny", "no
review");', (err, res)=>{
if(!err){
console.log('insert done');
}else{
console.log(err.message);
}
client.end;
})
the query should be in the following way:
client.query(`INSERT INTO "movies" ("moviename", "review") VALUES ('Jhonny',
'no review');`
the column names and the table name should be between ("") and the values should be between ('').

MongoDB and Mongoose Abnormal behavior?

I apologize for the maybe trivial question. I am accessing mongo db to obtain information on a user, verify the password, and save some data in Redis if the validation was successful.
But I'm misbehaving (probably my mistake).
My simple backend is built in express 4.17.2, node 17.2.0, and redis4.0.1.
Server-side MongoDB on cloud MongoDB Atlas (Version 4.4.10) and RedisLab (Redis in cloud version 6.2.3).
Here are some code snippets:
app.post('/identity/login', (req, res) => {
let userHash = '';
let result = false;
const user = req.body;
userHash = crud.login(user.username);
userHash.then(h => {
result = bcrypt.compareSync(user.password, h.password);
console.log('All object ',h);
console.log('Access to roles array',h.roles);
let value = {name:h.name, surname:h.surname, roles:h.roles, username:h.username};
console.log('value', value);
client.set(h.password, JSON.stringify(value), { EX: 60, NX: true });
res.send(result);
});
});
Making the POST call to that endpoint, I get the following (unexpected) results, highlighted by the logs:
All object {
_id: new ObjectId("61d1d38d0f4bac12d8f504ee"),
username: 'jdoe',
password: '$2a$14$5GULZRmiN0DXQXMaLwQiEO9S/OlBll5U4ZwkBn2NYpju0VRDFUqVO',
name: 'John',
surname: 'Doe',
roles: [ 'Admin' ]
}
Access to roles array undefined
value { name: 'John', surname: 'Doe', roles: undefined, username: 'jdoe' }
As you can see, if I log h.roles I get that the array is not defined, while logging the whole object the array property 'roles' is correctly valued. So obviously, when I go to build the object to save in Redis (as h.roles), the array is not created.
Why? Could you please help me and say me what I am doing wrong? My mistake, a mongoose problem (see 6.4.1)?
Below, I show the code to access MongoDB:
const login = async (username) => {
connectDB();
let dbPwd;
const User = mongoose.model('Users', UserSchema);
return await User.findOne({ username: username }, 'password name surname roles username').exec();
}
Thanks so much!

role does not exist and syntax error for inserting query in NodeJs into postgresql

I've created a table with the name of user in postgres which contains id, name, family columns (id is auto-increment). The query which I want to insert into the postgres likes the following:
var config = {
user: 'username',
database: 'databaseName',
password: 'password',
host: 'localhost',
port: 5432,
max: 10,
idleTimeoutMillis: 30000
};
const pool = new postgre.Pool(config);
let query = "INSERT INTO user(name, family) VALUES ('name', 'family')";
pool.query(query, [], function (err, result) {
if (err) {
return console.error("error running query", err);
}
console.log("done!");
});
But, I've got the following error:
Syntax error near user
Also, I have tried different approach to send the request liked the following:
const connectionString = "http://localhost:5432/databaseName?username=username&password=password";
const client = new postgre.Client(connectionString);
client.connect();
const query = client.query("INSERT INTO user(id, name, family) VALUES ('name', 'family')");
query.then(x => { console.log("done!")});
query.catch(x => { console.log(x)});
But I've got the following error:
role \"UserName\" does not exist
Also, the UserName (name of the server) in the body of the error is not the same as the username of the database.
I've checked the all possible fallacies such as the correctness of the username and password.
Therefore, the question is what is the problem?
All of these problems come from the table name. As mentioned in this post, the name of user is a reserved name in postgres. Therefore, these errors will be passed if change the name of the user table such as users.
user is a reserved word in PostgreSQL that refers to the currently logged in user. For that reason, in order to be able to use a table with name user, the name must always be inside double quotes - "user".

Mapnik renders map with datasource to empty vector tile

sorry if this a novice question. I am using node-mapnik to generate vector tiles from a database query, but the vector tiles always return empty, whereas I am pretty sure postgis actually returns data (tested the query separately on psql, with the same bounding box).
Is there something obvious missing in the following code ? I have not added a style to the map layer rendered to vector tile, because I thought that vector tiles are not actually "rendered" on the server, is this the issue ? Thanks for your answer,
pgMapRouter.get('/:z/:x/:y.pbf', function(req, res) {
var bbox = mercator.bbox(
+req.params.x,
+req.params.y,
+req.params.z,
false,
'4326'
);
var map = new mapnik.Map(256, 256);
map.extent = bbox;
var layer = new mapnik.Layer('fixture_layer');
layer.datasource = new mapnik.Datasource({
type: 'postgis',
dbname: 'citydb',
table: 'test2',
user: 'postgres',
password: 'postgres',
host: 'localhost',
port:5432,
geometry_field: 'wkb_geometry',
srid:4326,
extent: bbox
});
map.add_layer(layer);
var vtile = new mapnik.VectorTile(+req.params.z, +req.params.x, +req.params.y);
map.render(vtile, function (err, vtile) {
if (err) console.log(err);
res.setHeader('Content-Encoding', 'deflate');
res.setHeader('Content-Type', 'application/x-protobuf');
zlib.deflate(vtile.getData(), function(err, pbf) {
console.log(mapnik.VectorTile.info(pbf))
res.send(pbf);
});
});
});

Recommended way to change database in existing connecction with node-mysql

I am trying to change database in an existing connection in node.
The connection may or may not have database name in createConnection call.
Here's the code:
var mysql = require('mysql');
connection = mysql.createConnection( {
host : 'localhost',
user : 'me',
password : 'secret',
port : 3306,
/* database : 'test' // optional */
});
I am using node-mysql lib.
Currently I'm closing the connection and re-connecting.
Is there any better way to do that? Similar to mysql_select_db in php?
You can change some of the connection parameters* with the changeUser method:
connection.changeUser({database : 'my_database'}, function(err) {
if (err) throw err;
});
*List of these parameters:
user: The name of the new user (defaults to the previous one).
password: The password of the new user (defaults to the previous one).
charset: The new charset (defaults to the previous one).
database: The new database (defaults to the previous one).
The only way I have found is to use the 'USE' command
in your example it would look like this
let database = 'my_database';
connection.query(`USE ${database}`, (error, results, fields) => {
...
});
after that all queries using this connection would be sent to the new database
Or just use database name in query
conn.query('SELECT H FROM other.GeoDB WHERE H = \'r1r0f\' LIMIT 1 ', function (err, res, f)
conn.query('SELECT * FROM data.users WHERE U = \'123\' LIMIT 1 ', function (err, res, f)

Resources