adminforth 1.3.5 → 1.3.8

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.
package/dist/index.js CHANGED
@@ -179,7 +179,7 @@ class AdminForth {
179
179
  }
180
180
  // execute hook if needed
181
181
  for (const hook of listify((_e = (_d = resource.hooks) === null || _d === void 0 ? void 0 : _d.create) === null || _e === void 0 ? void 0 : _e.beforeSave)) {
182
- const resp = yield hook({ resource, record, adminUser });
182
+ const resp = yield hook({ recordId: undefined, resource, record, adminUser });
183
183
  if (!resp || (!resp.ok && !resp.error)) {
184
184
  throw new Error(`Hook beforeSave must return object with {ok: true} or { error: 'Error' } `);
185
185
  }
@@ -196,10 +196,11 @@ class AdminForth {
196
196
  const connector = this.connectors[resource.dataSource];
197
197
  process.env.HEAVY_DEBUG && console.log('🪲🪲🪲🪲 creating record createResourceRecord', record);
198
198
  yield connector.createRecord({ resource, record, adminUser });
199
+ const primaryKey = record[resource.columns.find((col) => col.primaryKey).name];
199
200
  // execute hook if needed
200
201
  for (const hook of listify((_g = (_f = resource.hooks) === null || _f === void 0 ? void 0 : _f.create) === null || _g === void 0 ? void 0 : _g.afterSave)) {
201
202
  console.log('Hook afterSave', hook);
202
- const resp = yield hook({ resource, record, adminUser });
203
+ const resp = yield hook({ recordId: primaryKey, resource, record, adminUser });
203
204
  if (!resp || (!resp.ok && !resp.error)) {
204
205
  throw new Error(`Hook afterSave must return object with {ok: true} or { error: 'Error' } `);
205
206
  }
@@ -251,10 +251,10 @@ export default class ConfigValidator {
251
251
  const record = yield connector.getRecordByPrimaryKey(res, recordId);
252
252
  yield Promise.all(res.hooks.delete.beforeSave.map((hook) => __awaiter(this, void 0, void 0, function* () {
253
253
  const resp = yield hook({
254
- primaryKey: recordId,
254
+ recordId: recordId,
255
255
  resource: res,
256
256
  record,
257
- adminUser
257
+ adminUser,
258
258
  });
259
259
  if (!error && resp.error) {
260
260
  error = resp.error;
@@ -267,8 +267,10 @@ export default class ConfigValidator {
267
267
  // call afterDelete hook
268
268
  yield Promise.all(res.hooks.delete.afterSave.map((hook) => __awaiter(this, void 0, void 0, function* () {
269
269
  yield hook({
270
- resource: res, record, adminUser,
271
- primaryKey: recordId
270
+ resource: res,
271
+ record,
272
+ adminUser,
273
+ recordId: recordId
272
274
  });
273
275
  })));
274
276
  })));
@@ -519,7 +519,7 @@ export default class AdminForthRestAPI {
519
519
  // execute hook if needed
520
520
  for (const hook of listify((_5 = (_4 = resource.hooks) === null || _4 === void 0 ? void 0 : _4.edit) === null || _5 === void 0 ? void 0 : _5.beforeSave)) {
521
521
  const resp = yield hook({
522
- primaryKey: recordId,
522
+ recordId,
523
523
  resource,
524
524
  record,
525
525
  adminUser
@@ -555,7 +555,7 @@ export default class AdminForthRestAPI {
555
555
  record,
556
556
  adminUser,
557
557
  oldRecord,
558
- primaryKey: recordId,
558
+ recordId,
559
559
  });
560
560
  if (!resp || (!resp.ok && !resp.error)) {
561
561
  throw new Error(`Hook afterSave must return object with {ok: true} or { error: 'Error' } `);
@@ -613,7 +613,7 @@ export default class AdminForthRestAPI {
613
613
  resource,
614
614
  record,
615
615
  adminUser,
616
- primaryKey: body['primaryKey']
616
+ recordId: body['primaryKey']
617
617
  });
618
618
  if (!resp || (!resp.ok && !resp.error)) {
619
619
  throw new Error(`Hook afterSave must return object with {ok: true} or { error: 'Error' } `);
package/index.ts CHANGED
@@ -224,7 +224,7 @@ class AdminForth implements IAdminForth {
224
224
 
225
225
  // execute hook if needed
226
226
  for (const hook of listify(resource.hooks?.create?.beforeSave as BeforeSaveFunction[])) {
227
- const resp = await hook({ resource, record, adminUser });
227
+ const resp = await hook({ recordId: undefined, resource, record, adminUser });
228
228
  if (!resp || (!resp.ok && !resp.error)) {
229
229
  throw new Error(`Hook beforeSave must return object with {ok: true} or { error: 'Error' } `);
230
230
  }
@@ -243,10 +243,13 @@ class AdminForth implements IAdminForth {
243
243
  const connector = this.connectors[resource.dataSource];
244
244
  process.env.HEAVY_DEBUG && console.log('🪲🪲🪲🪲 creating record createResourceRecord', record);
245
245
  await connector.createRecord({ resource, record, adminUser });
246
+
247
+ const primaryKey = record[resource.columns.find((col) => col.primaryKey).name];
248
+
246
249
  // execute hook if needed
247
250
  for (const hook of listify(resource.hooks?.create?.afterSave as AfterSaveFunction[])) {
248
251
  console.log('Hook afterSave', hook);
249
- const resp = await hook({ resource, record, adminUser });
252
+ const resp = await hook({ recordId: primaryKey, resource, record, adminUser });
250
253
  if (!resp || (!resp.ok && !resp.error)) {
251
254
  throw new Error(`Hook afterSave must return object with {ok: true} or { error: 'Error' } `);
252
255
  }
@@ -278,10 +278,10 @@ export default class ConfigValidator implements IConfigValidator {
278
278
  (res.hooks.delete.beforeSave as AfterSaveFunction[]).map(
279
279
  async (hook) => {
280
280
  const resp = await hook({
281
- primaryKey: recordId,
281
+ recordId: recordId,
282
282
  resource: res,
283
283
  record,
284
- adminUser
284
+ adminUser,
285
285
  });
286
286
  if (!error && resp.error) {
287
287
  error = resp.error;
@@ -300,8 +300,10 @@ export default class ConfigValidator implements IConfigValidator {
300
300
  (res.hooks.delete.afterSave as AfterSaveFunction[]).map(
301
301
  async (hook) => {
302
302
  await hook({
303
- resource: res, record, adminUser,
304
- primaryKey: recordId
303
+ resource: res,
304
+ record,
305
+ adminUser,
306
+ recordId: recordId
305
307
  });
306
308
  }
307
309
  )
@@ -391,8 +393,6 @@ export default class ConfigValidator implements IConfigValidator {
391
393
  }
392
394
  });
393
395
 
394
-
395
-
396
396
  if (!this.config.menu) {
397
397
  errors.push('No config.menu defined');
398
398
  }
@@ -605,7 +605,7 @@ export default class AdminForthRestAPI {
605
605
  // execute hook if needed
606
606
  for (const hook of listify(resource.hooks?.edit?.beforeSave as BeforeSaveFunction[])) {
607
607
  const resp = await hook({
608
- primaryKey: recordId,
608
+ recordId,
609
609
  resource,
610
610
  record,
611
611
  adminUser
@@ -644,7 +644,7 @@ export default class AdminForthRestAPI {
644
644
  record,
645
645
  adminUser,
646
646
  oldRecord,
647
- primaryKey: recordId,
647
+ recordId,
648
648
  });
649
649
  if (!resp || (!resp.ok && !resp.error)) {
650
650
  throw new Error(`Hook afterSave must return object with {ok: true} or { error: 'Error' } `);
@@ -709,7 +709,7 @@ export default class AdminForthRestAPI {
709
709
  resource,
710
710
  record,
711
711
  adminUser,
712
- primaryKey: body['primaryKey']
712
+ recordId: body['primaryKey']
713
713
  });
714
714
  if (!resp || (!resp.ok && !resp.error)) {
715
715
  throw new Error(`Hook afterSave must return object with {ok: true} or { error: 'Error' } `);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adminforth",
3
- "version": "1.3.5",
3
+ "version": "1.3.8",
4
4
  "description": "OpenSource Vue3 powered forth-generation admin panel",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -690,13 +690,13 @@ export type AfterDataSourceResponseFunction = (params: {resource: AdminForthReso
690
690
  * Modify record to change how data is saved to database.
691
691
  * Return ok: false and error: string to stop execution and show error message to user. Return ok: true to continue execution.
692
692
  */
693
- export type BeforeSaveFunction = (params: {resource: AdminForthResource, primaryKey: any, adminUser: AdminUser, record: any}) => Promise<{ok: boolean, error?: string}>;
693
+ export type BeforeSaveFunction = (params: {resource: AdminForthResource, recordId: any, adminUser: AdminUser, record: any}) => Promise<{ok: boolean, error?: string}>;
694
694
 
695
695
  /**
696
696
  * Modify record to change how data is saved to database.
697
697
  * Return ok: false and error: string to stop execution and show error message to user. Return ok: true to continue execution.
698
698
  */
699
- export type AfterSaveFunction = (params: {resource: AdminForthResource, primaryKey: any, adminUser: AdminUser, record: any}) => Promise<{ok: boolean, error?: string}>;
699
+ export type AfterSaveFunction = (params: {resource: AdminForthResource, recordId: any, adminUser: AdminUser, record: any}) => Promise<{ok: boolean, error?: string}>;
700
700
 
701
701
  /**
702
702
  * Allow to get user data before login confirmation, will triger when user try to login.