wabe 0.5.13 → 0.5.15

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
@@ -60334,35 +60334,6 @@ var require_create_node_ponyfill2 = __commonJS((exports, module) => {
60334
60334
  };
60335
60335
  });
60336
60336
 
60337
- // src/cache/InMemoryCache.ts
60338
- class InMemoryCache {
60339
- options;
60340
- store;
60341
- intervalId = undefined;
60342
- constructor(options) {
60343
- this.options = options;
60344
- this.store = {};
60345
- this._init();
60346
- }
60347
- _init() {
60348
- this.intervalId = setInterval(() => {
60349
- this.clear();
60350
- }, this.options.interval);
60351
- }
60352
- set(key, value) {
60353
- this.store[key] = value;
60354
- }
60355
- get(key) {
60356
- return this.store[key];
60357
- }
60358
- clear() {
60359
- this.store = {};
60360
- }
60361
- stop() {
60362
- clearInterval(this.intervalId);
60363
- }
60364
- }
60365
-
60366
60337
  // src/files/hookUploadFile.ts
60367
60338
  var handleFile = async (hookObject) => {
60368
60339
  const newData = hookObject.getNewData();
@@ -60386,23 +60357,26 @@ class HookObject {
60386
60357
  operationType;
60387
60358
  context;
60388
60359
  object;
60360
+ originalObject;
60389
60361
  constructor({
60390
60362
  newData,
60391
60363
  className,
60392
60364
  operationType,
60393
60365
  context,
60394
- object
60366
+ object,
60367
+ originalObject
60395
60368
  }) {
60396
60369
  this.newData = newData;
60397
60370
  this.className = className;
60398
60371
  this.operationType = operationType;
60399
60372
  this.context = context;
60400
60373
  this.object = object;
60374
+ this.originalObject = originalObject;
60401
60375
  }
60402
60376
  getUser() {
60403
60377
  return this.context.user;
60404
60378
  }
60405
- isFieldUpdate(field) {
60379
+ isFieldUpdated(field) {
60406
60380
  return this.newData && !!this.newData[field];
60407
60381
  }
60408
60382
  upsertNewData(field, value) {
@@ -60415,6 +60389,20 @@ class HookObject {
60415
60389
  getNewData() {
60416
60390
  return this.newData || {};
60417
60391
  }
60392
+ fetch() {
60393
+ const databaseController = this.context.wabe.controllers.database;
60394
+ if (!this.object?.id)
60395
+ return Promise.resolve(null);
60396
+ return databaseController.getObject({
60397
+ className: this.className,
60398
+ id: this.object.id,
60399
+ context: {
60400
+ ...this.context,
60401
+ isRoot: true
60402
+ },
60403
+ fields: ["*"]
60404
+ });
60405
+ }
60418
60406
  }
60419
60407
 
60420
60408
  // src/authentication/utils.ts
@@ -60438,7 +60426,7 @@ var hashPassword = (password) => hash(password, {
60438
60426
 
60439
60427
  // src/hooks/authentication.ts
60440
60428
  var defaultCallAuthenticationProviderOnBeforeCreateUser = async (hookObject) => {
60441
- if (!hookObject.isFieldUpdate("authentication") || hookObject.getNewData().isOauth)
60429
+ if (!hookObject.isFieldUpdated("authentication") || hookObject.getNewData().isOauth)
60442
60430
  return;
60443
60431
  const context = hookObject.context;
60444
60432
  const authentication = hookObject.getNewData().authentication;
@@ -60456,7 +60444,7 @@ var defaultCallAuthenticationProviderOnBeforeCreateUser = async (hookObject) =>
60456
60444
  });
60457
60445
  };
60458
60446
  var defaultCallAuthenticationProviderOnBeforeUpdateUser = async (hookObject) => {
60459
- if (!hookObject.isFieldUpdate("authentication") || hookObject.getNewData().isOauth)
60447
+ if (!hookObject.isFieldUpdated("authentication") || hookObject.getNewData().isOauth)
60460
60448
  return;
60461
60449
  const context = hookObject.context;
60462
60450
  const authentication = hookObject.getNewData().authentication;
@@ -60464,6 +60452,8 @@ var defaultCallAuthenticationProviderOnBeforeUpdateUser = async (hookObject) =>
60464
60452
  if (!provider.onUpdateAuthenticationData)
60465
60453
  return;
60466
60454
  const inputOfTheGoodAuthenticationMethod = authentication[name];
60455
+ if (!hookObject.object?.id)
60456
+ return;
60467
60457
  const { authenticationDataToSave } = await provider.onUpdateAuthenticationData({
60468
60458
  context,
60469
60459
  input: inputOfTheGoodAuthenticationMethod,
@@ -60501,9 +60491,9 @@ var tokenize = (value) => {
60501
60491
 
60502
60492
  // src/hooks/defaultFields.ts
60503
60493
  var defaultBeforeCreateForCreatedAt = (object) => {
60504
- if (!object.isFieldUpdate("createdAt"))
60494
+ if (!object.isFieldUpdated("createdAt"))
60505
60495
  object.upsertNewData("createdAt", new Date);
60506
- if (!object.isFieldUpdate("updatedAt"))
60496
+ if (!object.isFieldUpdated("updatedAt"))
60507
60497
  object.upsertNewData("updatedAt", new Date);
60508
60498
  };
60509
60499
  var defaultBeforeUpdateForUpdatedAt = (object) => {
@@ -60514,14 +60504,14 @@ var defaultBeforeCreateForDefaultValue = async (object) => {
60514
60504
  const allFields = Object.keys(schemaClass.fields);
60515
60505
  allFields.map((field) => {
60516
60506
  const currentSchemaField = schemaClass.fields[field];
60517
- if (!object.isFieldUpdate(field) && currentSchemaField.type !== "Pointer" && currentSchemaField.type !== "Relation" && currentSchemaField.type !== "File" && currentSchemaField.defaultValue !== undefined)
60507
+ if (!object.isFieldUpdated(field) && currentSchemaField.type !== "Pointer" && currentSchemaField.type !== "Relation" && currentSchemaField.type !== "File" && currentSchemaField.defaultValue !== undefined)
60518
60508
  object.upsertNewData(field, currentSchemaField.defaultValue);
60519
60509
  });
60520
60510
  };
60521
60511
 
60522
60512
  // src/hooks/deleteSession.ts
60523
60513
  var defaultDeleteSessionOnDeleteUser = async (object) => {
60524
- const userId = object.object.id;
60514
+ const userId = object.object?.id;
60525
60515
  await object.context.wabe.controllers.database.deleteObjects({
60526
60516
  className: "_Session",
60527
60517
  context: {
@@ -61291,41 +61281,23 @@ class MongoAdapter {
61291
61281
  async createObject(params) {
61292
61282
  if (!this.database)
61293
61283
  throw new Error("Connection to database is not established");
61294
- const { className, data, fields, context } = params;
61284
+ const { className, data, context } = params;
61295
61285
  const collection = await this.createClassIfNotExist(className, context);
61296
61286
  const res = await collection.insertOne(data, {});
61297
- const object = await context.wabe.controllers.database.getObject({
61298
- className,
61299
- id: res.insertedId.toString(),
61300
- context,
61301
- fields
61302
- });
61303
- return object;
61287
+ return { id: res.insertedId.toString() };
61304
61288
  }
61305
61289
  async createObjects(params) {
61306
61290
  if (!this.database)
61307
61291
  throw new Error("Connection to database is not established");
61308
- const { className, data, fields, offset, first, context, order } = params;
61292
+ const { className, data, context } = params;
61309
61293
  const collection = await this.createClassIfNotExist(className, context);
61310
61294
  const res = await collection.insertMany(data, {});
61311
- const orStatement = Object.entries(res.insertedIds).map(([, value]) => ({
61312
- id: { equalTo: value }
61313
- }));
61314
- const allObjects = await context.wabe.controllers.database.getObjects({
61315
- className,
61316
- where: { OR: orStatement },
61317
- fields,
61318
- offset,
61319
- first,
61320
- context,
61321
- order
61322
- });
61323
- return allObjects;
61295
+ return Object.values(res.insertedIds).map((id) => ({ id: id.toString() }));
61324
61296
  }
61325
61297
  async updateObject(params) {
61326
61298
  if (!this.database)
61327
61299
  throw new Error("Connection to database is not established");
61328
- const { className, id, data, fields, context, where } = params;
61300
+ const { className, id, data, context, where } = params;
61329
61301
  const whereBuilded = where ? buildMongoWhereQuery(where) : {};
61330
61302
  const collection = await this.createClassIfNotExist(className, context);
61331
61303
  const res = await collection.updateOne({
@@ -61336,18 +61308,12 @@ class MongoAdapter {
61336
61308
  });
61337
61309
  if (res.matchedCount === 0)
61338
61310
  throw new Error("Object not found");
61339
- const object = await context.wabe.controllers.database.getObject({
61340
- className,
61341
- context,
61342
- fields,
61343
- id
61344
- });
61345
- return object;
61311
+ return { id };
61346
61312
  }
61347
61313
  async updateObjects(params) {
61348
61314
  if (!this.database)
61349
61315
  throw new Error("Connection to database is not established");
61350
- const { className, where, data, fields, offset, first, context, order } = params;
61316
+ const { className, where, data, offset, first, context, order } = params;
61351
61317
  const whereBuilded = buildMongoWhereQuery(where);
61352
61318
  const collection = await this.createClassIfNotExist(className, context);
61353
61319
  const objectsBeforeUpdate = await context.wabe.controllers.database.getObjects({
@@ -61356,28 +61322,20 @@ class MongoAdapter {
61356
61322
  fields: ["id"],
61357
61323
  offset,
61358
61324
  first,
61359
- context,
61325
+ context: {
61326
+ ...context,
61327
+ isRoot: true
61328
+ },
61360
61329
  order
61361
61330
  });
61362
61331
  if (objectsBeforeUpdate.length === 0)
61363
- throw new Error("Object not found");
61332
+ return [];
61364
61333
  await collection.updateMany(whereBuilded, {
61365
61334
  $set: data
61366
61335
  });
61367
- const orStatement = objectsBeforeUpdate.map((object) => ({
61368
- id: { equalTo: import_mongodb.ObjectId.createFromHexString(object.id) }
61336
+ return Object.values(objectsBeforeUpdate).filter(notEmpty).map((object) => ({
61337
+ id: object?.id || ""
61369
61338
  }));
61370
- const objects = await context.wabe.controllers.database.getObjects({
61371
- className,
61372
- where: {
61373
- OR: orStatement
61374
- },
61375
- fields,
61376
- offset,
61377
- first,
61378
- context
61379
- });
61380
- return objects;
61381
61339
  }
61382
61340
  async deleteObject(params) {
61383
61341
  if (!this.database)
@@ -61521,12 +61479,12 @@ var defaultSearchableFieldsBeforeUpdate = (object) => {
61521
61479
  return;
61522
61480
  return stringExtraction(value);
61523
61481
  }).filter(notEmpty);
61524
- const oldExtractedSearcFieldForUpdateFields = Object.entries(object.object).flatMap(([key, value]) => {
61482
+ const oldExtractedSearcFieldForUpdateFields = Object.entries(object.object || {}).flatMap(([key, value]) => {
61525
61483
  if (!searchablesFields.includes(key) || !Object.keys(newData).includes(key))
61526
61484
  return;
61527
61485
  return stringExtraction(value);
61528
61486
  }).filter(notEmpty);
61529
- const actualSearch = object.object.search;
61487
+ const actualSearch = object.object?.search || [];
61530
61488
  const extractedSearchFields = [
61531
61489
  ...actualSearch.filter((element) => !oldExtractedSearcFieldForUpdateFields.includes(element)),
61532
61490
  ...newExtractedSearchField
@@ -61546,12 +61504,12 @@ var updateEmail = (object) => {
61546
61504
  object.upsertNewData("provider", provider);
61547
61505
  };
61548
61506
  var defaultSetEmail = (object) => {
61549
- if (object.isFieldUpdate("email"))
61507
+ if (object.isFieldUpdated("email"))
61550
61508
  return;
61551
61509
  updateEmail(object);
61552
61510
  };
61553
61511
  var defaultSetEmailOnUpdate = (object) => {
61554
- if (object.isFieldUpdate("email"))
61512
+ if (object.isFieldUpdated("email"))
61555
61513
  return;
61556
61514
  updateEmail(object);
61557
61515
  };
@@ -61568,7 +61526,7 @@ var setAcl = async ({
61568
61526
  if (!schemaPermissionsObject)
61569
61527
  return;
61570
61528
  const { acl } = schemaPermissionsObject;
61571
- if (hookObject.isFieldUpdate("acl") || !acl)
61529
+ if (hookObject.isFieldUpdated("acl") || !acl)
61572
61530
  return;
61573
61531
  if (acl.callback) {
61574
61532
  await acl.callback(hookObject);
@@ -61590,8 +61548,8 @@ var setAcl = async ({
61590
61548
  const idOfAllWriteRoles = await getIdOfAllAuthorizedRoles(acl.authorizedRoles, "write");
61591
61549
  const allRolesIdsWithoutDuplicate = [
61592
61550
  ...new Set([
61593
- ...idOfAllReadRoles.map((role) => role.id),
61594
- ...idOfAllWriteRoles.map((role) => role.id)
61551
+ ...idOfAllReadRoles.map((role) => role?.id).filter(notEmpty),
61552
+ ...idOfAllWriteRoles.map((role) => role?.id).filter(notEmpty)
61595
61553
  ])
61596
61554
  ];
61597
61555
  const isReadOrWriteSpecified = acl.authorizedUsers.read && acl.authorizedUsers.read.length > 0 || acl.authorizedUsers.write && acl.authorizedUsers.write.length > 0;
@@ -61600,14 +61558,14 @@ var setAcl = async ({
61600
61558
  roles: [
61601
61559
  ...allRolesIdsWithoutDuplicate.map((roleId) => ({
61602
61560
  roleId,
61603
- read: idOfAllReadRoles.some((role) => role.id === roleId),
61604
- write: idOfAllWriteRoles.some((role) => role.id === roleId)
61561
+ read: idOfAllReadRoles.some((role) => role?.id === roleId),
61562
+ write: idOfAllWriteRoles.some((role) => role?.id === roleId)
61605
61563
  }))
61606
61564
  ]
61607
61565
  };
61608
61566
  if (isBeforeHook)
61609
61567
  hookObject.upsertNewData("acl", aclObject);
61610
- else {
61568
+ else
61611
61569
  await hookObject.context.wabe.controllers.database.updateObject({
61612
61570
  className: hookObject.className,
61613
61571
  context: { ...hookObject.context, isRoot: true },
@@ -61617,7 +61575,6 @@ var setAcl = async ({
61617
61575
  },
61618
61576
  fields: []
61619
61577
  });
61620
- }
61621
61578
  };
61622
61579
  var defaultSetupAclBeforeCreate = async (hookObject) => {
61623
61580
  const userId = hookObject.getUser()?.id;
@@ -61626,7 +61583,7 @@ var defaultSetupAclBeforeCreate = async (hookObject) => {
61626
61583
  await setAcl({ hookObject, userId, isBeforeHook: true });
61627
61584
  };
61628
61585
  var defaultSetupAclOnUserAfterCreate = async (hookObject) => {
61629
- const userId = hookObject.object.id;
61586
+ const userId = hookObject.object?.id;
61630
61587
  if (!userId)
61631
61588
  return;
61632
61589
  await setAcl({ hookObject, userId, isBeforeHook: false });
@@ -61661,16 +61618,10 @@ var initializeHook = ({
61661
61618
  context
61662
61619
  }) => {
61663
61620
  const computeObject = ({
61664
- id,
61665
- object,
61666
- operationType
61621
+ id
61667
61622
  }) => {
61668
- if (object)
61669
- return Promise.resolve(object);
61670
- if (operationType === "beforeCreate" /* BeforeCreate */)
61671
- return newData;
61672
61623
  if (!id)
61673
- throw new Error("Object not found");
61624
+ return {};
61674
61625
  return context.wabe.controllers.database.getObject({
61675
61626
  className,
61676
61627
  context: {
@@ -61683,21 +61634,18 @@ var initializeHook = ({
61683
61634
  });
61684
61635
  };
61685
61636
  const computeObjects = async ({
61686
- objects,
61687
- operationType,
61688
- where
61637
+ where,
61638
+ ids
61689
61639
  }) => {
61690
- if (objects)
61691
- return objects;
61692
- if (operationType === "beforeCreate" /* BeforeCreate */)
61693
- return [newData];
61640
+ if (!where && ids.length === 0)
61641
+ return [{}];
61694
61642
  const res = await context.wabe.controllers.database.getObjects({
61695
61643
  className,
61696
61644
  context: {
61697
61645
  ...context,
61698
61646
  isRoot: true
61699
61647
  },
61700
- where,
61648
+ where: where ? where : { id: { in: ids } },
61701
61649
  fields: ["*"],
61702
61650
  skipHooks: true
61703
61651
  });
@@ -61707,30 +61655,25 @@ var initializeHook = ({
61707
61655
  };
61708
61656
  const hooksOrderByPriorities = _getHooksOrderByPriorities(context.wabe.config);
61709
61657
  return {
61710
- runOnSingleObject: async ({
61711
- operationType,
61712
- id,
61713
- object: inputObject
61714
- }) => {
61658
+ runOnSingleObject: async (options) => {
61715
61659
  if (hooksOrderByPriorities.length === 0)
61716
61660
  return { object: undefined, newData: {} };
61717
61661
  const object = await computeObject({
61718
- id,
61719
- operationType,
61720
- object: inputObject
61662
+ id: options.id
61721
61663
  });
61722
61664
  const hookObject = new HookObject({
61723
61665
  className,
61724
61666
  newData,
61725
- operationType,
61667
+ operationType: options.operationType,
61726
61668
  context,
61727
- object
61669
+ object,
61670
+ originalObject: options.originalObject
61728
61671
  });
61729
61672
  await hooksOrderByPriorities.reduce(async (acc, priority) => {
61730
61673
  await acc;
61731
61674
  const hooksToCompute = await _findHooksByPriority({
61732
61675
  className,
61733
- operationType,
61676
+ operationType: options.operationType,
61734
61677
  priority,
61735
61678
  config: context.wabe.config
61736
61679
  });
@@ -61738,31 +61681,29 @@ var initializeHook = ({
61738
61681
  }, Promise.resolve());
61739
61682
  return { object, newData: hookObject.getNewData() };
61740
61683
  },
61741
- runOnMultipleObjects: async ({
61742
- operationType,
61743
- where,
61744
- objects: inputObjects
61745
- }) => {
61684
+ runOnMultipleObjects: async (options) => {
61746
61685
  if (hooksOrderByPriorities.length === 0)
61747
61686
  return { objects: [], newData: [newData || {}] };
61748
61687
  const objects = await computeObjects({
61749
- where,
61750
- operationType,
61751
- objects: inputObjects
61688
+ where: options.where,
61689
+ ids: options.ids || []
61752
61690
  });
61753
- const newDataAfterHooks = await Promise.all(objects.map(async (object) => {
61691
+ const objectsToUseInMap = (options.operationType === "afterDelete" /* AfterDelete */ ? options.originalObjects : objects) || [];
61692
+ const newDataAfterHooks = await Promise.all(objectsToUseInMap.map(async (object) => {
61693
+ const originalObjectToUse = (options.originalObjects || []).find((originalObject) => originalObject?.id === object?.id);
61754
61694
  const hookObject = new HookObject({
61755
61695
  className,
61756
61696
  newData,
61757
- operationType,
61697
+ operationType: options.operationType,
61758
61698
  context,
61759
- object
61699
+ object,
61700
+ originalObject: originalObjectToUse
61760
61701
  });
61761
61702
  await hooksOrderByPriorities.reduce(async (acc, priority) => {
61762
61703
  await acc;
61763
61704
  const hooksToCompute = await _findHooksByPriority({
61764
61705
  className,
61765
- operationType,
61706
+ operationType: options.operationType,
61766
61707
  priority,
61767
61708
  config: context.wabe.config
61768
61709
  });
@@ -61876,19 +61817,8 @@ var getDefaultHooks = () => [
61876
61817
  // src/database/controllers/DatabaseController.ts
61877
61818
  class DatabaseController2 {
61878
61819
  adapter;
61879
- inMemoryCache;
61880
61820
  constructor(adapter) {
61881
61821
  this.adapter = adapter;
61882
- this.inMemoryCache = new InMemoryCache({ interval: 5000 });
61883
- }
61884
- connect() {
61885
- return this.adapter.connect();
61886
- }
61887
- close() {
61888
- return this.adapter.close();
61889
- }
61890
- createClassIfNotExist(className, context) {
61891
- return this.adapter.createClassIfNotExist(className, context);
61892
61822
  }
61893
61823
  _getPointerObject(className, fields, context) {
61894
61824
  const realClass = context.wabe.config.schema?.classes?.find((c) => c.name.toLowerCase() === className.toLowerCase());
@@ -61992,7 +61922,7 @@ class DatabaseController2 {
61992
61922
  return {
61993
61923
  ...acc,
61994
61924
  [typedWhereKey]: {
61995
- in: objects.map((object) => object.id)
61925
+ in: objects.map((object) => object?.id).filter(notEmpty)
61996
61926
  }
61997
61927
  };
61998
61928
  }, Promise.resolve({}));
@@ -62075,8 +62005,14 @@ class DatabaseController2 {
62075
62005
  ].filter(notEmpty)
62076
62006
  };
62077
62007
  }
62078
- _buildCacheKey(className, id, fields) {
62079
- return `${String(className)}-${id}-${fields.join(",")}`;
62008
+ connect() {
62009
+ return this.adapter.connect();
62010
+ }
62011
+ close() {
62012
+ return this.adapter.close();
62013
+ }
62014
+ createClassIfNotExist(className, context) {
62015
+ return this.adapter.createClassIfNotExist(className, context);
62080
62016
  }
62081
62017
  count(params) {
62082
62018
  return this.adapter.count(params);
@@ -62108,21 +62044,11 @@ class DatabaseController2 {
62108
62044
  ...fieldsWithoutPointers,
62109
62045
  ...pointersFieldsId || []
62110
62046
  ];
62111
- const object = await this.adapter.getObject({
62112
- className,
62113
- context,
62114
- id,
62115
- fields: fieldsWithPointerFields,
62116
- where: whereWithACLCondition
62117
- });
62118
- const keyCache = this._buildCacheKey(className, object.id, fieldsWithPointerFields);
62119
- this.inMemoryCache.set(keyCache, object);
62120
62047
  await hook?.runOnSingleObject({
62121
62048
  operationType: "afterRead" /* AfterRead */,
62122
- object
62049
+ id
62123
62050
  });
62124
- const cacheObject = this.inMemoryCache.get(keyCache);
62125
- const objectToReturn = cacheObject ? cacheObject : await this.adapter.getObject({
62051
+ const objectToReturn = await this.adapter.getObject({
62126
62052
  className,
62127
62053
  id,
62128
62054
  context,
@@ -62158,19 +62084,9 @@ class DatabaseController2 {
62158
62084
  ...fieldsWithoutPointers,
62159
62085
  ...pointersFieldsId || []
62160
62086
  ];
62161
- const objects = await this.adapter.getObjects({
62162
- className,
62163
- context,
62164
- first,
62165
- offset,
62166
- where: whereWithACLCondition,
62167
- fields: fieldsWithPointerFields,
62168
- order
62169
- });
62170
- objects.map((object) => this.inMemoryCache.set(this._buildCacheKey(className, object.id, fieldsWithPointerFields), object));
62171
62087
  await hook?.runOnMultipleObjects({
62172
62088
  operationType: "afterRead" /* AfterRead */,
62173
- objects
62089
+ where: whereWithACLCondition
62174
62090
  });
62175
62091
  const objectsToReturn = await this.adapter.getObjects({
62176
62092
  className,
@@ -62197,31 +62113,25 @@ class DatabaseController2 {
62197
62113
  const { newData } = await hook.runOnSingleObject({
62198
62114
  operationType: "beforeCreate" /* BeforeCreate */
62199
62115
  });
62200
- const object = await this.adapter.createObject({
62116
+ const { id } = await this.adapter.createObject({
62201
62117
  className,
62202
62118
  context,
62203
62119
  fields,
62204
62120
  data: newData
62205
62121
  });
62206
- const keyCache = this._buildCacheKey(className, object.id, fields);
62207
- this.inMemoryCache.set(keyCache, undefined);
62208
- const res = await hook.runOnSingleObject({
62122
+ await hook.runOnSingleObject({
62209
62123
  operationType: "afterCreate" /* AfterCreate */,
62210
- object
62124
+ id
62211
62125
  });
62212
- if (!res.object)
62213
- return object;
62214
- const objectToReturn = await this.getObject({
62126
+ if (fields.length === 0)
62127
+ return null;
62128
+ return this.getObject({
62215
62129
  className,
62216
- context: {
62217
- ...context,
62218
- isRoot: true
62219
- },
62130
+ context,
62220
62131
  fields,
62221
- id: object.id,
62132
+ id,
62222
62133
  skipHooks: true
62223
62134
  });
62224
- return objectToReturn;
62225
62135
  }
62226
62136
  async createObjects({
62227
62137
  data,
@@ -62242,7 +62152,7 @@ class DatabaseController2 {
62242
62152
  const arrayOfComputedData = await Promise.all(hooks.map(async (hook) => (await hook.runOnMultipleObjects({
62243
62153
  operationType: "beforeCreate" /* BeforeCreate */
62244
62154
  }))?.newData[0]));
62245
- const objects = await this.adapter.createObjects({
62155
+ const listOfIds = await this.adapter.createObjects({
62246
62156
  className,
62247
62157
  fields,
62248
62158
  context,
@@ -62251,28 +62161,23 @@ class DatabaseController2 {
62251
62161
  offset,
62252
62162
  order
62253
62163
  });
62254
- const objectsId = objects.map((object) => object.id);
62255
- for (const id of objectsId) {
62256
- const keyCache = this._buildCacheKey(className, id, fields);
62257
- this.inMemoryCache.set(keyCache, undefined);
62258
- }
62259
- const res = await Promise.all(hooks.map((hook) => hook.runOnMultipleObjects({
62164
+ const ids = listOfIds.map(({ id }) => id);
62165
+ await Promise.all(hooks.map((hook) => hook.runOnMultipleObjects({
62260
62166
  operationType: "afterCreate" /* AfterCreate */,
62261
- objects
62167
+ ids
62262
62168
  })));
62263
- if (res.filter((hook) => hook.objects.length > 0).length === 0)
62264
- return objects;
62265
- const objectsToReturn = await this.getObjects({
62169
+ if (fields.length === 0)
62170
+ return [];
62171
+ return this.getObjects({
62266
62172
  className,
62267
62173
  context,
62268
62174
  fields,
62269
- where: { id: { in: objectsId } },
62175
+ where: { id: { in: ids } },
62270
62176
  skipHooks: true,
62271
62177
  first,
62272
62178
  offset,
62273
62179
  order
62274
62180
  });
62275
- return objectsToReturn;
62276
62181
  }
62277
62182
  async updateObject({
62278
62183
  id,
@@ -62286,12 +62191,12 @@ class DatabaseController2 {
62286
62191
  context,
62287
62192
  newData: data
62288
62193
  });
62289
- const { newData } = await hook.runOnSingleObject({
62194
+ const { newData, object } = await hook.runOnSingleObject({
62290
62195
  operationType: "beforeUpdate" /* BeforeUpdate */,
62291
62196
  id
62292
62197
  });
62293
62198
  const whereWithACLCondition = this._buildWhereWithACL({}, context, "write");
62294
- const object = await this.adapter.updateObject({
62199
+ await this.adapter.updateObject({
62295
62200
  className,
62296
62201
  fields,
62297
62202
  id,
@@ -62299,22 +62204,20 @@ class DatabaseController2 {
62299
62204
  data: newData,
62300
62205
  where: whereWithACLCondition
62301
62206
  });
62302
- const keyCache = this._buildCacheKey(className, object.id, fields);
62303
- this.inMemoryCache.set(keyCache, undefined);
62304
- const res = await hook.runOnSingleObject({
62207
+ await hook.runOnSingleObject({
62305
62208
  operationType: "afterUpdate" /* AfterUpdate */,
62306
- object
62209
+ id,
62210
+ originalObject: object
62307
62211
  });
62308
- if (!res.object)
62309
- return object;
62310
- const objectToReturn = await this.getObject({
62212
+ if (fields.length === 0)
62213
+ return null;
62214
+ return this.getObject({
62311
62215
  className,
62312
62216
  context,
62313
62217
  fields,
62314
- id: object.id,
62218
+ id,
62315
62219
  skipHooks: true
62316
62220
  });
62317
- return objectToReturn;
62318
62221
  }
62319
62222
  async updateObjects({
62320
62223
  className,
@@ -62333,7 +62236,7 @@ class DatabaseController2 {
62333
62236
  newData: data
62334
62237
  });
62335
62238
  const whereWithACLCondition = this._buildWhereWithACL(whereObject, context, "write");
62336
- const { newData } = await hook.runOnMultipleObjects({
62239
+ const { newData, objects: objectsAfterBeforeUpdate } = await hook.runOnMultipleObjects({
62337
62240
  operationType: "beforeUpdate" /* BeforeUpdate */,
62338
62241
  where: whereWithACLCondition
62339
62242
  });
@@ -62347,18 +62250,15 @@ class DatabaseController2 {
62347
62250
  offset,
62348
62251
  order
62349
62252
  });
62350
- const objectsId = objects.map((object) => object.id);
62351
- for (const id of objectsId) {
62352
- const keyCache = this._buildCacheKey(className, id, fields);
62353
- this.inMemoryCache.set(keyCache, undefined);
62354
- }
62355
- const res = await hook.runOnMultipleObjects({
62253
+ const objectsId = objects.map((object) => object?.id).filter(notEmpty);
62254
+ await hook.runOnMultipleObjects({
62356
62255
  operationType: "afterUpdate" /* AfterUpdate */,
62357
- objects
62256
+ ids: objectsId,
62257
+ originalObjects: objectsAfterBeforeUpdate
62358
62258
  });
62359
- if (res.objects.length === 0)
62360
- return objects;
62361
- const objectsToReturn = await this.getObjects({
62259
+ if (fields.length === 0)
62260
+ return [];
62261
+ return this.getObjects({
62362
62262
  className,
62363
62263
  context,
62364
62264
  fields,
@@ -62368,7 +62268,6 @@ class DatabaseController2 {
62368
62268
  offset,
62369
62269
  order
62370
62270
  });
62371
- return objectsToReturn;
62372
62271
  }
62373
62272
  async deleteObject({
62374
62273
  context,
@@ -62381,13 +62280,15 @@ class DatabaseController2 {
62381
62280
  context
62382
62281
  });
62383
62282
  const whereWithACLCondition = this._buildWhereWithACL({}, context, "write");
62384
- const objectBeforeDelete = await this.getObject({
62385
- className,
62386
- fields,
62387
- id,
62388
- context
62389
- });
62390
- const { object } = await hook.runOnSingleObject({
62283
+ let objectBeforeDelete = null;
62284
+ if (fields.length > 0)
62285
+ objectBeforeDelete = await this.getObject({
62286
+ className,
62287
+ fields,
62288
+ id,
62289
+ context
62290
+ });
62291
+ const resultOfBeforeDelete = await hook.runOnSingleObject({
62391
62292
  operationType: "beforeDelete" /* BeforeDelete */,
62392
62293
  id
62393
62294
  });
@@ -62400,7 +62301,7 @@ class DatabaseController2 {
62400
62301
  });
62401
62302
  await hook.runOnSingleObject({
62402
62303
  operationType: "afterDelete" /* AfterDelete */,
62403
- object
62304
+ originalObject: resultOfBeforeDelete.object
62404
62305
  });
62405
62306
  return objectBeforeDelete;
62406
62307
  }
@@ -62419,18 +62320,18 @@ class DatabaseController2 {
62419
62320
  context
62420
62321
  });
62421
62322
  const whereWithACLCondition = this._buildWhereWithACL(whereObject, context, "write");
62422
- const objectsBeforeDelete = await this.getObjects({
62423
- className,
62424
- where,
62425
- fields,
62426
- context,
62427
- first,
62428
- offset,
62429
- order
62430
- });
62431
- if (objectsBeforeDelete.length === 0)
62432
- return objectsBeforeDelete;
62433
- const { objects } = await hook.runOnMultipleObjects({
62323
+ let objectsBeforeDelete = [];
62324
+ if (fields.length > 0)
62325
+ objectsBeforeDelete = await this.getObjects({
62326
+ className,
62327
+ where,
62328
+ fields,
62329
+ context,
62330
+ first,
62331
+ offset,
62332
+ order
62333
+ });
62334
+ const resultOfBeforeDelete = await hook.runOnMultipleObjects({
62434
62335
  operationType: "beforeDelete" /* BeforeDelete */,
62435
62336
  where: whereWithACLCondition
62436
62337
  });
@@ -62445,7 +62346,7 @@ class DatabaseController2 {
62445
62346
  });
62446
62347
  await hook.runOnMultipleObjects({
62447
62348
  operationType: "afterDelete" /* AfterDelete */,
62448
- objects
62349
+ originalObjects: resultOfBeforeDelete.objects
62449
62350
  });
62450
62351
  return objectsBeforeDelete;
62451
62352
  }
@@ -62491,7 +62392,7 @@ class Session {
62491
62392
  accessToken: { equalTo: accessToken }
62492
62393
  },
62493
62394
  first: 1,
62494
- fields: ["user.*", "user.role.*"],
62395
+ fields: ["user.*", "user.role.*", "id"],
62495
62396
  context
62496
62397
  });
62497
62398
  if (sessions.length === 0)
@@ -62511,7 +62412,7 @@ class Session {
62511
62412
  iat: Date.now(),
62512
62413
  exp: this.getRefreshTokenExpireAt(context.wabe.config).getTime()
62513
62414
  }, import.meta.env.JWT_SECRET || "dev");
62514
- const { id } = await context.wabe.controllers.database.createObject({
62415
+ const res = await context.wabe.controllers.database.createObject({
62515
62416
  className: "_Session",
62516
62417
  context,
62517
62418
  data: {
@@ -62523,10 +62424,12 @@ class Session {
62523
62424
  },
62524
62425
  fields: ["id"]
62525
62426
  });
62427
+ if (!res)
62428
+ throw new Error("Session not created");
62526
62429
  return {
62527
62430
  accessToken: this.accessToken,
62528
62431
  refreshToken: this.refreshToken,
62529
- sessionId: id
62432
+ sessionId: res.id
62530
62433
  };
62531
62434
  }
62532
62435
  async delete(context) {
@@ -62561,6 +62464,8 @@ class Session {
62561
62464
  accessToken: null,
62562
62465
  refreshToken: null
62563
62466
  };
62467
+ if (!session[0])
62468
+ throw new Error("Session not found");
62564
62469
  const {
62565
62470
  refreshTokenExpiresAt,
62566
62471
  user,
@@ -62659,7 +62564,7 @@ var signInWithResolver = async (_, {
62659
62564
  var signUpWithResolver = async (_, {
62660
62565
  input
62661
62566
  }, context) => {
62662
- const { id: userId } = await context.wabe.controllers.database.createObject({
62567
+ const res = await context.wabe.controllers.database.createObject({
62663
62568
  className: "User",
62664
62569
  data: {
62665
62570
  authentication: input.authentication
@@ -62671,7 +62576,9 @@ var signUpWithResolver = async (_, {
62671
62576
  fields: ["id"]
62672
62577
  });
62673
62578
  const session = new Session;
62674
- const { accessToken, refreshToken } = await session.create(userId, {
62579
+ if (!res)
62580
+ throw new Error("User not created");
62581
+ const { accessToken, refreshToken } = await session.create(res.id, {
62675
62582
  ...context,
62676
62583
  isRoot: true
62677
62584
  });
@@ -62691,7 +62598,7 @@ var signUpWithResolver = async (_, {
62691
62598
  expires: session.getAccessTokenExpireAt(context.wabe.config)
62692
62599
  });
62693
62600
  }
62694
- return { accessToken, refreshToken, id: userId };
62601
+ return { accessToken, refreshToken, id: res.id };
62695
62602
  };
62696
62603
  // src/authentication/oauth/utils.ts
62697
62604
  import crypto from "node:crypto";
@@ -62943,7 +62850,9 @@ var resetPasswordResolver = async (_, { input: { email, password, otp, provider
62943
62850
  });
62944
62851
  if (user.length === 0)
62945
62852
  return true;
62946
- const userId = user[0].id;
62853
+ const userId = user[0]?.id;
62854
+ if (!userId)
62855
+ return false;
62947
62856
  const otpClass = new OTP(context.wabe.config.rootKey);
62948
62857
  const isOtpValid = otpClass.verify(otp, userId);
62949
62858
  if (false)
@@ -63113,7 +63022,9 @@ var sendOtpCodeResolver = async (_, { input }, context) => {
63113
63022
  });
63114
63023
  if (user.length === 0)
63115
63024
  return true;
63116
- const userId = user[0].id;
63025
+ const userId = user[0]?.id;
63026
+ if (!userId)
63027
+ return false;
63117
63028
  const otpClass = new OTP(context.wabe.config.rootKey);
63118
63029
  const otp = otpClass.generate(userId);
63119
63030
  const mainEmail = context.wabe.config.email?.mainEmail || "noreply@wabe.com";
@@ -64239,13 +64150,13 @@ var createAndLink = async ({
64239
64150
  className
64240
64151
  }) => {
64241
64152
  const classInSchema = getClassFromClassName(className, context.wabe.config);
64242
- const { id } = await context.wabe.controllers.database.createObject({
64153
+ const res = await context.wabe.controllers.database.createObject({
64243
64154
  className: classInSchema.fields[fieldName].class,
64244
64155
  data: createAndLink2,
64245
64156
  fields: ["id"],
64246
64157
  context
64247
64158
  });
64248
- return id;
64159
+ return res?.id;
64249
64160
  };
64250
64161
  var createAndAdd = async ({
64251
64162
  createAndAdd: createAndAdd2,
@@ -64282,7 +64193,7 @@ var add = async ({
64282
64193
  fields: [fieldName],
64283
64194
  context
64284
64195
  });
64285
- return [...currentValue[fieldName] || [], ...add2];
64196
+ return [...currentValue?.[fieldName] || [], ...add2];
64286
64197
  }
64287
64198
  if (typeOfExecution === "updateMany" && where) {
64288
64199
  const allObjectsMatchedWithWhere = await context.wabe.controllers.database.getObjects({
@@ -64323,7 +64234,7 @@ var remove = async ({
64323
64234
  fields: [fieldName],
64324
64235
  context
64325
64236
  });
64326
- const olderValues = currentValue[fieldName] || [];
64237
+ const olderValues = currentValue?.[fieldName] || [];
64327
64238
  return olderValues.filter((olderValue) => !remove2.includes(olderValue));
64328
64239
  }
64329
64240
  if (typeOfExecution === "updateMany" && where) {
@@ -64373,6 +64284,24 @@ var getFieldsFromInfo = (info, className) => {
64373
64284
  throw new Error("No fields provided");
64374
64285
  return fields;
64375
64286
  };
64287
+ var getFieldsOfClassName = ({
64288
+ fields,
64289
+ className,
64290
+ context
64291
+ }) => {
64292
+ const classFields = context.wabe.config.schema?.classes?.find((schemaClass) => schemaClass.name === className)?.fields;
64293
+ if (!classFields)
64294
+ return { classFields: [], othersFields: fields };
64295
+ const sameFieldsAsClass = fields.filter((field) => {
64296
+ if (classFields[field] || field === "id")
64297
+ return true;
64298
+ if (Object.keys(classFields).find((classField) => field.includes(classField)))
64299
+ return true;
64300
+ return false;
64301
+ });
64302
+ const othersFields = fields.filter((field) => !sameFieldsAsClass.includes(field));
64303
+ return { classFields: sameFieldsAsClass, othersFields };
64304
+ };
64376
64305
  var executeRelationOnFields = ({
64377
64306
  className,
64378
64307
  fields,
@@ -64469,7 +64398,11 @@ var queryForMultipleObject = async (_, { where, offset, first, order }, context,
64469
64398
  };
64470
64399
  };
64471
64400
  var mutationToCreateObject = async (_, args, context, info, className) => {
64472
- const fields = getFieldsFromInfo(info, className);
64401
+ const { classFields, othersFields } = getFieldsOfClassName({
64402
+ fields: getFieldsFromInfo(info, className),
64403
+ className,
64404
+ context
64405
+ });
64473
64406
  const updatedFieldsToCreate = await executeRelationOnFields({
64474
64407
  className,
64475
64408
  fields: args.input?.fields,
@@ -64479,9 +64412,10 @@ var mutationToCreateObject = async (_, args, context, info, className) => {
64479
64412
  [firstLetterInLowerCase(className)]: await context.wabe.controllers.database.createObject({
64480
64413
  className,
64481
64414
  data: updatedFieldsToCreate,
64482
- fields,
64415
+ fields: classFields,
64483
64416
  context
64484
- })
64417
+ }),
64418
+ ...othersFields.includes("ok") ? { ok: true } : {}
64485
64419
  };
64486
64420
  };
64487
64421
  var mutationToCreateMultipleObjects = async (_, { input: { fields, offset, first, order } }, context, info, className) => {
@@ -64506,7 +64440,11 @@ var mutationToCreateMultipleObjects = async (_, { input: { fields, offset, first
64506
64440
  };
64507
64441
  };
64508
64442
  var mutationToUpdateObject = async (_, args, context, info, className) => {
64509
- const fields = getFieldsFromInfo(info, className);
64443
+ const { classFields, othersFields } = getFieldsOfClassName({
64444
+ fields: getFieldsFromInfo(info, className),
64445
+ className,
64446
+ context
64447
+ });
64510
64448
  const updatedFields = await executeRelationOnFields({
64511
64449
  className,
64512
64450
  fields: args.input?.fields,
@@ -64519,9 +64457,10 @@ var mutationToUpdateObject = async (_, args, context, info, className) => {
64519
64457
  className,
64520
64458
  id: args.input?.id,
64521
64459
  data: updatedFields,
64522
- fields,
64460
+ fields: classFields,
64523
64461
  context
64524
- })
64462
+ }),
64463
+ ...othersFields.includes("ok") ? { ok: true } : {}
64525
64464
  };
64526
64465
  };
64527
64466
  var mutationToUpdateMultipleObjects = async (_, { input: { fields, where, offset, first, order } }, context, info, className) => {
@@ -64548,14 +64487,19 @@ var mutationToUpdateMultipleObjects = async (_, { input: { fields, where, offset
64548
64487
  };
64549
64488
  };
64550
64489
  var mutationToDeleteObject = async (_, args, context, info, className) => {
64551
- const fields = getFieldsFromInfo(info, className);
64490
+ const { classFields, othersFields } = getFieldsOfClassName({
64491
+ fields: getFieldsFromInfo(info, className),
64492
+ className,
64493
+ context
64494
+ });
64552
64495
  return {
64553
64496
  [firstLetterInLowerCase(className)]: await context.wabe.controllers.database.deleteObject({
64554
64497
  className,
64555
64498
  id: args.input?.id,
64556
- fields,
64499
+ fields: classFields,
64557
64500
  context
64558
- })
64501
+ }),
64502
+ ...othersFields.includes("ok") ? { ok: true } : {}
64559
64503
  };
64560
64504
  };
64561
64505
  var mutationToDeleteMultipleObjects = async (_, { input: { where, offset, first, order } }, context, info, className) => {
@@ -65025,7 +64969,7 @@ class GraphQLSchema {
65025
64969
  name: `Create${className}Payload`,
65026
64970
  fields: () => ({
65027
64971
  [classNameWithFirstLetterLowerCase]: { type: object },
65028
- clientMutationId: { type: import_graphql5.GraphQLString }
64972
+ ok: { type: import_graphql5.GraphQLBoolean }
65029
64973
  })
65030
64974
  });
65031
64975
  const createInputType = new import_graphql5.GraphQLInputObjectType({
@@ -65049,7 +64993,7 @@ class GraphQLSchema {
65049
64993
  name: `Update${className}Payload`,
65050
64994
  fields: () => ({
65051
64995
  [classNameWithFirstLetterLowerCase]: { type: object },
65052
- clientMutationId: { type: import_graphql5.GraphQLString }
64996
+ ok: { type: import_graphql5.GraphQLBoolean }
65053
64997
  })
65054
64998
  });
65055
64999
  const updateInputType = new import_graphql5.GraphQLInputObjectType({
@@ -65073,7 +65017,7 @@ class GraphQLSchema {
65073
65017
  name: `Delete${className}Payload`,
65074
65018
  fields: () => ({
65075
65019
  [classNameWithFirstLetterLowerCase]: { type: object },
65076
- clientMutationId: { type: import_graphql5.GraphQLString }
65020
+ ok: { type: import_graphql5.GraphQLBoolean }
65077
65021
  })
65078
65022
  });
65079
65023
  const deleteInputType = new import_graphql5.GraphQLInputObjectType({
@@ -65438,13 +65382,13 @@ class EmailPassword {
65438
65382
  ...context,
65439
65383
  isRoot: true
65440
65384
  },
65441
- fields: ["authentication"],
65385
+ fields: ["id", "authentication"],
65442
65386
  first: 1
65443
65387
  });
65444
65388
  if (users.length === 0)
65445
65389
  throw new Error("Invalid authentication credentials");
65446
65390
  const user = users[0];
65447
- const userDatabasePassword = user.authentication?.emailPassword?.password;
65391
+ const userDatabasePassword = user?.authentication?.emailPassword?.password;
65448
65392
  if (!userDatabasePassword)
65449
65393
  throw new Error("Invalid authentication credentials");
65450
65394
  const isPasswordEquals = await verify(userDatabasePassword, input.password, {
@@ -65500,8 +65444,8 @@ class EmailPassword {
65500
65444
  const user = users[0];
65501
65445
  return {
65502
65446
  authenticationDataToSave: {
65503
- email: input.email ?? user.authentication?.emailPassword?.email,
65504
- password: input.password ? await hashPassword(input.password) : user.authentication?.emailPassword?.password
65447
+ email: input.email ?? user?.authentication?.emailPassword?.email,
65448
+ password: input.password ? await hashPassword(input.password) : user?.authentication?.emailPassword?.password
65505
65449
  }
65506
65450
  };
65507
65451
  }
@@ -65562,8 +65506,10 @@ class Google3 {
65562
65506
  ...context,
65563
65507
  isRoot: true
65564
65508
  },
65565
- fields: ["*"]
65509
+ fields: ["*", "id"]
65566
65510
  });
65511
+ if (!createdUser)
65512
+ throw new Error("User not found");
65567
65513
  return {
65568
65514
  user: createdUser,
65569
65515
  oauth: {
@@ -65574,6 +65520,8 @@ class Google3 {
65574
65520
  }
65575
65521
  };
65576
65522
  }
65523
+ if (!user[0])
65524
+ throw new Error("User not found");
65577
65525
  return {
65578
65526
  user: user[0],
65579
65527
  oauth: {
@@ -75634,7 +75582,7 @@ var initializeRoles = async (wabe) => {
75634
75582
  }
75635
75583
  }
75636
75584
  });
75637
- const alreadyCreatedRoles = res.map((role) => role.name);
75585
+ const alreadyCreatedRoles = res.map((role) => role?.name).filter(notEmpty);
75638
75586
  const objectsToCreate = roles.filter((role) => !alreadyCreatedRoles.includes(role)).map((role) => ({ name: role }));
75639
75587
  if (objectsToCreate.length === 0)
75640
75588
  return;