elseware-nodejs 1.9.0 → 1.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -2000,6 +2000,7 @@ declare class MongoRepository<T> implements Repository<T> {
2000
2000
  private buildSelect;
2001
2001
  private applyQueryOptions;
2002
2002
  private toPlain;
2003
+ private toPlainArray;
2003
2004
  /**
2004
2005
  * Create
2005
2006
  */
package/dist/index.d.ts CHANGED
@@ -2000,6 +2000,7 @@ declare class MongoRepository<T> implements Repository<T> {
2000
2000
  private buildSelect;
2001
2001
  private applyQueryOptions;
2002
2002
  private toPlain;
2003
+ private toPlainArray;
2003
2004
  /**
2004
2005
  * Create
2005
2006
  */
package/dist/index.js CHANGED
@@ -5423,7 +5423,9 @@ var MongoRepository = class {
5423
5423
  this.model = model;
5424
5424
  }
5425
5425
  buildSelect(select) {
5426
- if (!select) return void 0;
5426
+ if (!select) {
5427
+ return void 0;
5428
+ }
5427
5429
  if (Array.isArray(select)) {
5428
5430
  return select.join(" ");
5429
5431
  }
@@ -5431,14 +5433,22 @@ var MongoRepository = class {
5431
5433
  }
5432
5434
  applyQueryOptions(query, options) {
5433
5435
  const select = this.buildSelect(options.select);
5434
- if (select) query = query.select(select);
5435
- if (options.sort) query = query.sort(options.sort);
5436
- if (options.limit) query = query.limit(options.limit);
5437
- if (options.skip) query = query.skip(options.skip);
5436
+ if (select) {
5437
+ query = query.select(select);
5438
+ }
5439
+ if (options.sort) {
5440
+ query = query.sort(options.sort);
5441
+ }
5442
+ if (options.limit) {
5443
+ query = query.limit(options.limit);
5444
+ }
5445
+ if (options.skip) {
5446
+ query = query.skip(options.skip);
5447
+ }
5438
5448
  if (options.populate) {
5439
5449
  if (Array.isArray(options.populate)) {
5440
- options.populate.forEach((p) => {
5441
- query = query.populate(p);
5450
+ options.populate.forEach((item) => {
5451
+ query = query.populate(item);
5442
5452
  });
5443
5453
  } else {
5444
5454
  query = query.populate(options.populate);
@@ -5447,7 +5457,13 @@ var MongoRepository = class {
5447
5457
  return query;
5448
5458
  }
5449
5459
  toPlain(doc) {
5450
- return doc.toObject();
5460
+ if (!doc) {
5461
+ return doc;
5462
+ }
5463
+ return typeof doc.toObject === "function" ? doc.toObject() : doc;
5464
+ }
5465
+ toPlainArray(docs) {
5466
+ return docs.map((doc) => this.toPlain(doc));
5451
5467
  }
5452
5468
  /**
5453
5469
  * Create
@@ -5458,7 +5474,7 @@ var MongoRepository = class {
5458
5474
  }
5459
5475
  async createMany(data) {
5460
5476
  const docs = await this.model.insertMany(data);
5461
- return docs.map((doc) => this.toPlain(doc));
5477
+ return this.toPlainArray(docs);
5462
5478
  }
5463
5479
  /**
5464
5480
  * Read
@@ -5466,22 +5482,26 @@ var MongoRepository = class {
5466
5482
  async findAll(filter = {}, options = {}) {
5467
5483
  let query = this.model.find(filter);
5468
5484
  query = this.applyQueryOptions(query, options);
5469
- return query.exec();
5485
+ return query.lean().exec();
5470
5486
  }
5471
5487
  async findById(id, options = {}) {
5472
5488
  let query = this.model.findById(id);
5473
5489
  query = this.applyQueryOptions(query, options);
5474
- return query.exec();
5490
+ return query.lean().exec();
5475
5491
  }
5476
5492
  async findOne(filter = {}, options = {}) {
5477
5493
  let query = this.model.findOne(filter);
5478
5494
  query = this.applyQueryOptions(query, options);
5479
- return query.exec();
5495
+ return query.lean().exec();
5480
5496
  }
5481
5497
  async findManyByIds(ids, options = {}) {
5482
- let query = this.model.find({ _id: { $in: ids } });
5498
+ let query = this.model.find({
5499
+ _id: {
5500
+ $in: ids
5501
+ }
5502
+ });
5483
5503
  query = this.applyQueryOptions(query, options);
5484
- return query.exec();
5504
+ return query.lean().exec();
5485
5505
  }
5486
5506
  async count(filter) {
5487
5507
  return this.model.countDocuments(filter).exec();
@@ -5494,12 +5514,22 @@ var MongoRepository = class {
5494
5514
  * Update
5495
5515
  */
5496
5516
  async updateById(id, data, options) {
5497
- const finalOptions = { new: true, runValidators: true, ...options };
5498
- return this.model.findByIdAndUpdate(id, data, finalOptions).exec();
5517
+ const finalOptions = {
5518
+ new: true,
5519
+ runValidators: true,
5520
+ ...options
5521
+ };
5522
+ const doc = await this.model.findByIdAndUpdate(id, data, finalOptions).exec();
5523
+ return doc ? this.toPlain(doc) : null;
5499
5524
  }
5500
5525
  async updateOne(filter, data, options) {
5501
- const finalOptions = { new: true, runValidators: true, ...options };
5502
- return this.model.findOneAndUpdate(filter, data, finalOptions).exec();
5526
+ const finalOptions = {
5527
+ new: true,
5528
+ runValidators: true,
5529
+ ...options
5530
+ };
5531
+ const doc = await this.model.findOneAndUpdate(filter, data, finalOptions).exec();
5532
+ return doc ? this.toPlain(doc) : null;
5503
5533
  }
5504
5534
  async updateMany(filter, data) {
5505
5535
  const result = await this.model.updateMany(filter, data).exec();
@@ -5509,10 +5539,12 @@ var MongoRepository = class {
5509
5539
  * Delete
5510
5540
  */
5511
5541
  async deleteById(id) {
5512
- return this.model.findByIdAndDelete(id).exec();
5542
+ const doc = await this.model.findByIdAndDelete(id).exec();
5543
+ return doc ? this.toPlain(doc) : null;
5513
5544
  }
5514
5545
  async deleteOne(filter) {
5515
- return this.model.findOneAndDelete(filter).exec();
5546
+ const doc = await this.model.findOneAndDelete(filter).exec();
5547
+ return doc ? this.toPlain(doc) : null;
5516
5548
  }
5517
5549
  async deleteMany(filter) {
5518
5550
  const result = await this.model.deleteMany(filter).exec();