ToolJet/plugins/packages/cosmosdb/lib/index.ts
Ganesh Kumar 0eb2440023
Release: Marketplace sprint 12 (#13207)
* Feature: Prometheus plugin (#13161)

* prometheus plugin

* added existing props

* Host and database can be dynamically configured in query builder for PostgreSQL and MySQL data sources (#13163)

* Fix: Postgresql datasource tries to connect via ssl even when ssl toggle is off (#13167)

* The ability to provide a partition key for deleting items in CosmosDB datasource has been enabled (#13166)

* Feature: Ability to configure the database name in Redis datasource (#13165)

* Fix: Avoid setting Content-Type header for requests without body and configure different host for all environments in OpenAPI [PRE-RELEASE] (#13230)

* Send content-type only with body in request

* Persist OpenAPI parameters per operation only

* Configure different host

* Add disable styles to the select input

* Feat: New fields 'client id' and 'client secret' have been introduced in the Slack datasource configuration page in pre-release (#13162)

* Update slack frontend

* Update slack backend to handle custom creds

* Add backfill migrations

* Dynamically change dropdown according to versions

* Change migration file name

* Correctly access scope in chat:write logic

---------

Co-authored-by: Akshay Sasidharan <akshaysasidharan93@gmail.com>
Co-authored-by: Parth <108089718+parthy007@users.noreply.github.com>
Co-authored-by: Akshay <akshaysasidrn@gmail.com>
2025-07-11 12:15:39 +05:30

81 lines
2.7 KiB
TypeScript

import { QueryError, QueryResult, QueryService, ConnectionTestResult } from '@tooljet-plugins/common';
import { SourceOptions, QueryOptions } from './types';
import { CosmosClient } from '@azure/cosmos';
import { deleteItem, getItem, insertItems, listContainers, listDatabases, queryDatabase } from './operations';
export default class Cosmosdb implements QueryService {
async run(sourceOptions: SourceOptions, queryOptions: QueryOptions, dataSourceId: string): Promise<QueryResult> {
const { endpoint, key } = sourceOptions;
const operation = queryOptions.operation;
const client = new CosmosClient({ endpoint, key });
let result = {};
try {
switch (operation) {
case 'list_databases':
result = await listDatabases(client);
break;
case 'list_containers':
result = await listContainers(client, queryOptions.database);
break;
case 'insert_items':
result = await insertItems(client, queryOptions.database, queryOptions.container, queryOptions.items);
break;
case 'read_item':
result = await getItem(client, queryOptions.database, queryOptions.container, queryOptions.itemId);
break;
case 'delete_item':
result = await deleteItem(
client,
queryOptions.database,
queryOptions.container,
queryOptions.itemId,
queryOptions?.partitionKey
);
break;
case 'query_database':
result = await queryDatabase(client, queryOptions.database, queryOptions.container, queryOptions.query);
break;
default:
break;
}
} catch (error) {
console.log(error);
throw new QueryError('Query could not be completed', error.message, {});
}
return {
status: 'ok',
data: result,
};
}
async testConnection(sourceOptions: SourceOptions): Promise<ConnectionTestResult> {
const { endpoint, key } = sourceOptions;
const genericClient = new CosmosClient({ endpoint, key });
await genericClient.getDatabaseAccount();
return {
status: 'ok',
};
}
async deleteDatabase(sourceOptions: SourceOptions, databaseId: string) {
const { endpoint, key } = sourceOptions;
const genericClient = new CosmosClient({ endpoint, key });
//check if database exits
const database = await (await genericClient.databases.readAll().fetchAll()).resources;
if (database.find((db) => db.id === databaseId)) {
await genericClient.database(databaseId).delete();
return {
status: 'ok',
};
}
return {
status: 'Database with id ' + databaseId + ' does not exist',
};
}
}