lnoir commented on issue #221:
URL: https://github.com/apache/incubator-age/issues/221#issuecomment-1137748799
I haven't got to the bottom of this yet, I'm still hoping someone can help
me out. Below is a Node.js script that will output the results described above,
to maybe save time for anyone who wants to look at this. I'll report back if I
get any further.
```
const { types, Pool } = require('pg');
const { setAGETypes } = require('./age-driver/dist');
class Testing {
graphName;
config;
pool;
constructor() {
this.config = {
host: 'localhost',
port: 5432,
user: 'postgres',
pass: 'mypassword',
};
this.graphName = 'nesteddemo';
}
createPool() {
return new Promise((resolve) => {
this.pool = new Pool({
user: this.config.user,
host: this.config.host,
password: this.config.pass,
port: this.config.port,
});
this.pool.on('connect', client => {
console.log('connected')
this.init(client);
resolve();
});
this.pool.connect();
});
}
async init(client) {
console.log('@graphName', this.graphName)
try {
await client.query(`
CREATE EXTENSION IF NOT EXISTS age;
LOAD 'age';
SET search_path = ag_catalog, "$user", public;
`);
await setAGETypes(client, types);
await client.query(`SELECT create_graph('${this.graphName}')`);
}
catch(err) {
console.error(err.message);
}
}
formatQuery(query, params, varsKeys) {
let q = query;
const agPlaceholder = params ? ', $1' : '';
const types = (varsKeys || []).map(vk => `${vk?.var || vk}
ag_catalog.agtype`).join(', ');
const fulLStatement = `
SELECT * FROM ag_catalog.cypher('${this.graphName}', $$
${q}
$$${agPlaceholder}) as (${types || 'n ag_catalog.agtype'});
`;
return fulLStatement;
}
async query(query, params, varsKeys) {
try {
const statement = this.formatQuery(query, params, varsKeys, '$');
if (!this.pool) await this.createPool();
const res = await this.pool.query(statement, params ? [params] : null);
return {res};
}
catch(err) {
console.error(err);
return {err, res: null};
}
}
async run() {
const query = `
MATCH (b:Building)<-[:lives_in]-(u:User)-[:likes]->(c:Comestible)
WITH {name: u.name, favourites: COLLECT(c)} AS person, u, b
RETURN b.name, COLLECT([person]) AS people
`;
const {res} = await this.query(query, null, ['name','people']);
res.rows.forEach(r => {
r.people = r.people.map(p => Object.fromEntries(p))
});
console.log(JSON.stringify(res.rows, null, 2))
}
}
const testing = new Testing();
testing.run();
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]