mirror of
https://github.com/ToolJet/ToolJet
synced 2026-04-24 06:57:40 +00:00
* init
* cosmos db icon
* manifest file
* adds azure package
* test connection
* operations: list databases
* operation: read item, insert items with a lookup function
* operation: delete item
* operation: query database using SQL-like syntax
* adds unit tests
* adds unit test: query db
* clean up
* docs: cosmosdb
* docs: cleanup
* typo
* updated the image(docs)
* updated inserting operation: field: placeholder
* item id for delete operation
* Revert "item id for delete operation"
This reverts commit bc2d1b87e4.
* item id for delete operation
75 lines
2.6 KiB
TypeScript
75 lines
2.6 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);
|
|
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',
|
|
};
|
|
}
|
|
}
|