Skip to content

Commit 56f1bf4

Browse files
committed
feat: add methods to retrieve all tables and columns for MongoDB and PostgreSQL connectors
1 parent db95c72 commit 56f1bf4

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

adminforth/dataConnectors/mongo.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,39 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
4646
[AdminForthSortDirections.asc]: 1,
4747
[AdminForthSortDirections.desc]: -1,
4848
};
49+
async getAllTables(): Promise<Array<string>>{
50+
const db = this.client.db();
51+
52+
const collections = await db.listCollections().toArray();
53+
54+
return collections.map(col => col.name);
55+
}
4956

57+
async getAllColumnsInTable(collectionName: string): Promise<Array<string>> {
58+
59+
const sampleDocs = await this.client.db().collection(collectionName).find({}).limit(100).toArray();
60+
61+
const fieldSet = new Set<string>();
62+
63+
function flattenObject(obj: any, prefix = '') {
64+
Object.entries(obj).forEach(([key, value]) => {
65+
const fullKey = prefix ? `${prefix}.${key}` : key;
66+
if (value && typeof value === 'object' && !Array.isArray(value) && !(value instanceof Date)) {
67+
flattenObject(value, fullKey);
68+
} else {
69+
fieldSet.add(fullKey);
70+
}
71+
});
72+
}
73+
74+
for (const doc of sampleDocs) {
75+
flattenObject(doc);
76+
}
77+
78+
return Array.from(fieldSet);
79+
}
80+
81+
5082
async discoverFields(resource) {
5183
return resource.columns.filter((col) => !col.virtual).reduce((acc, col) => {
5284
if (!col.type) {

adminforth/dataConnectors/postgres.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ class PostgresConnector extends AdminForthBaseConnector implements IAdminForthDa
4545
[AdminForthSortDirections.desc]: 'DESC',
4646
};
4747

48+
async getAllTables(): Promise<Array<string>> {
49+
const res = await this.client.query(`
50+
SELECT table_name
51+
FROM information_schema.tables
52+
WHERE table_schema = 'public' AND table_type = 'BASE TABLE';
53+
`);
54+
return res.rows.map(row => row.table_name);
55+
}
56+
57+
async getAllColumnsInTable(tableName: string): Promise<Array<string>> {
58+
const res = await this.client.query(`
59+
SELECT column_name
60+
FROM information_schema.columns
61+
WHERE table_name = $1 AND table_schema = 'public';
62+
`, [tableName]);
63+
return res.rows.map(row => row.column_name);
64+
}
65+
4866
async discoverFields(resource) {
4967

5068
const tableName = resource.table;

adminforth/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ class AdminForth implements IAdminForth {
391391
// console.log(`Connector ${dataSourceId} does not have getAllTables method`);
392392
results[dataSourceId] = [];
393393
}
394+
await connector.close();
394395
})
395396
);
396397

@@ -419,6 +420,7 @@ class AdminForth implements IAdminForth {
419420
} else {
420421
results[dataSourceId] = [];
421422
}
423+
await connector.close();
422424
})
423425
);
424426

0 commit comments

Comments
 (0)