Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to connect azure cosmos db #648

Open
Gunasekaranv opened this issue May 15, 2020 · 3 comments
Open

How to connect azure cosmos db #648

Gunasekaranv opened this issue May 15, 2020 · 3 comments

Comments

@Gunasekaranv
Copy link

@Gunasekaranv Gunasekaranv commented May 15, 2020

Hello, i am working on a project where i am using azure cosmos db for database queries, however i am not able to connect cubejs with azure cosmos db as datasource. Is there any community connector for azure cosmos db as it is not supported offically, if so i will really appreciate any help and if there isn't, are there any plans in future to provide a support?

@paveltiunov
Copy link
Contributor

@paveltiunov paveltiunov commented May 15, 2020

Hey @Gunasekaranv ! Thanks for posting this! I'm not aware of any community drivers for cosmo db and we don't have it on our road map as of right now. However we'd love this contribution! Here's more info on it: https://github.com/cube-js/cube.js/blob/master/CONTRIBUTING.md#implementing-driver

@Gunasekaranv
Copy link
Author

@Gunasekaranv Gunasekaranv commented May 19, 2020

Hi @paveltiunov , i written Cosmos driver
`const CosmosClient = require("@azure/cosmos").CosmosClient;
const BaseDriver = require('@cubejs-backend/query-orchestrator/driver/BaseDriver');
const SqlString = require('sqlstring');
//const { promisify } = require('util');

const applyParams = (query, params) => {
return SqlString.format(query, params);
};
class CosmosDriver extends BaseDriver {
constructor(config) {
super();

this.config = {
  endpoint: process.env.CUBEJS_DB_HOST,
  key: process.env.CUBEJS_DB_KEY,
  databaseId: process.env.CUBEJS_DB_NAME,
  containerId: process.env.CUBEJS_DB_CONTAINERID,
  ...config
};

this.client = new CosmosClient(this.config);
this.database = this.client.database(this.config.databaseId);
this.container = this.database.container(this.config.containerId);

}

testConnection() {
return this.query('SELECT *from c', []);
}
sleep(ms) {
return new Promise((resolve) => {
setTimeout(() => resolve(), ms);
});
}

query(query, values) {
try {
console.log(query);
const queryString = applyParams(
query,
(values || []).map(s => (typeof s === 'string' ? {
toSqlString: () => SqlString.escape(s).replace(/\\([_%])/g, '\$1').replace(/\'/g, '''')
} : s))
);
while (true) {
console.log(queryString);
const queryExecution = this.container.items
.query(queryString)
.fetchAll();
const status = queryExecution.QueryExecution.Status.State;
if (status === 'FAILED') {
throw new Error(queryExecution.QueryExecution.Status.StateChangeReason);
}
if (status === 'CANCELLED') {
throw new Error('Query has been cancelled');
}
if (
status === 'SUCCEEDED'
) {
let results = this.container.items
.query(queryString)
.fetchAll();
const [header, ...tableRows] = results.ResultSet.Rows;
allRows.push(...(allRows.length ? results.ResultSet.Rows : tableRows));
if (!columnInfo) {
columnInfo = results.ResultSet.ResultSetMetadata.ColumnInfo
}
return allRows.map(r =>
columnInfo
.map((c, i) => ({ [c.Name]: r.Data[i].VarCharValue }))
.reduce((a, b) => ({ ...a, ...b }), {})
);
}
this.sleep(500);
}
} catch (err) {
console.log(err.message);
}
}
}

module.exports = CosmosDriver;
`
but i am getting below error so please help me out,
Error:

Error while loading DB schema
TypeError: Cannot read property 'then' of undefined at CosmosDriver.tablesSchema (E:\Developements\cubejs\voca\node_modules@cubejs-backend\cosmosdb-driver\node_modules@cubejs-backend\query-orchestrator\driver\BaseDriver.js:74:29) at E:\Developements\cubejs\voca\node_modules@cubejs-backend\server-core\core\DevServer.js:51:41 at processTicksAndRejections (internal/process/task_queues.js:97:5) at async E:\Developements\cubejs\voca\node_modules@cubejs-backend\server-core\core\DevServer.js:29:9

@lawrence999
Copy link

@lawrence999 lawrence999 commented Jul 17, 2020

@Gunasekaranv Were you able to connect the CosmosDB with CubeJS?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.