adminforth 1.2.98 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -51,16 +51,18 @@ export interface IHttpServer {
51
51
 
52
52
  export type AdminUser = {
53
53
  /**
54
- * primaryKey field value of user in table which is defined by {@link AdminForthConfig.auth.resourceId}
55
- * or null if it is user logged in as {@link AdminForthConfig.rootUser}
54
+ * primaryKey field value of user in table which is defined by {@link AdminForthConfig.auth.usersResourceId}
56
55
  */
57
56
  pk: string | null,
58
57
 
59
58
  /**
60
- * Username which takend from {@link AdminForthConfig.auth.usernameField} field in user resource {@link AdminForthConfig.auth.resourceId}
59
+ * Username which taken from {@link AdminForthConfig.auth.usernameField} field in user resource {@link AdminForthConfig.auth.usersResourceId}
61
60
  */
62
61
  username: string,
63
- isRoot: boolean,
62
+
63
+ /**
64
+ * User record fetched from database, from resource defined in {@link AdminForthConfig.auth.usersResourceId}
65
+ */
64
66
  dbUser: any,
65
67
  }
66
68
 
@@ -90,6 +92,17 @@ export interface IExpressHttpServer extends IHttpServer {
90
92
  authorize(callable: Function): void;
91
93
  }
92
94
 
95
+ export interface IAdminForthFilter {
96
+ field: string;
97
+ operator: AdminForthFilterOperators;
98
+ value: any;
99
+ }
100
+
101
+ export interface IAdminForthSort {
102
+ field: string,
103
+ direction: AdminForthSortDirections
104
+ }
105
+
93
106
  export interface IAdminForthDataSourceConnector {
94
107
 
95
108
  /**
@@ -145,15 +158,21 @@ export interface IAdminForthDataSourceConnector {
145
158
  *
146
159
  * Fields are returned from db "as is" then {@link AdminForthBaseConnector.getData} will transform each field using {@link IAdminForthDataSourceConnector.getFieldValue}
147
160
  */
148
- getDataWithOriginalTypes({ resource, limit, offset, sort, filters, getTotals }: {
161
+ getDataWithOriginalTypes({ resource, limit, offset, sort, filters }: {
149
162
  resource: AdminForthResource,
150
163
  limit: number,
151
164
  offset: number,
152
- sort: { field: string, direction: AdminForthSortDirections }[],
153
- filters: { field: string, operator: AdminForthFilterOperators, value: any }[],
154
- getTotals?: boolean
155
- }): Promise<{data: Array<any>, total: number}>;
165
+ sort: IAdminForthSort[],
166
+ filters: IAdminForthFilter[],
167
+ }): Promise<Array<any>>;
156
168
 
169
+ /**
170
+ * Used to get count of records in database.
171
+ */
172
+ getCount({ resource, filters }: {
173
+ resource: AdminForthResource,
174
+ filters: IAdminForthFilter[],
175
+ }): Promise<number>;
157
176
 
158
177
  /**
159
178
  * Optional method which used to get min and max values for columns in resource.
@@ -181,7 +200,7 @@ export interface IAdminForthDataSourceConnector {
181
200
  /**
182
201
  * Used to delete record in database.
183
202
  */
184
- deleteRecord({ resource, recordId }: { resource: AdminForthResource, recordId: any }): Promise<void>;
203
+ deleteRecord({ resource, recordId }: { resource: AdminForthResource, recordId: any }): Promise<boolean>;
185
204
  }
186
205
 
187
206
 
@@ -196,12 +215,19 @@ export interface IAdminForthDataSourceConnectorBase extends IAdminForthDataSourc
196
215
  resource: AdminForthResource,
197
216
  limit: number,
198
217
  offset: number,
199
- sort: { field: string, direction: AdminForthSortDirections }[],
200
- filters: { field: string, operator: AdminForthFilterOperators, value: any }[]
218
+ sort: IAdminForthSort[],
219
+ filters: IAdminForthFilter[],
220
+ getTotals?: boolean,
201
221
  }): Promise<{ data: Array<any>, total: number }>;
202
222
 
203
223
  getRecordByPrimaryKey(resource: AdminForthResource, recordId: string): Promise<any>;
204
224
 
225
+ createRecord({ resource, record, adminUser }: {
226
+ resource: AdminForthResource,
227
+ record: any
228
+ adminUser: AdminUser
229
+ }): Promise<void>;
230
+
205
231
  getMinMaxForColumns({ resource, columns }: { resource: AdminForthResource, columns: AdminForthResourceColumn[] }): Promise<{ [key: string]: { min: any, max: any } }>;
206
232
  }
207
233
 
@@ -724,7 +750,7 @@ export type AdminForthBulkAction = {
724
750
  *
725
751
  * ```ts
726
752
  * allowed: async ({ resource, adminUser, selectedIds }) => {
727
- * if (adminUser.isRoot || adminUser.dbUser.role !== 'superadmin') {
753
+ * if (adminUser.dbUser.role !== 'superadmin') {
728
754
  * return false;
729
755
  * }
730
756
  * return true;
@@ -766,7 +792,7 @@ export type AdminForthBulkAction = {
766
792
  * ],
767
793
  * allowedActions: \{
768
794
  * edit: (\{ resource, adminUser, recordIds \}) => \{
769
- * return adminUser.isRoot || adminUser.dbUser.role === 'superadmin';
795
+ * return adminUser.dbUser.role === 'superadmin';
770
796
  * \}
771
797
  * \}
772
798
  * \}
@@ -959,8 +985,8 @@ export type AdminForthResource = {
959
985
  * ```ts
960
986
  * allowedActions: {
961
987
  * create: ({ resource, adminUser }) => {
962
- * // Allow only superadmin or root user to create records
963
- * return adminUser.isRoot || adminUser.dbUser.role === 'superadmin';
988
+ * // Allow only superadmin to create records
989
+ * return adminUser.dbUser.role === 'superadmin';
964
990
  * },
965
991
  * delete: false, // disable delete action for all users
966
992
  * }
@@ -1065,33 +1091,21 @@ export type AdminForthDataSource = {
1065
1091
  */
1066
1092
  export type AdminForthConfig = {
1067
1093
 
1068
- /**
1069
- * Root user should be used to login to the admin panel first time.
1070
- * Then you should create User for yourself using AdminForth (so it will be persisted in DB),
1071
- * and then disable this option as it is less secure
1072
- */
1073
- rootUser?: {
1074
- /**
1075
- * Username for root user
1076
- */
1077
- username: string,
1078
-
1079
- /**
1080
- * Password for root user
1081
- */
1082
- password: string,
1083
- },
1084
-
1085
1094
  /**
1086
1095
  * Authorization module configuration
1087
1096
  */
1088
1097
  auth?: {
1089
1098
  /**
1090
- * Resource ID for user resource.
1099
+ * Resource ID for resource which stores user table.
1091
1100
  * Resource is a table in database where users will be stored and fetched from. Resources and their ids are defined in resources section of the config.
1092
1101
  * In other words this setting is a reference to a table in database where users will be fetched from on login.
1093
1102
  */
1094
- resourceId: string,
1103
+ usersResourceId?: string,
1104
+
1105
+ /**
1106
+ * Legacy field left for backward compatibility. Use usersResourceId instead.
1107
+ */
1108
+ resourceId?: string,
1095
1109
 
1096
1110
  /**
1097
1111
  * Field name (column name) in user resource which will be used as username for searching user in database during login.
@@ -1302,10 +1316,75 @@ export type AdminForthConfig = {
1302
1316
  baseUrl?: string,
1303
1317
 
1304
1318
 
1319
+ /**
1320
+ * Whether to show delete confirmation dialog before deleting record.
1321
+ * By default it is true.
1322
+ */
1305
1323
  deleteConfirmation?: boolean,
1306
1324
 
1307
1325
 
1308
1326
  }
1327
+
1328
+ // define typescript objects which I can instantiate as Filters.EQ(field, value) and they woudl
1329
+ // return { field: field, operator: 'eq', value: value }. They should be exported with Filters namespace so I can import Filters from this file
1330
+ // and use Filters.EQ(field, value) in my code
1331
+
1332
+ export type FDataFilter = (field: string, value: any) => IAdminForthFilter;
1333
+
1334
+ export class Filters {
1335
+ static EQ(field: string, value: any): IAdminForthFilter {
1336
+ return { field, operator: AdminForthFilterOperators.EQ, value };
1337
+ }
1338
+ static NEQ(field: string, value: any): IAdminForthFilter {
1339
+ return { field, operator: AdminForthFilterOperators.NE, value };
1340
+ }
1341
+ static GT(field: string, value: any): IAdminForthFilter {
1342
+ return { field, operator: AdminForthFilterOperators.GT, value };
1343
+ }
1344
+ static GTE(field: string, value: any): IAdminForthFilter {
1345
+ return { field, operator: AdminForthFilterOperators.GTE, value };
1346
+ }
1347
+ static LT(field: string, value: any): IAdminForthFilter {
1348
+ return { field, operator: AdminForthFilterOperators.LT, value };
1349
+ }
1350
+ static LTE(field: string, value: any): IAdminForthFilter {
1351
+ return { field, operator: AdminForthFilterOperators.LTE, value };
1352
+ }
1353
+ static IN(field: string, value: any): IAdminForthFilter {
1354
+ return { field, operator: AdminForthFilterOperators.IN, value };
1355
+ }
1356
+ static NOT_IN(field: string, value: any): IAdminForthFilter {
1357
+ return { field, operator: AdminForthFilterOperators.NIN, value };
1358
+ }
1359
+ static LIKE(field: string, value: any): IAdminForthFilter {
1360
+ return { field, operator: AdminForthFilterOperators.LIKE, value };
1361
+ }
1362
+ }
1363
+
1364
+ export type FDataSort = (field: string, direction: AdminForthSortDirections) => IAdminForthSort;
1365
+
1366
+ export class Sorts {
1367
+ static ASC(field: string): IAdminForthSort {
1368
+ return { field, direction: AdminForthSortDirections.asc };
1369
+ }
1370
+ static DESC(field: string): IAdminForthSort {
1371
+ return { field, direction: AdminForthSortDirections.desc };
1372
+ }
1373
+ }
1374
+
1375
+ export interface IOperationalResource {
1376
+ get: (filter: IAdminForthFilter | IAdminForthFilter[]) => Promise<any | null>;
1377
+
1378
+ list: (filter: IAdminForthFilter | IAdminForthFilter[], limit: number, offset: number, sort: IAdminForthSort | IAdminForthSort[]) => Promise<any[]>;
1379
+
1380
+ count: (filter: IAdminForthFilter | IAdminForthFilter[]) => Promise<number>;
1381
+
1382
+ create: (record: any) => Promise<any>;
1383
+
1384
+ update: (primaryKey: any, record: any) => Promise<any>;
1385
+
1386
+ delete: (primaryKey: any) => Promise<boolean>;
1387
+ }
1309
1388
 
1310
1389
 
1311
1390
  export enum AllowedActionsEnum {