adminforth 1.2.98 → 1.2.99

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.
@@ -0,0 +1,97 @@
1
+ import { IAdminForthFilter, IAdminForthSort, IOperationalResource, IAdminForthDataSourceConnectorBase, AdminForthResource, IAdminForth } from '../types/AdminForthConfig.js';
2
+
3
+
4
+ // export interface IOperationalResource {
5
+ // get: (filters: IAdminForthFilter | IAdminForthFilter[]) => Promise<any[]>;
6
+
7
+ // list: (filters: IAdminForthFilter | IAdminForthFilter[], limit: number, offset: number, sort: IAdminForthSort | IAdminForthSort[]) => Promise<any[]>;
8
+
9
+ // count: (filters: IAdminForthFilter | IAdminForthFilter[]) => Promise<number>;
10
+
11
+ // create: (record: any) => Promise<any>;
12
+
13
+ // update: (primaryKey: any, record: any) => Promise<any>;
14
+
15
+ // delete: (primaryKey: any) => Promise<boolean>;
16
+
17
+ // deleteMany: (primaryKeys: any[]) => Promise<boolean>;
18
+ // }
19
+
20
+
21
+ // async getData({ resource, limit, offset, sort, filters }: {
22
+ // resource: AdminForthResource,
23
+ // limit: number,
24
+ // offset: number,
25
+ // sort: { field: string, direction: AdminForthSortDirections }[],
26
+ // filters: { field: string, operator: AdminForthFilterOperators, value: any }[]
27
+ // }): Promise<{ data: any[], total: number }> {
28
+
29
+ function filtersIfFilter(filter: IAdminForthFilter | IAdminForthFilter[]): IAdminForthFilter[] {
30
+ return (typeof filter === 'object' ? [filter] : filter) as IAdminForthFilter[];
31
+ }
32
+
33
+ function sortsIfSort(sort: IAdminForthSort | IAdminForthSort[]): IAdminForthSort[] {
34
+ return (typeof sort === 'object' ? [sort] : sort) as IAdminForthSort[];
35
+ }
36
+
37
+ export class OperationalResource implements IOperationalResource {
38
+ dataConnector: IAdminForthDataSourceConnectorBase;
39
+ resourceConfig: AdminForthResource;
40
+
41
+ constructor(dataConnector: IAdminForthDataSourceConnectorBase, resourceConfig: AdminForthResource) {
42
+ this.dataConnector = dataConnector;
43
+ this.resourceConfig = resourceConfig;
44
+ }
45
+
46
+ async get(filter: IAdminForthFilter | IAdminForthFilter[]): Promise<any[]> {
47
+ return await this.dataConnector.getData({
48
+ resource: this.resourceConfig,
49
+ filters: filtersIfFilter(filter),
50
+ limit: 1,
51
+ offset: 0,
52
+ sort: [],
53
+ })[0];
54
+ }
55
+
56
+ async list(
57
+ filter: IAdminForthFilter | IAdminForthFilter[],
58
+ limit: number,
59
+ offset: number,
60
+ sort: IAdminForthSort | IAdminForthSort[]
61
+ ): Promise<any[]> {
62
+ const { data } = await this.dataConnector.getData({
63
+ resource: this.resourceConfig,
64
+ filters: filtersIfFilter(filter),
65
+ limit,
66
+ offset,
67
+ sort: sortsIfSort(sort),
68
+ getTotals: false,
69
+ });
70
+ return data;
71
+ }
72
+
73
+ async count(filter: IAdminForthFilter | IAdminForthFilter[]): Promise<number> {
74
+ return await this.dataConnector.getCount({
75
+ resource: this.resourceConfig,
76
+ filters: filtersIfFilter(filter),
77
+ });
78
+ }
79
+
80
+ async create(record: any): Promise<any> {
81
+ return await this.dataConnector.createRecord({ resource: this.resourceConfig, record });
82
+ }
83
+
84
+ async update(primaryKey: any, record: any): Promise<any> {
85
+ return await this.dataConnector.updateRecord({
86
+ resource: this.resourceConfig,
87
+ recordId: primaryKey,
88
+ newValues: record
89
+ });
90
+ }
91
+
92
+ async delete(primaryKey: any): Promise<boolean> {
93
+ return await this.dataConnector.deleteRecord({ resource: this.resourceConfig, recordId: primaryKey });
94
+ }
95
+
96
+
97
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adminforth",
3
- "version": "1.2.98",
3
+ "version": "1.2.99",
4
4
  "description": "OpenSource Vue3 powered forth-generation admin panel",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -90,6 +90,17 @@ export interface IExpressHttpServer extends IHttpServer {
90
90
  authorize(callable: Function): void;
91
91
  }
92
92
 
93
+ export interface IAdminForthFilter {
94
+ field: string;
95
+ operator: AdminForthFilterOperators;
96
+ value: any;
97
+ }
98
+
99
+ export interface IAdminForthSort {
100
+ field: string,
101
+ direction: AdminForthSortDirections
102
+ }
103
+
93
104
  export interface IAdminForthDataSourceConnector {
94
105
 
95
106
  /**
@@ -145,15 +156,21 @@ export interface IAdminForthDataSourceConnector {
145
156
  *
146
157
  * Fields are returned from db "as is" then {@link AdminForthBaseConnector.getData} will transform each field using {@link IAdminForthDataSourceConnector.getFieldValue}
147
158
  */
148
- getDataWithOriginalTypes({ resource, limit, offset, sort, filters, getTotals }: {
159
+ getDataWithOriginalTypes({ resource, limit, offset, sort, filters }: {
149
160
  resource: AdminForthResource,
150
161
  limit: number,
151
162
  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}>;
163
+ sort: IAdminForthSort[],
164
+ filters: IAdminForthFilter[],
165
+ }): Promise<Array<any>>;
156
166
 
167
+ /**
168
+ * Used to get count of records in database.
169
+ */
170
+ getCount({ resource, filters }: {
171
+ resource: AdminForthResource,
172
+ filters: IAdminForthFilter[],
173
+ }): Promise<number>;
157
174
 
158
175
  /**
159
176
  * Optional method which used to get min and max values for columns in resource.
@@ -181,7 +198,7 @@ export interface IAdminForthDataSourceConnector {
181
198
  /**
182
199
  * Used to delete record in database.
183
200
  */
184
- deleteRecord({ resource, recordId }: { resource: AdminForthResource, recordId: any }): Promise<void>;
201
+ deleteRecord({ resource, recordId }: { resource: AdminForthResource, recordId: any }): Promise<boolean>;
185
202
  }
186
203
 
187
204
 
@@ -196,12 +213,15 @@ export interface IAdminForthDataSourceConnectorBase extends IAdminForthDataSourc
196
213
  resource: AdminForthResource,
197
214
  limit: number,
198
215
  offset: number,
199
- sort: { field: string, direction: AdminForthSortDirections }[],
200
- filters: { field: string, operator: AdminForthFilterOperators, value: any }[]
216
+ sort: IAdminForthSort[],
217
+ filters: IAdminForthFilter[],
218
+ getTotals?: boolean,
201
219
  }): Promise<{ data: Array<any>, total: number }>;
202
220
 
203
221
  getRecordByPrimaryKey(resource: AdminForthResource, recordId: string): Promise<any>;
204
222
 
223
+ createRecord({ resource, record }: { resource: AdminForthResource, record: any }): Promise<void>;
224
+
205
225
  getMinMaxForColumns({ resource, columns }: { resource: AdminForthResource, columns: AdminForthResourceColumn[] }): Promise<{ [key: string]: { min: any, max: any } }>;
206
226
  }
207
227
 
@@ -1306,6 +1326,67 @@ export type AdminForthConfig = {
1306
1326
 
1307
1327
 
1308
1328
  }
1329
+
1330
+ // define typescript objects which I can instantiate as Filters.EQ(field, value) and they woudl
1331
+ // return { field: field, operator: 'eq', value: value }. They should be exported with Filters namespace so I can import Filters from this file
1332
+ // and use Filters.EQ(field, value) in my code
1333
+
1334
+ export type FDataFilter = (field: string, value: any) => IAdminForthFilter;
1335
+
1336
+ export class Filters {
1337
+ static EQ(field: string, value: any): IAdminForthFilter {
1338
+ return { field, operator: AdminForthFilterOperators.EQ, value };
1339
+ }
1340
+ static NEQ(field: string, value: any): IAdminForthFilter {
1341
+ return { field, operator: AdminForthFilterOperators.NE, value };
1342
+ }
1343
+ static GT(field: string, value: any): IAdminForthFilter {
1344
+ return { field, operator: AdminForthFilterOperators.GT, value };
1345
+ }
1346
+ static GTE(field: string, value: any): IAdminForthFilter {
1347
+ return { field, operator: AdminForthFilterOperators.GTE, value };
1348
+ }
1349
+ static LT(field: string, value: any): IAdminForthFilter {
1350
+ return { field, operator: AdminForthFilterOperators.LT, value };
1351
+ }
1352
+ static LTE(field: string, value: any): IAdminForthFilter {
1353
+ return { field, operator: AdminForthFilterOperators.LTE, value };
1354
+ }
1355
+ static IN(field: string, value: any): IAdminForthFilter {
1356
+ return { field, operator: AdminForthFilterOperators.IN, value };
1357
+ }
1358
+ static NOT_IN(field: string, value: any): IAdminForthFilter {
1359
+ return { field, operator: AdminForthFilterOperators.NIN, value };
1360
+ }
1361
+ static LIKE(field: string, value: any): IAdminForthFilter {
1362
+ return { field, operator: AdminForthFilterOperators.LIKE, value };
1363
+ }
1364
+ }
1365
+
1366
+ export type FDataSort = (field: string, direction: AdminForthSortDirections) => IAdminForthSort;
1367
+
1368
+ export class Sorts {
1369
+ static ASC(field: string): IAdminForthSort {
1370
+ return { field, direction: AdminForthSortDirections.asc };
1371
+ }
1372
+ static DESC(field: string): IAdminForthSort {
1373
+ return { field, direction: AdminForthSortDirections.desc };
1374
+ }
1375
+ }
1376
+
1377
+ export interface IOperationalResource {
1378
+ get: (filter: IAdminForthFilter | IAdminForthFilter[]) => Promise<any[]>;
1379
+
1380
+ list: (filter: IAdminForthFilter | IAdminForthFilter[], limit: number, offset: number, sort: IAdminForthSort | IAdminForthSort[]) => Promise<any[]>;
1381
+
1382
+ count: (filter: IAdminForthFilter | IAdminForthFilter[]) => Promise<number>;
1383
+
1384
+ create: (record: any) => Promise<any>;
1385
+
1386
+ update: (primaryKey: any, record: any) => Promise<any>;
1387
+
1388
+ delete: (primaryKey: any) => Promise<boolean>;
1389
+ }
1309
1390
 
1310
1391
 
1311
1392
  export enum AllowedActionsEnum {