bigal 15.7.0 → 15.8.0

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/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ # [15.8.0](https://github.com/bigalorm/bigal/compare/v15.7.0...v15.8.0) (2026-01-24)
2
+
3
+ ### Features
4
+
5
+ - Support .toJSON for create, update, and delete calls ([#284](https://github.com/bigalorm/bigal/issues/284)) ([d74c6f0](https://github.com/bigalorm/bigal/commit/d74c6f07a036b44cf43cdcce9224c3b256dbb3dc))
6
+
1
7
  # [15.7.0](https://github.com/bigalorm/bigal/compare/v15.6.0...v15.7.0) (2026-01-19)
2
8
 
3
9
  ### Features
package/dist/index.cjs CHANGED
@@ -3108,21 +3108,12 @@ class Repository extends ReadonlyRepository {
3108
3108
  * @param {object} [options.onConflict] - Options to handle conflicts due to a unique constraint or exclusion constraint error during insert
3109
3109
  * @returns {object|object[]|void} Return value from the db
3110
3110
  */
3111
- async create(values, options) {
3111
+ create(values, options) {
3112
3112
  if (this.model.readonly) {
3113
3113
  throw new Error(`${this.model.name} is readonly.`);
3114
3114
  }
3115
- if (Array.isArray(values) && !values.length) {
3116
- return [];
3117
- }
3118
- const beforeCreate = this._type.beforeCreate;
3119
- if (beforeCreate) {
3120
- if (Array.isArray(values)) {
3121
- values = await Promise.all(values.map(async (value) => beforeCreate(value)));
3122
- } else {
3123
- values = await beforeCreate(values);
3124
- }
3125
- }
3115
+ const modelInstance = this;
3116
+ const isArray = Array.isArray(values);
3126
3117
  let returnRecords = true;
3127
3118
  let returnSelect;
3128
3119
  if (options) {
@@ -3132,26 +3123,56 @@ class Repository extends ReadonlyRepository {
3132
3123
  returnSelect = options.returnSelect;
3133
3124
  }
3134
3125
  }
3135
- const { query, params } = getInsertQueryAndParams({
3136
- repositoriesByModelNameLowered: this._repositoriesByModelNameLowered,
3137
- model: this.model,
3138
- values,
3139
- returnRecords,
3140
- returnSelect,
3141
- onConflict: options?.onConflict
3142
- });
3143
- const results = await this._pool.query(query, params);
3144
- if (returnRecords) {
3145
- if (Array.isArray(values)) {
3146
- return this._buildInstances(results.rows);
3147
- }
3148
- const firstResult = results.rows[0];
3149
- if (firstResult) {
3150
- return this._buildInstance(firstResult);
3126
+ let returnAsPlainObjects = false;
3127
+ const result = {
3128
+ toJSON() {
3129
+ returnAsPlainObjects = true;
3130
+ return result;
3131
+ },
3132
+ async then(resolve, reject) {
3133
+ try {
3134
+ if (isArray && !values.length) {
3135
+ return resolve ? await resolve([]) : [];
3136
+ }
3137
+ const beforeCreate = modelInstance._type.beforeCreate;
3138
+ if (beforeCreate) {
3139
+ if (isArray) {
3140
+ values = await Promise.all(values.map(async (value) => beforeCreate(value)));
3141
+ } else {
3142
+ values = await beforeCreate(values);
3143
+ }
3144
+ }
3145
+ const { query, params } = getInsertQueryAndParams({
3146
+ repositoriesByModelNameLowered: modelInstance._repositoriesByModelNameLowered,
3147
+ model: modelInstance.model,
3148
+ values,
3149
+ returnRecords,
3150
+ returnSelect,
3151
+ onConflict: options?.onConflict
3152
+ });
3153
+ const results = await modelInstance._pool.query(query, params);
3154
+ if (returnRecords) {
3155
+ if (isArray) {
3156
+ const entities = returnAsPlainObjects ? modelInstance._buildPlainObjects(results.rows) : modelInstance._buildInstances(results.rows);
3157
+ return resolve ? await resolve(entities) : entities;
3158
+ }
3159
+ const firstResult = results.rows[0];
3160
+ if (firstResult) {
3161
+ const entity = returnAsPlainObjects ? modelInstance._buildPlainObject(firstResult) : modelInstance._buildInstance(firstResult);
3162
+ return resolve ? await resolve(entity) : entity;
3163
+ }
3164
+ throw new Error("Unknown error getting created rows back from the database");
3165
+ }
3166
+ return resolve ? await resolve(void 0) : void 0;
3167
+ } catch (ex) {
3168
+ if (reject) {
3169
+ return reject(ex);
3170
+ }
3171
+ throw ex;
3172
+ }
3151
3173
  }
3152
- throw new Error("Unknown error getting created rows back from the database");
3153
- }
3154
- return void 0;
3174
+ };
3175
+ return result;
3155
3176
  }
3156
3177
  /**
3157
3178
  * Updates object(s) matching the where query, with the specified values
@@ -3162,16 +3183,14 @@ class Repository extends ReadonlyRepository {
3162
3183
  * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
3163
3184
  * @returns {object[]|void} Return values from the db or `true` if returnRecords=false
3164
3185
  */
3165
- async update(where, values, options) {
3186
+ update(where, values, options) {
3166
3187
  if (this.model.readonly) {
3167
3188
  throw new Error(`${this.model.name} is readonly.`);
3168
3189
  }
3169
3190
  if (typeof where === "string") {
3170
3191
  throw new Error("The query cannot be a string, it must be an object");
3171
3192
  }
3172
- if (this._type.beforeUpdate) {
3173
- values = await this._type.beforeUpdate(values);
3174
- }
3193
+ const modelInstance = this;
3175
3194
  let returnRecords = true;
3176
3195
  let returnSelect;
3177
3196
  if (options) {
@@ -3181,19 +3200,40 @@ class Repository extends ReadonlyRepository {
3181
3200
  returnSelect = options.returnSelect;
3182
3201
  }
3183
3202
  }
3184
- const { query, params } = getUpdateQueryAndParams({
3185
- repositoriesByModelNameLowered: this._repositoriesByModelNameLowered,
3186
- model: this.model,
3187
- where,
3188
- values,
3189
- returnRecords,
3190
- returnSelect
3191
- });
3192
- const results = await this._pool.query(query, params);
3193
- if (returnRecords) {
3194
- return this._buildInstances(results.rows);
3195
- }
3196
- return void 0;
3203
+ let returnAsPlainObjects = false;
3204
+ const result = {
3205
+ toJSON() {
3206
+ returnAsPlainObjects = true;
3207
+ return result;
3208
+ },
3209
+ async then(resolve, reject) {
3210
+ try {
3211
+ if (modelInstance._type.beforeUpdate) {
3212
+ values = await modelInstance._type.beforeUpdate(values);
3213
+ }
3214
+ const { query, params } = getUpdateQueryAndParams({
3215
+ repositoriesByModelNameLowered: modelInstance._repositoriesByModelNameLowered,
3216
+ model: modelInstance.model,
3217
+ where,
3218
+ values,
3219
+ returnRecords,
3220
+ returnSelect
3221
+ });
3222
+ const results = await modelInstance._pool.query(query, params);
3223
+ if (returnRecords) {
3224
+ const entities = returnAsPlainObjects ? modelInstance._buildPlainObjects(results.rows) : modelInstance._buildInstances(results.rows);
3225
+ return resolve ? await resolve(entities) : entities;
3226
+ }
3227
+ return resolve ? await resolve(void 0) : void 0;
3228
+ } catch (ex) {
3229
+ if (reject) {
3230
+ return reject(ex);
3231
+ }
3232
+ throw ex;
3233
+ }
3234
+ }
3235
+ };
3236
+ return result;
3197
3237
  }
3198
3238
  /**
3199
3239
  * Destroys object(s) matching the where query
@@ -3211,15 +3251,15 @@ class Repository extends ReadonlyRepository {
3211
3251
  const modelInstance = this;
3212
3252
  const returnSelect = options?.returnSelect;
3213
3253
  const returnRecords = options?.returnRecords ?? !!returnSelect;
3214
- return {
3215
- /**
3216
- * Filters the query
3217
- * @param {object} value - Object representing the where query
3218
- * @returns Query instance
3219
- */
3254
+ let returnAsPlainObjects = false;
3255
+ const result = {
3220
3256
  where(value) {
3221
3257
  where = value;
3222
- return this;
3258
+ return result;
3259
+ },
3260
+ toJSON() {
3261
+ returnAsPlainObjects = true;
3262
+ return result;
3223
3263
  },
3224
3264
  async then(resolve, reject) {
3225
3265
  if (typeof where === "string") {
@@ -3233,9 +3273,10 @@ class Repository extends ReadonlyRepository {
3233
3273
  returnRecords,
3234
3274
  returnSelect
3235
3275
  });
3236
- const result = await modelInstance._pool.query(query, params);
3276
+ const queryResult = await modelInstance._pool.query(query, params);
3237
3277
  if (returnRecords) {
3238
- return await resolve(modelInstance._buildInstances(result.rows));
3278
+ const entities = returnAsPlainObjects ? modelInstance._buildPlainObjects(queryResult.rows) : modelInstance._buildInstances(queryResult.rows);
3279
+ return await resolve(entities);
3239
3280
  }
3240
3281
  return await resolve();
3241
3282
  } catch (ex) {
@@ -3251,6 +3292,7 @@ ${stack}`;
3251
3292
  }
3252
3293
  }
3253
3294
  };
3295
+ return result;
3254
3296
  }
3255
3297
  }
3256
3298
 
package/dist/index.d.cts CHANGED
@@ -466,6 +466,47 @@ interface CountResult<TEntity extends Entity> extends PromiseLike<number> {
466
466
  where(args: WhereQuery<TEntity>): CountResult<TEntity> | number;
467
467
  }
468
468
 
469
+ /**
470
+ * Result of a create operation that returns a plain object (after calling toJSON())
471
+ */
472
+ interface CreateResultJSON<T extends Entity> extends PromiseLike<PlainObject<QueryResult<T>>> {
473
+ /**
474
+ * Returns results as plain objects instead of entity class instances.
475
+ * Useful for when data must be serializable.
476
+ */
477
+ toJSON(): CreateResultJSON<T>;
478
+ }
479
+ /**
480
+ * Result of a create operation that returns multiple plain objects (after calling toJSON())
481
+ */
482
+ interface CreateResultArrayJSON<T extends Entity> extends PromiseLike<PlainObject<QueryResult<T>>[]> {
483
+ /**
484
+ * Returns results as plain objects instead of entity class instances.
485
+ * Useful for when data must be serializable.
486
+ */
487
+ toJSON(): CreateResultArrayJSON<T>;
488
+ }
489
+ /**
490
+ * Result of a create operation that returns a single record
491
+ */
492
+ interface CreateResult<T extends Entity> extends PromiseLike<QueryResult<T>> {
493
+ /**
494
+ * Returns results as plain objects instead of entity class instances.
495
+ * Useful for when data must be serializable.
496
+ */
497
+ toJSON(): CreateResultJSON<T>;
498
+ }
499
+ /**
500
+ * Result of a create operation that returns multiple records
501
+ */
502
+ interface CreateResultArray<T extends Entity> extends PromiseLike<QueryResult<T>[]> {
503
+ /**
504
+ * Returns results as plain objects instead of entity class instances.
505
+ * Useful for when data must be serializable.
506
+ */
507
+ toJSON(): CreateResultArrayJSON<T>;
508
+ }
509
+
469
510
  declare class ScalarSubquery<TValue> {
470
511
  private readonly _resultType;
471
512
  readonly _aggregate: 'avg' | 'count' | 'max' | 'min' | 'sum';
@@ -654,9 +695,50 @@ interface ReturnRecords<T extends Entity, K extends keyof T> {
654
695
  }
655
696
  type DeleteOptions<T extends Entity, K extends keyof T = keyof T> = ReturnRecords<T, K> | ReturnSelect<T, K>;
656
697
 
698
+ /**
699
+ * Result of a destroy operation that returns plain objects (after calling toJSON())
700
+ */
701
+ interface DestroyResultJSON<TEntity extends Entity, TReturn> extends PromiseLike<PlainObject<TReturn>[]> {
702
+ where(args: WhereQuery<TEntity>): DestroyResultJSON<TEntity, TReturn>;
703
+ }
704
+ /**
705
+ * Result of a destroy operation that does not return records
706
+ */
657
707
  interface DestroyResult<TEntity extends Entity, TReturn> extends PromiseLike<TReturn> {
658
708
  where(args: WhereQuery<TEntity>): DestroyResult<TEntity, TReturn>;
659
709
  }
710
+ /**
711
+ * Result of a destroy operation that returns records (includes toJSON method)
712
+ */
713
+ interface DestroyResultWithRecords<TEntity extends Entity, TReturn> extends PromiseLike<TReturn[]> {
714
+ where(args: WhereQuery<TEntity>): DestroyResultWithRecords<TEntity, TReturn>;
715
+ /**
716
+ * Returns results as plain objects instead of entity class instances.
717
+ * Useful for when data must be serializable.
718
+ */
719
+ toJSON(): DestroyResultJSON<TEntity, TReturn>;
720
+ }
721
+
722
+ /**
723
+ * Result of an update operation that returns plain objects (after calling toJSON())
724
+ */
725
+ interface UpdateResultJSON<T extends Entity> extends PromiseLike<PlainObject<QueryResult<T>>[]> {
726
+ /**
727
+ * Returns results as plain objects instead of entity class instances.
728
+ * Useful for when data must be serializable.
729
+ */
730
+ toJSON(): UpdateResultJSON<T>;
731
+ }
732
+ /**
733
+ * Result of an update operation that returns records
734
+ */
735
+ interface UpdateResult<T extends Entity> extends PromiseLike<QueryResult<T>[]> {
736
+ /**
737
+ * Returns results as plain objects instead of entity class instances.
738
+ * Useful for when data must be serializable.
739
+ */
740
+ toJSON(): UpdateResultJSON<T>;
741
+ }
660
742
 
661
743
  interface FindOneArgs<T extends Entity, K extends keyof T = string & keyof OmitFunctions<OmitEntityCollections<T>> & keyof T> {
662
744
  select?: (K & string & keyof OmitFunctions<OmitEntityCollections<T>>)[];
@@ -795,7 +877,7 @@ interface IRepository<T extends Entity> extends IReadonlyRepository<T> {
795
877
  * @param {object} [options.onConflict] - Options to handle conflicts due to a unique constraint or exclusion constraint error during insert
796
878
  * @returns {object}
797
879
  */
798
- create(values: CreateUpdateParams<T>, options?: OnConflictOptions<T> | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): Promise<QueryResult<T>>;
880
+ create(values: CreateUpdateParams<T>, options?: OnConflictOptions<T> | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): CreateResult<T>;
799
881
  /**
800
882
  * Creates an object or objects using the specified values
801
883
  * @param {object|object[]} values - Values to insert as multiple new objects.
@@ -813,7 +895,7 @@ interface IRepository<T extends Entity> extends IReadonlyRepository<T> {
813
895
  * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
814
896
  * @returns {object[]}
815
897
  */
816
- create(values: CreateUpdateParams<T>[], options?: (OnConflictOptions<T> & Partial<ReturnSelect$1<T>>) | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): Promise<QueryResult<T>[]>;
898
+ create(values: CreateUpdateParams<T>[], options?: (OnConflictOptions<T> & Partial<ReturnSelect$1<T>>) | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): CreateResultArray<T>;
817
899
  /**
818
900
  * Creates an object using the specified values
819
901
  * @param {object|object[]} values - Values to insert as a new object. If an array is specified, multiple rows will be inserted
@@ -823,7 +905,7 @@ interface IRepository<T extends Entity> extends IReadonlyRepository<T> {
823
905
  * @param {object} [options.onConflict] - Options to handle conflicts due to a unique constraint or exclusion constraint error during insert
824
906
  * @returns {object|object[]|void} Return value from the db
825
907
  */
826
- create(values: CreateUpdateParams<T> | CreateUpdateParams<T>[], options?: CreateOptions<T>): Promise<QueryResult<T>[]> | Promise<QueryResult<T>> | Promise<void>;
908
+ create(values: CreateUpdateParams<T> | CreateUpdateParams<T>[], options?: CreateOptions<T>): CreateResult<T> | CreateResultArray<T> | Promise<void>;
827
909
  /**
828
910
  * Updates object(s) matching the where query, with the specified values
829
911
  * @param {object} where - Object representing the where query
@@ -842,7 +924,7 @@ interface IRepository<T extends Entity> extends IReadonlyRepository<T> {
842
924
  * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
843
925
  * @returns {object[]}
844
926
  */
845
- update(where: WhereQuery<T>, values: CreateUpdateParams<T>, options?: ReturnSelect$1<T>): Promise<QueryResult<T>[]>;
927
+ update(where: WhereQuery<T>, values: CreateUpdateParams<T>, options?: ReturnSelect$1<T>): UpdateResult<T>;
846
928
  /**
847
929
  * Updates object(s) matching the where query, with the specified values
848
930
  * @param {object} where - Object representing the where query
@@ -852,7 +934,7 @@ interface IRepository<T extends Entity> extends IReadonlyRepository<T> {
852
934
  * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
853
935
  * @returns {object[]|void} Return values from the db or `true` if returnRecords=false
854
936
  */
855
- update(where: WhereQuery<T>, values: CreateUpdateParams<T>, options?: CreateUpdateOptions<T>): Promise<QueryResult<T>[]> | Promise<void>;
937
+ update(where: WhereQuery<T>, values: CreateUpdateParams<T>, options?: CreateUpdateOptions<T>): Promise<void> | UpdateResult<T>;
856
938
  /**
857
939
  * Destroys object(s) matching the where query
858
940
  * @param {object} [where] - Object representing the where query
@@ -867,16 +949,7 @@ interface IRepository<T extends Entity> extends IReadonlyRepository<T> {
867
949
  * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
868
950
  * @returns {object[]}
869
951
  */
870
- destroy(where: WhereQuery<T>, options: DeleteOptions<T>): DestroyResult<T, QueryResult<T>[]>;
871
- /**
872
- * Destroys object(s) matching the where query
873
- * @param {object} where - Object representing the where query
874
- * @param {object} [options]
875
- * @param {boolean} [options.returnRecords=false] - Determines if inserted records should be returned
876
- * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
877
- * @returns {object[]|void} `void` or records affected if returnRecords=true
878
- */
879
- destroy<TOptions extends DeleteOptions<T> = DeleteOptions<T>>(where: WhereQuery<T>, options?: TOptions): TOptions extends DeleteOptions<T> ? Promise<void> : DestroyResult<T, QueryResult<T>[]>;
952
+ destroy(where: WhereQuery<T>, options: DeleteOptions<T>): DestroyResultWithRecords<T, QueryResult<T>>;
880
953
  }
881
954
 
882
955
  declare function subquery<T extends Entity>(repository: IReadonlyRepository<T> | IRepository<T>): SubqueryBuilder<T>;
@@ -1017,7 +1090,7 @@ declare class Repository<T extends Entity> extends ReadonlyRepository<T> impleme
1017
1090
  * @param {object} [options.onConflict] - Options to handle conflicts due to a unique constraint or exclusion constraint error during insert
1018
1091
  * @returns {object}
1019
1092
  */
1020
- create(values: CreateUpdateParams<T>, options?: OnConflictOptions<T> | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): Promise<QueryResult<T>>;
1093
+ create(values: CreateUpdateParams<T>, options?: OnConflictOptions<T> | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): CreateResult<T>;
1021
1094
  /**
1022
1095
  * Creates an object or objects using the specified values
1023
1096
  * @param {object|object[]} values - Values to insert as multiple new objects.
@@ -1035,7 +1108,7 @@ declare class Repository<T extends Entity> extends ReadonlyRepository<T> impleme
1035
1108
  * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
1036
1109
  * @returns {object[]}
1037
1110
  */
1038
- create(values: CreateUpdateParams<T>[], options?: (OnConflictOptions<T> & Partial<ReturnSelect$1<T>>) | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): Promise<QueryResult<T>[]>;
1111
+ create(values: CreateUpdateParams<T>[], options?: (OnConflictOptions<T> & Partial<ReturnSelect$1<T>>) | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): CreateResultArray<T>;
1039
1112
  /**
1040
1113
  * Updates object(s) matching the where query, with the specified values
1041
1114
  * @param {object} where - Object representing the where query
@@ -1053,7 +1126,7 @@ declare class Repository<T extends Entity> extends ReadonlyRepository<T> impleme
1053
1126
  * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
1054
1127
  * @returns {object[]}
1055
1128
  */
1056
- update(where: WhereQuery<T>, values: CreateUpdateParams<T>, options?: ReturnSelect$1<T>): Promise<QueryResult<T>[]>;
1129
+ update(where: WhereQuery<T>, values: CreateUpdateParams<T>, options?: ReturnSelect$1<T>): UpdateResult<T>;
1057
1130
  /**
1058
1131
  * Destroys object(s) matching the where query
1059
1132
  * @param {object} [where] - Object representing the where query
@@ -1068,7 +1141,7 @@ declare class Repository<T extends Entity> extends ReadonlyRepository<T> impleme
1068
1141
  * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
1069
1142
  * @returns {object[]}
1070
1143
  */
1071
- destroy(where: WhereQuery<T>, options: DeleteOptions<T>): DestroyResult<T, QueryResult<T>[]>;
1144
+ destroy(where: WhereQuery<T>, options: DeleteOptions<T>): DestroyResultWithRecords<T, QueryResult<T>>;
1072
1145
  }
1073
1146
 
1074
1147
  interface ColumnBaseOptions {
@@ -1198,4 +1271,4 @@ interface InitializeOptions extends IConnection {
1198
1271
  declare function initialize({ models, pool, readonlyPool, connections, expose }: InitializeOptions): Record<string, IReadonlyRepository<Entity> | IRepository<Entity>>;
1199
1272
 
1200
1273
  export { ColumnBaseMetadata, ColumnCollectionMetadata, ColumnModelMetadata, ColumnTypeMetadata, Entity, ModelMetadata, QueryError, ReadonlyRepository, Repository, ScalarSubquery, SelectBuilder, SubqueryBuilder, column, createDateColumn, getMetadataStorage, initialize, isSubqueryJoin, primaryColumn, subquery, table, updateDateColumn, versionColumn };
1201
- export type { AggregateBuilder, ClassLike, ColumnBaseMetadataOptions, ColumnCollectionMetadataOptions, ColumnMetadata, ColumnModelMetadataOptions, ColumnModifierMetadata, ColumnTypeMetadataOptions, Comparer, CountResult, CreateUpdateOptions, CreateUpdateParams, DeleteOptions, DestroyResult, DoNotReturnRecords, EntityFieldValue, EntityPrimitiveOrId, EntityStatic, ExcludeEntityCollections, ExcludeFunctions, FindArgs, FindOneArgs, FindOneResult, FindOneResultJSON, FindQueryWithCount, FindQueryWithCountJSON, FindResult, FindResultJSON, FindWithCountResult, GetValueType, HavingComparer, HavingCondition, IConnection, IReadonlyRepository, IRepository, IRepositoryOptions, IncludeFunctions, InitializeOptions, IsValueOfType, JoinDefinition, JoinInfo, JoinType, JoinedSort, JoinedWhereQuery, JsonConstraint, LiteralValues, ModelJoinDefinition, ModelMetadataOptions, ModelRelationshipKeys, MultipleSortString, NegatableConstraint, NotEntity, NotEntityBrand, NumberOrDateConstraint, NumberOrDateConstraintWithSubquery, OmitEntityCollections, OmitFunctions, OrderBy, PaginateOptions, PickAsType, PickByValueType, PickFunctions, PlainObject, PoolLike, PoolQueryResult, PopulateArgs, Populated, QueryResult, QueryResultOptionalPopulated, QueryResultPopulated, QueryResultRow, ReturnSelect$1 as ReturnSelect, ScalarSubqueryConstraint, SelectAggregateExpression, SelectItem, Sort, SortObject, SortObjectValue, SortString, StringConstraint, SubqueryBuilderLike, SubqueryInConstraint, SubqueryJoinDefinition, SubqueryJoinOnCondition, WhereClauseValue, WhereQuery, WhereQueryStatement };
1274
+ export type { AggregateBuilder, ClassLike, ColumnBaseMetadataOptions, ColumnCollectionMetadataOptions, ColumnMetadata, ColumnModelMetadataOptions, ColumnModifierMetadata, ColumnTypeMetadataOptions, Comparer, CountResult, CreateResult, CreateResultArray, CreateResultArrayJSON, CreateResultJSON, CreateUpdateOptions, CreateUpdateParams, DeleteOptions, DestroyResult, DestroyResultJSON, DestroyResultWithRecords, DoNotReturnRecords, EntityFieldValue, EntityPrimitiveOrId, EntityStatic, ExcludeEntityCollections, ExcludeFunctions, FindArgs, FindOneArgs, FindOneResult, FindOneResultJSON, FindQueryWithCount, FindQueryWithCountJSON, FindResult, FindResultJSON, FindWithCountResult, GetValueType, HavingComparer, HavingCondition, IConnection, IReadonlyRepository, IRepository, IRepositoryOptions, IncludeFunctions, InitializeOptions, IsValueOfType, JoinDefinition, JoinInfo, JoinType, JoinedSort, JoinedWhereQuery, JsonConstraint, LiteralValues, ModelJoinDefinition, ModelMetadataOptions, ModelRelationshipKeys, MultipleSortString, NegatableConstraint, NotEntity, NotEntityBrand, NumberOrDateConstraint, NumberOrDateConstraintWithSubquery, OmitEntityCollections, OmitFunctions, OrderBy, PaginateOptions, PickAsType, PickByValueType, PickFunctions, PlainObject, PoolLike, PoolQueryResult, PopulateArgs, Populated, QueryResult, QueryResultOptionalPopulated, QueryResultPopulated, QueryResultRow, ReturnSelect$1 as ReturnSelect, ScalarSubqueryConstraint, SelectAggregateExpression, SelectItem, Sort, SortObject, SortObjectValue, SortString, StringConstraint, SubqueryBuilderLike, SubqueryInConstraint, SubqueryJoinDefinition, SubqueryJoinOnCondition, UpdateResult, UpdateResultJSON, WhereClauseValue, WhereQuery, WhereQueryStatement };
package/dist/index.d.mts CHANGED
@@ -466,6 +466,47 @@ interface CountResult<TEntity extends Entity> extends PromiseLike<number> {
466
466
  where(args: WhereQuery<TEntity>): CountResult<TEntity> | number;
467
467
  }
468
468
 
469
+ /**
470
+ * Result of a create operation that returns a plain object (after calling toJSON())
471
+ */
472
+ interface CreateResultJSON<T extends Entity> extends PromiseLike<PlainObject<QueryResult<T>>> {
473
+ /**
474
+ * Returns results as plain objects instead of entity class instances.
475
+ * Useful for when data must be serializable.
476
+ */
477
+ toJSON(): CreateResultJSON<T>;
478
+ }
479
+ /**
480
+ * Result of a create operation that returns multiple plain objects (after calling toJSON())
481
+ */
482
+ interface CreateResultArrayJSON<T extends Entity> extends PromiseLike<PlainObject<QueryResult<T>>[]> {
483
+ /**
484
+ * Returns results as plain objects instead of entity class instances.
485
+ * Useful for when data must be serializable.
486
+ */
487
+ toJSON(): CreateResultArrayJSON<T>;
488
+ }
489
+ /**
490
+ * Result of a create operation that returns a single record
491
+ */
492
+ interface CreateResult<T extends Entity> extends PromiseLike<QueryResult<T>> {
493
+ /**
494
+ * Returns results as plain objects instead of entity class instances.
495
+ * Useful for when data must be serializable.
496
+ */
497
+ toJSON(): CreateResultJSON<T>;
498
+ }
499
+ /**
500
+ * Result of a create operation that returns multiple records
501
+ */
502
+ interface CreateResultArray<T extends Entity> extends PromiseLike<QueryResult<T>[]> {
503
+ /**
504
+ * Returns results as plain objects instead of entity class instances.
505
+ * Useful for when data must be serializable.
506
+ */
507
+ toJSON(): CreateResultArrayJSON<T>;
508
+ }
509
+
469
510
  declare class ScalarSubquery<TValue> {
470
511
  private readonly _resultType;
471
512
  readonly _aggregate: 'avg' | 'count' | 'max' | 'min' | 'sum';
@@ -654,9 +695,50 @@ interface ReturnRecords<T extends Entity, K extends keyof T> {
654
695
  }
655
696
  type DeleteOptions<T extends Entity, K extends keyof T = keyof T> = ReturnRecords<T, K> | ReturnSelect<T, K>;
656
697
 
698
+ /**
699
+ * Result of a destroy operation that returns plain objects (after calling toJSON())
700
+ */
701
+ interface DestroyResultJSON<TEntity extends Entity, TReturn> extends PromiseLike<PlainObject<TReturn>[]> {
702
+ where(args: WhereQuery<TEntity>): DestroyResultJSON<TEntity, TReturn>;
703
+ }
704
+ /**
705
+ * Result of a destroy operation that does not return records
706
+ */
657
707
  interface DestroyResult<TEntity extends Entity, TReturn> extends PromiseLike<TReturn> {
658
708
  where(args: WhereQuery<TEntity>): DestroyResult<TEntity, TReturn>;
659
709
  }
710
+ /**
711
+ * Result of a destroy operation that returns records (includes toJSON method)
712
+ */
713
+ interface DestroyResultWithRecords<TEntity extends Entity, TReturn> extends PromiseLike<TReturn[]> {
714
+ where(args: WhereQuery<TEntity>): DestroyResultWithRecords<TEntity, TReturn>;
715
+ /**
716
+ * Returns results as plain objects instead of entity class instances.
717
+ * Useful for when data must be serializable.
718
+ */
719
+ toJSON(): DestroyResultJSON<TEntity, TReturn>;
720
+ }
721
+
722
+ /**
723
+ * Result of an update operation that returns plain objects (after calling toJSON())
724
+ */
725
+ interface UpdateResultJSON<T extends Entity> extends PromiseLike<PlainObject<QueryResult<T>>[]> {
726
+ /**
727
+ * Returns results as plain objects instead of entity class instances.
728
+ * Useful for when data must be serializable.
729
+ */
730
+ toJSON(): UpdateResultJSON<T>;
731
+ }
732
+ /**
733
+ * Result of an update operation that returns records
734
+ */
735
+ interface UpdateResult<T extends Entity> extends PromiseLike<QueryResult<T>[]> {
736
+ /**
737
+ * Returns results as plain objects instead of entity class instances.
738
+ * Useful for when data must be serializable.
739
+ */
740
+ toJSON(): UpdateResultJSON<T>;
741
+ }
660
742
 
661
743
  interface FindOneArgs<T extends Entity, K extends keyof T = string & keyof OmitFunctions<OmitEntityCollections<T>> & keyof T> {
662
744
  select?: (K & string & keyof OmitFunctions<OmitEntityCollections<T>>)[];
@@ -795,7 +877,7 @@ interface IRepository<T extends Entity> extends IReadonlyRepository<T> {
795
877
  * @param {object} [options.onConflict] - Options to handle conflicts due to a unique constraint or exclusion constraint error during insert
796
878
  * @returns {object}
797
879
  */
798
- create(values: CreateUpdateParams<T>, options?: OnConflictOptions<T> | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): Promise<QueryResult<T>>;
880
+ create(values: CreateUpdateParams<T>, options?: OnConflictOptions<T> | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): CreateResult<T>;
799
881
  /**
800
882
  * Creates an object or objects using the specified values
801
883
  * @param {object|object[]} values - Values to insert as multiple new objects.
@@ -813,7 +895,7 @@ interface IRepository<T extends Entity> extends IReadonlyRepository<T> {
813
895
  * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
814
896
  * @returns {object[]}
815
897
  */
816
- create(values: CreateUpdateParams<T>[], options?: (OnConflictOptions<T> & Partial<ReturnSelect$1<T>>) | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): Promise<QueryResult<T>[]>;
898
+ create(values: CreateUpdateParams<T>[], options?: (OnConflictOptions<T> & Partial<ReturnSelect$1<T>>) | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): CreateResultArray<T>;
817
899
  /**
818
900
  * Creates an object using the specified values
819
901
  * @param {object|object[]} values - Values to insert as a new object. If an array is specified, multiple rows will be inserted
@@ -823,7 +905,7 @@ interface IRepository<T extends Entity> extends IReadonlyRepository<T> {
823
905
  * @param {object} [options.onConflict] - Options to handle conflicts due to a unique constraint or exclusion constraint error during insert
824
906
  * @returns {object|object[]|void} Return value from the db
825
907
  */
826
- create(values: CreateUpdateParams<T> | CreateUpdateParams<T>[], options?: CreateOptions<T>): Promise<QueryResult<T>[]> | Promise<QueryResult<T>> | Promise<void>;
908
+ create(values: CreateUpdateParams<T> | CreateUpdateParams<T>[], options?: CreateOptions<T>): CreateResult<T> | CreateResultArray<T> | Promise<void>;
827
909
  /**
828
910
  * Updates object(s) matching the where query, with the specified values
829
911
  * @param {object} where - Object representing the where query
@@ -842,7 +924,7 @@ interface IRepository<T extends Entity> extends IReadonlyRepository<T> {
842
924
  * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
843
925
  * @returns {object[]}
844
926
  */
845
- update(where: WhereQuery<T>, values: CreateUpdateParams<T>, options?: ReturnSelect$1<T>): Promise<QueryResult<T>[]>;
927
+ update(where: WhereQuery<T>, values: CreateUpdateParams<T>, options?: ReturnSelect$1<T>): UpdateResult<T>;
846
928
  /**
847
929
  * Updates object(s) matching the where query, with the specified values
848
930
  * @param {object} where - Object representing the where query
@@ -852,7 +934,7 @@ interface IRepository<T extends Entity> extends IReadonlyRepository<T> {
852
934
  * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
853
935
  * @returns {object[]|void} Return values from the db or `true` if returnRecords=false
854
936
  */
855
- update(where: WhereQuery<T>, values: CreateUpdateParams<T>, options?: CreateUpdateOptions<T>): Promise<QueryResult<T>[]> | Promise<void>;
937
+ update(where: WhereQuery<T>, values: CreateUpdateParams<T>, options?: CreateUpdateOptions<T>): Promise<void> | UpdateResult<T>;
856
938
  /**
857
939
  * Destroys object(s) matching the where query
858
940
  * @param {object} [where] - Object representing the where query
@@ -867,16 +949,7 @@ interface IRepository<T extends Entity> extends IReadonlyRepository<T> {
867
949
  * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
868
950
  * @returns {object[]}
869
951
  */
870
- destroy(where: WhereQuery<T>, options: DeleteOptions<T>): DestroyResult<T, QueryResult<T>[]>;
871
- /**
872
- * Destroys object(s) matching the where query
873
- * @param {object} where - Object representing the where query
874
- * @param {object} [options]
875
- * @param {boolean} [options.returnRecords=false] - Determines if inserted records should be returned
876
- * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
877
- * @returns {object[]|void} `void` or records affected if returnRecords=true
878
- */
879
- destroy<TOptions extends DeleteOptions<T> = DeleteOptions<T>>(where: WhereQuery<T>, options?: TOptions): TOptions extends DeleteOptions<T> ? Promise<void> : DestroyResult<T, QueryResult<T>[]>;
952
+ destroy(where: WhereQuery<T>, options: DeleteOptions<T>): DestroyResultWithRecords<T, QueryResult<T>>;
880
953
  }
881
954
 
882
955
  declare function subquery<T extends Entity>(repository: IReadonlyRepository<T> | IRepository<T>): SubqueryBuilder<T>;
@@ -1017,7 +1090,7 @@ declare class Repository<T extends Entity> extends ReadonlyRepository<T> impleme
1017
1090
  * @param {object} [options.onConflict] - Options to handle conflicts due to a unique constraint or exclusion constraint error during insert
1018
1091
  * @returns {object}
1019
1092
  */
1020
- create(values: CreateUpdateParams<T>, options?: OnConflictOptions<T> | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): Promise<QueryResult<T>>;
1093
+ create(values: CreateUpdateParams<T>, options?: OnConflictOptions<T> | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): CreateResult<T>;
1021
1094
  /**
1022
1095
  * Creates an object or objects using the specified values
1023
1096
  * @param {object|object[]} values - Values to insert as multiple new objects.
@@ -1035,7 +1108,7 @@ declare class Repository<T extends Entity> extends ReadonlyRepository<T> impleme
1035
1108
  * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
1036
1109
  * @returns {object[]}
1037
1110
  */
1038
- create(values: CreateUpdateParams<T>[], options?: (OnConflictOptions<T> & Partial<ReturnSelect$1<T>>) | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): Promise<QueryResult<T>[]>;
1111
+ create(values: CreateUpdateParams<T>[], options?: (OnConflictOptions<T> & Partial<ReturnSelect$1<T>>) | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): CreateResultArray<T>;
1039
1112
  /**
1040
1113
  * Updates object(s) matching the where query, with the specified values
1041
1114
  * @param {object} where - Object representing the where query
@@ -1053,7 +1126,7 @@ declare class Repository<T extends Entity> extends ReadonlyRepository<T> impleme
1053
1126
  * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
1054
1127
  * @returns {object[]}
1055
1128
  */
1056
- update(where: WhereQuery<T>, values: CreateUpdateParams<T>, options?: ReturnSelect$1<T>): Promise<QueryResult<T>[]>;
1129
+ update(where: WhereQuery<T>, values: CreateUpdateParams<T>, options?: ReturnSelect$1<T>): UpdateResult<T>;
1057
1130
  /**
1058
1131
  * Destroys object(s) matching the where query
1059
1132
  * @param {object} [where] - Object representing the where query
@@ -1068,7 +1141,7 @@ declare class Repository<T extends Entity> extends ReadonlyRepository<T> impleme
1068
1141
  * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
1069
1142
  * @returns {object[]}
1070
1143
  */
1071
- destroy(where: WhereQuery<T>, options: DeleteOptions<T>): DestroyResult<T, QueryResult<T>[]>;
1144
+ destroy(where: WhereQuery<T>, options: DeleteOptions<T>): DestroyResultWithRecords<T, QueryResult<T>>;
1072
1145
  }
1073
1146
 
1074
1147
  interface ColumnBaseOptions {
@@ -1198,4 +1271,4 @@ interface InitializeOptions extends IConnection {
1198
1271
  declare function initialize({ models, pool, readonlyPool, connections, expose }: InitializeOptions): Record<string, IReadonlyRepository<Entity> | IRepository<Entity>>;
1199
1272
 
1200
1273
  export { ColumnBaseMetadata, ColumnCollectionMetadata, ColumnModelMetadata, ColumnTypeMetadata, Entity, ModelMetadata, QueryError, ReadonlyRepository, Repository, ScalarSubquery, SelectBuilder, SubqueryBuilder, column, createDateColumn, getMetadataStorage, initialize, isSubqueryJoin, primaryColumn, subquery, table, updateDateColumn, versionColumn };
1201
- export type { AggregateBuilder, ClassLike, ColumnBaseMetadataOptions, ColumnCollectionMetadataOptions, ColumnMetadata, ColumnModelMetadataOptions, ColumnModifierMetadata, ColumnTypeMetadataOptions, Comparer, CountResult, CreateUpdateOptions, CreateUpdateParams, DeleteOptions, DestroyResult, DoNotReturnRecords, EntityFieldValue, EntityPrimitiveOrId, EntityStatic, ExcludeEntityCollections, ExcludeFunctions, FindArgs, FindOneArgs, FindOneResult, FindOneResultJSON, FindQueryWithCount, FindQueryWithCountJSON, FindResult, FindResultJSON, FindWithCountResult, GetValueType, HavingComparer, HavingCondition, IConnection, IReadonlyRepository, IRepository, IRepositoryOptions, IncludeFunctions, InitializeOptions, IsValueOfType, JoinDefinition, JoinInfo, JoinType, JoinedSort, JoinedWhereQuery, JsonConstraint, LiteralValues, ModelJoinDefinition, ModelMetadataOptions, ModelRelationshipKeys, MultipleSortString, NegatableConstraint, NotEntity, NotEntityBrand, NumberOrDateConstraint, NumberOrDateConstraintWithSubquery, OmitEntityCollections, OmitFunctions, OrderBy, PaginateOptions, PickAsType, PickByValueType, PickFunctions, PlainObject, PoolLike, PoolQueryResult, PopulateArgs, Populated, QueryResult, QueryResultOptionalPopulated, QueryResultPopulated, QueryResultRow, ReturnSelect$1 as ReturnSelect, ScalarSubqueryConstraint, SelectAggregateExpression, SelectItem, Sort, SortObject, SortObjectValue, SortString, StringConstraint, SubqueryBuilderLike, SubqueryInConstraint, SubqueryJoinDefinition, SubqueryJoinOnCondition, WhereClauseValue, WhereQuery, WhereQueryStatement };
1274
+ export type { AggregateBuilder, ClassLike, ColumnBaseMetadataOptions, ColumnCollectionMetadataOptions, ColumnMetadata, ColumnModelMetadataOptions, ColumnModifierMetadata, ColumnTypeMetadataOptions, Comparer, CountResult, CreateResult, CreateResultArray, CreateResultArrayJSON, CreateResultJSON, CreateUpdateOptions, CreateUpdateParams, DeleteOptions, DestroyResult, DestroyResultJSON, DestroyResultWithRecords, DoNotReturnRecords, EntityFieldValue, EntityPrimitiveOrId, EntityStatic, ExcludeEntityCollections, ExcludeFunctions, FindArgs, FindOneArgs, FindOneResult, FindOneResultJSON, FindQueryWithCount, FindQueryWithCountJSON, FindResult, FindResultJSON, FindWithCountResult, GetValueType, HavingComparer, HavingCondition, IConnection, IReadonlyRepository, IRepository, IRepositoryOptions, IncludeFunctions, InitializeOptions, IsValueOfType, JoinDefinition, JoinInfo, JoinType, JoinedSort, JoinedWhereQuery, JsonConstraint, LiteralValues, ModelJoinDefinition, ModelMetadataOptions, ModelRelationshipKeys, MultipleSortString, NegatableConstraint, NotEntity, NotEntityBrand, NumberOrDateConstraint, NumberOrDateConstraintWithSubquery, OmitEntityCollections, OmitFunctions, OrderBy, PaginateOptions, PickAsType, PickByValueType, PickFunctions, PlainObject, PoolLike, PoolQueryResult, PopulateArgs, Populated, QueryResult, QueryResultOptionalPopulated, QueryResultPopulated, QueryResultRow, ReturnSelect$1 as ReturnSelect, ScalarSubqueryConstraint, SelectAggregateExpression, SelectItem, Sort, SortObject, SortObjectValue, SortString, StringConstraint, SubqueryBuilderLike, SubqueryInConstraint, SubqueryJoinDefinition, SubqueryJoinOnCondition, UpdateResult, UpdateResultJSON, WhereClauseValue, WhereQuery, WhereQueryStatement };
package/dist/index.d.ts CHANGED
@@ -466,6 +466,47 @@ interface CountResult<TEntity extends Entity> extends PromiseLike<number> {
466
466
  where(args: WhereQuery<TEntity>): CountResult<TEntity> | number;
467
467
  }
468
468
 
469
+ /**
470
+ * Result of a create operation that returns a plain object (after calling toJSON())
471
+ */
472
+ interface CreateResultJSON<T extends Entity> extends PromiseLike<PlainObject<QueryResult<T>>> {
473
+ /**
474
+ * Returns results as plain objects instead of entity class instances.
475
+ * Useful for when data must be serializable.
476
+ */
477
+ toJSON(): CreateResultJSON<T>;
478
+ }
479
+ /**
480
+ * Result of a create operation that returns multiple plain objects (after calling toJSON())
481
+ */
482
+ interface CreateResultArrayJSON<T extends Entity> extends PromiseLike<PlainObject<QueryResult<T>>[]> {
483
+ /**
484
+ * Returns results as plain objects instead of entity class instances.
485
+ * Useful for when data must be serializable.
486
+ */
487
+ toJSON(): CreateResultArrayJSON<T>;
488
+ }
489
+ /**
490
+ * Result of a create operation that returns a single record
491
+ */
492
+ interface CreateResult<T extends Entity> extends PromiseLike<QueryResult<T>> {
493
+ /**
494
+ * Returns results as plain objects instead of entity class instances.
495
+ * Useful for when data must be serializable.
496
+ */
497
+ toJSON(): CreateResultJSON<T>;
498
+ }
499
+ /**
500
+ * Result of a create operation that returns multiple records
501
+ */
502
+ interface CreateResultArray<T extends Entity> extends PromiseLike<QueryResult<T>[]> {
503
+ /**
504
+ * Returns results as plain objects instead of entity class instances.
505
+ * Useful for when data must be serializable.
506
+ */
507
+ toJSON(): CreateResultArrayJSON<T>;
508
+ }
509
+
469
510
  declare class ScalarSubquery<TValue> {
470
511
  private readonly _resultType;
471
512
  readonly _aggregate: 'avg' | 'count' | 'max' | 'min' | 'sum';
@@ -654,9 +695,50 @@ interface ReturnRecords<T extends Entity, K extends keyof T> {
654
695
  }
655
696
  type DeleteOptions<T extends Entity, K extends keyof T = keyof T> = ReturnRecords<T, K> | ReturnSelect<T, K>;
656
697
 
698
+ /**
699
+ * Result of a destroy operation that returns plain objects (after calling toJSON())
700
+ */
701
+ interface DestroyResultJSON<TEntity extends Entity, TReturn> extends PromiseLike<PlainObject<TReturn>[]> {
702
+ where(args: WhereQuery<TEntity>): DestroyResultJSON<TEntity, TReturn>;
703
+ }
704
+ /**
705
+ * Result of a destroy operation that does not return records
706
+ */
657
707
  interface DestroyResult<TEntity extends Entity, TReturn> extends PromiseLike<TReturn> {
658
708
  where(args: WhereQuery<TEntity>): DestroyResult<TEntity, TReturn>;
659
709
  }
710
+ /**
711
+ * Result of a destroy operation that returns records (includes toJSON method)
712
+ */
713
+ interface DestroyResultWithRecords<TEntity extends Entity, TReturn> extends PromiseLike<TReturn[]> {
714
+ where(args: WhereQuery<TEntity>): DestroyResultWithRecords<TEntity, TReturn>;
715
+ /**
716
+ * Returns results as plain objects instead of entity class instances.
717
+ * Useful for when data must be serializable.
718
+ */
719
+ toJSON(): DestroyResultJSON<TEntity, TReturn>;
720
+ }
721
+
722
+ /**
723
+ * Result of an update operation that returns plain objects (after calling toJSON())
724
+ */
725
+ interface UpdateResultJSON<T extends Entity> extends PromiseLike<PlainObject<QueryResult<T>>[]> {
726
+ /**
727
+ * Returns results as plain objects instead of entity class instances.
728
+ * Useful for when data must be serializable.
729
+ */
730
+ toJSON(): UpdateResultJSON<T>;
731
+ }
732
+ /**
733
+ * Result of an update operation that returns records
734
+ */
735
+ interface UpdateResult<T extends Entity> extends PromiseLike<QueryResult<T>[]> {
736
+ /**
737
+ * Returns results as plain objects instead of entity class instances.
738
+ * Useful for when data must be serializable.
739
+ */
740
+ toJSON(): UpdateResultJSON<T>;
741
+ }
660
742
 
661
743
  interface FindOneArgs<T extends Entity, K extends keyof T = string & keyof OmitFunctions<OmitEntityCollections<T>> & keyof T> {
662
744
  select?: (K & string & keyof OmitFunctions<OmitEntityCollections<T>>)[];
@@ -795,7 +877,7 @@ interface IRepository<T extends Entity> extends IReadonlyRepository<T> {
795
877
  * @param {object} [options.onConflict] - Options to handle conflicts due to a unique constraint or exclusion constraint error during insert
796
878
  * @returns {object}
797
879
  */
798
- create(values: CreateUpdateParams<T>, options?: OnConflictOptions<T> | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): Promise<QueryResult<T>>;
880
+ create(values: CreateUpdateParams<T>, options?: OnConflictOptions<T> | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): CreateResult<T>;
799
881
  /**
800
882
  * Creates an object or objects using the specified values
801
883
  * @param {object|object[]} values - Values to insert as multiple new objects.
@@ -813,7 +895,7 @@ interface IRepository<T extends Entity> extends IReadonlyRepository<T> {
813
895
  * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
814
896
  * @returns {object[]}
815
897
  */
816
- create(values: CreateUpdateParams<T>[], options?: (OnConflictOptions<T> & Partial<ReturnSelect$1<T>>) | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): Promise<QueryResult<T>[]>;
898
+ create(values: CreateUpdateParams<T>[], options?: (OnConflictOptions<T> & Partial<ReturnSelect$1<T>>) | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): CreateResultArray<T>;
817
899
  /**
818
900
  * Creates an object using the specified values
819
901
  * @param {object|object[]} values - Values to insert as a new object. If an array is specified, multiple rows will be inserted
@@ -823,7 +905,7 @@ interface IRepository<T extends Entity> extends IReadonlyRepository<T> {
823
905
  * @param {object} [options.onConflict] - Options to handle conflicts due to a unique constraint or exclusion constraint error during insert
824
906
  * @returns {object|object[]|void} Return value from the db
825
907
  */
826
- create(values: CreateUpdateParams<T> | CreateUpdateParams<T>[], options?: CreateOptions<T>): Promise<QueryResult<T>[]> | Promise<QueryResult<T>> | Promise<void>;
908
+ create(values: CreateUpdateParams<T> | CreateUpdateParams<T>[], options?: CreateOptions<T>): CreateResult<T> | CreateResultArray<T> | Promise<void>;
827
909
  /**
828
910
  * Updates object(s) matching the where query, with the specified values
829
911
  * @param {object} where - Object representing the where query
@@ -842,7 +924,7 @@ interface IRepository<T extends Entity> extends IReadonlyRepository<T> {
842
924
  * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
843
925
  * @returns {object[]}
844
926
  */
845
- update(where: WhereQuery<T>, values: CreateUpdateParams<T>, options?: ReturnSelect$1<T>): Promise<QueryResult<T>[]>;
927
+ update(where: WhereQuery<T>, values: CreateUpdateParams<T>, options?: ReturnSelect$1<T>): UpdateResult<T>;
846
928
  /**
847
929
  * Updates object(s) matching the where query, with the specified values
848
930
  * @param {object} where - Object representing the where query
@@ -852,7 +934,7 @@ interface IRepository<T extends Entity> extends IReadonlyRepository<T> {
852
934
  * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
853
935
  * @returns {object[]|void} Return values from the db or `true` if returnRecords=false
854
936
  */
855
- update(where: WhereQuery<T>, values: CreateUpdateParams<T>, options?: CreateUpdateOptions<T>): Promise<QueryResult<T>[]> | Promise<void>;
937
+ update(where: WhereQuery<T>, values: CreateUpdateParams<T>, options?: CreateUpdateOptions<T>): Promise<void> | UpdateResult<T>;
856
938
  /**
857
939
  * Destroys object(s) matching the where query
858
940
  * @param {object} [where] - Object representing the where query
@@ -867,16 +949,7 @@ interface IRepository<T extends Entity> extends IReadonlyRepository<T> {
867
949
  * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
868
950
  * @returns {object[]}
869
951
  */
870
- destroy(where: WhereQuery<T>, options: DeleteOptions<T>): DestroyResult<T, QueryResult<T>[]>;
871
- /**
872
- * Destroys object(s) matching the where query
873
- * @param {object} where - Object representing the where query
874
- * @param {object} [options]
875
- * @param {boolean} [options.returnRecords=false] - Determines if inserted records should be returned
876
- * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
877
- * @returns {object[]|void} `void` or records affected if returnRecords=true
878
- */
879
- destroy<TOptions extends DeleteOptions<T> = DeleteOptions<T>>(where: WhereQuery<T>, options?: TOptions): TOptions extends DeleteOptions<T> ? Promise<void> : DestroyResult<T, QueryResult<T>[]>;
952
+ destroy(where: WhereQuery<T>, options: DeleteOptions<T>): DestroyResultWithRecords<T, QueryResult<T>>;
880
953
  }
881
954
 
882
955
  declare function subquery<T extends Entity>(repository: IReadonlyRepository<T> | IRepository<T>): SubqueryBuilder<T>;
@@ -1017,7 +1090,7 @@ declare class Repository<T extends Entity> extends ReadonlyRepository<T> impleme
1017
1090
  * @param {object} [options.onConflict] - Options to handle conflicts due to a unique constraint or exclusion constraint error during insert
1018
1091
  * @returns {object}
1019
1092
  */
1020
- create(values: CreateUpdateParams<T>, options?: OnConflictOptions<T> | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): Promise<QueryResult<T>>;
1093
+ create(values: CreateUpdateParams<T>, options?: OnConflictOptions<T> | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): CreateResult<T>;
1021
1094
  /**
1022
1095
  * Creates an object or objects using the specified values
1023
1096
  * @param {object|object[]} values - Values to insert as multiple new objects.
@@ -1035,7 +1108,7 @@ declare class Repository<T extends Entity> extends ReadonlyRepository<T> impleme
1035
1108
  * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
1036
1109
  * @returns {object[]}
1037
1110
  */
1038
- create(values: CreateUpdateParams<T>[], options?: (OnConflictOptions<T> & Partial<ReturnSelect$1<T>>) | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): Promise<QueryResult<T>[]>;
1111
+ create(values: CreateUpdateParams<T>[], options?: (OnConflictOptions<T> & Partial<ReturnSelect$1<T>>) | (Partial<OnConflictOptions<T>> & ReturnSelect$1<T>)): CreateResultArray<T>;
1039
1112
  /**
1040
1113
  * Updates object(s) matching the where query, with the specified values
1041
1114
  * @param {object} where - Object representing the where query
@@ -1053,7 +1126,7 @@ declare class Repository<T extends Entity> extends ReadonlyRepository<T> impleme
1053
1126
  * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
1054
1127
  * @returns {object[]}
1055
1128
  */
1056
- update(where: WhereQuery<T>, values: CreateUpdateParams<T>, options?: ReturnSelect$1<T>): Promise<QueryResult<T>[]>;
1129
+ update(where: WhereQuery<T>, values: CreateUpdateParams<T>, options?: ReturnSelect$1<T>): UpdateResult<T>;
1057
1130
  /**
1058
1131
  * Destroys object(s) matching the where query
1059
1132
  * @param {object} [where] - Object representing the where query
@@ -1068,7 +1141,7 @@ declare class Repository<T extends Entity> extends ReadonlyRepository<T> impleme
1068
1141
  * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
1069
1142
  * @returns {object[]}
1070
1143
  */
1071
- destroy(where: WhereQuery<T>, options: DeleteOptions<T>): DestroyResult<T, QueryResult<T>[]>;
1144
+ destroy(where: WhereQuery<T>, options: DeleteOptions<T>): DestroyResultWithRecords<T, QueryResult<T>>;
1072
1145
  }
1073
1146
 
1074
1147
  interface ColumnBaseOptions {
@@ -1198,4 +1271,4 @@ interface InitializeOptions extends IConnection {
1198
1271
  declare function initialize({ models, pool, readonlyPool, connections, expose }: InitializeOptions): Record<string, IReadonlyRepository<Entity> | IRepository<Entity>>;
1199
1272
 
1200
1273
  export { ColumnBaseMetadata, ColumnCollectionMetadata, ColumnModelMetadata, ColumnTypeMetadata, Entity, ModelMetadata, QueryError, ReadonlyRepository, Repository, ScalarSubquery, SelectBuilder, SubqueryBuilder, column, createDateColumn, getMetadataStorage, initialize, isSubqueryJoin, primaryColumn, subquery, table, updateDateColumn, versionColumn };
1201
- export type { AggregateBuilder, ClassLike, ColumnBaseMetadataOptions, ColumnCollectionMetadataOptions, ColumnMetadata, ColumnModelMetadataOptions, ColumnModifierMetadata, ColumnTypeMetadataOptions, Comparer, CountResult, CreateUpdateOptions, CreateUpdateParams, DeleteOptions, DestroyResult, DoNotReturnRecords, EntityFieldValue, EntityPrimitiveOrId, EntityStatic, ExcludeEntityCollections, ExcludeFunctions, FindArgs, FindOneArgs, FindOneResult, FindOneResultJSON, FindQueryWithCount, FindQueryWithCountJSON, FindResult, FindResultJSON, FindWithCountResult, GetValueType, HavingComparer, HavingCondition, IConnection, IReadonlyRepository, IRepository, IRepositoryOptions, IncludeFunctions, InitializeOptions, IsValueOfType, JoinDefinition, JoinInfo, JoinType, JoinedSort, JoinedWhereQuery, JsonConstraint, LiteralValues, ModelJoinDefinition, ModelMetadataOptions, ModelRelationshipKeys, MultipleSortString, NegatableConstraint, NotEntity, NotEntityBrand, NumberOrDateConstraint, NumberOrDateConstraintWithSubquery, OmitEntityCollections, OmitFunctions, OrderBy, PaginateOptions, PickAsType, PickByValueType, PickFunctions, PlainObject, PoolLike, PoolQueryResult, PopulateArgs, Populated, QueryResult, QueryResultOptionalPopulated, QueryResultPopulated, QueryResultRow, ReturnSelect$1 as ReturnSelect, ScalarSubqueryConstraint, SelectAggregateExpression, SelectItem, Sort, SortObject, SortObjectValue, SortString, StringConstraint, SubqueryBuilderLike, SubqueryInConstraint, SubqueryJoinDefinition, SubqueryJoinOnCondition, WhereClauseValue, WhereQuery, WhereQueryStatement };
1274
+ export type { AggregateBuilder, ClassLike, ColumnBaseMetadataOptions, ColumnCollectionMetadataOptions, ColumnMetadata, ColumnModelMetadataOptions, ColumnModifierMetadata, ColumnTypeMetadataOptions, Comparer, CountResult, CreateResult, CreateResultArray, CreateResultArrayJSON, CreateResultJSON, CreateUpdateOptions, CreateUpdateParams, DeleteOptions, DestroyResult, DestroyResultJSON, DestroyResultWithRecords, DoNotReturnRecords, EntityFieldValue, EntityPrimitiveOrId, EntityStatic, ExcludeEntityCollections, ExcludeFunctions, FindArgs, FindOneArgs, FindOneResult, FindOneResultJSON, FindQueryWithCount, FindQueryWithCountJSON, FindResult, FindResultJSON, FindWithCountResult, GetValueType, HavingComparer, HavingCondition, IConnection, IReadonlyRepository, IRepository, IRepositoryOptions, IncludeFunctions, InitializeOptions, IsValueOfType, JoinDefinition, JoinInfo, JoinType, JoinedSort, JoinedWhereQuery, JsonConstraint, LiteralValues, ModelJoinDefinition, ModelMetadataOptions, ModelRelationshipKeys, MultipleSortString, NegatableConstraint, NotEntity, NotEntityBrand, NumberOrDateConstraint, NumberOrDateConstraintWithSubquery, OmitEntityCollections, OmitFunctions, OrderBy, PaginateOptions, PickAsType, PickByValueType, PickFunctions, PlainObject, PoolLike, PoolQueryResult, PopulateArgs, Populated, QueryResult, QueryResultOptionalPopulated, QueryResultPopulated, QueryResultRow, ReturnSelect$1 as ReturnSelect, ScalarSubqueryConstraint, SelectAggregateExpression, SelectItem, Sort, SortObject, SortObjectValue, SortString, StringConstraint, SubqueryBuilderLike, SubqueryInConstraint, SubqueryJoinDefinition, SubqueryJoinOnCondition, UpdateResult, UpdateResultJSON, WhereClauseValue, WhereQuery, WhereQueryStatement };
package/dist/index.mjs CHANGED
@@ -3106,21 +3106,12 @@ class Repository extends ReadonlyRepository {
3106
3106
  * @param {object} [options.onConflict] - Options to handle conflicts due to a unique constraint or exclusion constraint error during insert
3107
3107
  * @returns {object|object[]|void} Return value from the db
3108
3108
  */
3109
- async create(values, options) {
3109
+ create(values, options) {
3110
3110
  if (this.model.readonly) {
3111
3111
  throw new Error(`${this.model.name} is readonly.`);
3112
3112
  }
3113
- if (Array.isArray(values) && !values.length) {
3114
- return [];
3115
- }
3116
- const beforeCreate = this._type.beforeCreate;
3117
- if (beforeCreate) {
3118
- if (Array.isArray(values)) {
3119
- values = await Promise.all(values.map(async (value) => beforeCreate(value)));
3120
- } else {
3121
- values = await beforeCreate(values);
3122
- }
3123
- }
3113
+ const modelInstance = this;
3114
+ const isArray = Array.isArray(values);
3124
3115
  let returnRecords = true;
3125
3116
  let returnSelect;
3126
3117
  if (options) {
@@ -3130,26 +3121,56 @@ class Repository extends ReadonlyRepository {
3130
3121
  returnSelect = options.returnSelect;
3131
3122
  }
3132
3123
  }
3133
- const { query, params } = getInsertQueryAndParams({
3134
- repositoriesByModelNameLowered: this._repositoriesByModelNameLowered,
3135
- model: this.model,
3136
- values,
3137
- returnRecords,
3138
- returnSelect,
3139
- onConflict: options?.onConflict
3140
- });
3141
- const results = await this._pool.query(query, params);
3142
- if (returnRecords) {
3143
- if (Array.isArray(values)) {
3144
- return this._buildInstances(results.rows);
3145
- }
3146
- const firstResult = results.rows[0];
3147
- if (firstResult) {
3148
- return this._buildInstance(firstResult);
3124
+ let returnAsPlainObjects = false;
3125
+ const result = {
3126
+ toJSON() {
3127
+ returnAsPlainObjects = true;
3128
+ return result;
3129
+ },
3130
+ async then(resolve, reject) {
3131
+ try {
3132
+ if (isArray && !values.length) {
3133
+ return resolve ? await resolve([]) : [];
3134
+ }
3135
+ const beforeCreate = modelInstance._type.beforeCreate;
3136
+ if (beforeCreate) {
3137
+ if (isArray) {
3138
+ values = await Promise.all(values.map(async (value) => beforeCreate(value)));
3139
+ } else {
3140
+ values = await beforeCreate(values);
3141
+ }
3142
+ }
3143
+ const { query, params } = getInsertQueryAndParams({
3144
+ repositoriesByModelNameLowered: modelInstance._repositoriesByModelNameLowered,
3145
+ model: modelInstance.model,
3146
+ values,
3147
+ returnRecords,
3148
+ returnSelect,
3149
+ onConflict: options?.onConflict
3150
+ });
3151
+ const results = await modelInstance._pool.query(query, params);
3152
+ if (returnRecords) {
3153
+ if (isArray) {
3154
+ const entities = returnAsPlainObjects ? modelInstance._buildPlainObjects(results.rows) : modelInstance._buildInstances(results.rows);
3155
+ return resolve ? await resolve(entities) : entities;
3156
+ }
3157
+ const firstResult = results.rows[0];
3158
+ if (firstResult) {
3159
+ const entity = returnAsPlainObjects ? modelInstance._buildPlainObject(firstResult) : modelInstance._buildInstance(firstResult);
3160
+ return resolve ? await resolve(entity) : entity;
3161
+ }
3162
+ throw new Error("Unknown error getting created rows back from the database");
3163
+ }
3164
+ return resolve ? await resolve(void 0) : void 0;
3165
+ } catch (ex) {
3166
+ if (reject) {
3167
+ return reject(ex);
3168
+ }
3169
+ throw ex;
3170
+ }
3149
3171
  }
3150
- throw new Error("Unknown error getting created rows back from the database");
3151
- }
3152
- return void 0;
3172
+ };
3173
+ return result;
3153
3174
  }
3154
3175
  /**
3155
3176
  * Updates object(s) matching the where query, with the specified values
@@ -3160,16 +3181,14 @@ class Repository extends ReadonlyRepository {
3160
3181
  * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
3161
3182
  * @returns {object[]|void} Return values from the db or `true` if returnRecords=false
3162
3183
  */
3163
- async update(where, values, options) {
3184
+ update(where, values, options) {
3164
3185
  if (this.model.readonly) {
3165
3186
  throw new Error(`${this.model.name} is readonly.`);
3166
3187
  }
3167
3188
  if (typeof where === "string") {
3168
3189
  throw new Error("The query cannot be a string, it must be an object");
3169
3190
  }
3170
- if (this._type.beforeUpdate) {
3171
- values = await this._type.beforeUpdate(values);
3172
- }
3191
+ const modelInstance = this;
3173
3192
  let returnRecords = true;
3174
3193
  let returnSelect;
3175
3194
  if (options) {
@@ -3179,19 +3198,40 @@ class Repository extends ReadonlyRepository {
3179
3198
  returnSelect = options.returnSelect;
3180
3199
  }
3181
3200
  }
3182
- const { query, params } = getUpdateQueryAndParams({
3183
- repositoriesByModelNameLowered: this._repositoriesByModelNameLowered,
3184
- model: this.model,
3185
- where,
3186
- values,
3187
- returnRecords,
3188
- returnSelect
3189
- });
3190
- const results = await this._pool.query(query, params);
3191
- if (returnRecords) {
3192
- return this._buildInstances(results.rows);
3193
- }
3194
- return void 0;
3201
+ let returnAsPlainObjects = false;
3202
+ const result = {
3203
+ toJSON() {
3204
+ returnAsPlainObjects = true;
3205
+ return result;
3206
+ },
3207
+ async then(resolve, reject) {
3208
+ try {
3209
+ if (modelInstance._type.beforeUpdate) {
3210
+ values = await modelInstance._type.beforeUpdate(values);
3211
+ }
3212
+ const { query, params } = getUpdateQueryAndParams({
3213
+ repositoriesByModelNameLowered: modelInstance._repositoriesByModelNameLowered,
3214
+ model: modelInstance.model,
3215
+ where,
3216
+ values,
3217
+ returnRecords,
3218
+ returnSelect
3219
+ });
3220
+ const results = await modelInstance._pool.query(query, params);
3221
+ if (returnRecords) {
3222
+ const entities = returnAsPlainObjects ? modelInstance._buildPlainObjects(results.rows) : modelInstance._buildInstances(results.rows);
3223
+ return resolve ? await resolve(entities) : entities;
3224
+ }
3225
+ return resolve ? await resolve(void 0) : void 0;
3226
+ } catch (ex) {
3227
+ if (reject) {
3228
+ return reject(ex);
3229
+ }
3230
+ throw ex;
3231
+ }
3232
+ }
3233
+ };
3234
+ return result;
3195
3235
  }
3196
3236
  /**
3197
3237
  * Destroys object(s) matching the where query
@@ -3209,15 +3249,15 @@ class Repository extends ReadonlyRepository {
3209
3249
  const modelInstance = this;
3210
3250
  const returnSelect = options?.returnSelect;
3211
3251
  const returnRecords = options?.returnRecords ?? !!returnSelect;
3212
- return {
3213
- /**
3214
- * Filters the query
3215
- * @param {object} value - Object representing the where query
3216
- * @returns Query instance
3217
- */
3252
+ let returnAsPlainObjects = false;
3253
+ const result = {
3218
3254
  where(value) {
3219
3255
  where = value;
3220
- return this;
3256
+ return result;
3257
+ },
3258
+ toJSON() {
3259
+ returnAsPlainObjects = true;
3260
+ return result;
3221
3261
  },
3222
3262
  async then(resolve, reject) {
3223
3263
  if (typeof where === "string") {
@@ -3231,9 +3271,10 @@ class Repository extends ReadonlyRepository {
3231
3271
  returnRecords,
3232
3272
  returnSelect
3233
3273
  });
3234
- const result = await modelInstance._pool.query(query, params);
3274
+ const queryResult = await modelInstance._pool.query(query, params);
3235
3275
  if (returnRecords) {
3236
- return await resolve(modelInstance._buildInstances(result.rows));
3276
+ const entities = returnAsPlainObjects ? modelInstance._buildPlainObjects(queryResult.rows) : modelInstance._buildInstances(queryResult.rows);
3277
+ return await resolve(entities);
3237
3278
  }
3238
3279
  return await resolve();
3239
3280
  } catch (ex) {
@@ -3249,6 +3290,7 @@ ${stack}`;
3249
3290
  }
3250
3291
  }
3251
3292
  };
3293
+ return result;
3252
3294
  }
3253
3295
  }
3254
3296
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bigal",
3
- "version": "15.7.0",
3
+ "version": "15.8.0",
4
4
  "description": "A fast and lightweight orm for postgres and node.js, written in typescript.",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",
@@ -46,14 +46,14 @@
46
46
  "@types/node": ">=20",
47
47
  "chai": "6.2.2",
48
48
  "eslint": "9.39.2",
49
- "eslint-config-decent": "3.1.99",
49
+ "eslint-config-decent": "3.1.104",
50
50
  "husky": "9.1.7",
51
51
  "lint-staged": "16.2.7",
52
52
  "markdownlint-cli": "0.47.0",
53
53
  "mocha": "11.7.5",
54
54
  "npm-run-all2": "8.0.4",
55
55
  "pinst": "3.0.0",
56
- "prettier": "3.8.0",
56
+ "prettier": "3.8.1",
57
57
  "semantic-release": "25.0.2",
58
58
  "strict-event-emitter-types": "2.0.0",
59
59
  "postgres-pool": "11.0.2",