Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions adminforth/dataConnectors/baseConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ import { AdminForthFilterOperators, AdminForthSortDirections } from "../types/Co


export default class AdminForthBaseConnector implements IAdminForthDataSourceConnectorBase {

client: any;

get db() {
console.warn('db is deprecated, use client instead');
return this.client;
}

setupClient(url: string): Promise<void> {
throw new Error('Method not implemented.');
}

getPrimaryKey(resource: AdminForthResource): string {
for (const col of resource.dataSourceColumns) {
if (col.primaryKey) {
Expand Down
51 changes: 22 additions & 29 deletions adminforth/dataConnectors/clickhouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,30 @@ import { createClient } from '@clickhouse/client'
import { AdminForthDataTypes, AdminForthFilterOperators, AdminForthSortDirections } from '../types/Common.js';

class ClickhouseConnector extends AdminForthBaseConnector implements IAdminForthDataSourceConnector {

client: any;

dbName: string;
url: string;

/**
* url: http[s]://[username:password@]hostname:port[/database][?param1=value1&param2=value2]
* @param param0
*/
constructor({ url }: { url: string }) {
super();
this.dbName = new URL(url).pathname.replace('/', '');
this.url = url;
// create connection here
this.client = createClient({
url: url.replace('clickhouse://', 'http://'),
clickhouse_settings: {
// Allows to insert serialized JS Dates (such as '2023-12-06T10:54:48.000Z')
date_time_input_format: 'best_effort',

// Recommended for cluster usage to avoid situations where a query processing error occurred after the response code,
// and HTTP headers were already sent to the client.
// See https://clickhouse.com/docs/en/interfaces/http/#response-buffering
wait_end_of_query: 1,
},
// log:{
// level: ClickHouseLogLevel.TRACE,
// }
});

}

async setupClient(url): Promise<void> {
this.dbName = new URL(url).pathname.replace('/', '');
this.url = url;
// create connection here
this.client = createClient({
url: url.replace('clickhouse://', 'http://'),
clickhouse_settings: {
// Allows to insert serialized JS Dates (such as '2023-12-06T10:54:48.000Z')
date_time_input_format: 'best_effort',

// Recommended for cluster usage to avoid situations where a query processing error occurred after the response code,
// and HTTP headers were already sent to the client.
// See https://clickhouse.com/docs/en/interfaces/http/#response-buffering
wait_end_of_query: 1,
},
// log:{
// level: ClickHouseLogLevel.TRACE,
// }
});
}

async discoverFields(resource: AdminForthResource): Promise<{[key: string]: AdminForthResourceColumn}> {
const tableName = resource.table;
Expand Down
28 changes: 13 additions & 15 deletions adminforth/dataConnectors/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ const escapeRegex = (value) => {
};

class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataSourceConnector {
db: MongoClient

constructor({ url }: { url: string }) {
super();
this.db = new MongoClient(url);
async setupClient(url): Promise<void> {
this.client = new MongoClient(url);
(async () => {
try {
await this.db.connect();
this.db.on('error', (err) => {
await this.client.connect();
this.client.on('error', (err) => {
console.log('Mongo error: ', err.message)
});
});
console.log('Connected to Mongo');
} catch (e) {
console.error('ERROR: Failed to connect to Mongo', e);
throw new Error(`Failed to connect to Mongo: ${e}`);
}
})();
}
Expand Down Expand Up @@ -133,7 +131,7 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
// const columns = resource.dataSourceColumns.filter(c=> !c.virtual).map((col) => col.name).join(', ');
const tableName = resource.table;

const collection = this.db.db().collection(tableName);
const collection = this.client.db().collection(tableName);
const query = await this.genQuery({ filters });

const sortArray: any[] = sort.map((s) => {
Expand All @@ -154,7 +152,7 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
filters: { field: string, operator: AdminForthFilterOperators, value: any }[]
}): Promise<number> {

const collection = this.db.db().collection(resource.table);
const collection = this.client.db().collection(resource.table);
const query = {};
for (const filter of filters) {
query[filter.field] = this.OperatorsMap[filter.operator](filter.value);
Expand All @@ -164,7 +162,7 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS

async getMinMaxForColumnsWithOriginalTypes({ resource, columns }) {
const tableName = resource.table;
const collection = this.db.db().collection(tableName);
const collection = this.client.db().collection(tableName);
const result = {};
for (const column of columns) {
result[column] = await collection
Expand All @@ -178,7 +176,7 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS

async createRecordOriginalValues({ resource, record }) {
const tableName = resource.table;
const collection = this.db.db().collection(tableName);
const collection = this.client.db().collection(tableName);
const columns = Object.keys(record);
const newRecord = {};
for (const colName of columns) {
Expand All @@ -188,19 +186,19 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
}

async updateRecordOriginalValues({ resource, recordId, newValues }) {
const collection = this.db.db().collection(resource.table);
const collection = this.client.db().collection(resource.table);
await collection.updateOne({ [this.getPrimaryKey(resource)]: recordId }, { $set: newValues });
}

async deleteRecord({ resource, recordId }): Promise<boolean> {
const primaryKey = this.getPrimaryKey(resource);
const collection = this.db.db().collection(resource.table);
const collection = this.client.db().collection(resource.table);
const res = await collection.deleteOne({ [primaryKey]: recordId });
return res.deletedCount > 0;
}

async close() {
await this.db.close()
await this.client.close()
}
}

Expand Down
Loading