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

Added plugin for Amazon DocumentDB #9532

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions marketplace/plugins/documentdb/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
lib/*.d.*
lib/*.js
lib/*.js.map
dist/*
4 changes: 4 additions & 0 deletions marketplace/plugins/documentdb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

# Documentdb

Documentation on: https://docs.tooljet.com/docs/data-sources/documentdb
7 changes: 7 additions & 0 deletions marketplace/plugins/documentdb/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const documentdb = require('../lib');

describe('documentdb', () => {
it.todo('needs tests');
});
14 changes: 14 additions & 0 deletions marketplace/plugins/documentdb/lib/icon.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
207 changes: 207 additions & 0 deletions marketplace/plugins/documentdb/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
import { QueryError, QueryResult, QueryService, ConnectionTestResult } from '@tooljet-marketplace/common';
import { SourceOptions, QueryOptions } from './types';
const { MongoClient } = require('mongodb');
const JSON5 = require('json5');
import { EJSON } from 'bson';

const tlsCAFilePath = process.env.TLS_CA_FILE_PATH || 'global-bundle.pem';

export default class Documentdb implements QueryService {
async run(sourceOptions: SourceOptions, queryOptions: QueryOptions, dataSourceId: string): Promise<QueryResult> {
const { db, close } = await this.getConnection(sourceOptions);
let result = {};
const operation = queryOptions.operation;

try {
switch (operation) {
case 'list_collections':
result = await db.listCollections().toArray();
break;
case 'insert_one':
result = await db
.collection(queryOptions.collection)
.insertOne(this.parseEJSON(queryOptions.document), this.parseEJSON(queryOptions.options));
break;
case 'insert_many':
result = await db
.collection(queryOptions.collection)
.insertMany(this.parseEJSON(queryOptions.documents), this.parseEJSON(queryOptions.options));
break;
case 'find_one':
result = await db
.collection(queryOptions.collection)
.findOne(this.parseEJSON(queryOptions.filter), this.parseEJSON(queryOptions.options));
break;
case 'find_many':
result = await db
.collection(queryOptions.collection)
.find(this.parseEJSON(queryOptions.filter), this.parseEJSON(queryOptions.options))
.toArray();
break;
case 'count_total':
result = await db
.collection(queryOptions.collection)
.estimatedDocumentCount(this.parseEJSON(queryOptions.options));
result = { count: result };
break;
case 'count':
result = await db
.collection(queryOptions.collection)
.countDocuments(this.parseEJSON(queryOptions.filter), this.parseEJSON(queryOptions.options));
result = { count: result };
break;
case 'distinct':
result = await db
.collection(queryOptions.collection)
.distinct(queryOptions.field, this.parseEJSON(queryOptions.filter), this.parseEJSON(queryOptions.options));
break;
case 'update_one':
result = await db
.collection(queryOptions.collection)
.updateOne(
this.parseEJSON(queryOptions.filter),
this.parseEJSON(queryOptions.update),
this.parseEJSON(queryOptions.options)
);
break;
case 'update_many':
result = await db
.collection(queryOptions.collection)
.updateMany(
this.parseEJSON(queryOptions.filter),
this.parseEJSON(queryOptions.update),
this.parseEJSON(queryOptions.options)
);
break;
case 'replace_one':
result = await db
.collection(queryOptions.collection)
.replaceOne(
this.parseEJSON(queryOptions.filter),
this.parseEJSON(queryOptions.replacement),
this.parseEJSON(queryOptions.options)
);
break;
case 'find_one_replace':
result = await db
.collection(queryOptions.collection)
.findOneAndReplace(
this.parseEJSON(queryOptions.filter),
this.parseEJSON(queryOptions.replacement),
this.parseEJSON(queryOptions.options)
);
break;
case 'find_one_update':
result = await db
.collection(queryOptions.collection)
.findOneAndUpdate(
this.parseEJSON(queryOptions.filter),
this.parseEJSON(queryOptions.update),
this.parseEJSON(queryOptions.options)
);
break;
case 'find_one_delete':
result = await db
.collection(queryOptions.collection)
.findOneAndDelete(this.parseEJSON(queryOptions.filter), this.parseEJSON(queryOptions.options));
break;
case 'delete_one':
result = await db
.collection(queryOptions.collection)
.deleteOne(this.parseEJSON(queryOptions.filter), this.parseEJSON(queryOptions.options));
break;
case 'delete_many':
result = await db
.collection(queryOptions.collection)
.deleteMany(this.parseEJSON(queryOptions.filter), this.parseEJSON(queryOptions.options));
break;
case 'bulk_write':
result = await db
.collection(queryOptions.collection)
.bulkWrite(this.parseEJSON(queryOptions.operations), this.parseEJSON(queryOptions.options));
break;
case 'aggregate':
result = await db
.collection(queryOptions.collection)
.aggregate(this.parseEJSON(queryOptions.pipeline), this.parseEJSON(queryOptions.options))
.toArray();
break;
}
} catch (err) {
console.log(err);
throw new QueryError('Query could not be completed', err.message, {});
} finally {
await close();
}

return {
status: 'ok',
data: result,
};
}

parseEJSON(maybeEJSON?: string): any {
if (!maybeEJSON) return {};
return EJSON.parse(JSON.stringify(JSON5.parse(maybeEJSON)));
}

async testConnection(sourceOptions: SourceOptions): Promise<ConnectionTestResult> {
const { db, close } = await this.getConnection(sourceOptions);
try {
await db.listCollections().toArray();
return {
status: 'ok',
};
} catch (error) {
console.error('Failed to test connection:', error);
return {
status: 'failed',
message: `Error testing connection: ${error.message}`,
};
} finally {
await close();
}
}

async getConnection(sourceOptions: SourceOptions): Promise<any> {
let db = null,
client;
const connectionType = sourceOptions['connection_type'];

if (connectionType === 'manual') {
const database = sourceOptions.database;
const host = sourceOptions.host;
const port = sourceOptions.port;
const username = encodeURIComponent(sourceOptions.username);
const password = encodeURIComponent(sourceOptions.password);

const uri = `mongodb://${username}:${password}@${host}:${port}`

client = new MongoClient(uri, {
tlsCAFile: tlsCAFilePath,
});
await client.connect();

db = client.db(database);
} else {
const connectionString = sourceOptions['connection_string'];

const password = connectionString.match(/(?<=:\/\/)(.*):(.*)@/)[2];

const encodedPassword = encodeURIComponent(password);

const encodedConnectionString = connectionString.replace(password, encodedPassword);

client = new MongoClient(encodedConnectionString, { tlsCAFile: tlsCAFilePath });
await client.connect();
db = client.db();
}

return {
db,
close: async () => {
await client?.close?.();
},
};
}
}
72 changes: 72 additions & 0 deletions marketplace/plugins/documentdb/lib/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

{
"$schema": "https://raw.githubusercontent.com/ToolJet/ToolJet/develop/plugins/schemas/manifest.schema.json",
"title": "Documentdb datasource",
"description": "A schema defining Documentdb datasource",
"type": "database",
"source": {
"name": "Documentdb",
"kind": "documentdb",
"exposedVariables": {
"isLoading": false,
"data": {},
"rawData": {}
},
"options": {}
},
"defaults": {},
"properties": {
"connection_type": {
"label": "",
"key": "connection_type",
"type": "dropdown-component-flip",
"description": "Single select dropdown for connection_type",
"list": [
{
"name": "Manual connection",
"value": "manual"
},
{
"name": "Connect using connection string",
"value": "string"
}
]
},
"manual": {
"host": {
"label": "Host",
"key": "host",
"type": "text",
"description": "Enter host"
},
"port": {
"label": "Port",
"key": "port",
"type": "text",
"description": "Enter port"
},
"username": {
"label": "Username",
"key": "username",
"type": "text",
"description": "Enter username"
},
"password": {
"label": "Password",
"key": "password",
"type": "password",
"description": "Enter password"
}
},
"string": {
"connection_string": {
"label": "Connection string",
"key": "connection_string",
"type": "text",
"encrypted": true,
"description": "mongodb://tooljet:<password>@docdb.cfki0io0krwn.ap-south-1.docdb.amazonaws.com/?tls=true&retryWrites=false"
}
}
},
"required": []
}