Skip to content

Commit 84bd25a

Browse files
authored
Merge pull request #87 from devforth/next
Next
2 parents edce85f + f8eb589 commit 84bd25a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1178
-250
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ cd adminforth
6161
npm ci
6262
npm run build
6363
64-
# create link to built "adminforth" package in local system
65-
npm link
6664
6765
# this will install all official plugins and link adminforth package, if plugin installed it will git pull and npm ci
6866
npm run install-plugins

adminforth/commands/createApp/templates/users.ts.hbs renamed to adminforth/commands/createApp/templates/adminuser.ts.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ async function canModifyUsers({ adminUser }: { adminUser: AdminUser }): Promise<
88
export default {
99
dataSource: 'maindb',
1010
table: 'adminuser',
11-
resourceId: 'users',
11+
resourceId: 'adminuser',
1212
label: 'Users',
1313
recordLabel: (r) => `👤 ${r.email}`,
1414
options: {
@@ -44,7 +44,7 @@ export default {
4444
{
4545
name: 'created_at',
4646
type: AdminForthDataTypes.DATETIME,
47-
showIn: [{
47+
showIn: {
4848
edit: false,
4949
create: false,
5050
},

adminforth/commands/createApp/templates/index.ts.hbs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import express from 'express';
22
import AdminForth, { Filters } from 'adminforth';
3-
import usersResource from "./resources/users";
3+
import usersResource from "./resources/adminuser";
44

55
const ADMIN_BASE_URL = '';
66

77
export const admin = new AdminForth({
88
baseUrl: ADMIN_BASE_URL,
99
auth: {
10-
usersResourceId: 'users',
10+
usersResourceId: 'adminuser',
1111
usernameField: 'email',
1212
passwordHashField: 'password_hash',
1313
rememberMeDays: 30,
@@ -51,7 +51,7 @@ export const admin = new AdminForth({
5151
{
5252
label: 'Users',
5353
icon: 'flowbite:user-solid',
54-
resourceId: 'users'
54+
resourceId: 'adminuser'
5555
}
5656
],
5757
});
@@ -68,8 +68,8 @@ if (import.meta.url === `file://${process.argv[1]}`) {
6868
admin.express.serve(app)
6969

7070
admin.discoverDatabases().then(async () => {
71-
if (!await admin.resource('users').get([Filters.EQ('email', 'adminforth')])) {
72-
await admin.resource('users').create({
71+
if (!await admin.resource('adminuser').get([Filters.EQ('email', 'adminforth')])) {
72+
await admin.resource('adminuser').create({
7373
email: 'adminforth',
7474
password_hash: await AdminForth.Utils.generatePasswordHash('adminforth'),
7575
role: 'superadmin',

adminforth/commands/createApp/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ async function writeTemplateFiles(dirname, cwd, options) {
191191
data: { dbUrl, prismaDbUrl },
192192
},
193193
{
194-
src: 'users.ts.hbs',
195-
dest: 'resources/users.ts',
194+
src: 'adminuser.ts.hbs',
195+
dest: 'resources/adminuser.ts',
196196
data: {},
197197
},
198198
{

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+
console.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)