Skip to content

Commit 2611a76

Browse files
authored
Merge pull request #88 from devforth/mysql-datasource
feat: add support for mysql datasource
2 parents 8f2b489 + 1f2c7e8 commit 2611a76

File tree

14 files changed

+732
-89
lines changed

14 files changed

+732
-89
lines changed

adminforth/dataConnectors/baseConnector.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ import { AdminForthFilterOperators, AdminForthSortDirections } from "../types/Co
1111

1212

1313
export default class AdminForthBaseConnector implements IAdminForthDataSourceConnectorBase {
14+
15+
client: any;
16+
17+
get db() {
18+
console.warn('db is deprecated, use client instead');
19+
return this.client;
20+
}
21+
22+
setupClient(url: string): Promise<void> {
23+
throw new Error('Method not implemented.');
24+
}
25+
1426
getPrimaryKey(resource: AdminForthResource): string {
1527
for (const col of resource.dataSourceColumns) {
1628
if (col.primaryKey) {

adminforth/dataConnectors/clickhouse.ts

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,30 @@ import { createClient } from '@clickhouse/client'
66
import { AdminForthDataTypes, AdminForthFilterOperators, AdminForthSortDirections } from '../types/Common.js';
77

88
class ClickhouseConnector extends AdminForthBaseConnector implements IAdminForthDataSourceConnector {
9-
10-
client: any;
9+
1110
dbName: string;
1211
url: string;
13-
14-
/**
15-
* url: http[s]://[username:password@]hostname:port[/database][?param1=value1&param2=value2]
16-
* @param param0
17-
*/
18-
constructor({ url }: { url: string }) {
19-
super();
20-
this.dbName = new URL(url).pathname.replace('/', '');
21-
this.url = url;
22-
// create connection here
23-
this.client = createClient({
24-
url: url.replace('clickhouse://', 'http://'),
25-
clickhouse_settings: {
26-
// Allows to insert serialized JS Dates (such as '2023-12-06T10:54:48.000Z')
27-
date_time_input_format: 'best_effort',
28-
29-
// Recommended for cluster usage to avoid situations where a query processing error occurred after the response code,
30-
// and HTTP headers were already sent to the client.
31-
// See https://clickhouse.com/docs/en/interfaces/http/#response-buffering
32-
wait_end_of_query: 1,
33-
},
34-
// log:{
35-
// level: ClickHouseLogLevel.TRACE,
36-
// }
37-
});
38-
39-
}
12+
13+
async setupClient(url): Promise<void> {
14+
this.dbName = new URL(url).pathname.replace('/', '');
15+
this.url = url;
16+
// create connection here
17+
this.client = createClient({
18+
url: url.replace('clickhouse://', 'http://'),
19+
clickhouse_settings: {
20+
// Allows to insert serialized JS Dates (such as '2023-12-06T10:54:48.000Z')
21+
date_time_input_format: 'best_effort',
22+
23+
// Recommended for cluster usage to avoid situations where a query processing error occurred after the response code,
24+
// and HTTP headers were already sent to the client.
25+
// See https://clickhouse.com/docs/en/interfaces/http/#response-buffering
26+
wait_end_of_query: 1,
27+
},
28+
// log:{
29+
// level: ClickHouseLogLevel.TRACE,
30+
// }
31+
});
32+
}
4033

4134
async discoverFields(resource: AdminForthResource): Promise<{[key: string]: AdminForthResourceColumn}> {
4235
const tableName = resource.table;

adminforth/dataConnectors/mongo.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,18 @@ const escapeRegex = (value) => {
1010
};
1111

1212
class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataSourceConnector {
13-
db: MongoClient
1413

15-
constructor({ url }: { url: string }) {
16-
super();
17-
this.db = new MongoClient(url);
14+
async setupClient(url): Promise<void> {
15+
this.client = new MongoClient(url);
1816
(async () => {
1917
try {
20-
await this.db.connect();
21-
this.db.on('error', (err) => {
18+
await this.client.connect();
19+
this.client.on('error', (err) => {
2220
console.log('Mongo error: ', err.message)
23-
});
21+
});
2422
console.log('Connected to Mongo');
2523
} catch (e) {
26-
console.error('ERROR: Failed to connect to Mongo', e);
24+
throw new Error(`Failed to connect to Mongo: ${e}`);
2725
}
2826
})();
2927
}
@@ -133,7 +131,7 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
133131
// const columns = resource.dataSourceColumns.filter(c=> !c.virtual).map((col) => col.name).join(', ');
134132
const tableName = resource.table;
135133

136-
const collection = this.db.db().collection(tableName);
134+
const collection = this.client.db().collection(tableName);
137135
const query = await this.genQuery({ filters });
138136

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

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

165163
async getMinMaxForColumnsWithOriginalTypes({ resource, columns }) {
166164
const tableName = resource.table;
167-
const collection = this.db.db().collection(tableName);
165+
const collection = this.client.db().collection(tableName);
168166
const result = {};
169167
for (const column of columns) {
170168
result[column] = await collection
@@ -178,7 +176,7 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
178176

179177
async createRecordOriginalValues({ resource, record }) {
180178
const tableName = resource.table;
181-
const collection = this.db.db().collection(tableName);
179+
const collection = this.client.db().collection(tableName);
182180
const columns = Object.keys(record);
183181
const newRecord = {};
184182
for (const colName of columns) {
@@ -188,19 +186,19 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
188186
}
189187

190188
async updateRecordOriginalValues({ resource, recordId, newValues }) {
191-
const collection = this.db.db().collection(resource.table);
189+
const collection = this.client.db().collection(resource.table);
192190
await collection.updateOne({ [this.getPrimaryKey(resource)]: recordId }, { $set: newValues });
193191
}
194192

195193
async deleteRecord({ resource, recordId }): Promise<boolean> {
196194
const primaryKey = this.getPrimaryKey(resource);
197-
const collection = this.db.db().collection(resource.table);
195+
const collection = this.client.db().collection(resource.table);
198196
const res = await collection.deleteOne({ [primaryKey]: recordId });
199197
return res.deletedCount > 0;
200198
}
201199

202200
async close() {
203-
await this.db.close()
201+
await this.client.close()
204202
}
205203
}
206204

0 commit comments

Comments
 (0)