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.cjs CHANGED
@@ -5438,7 +5438,9 @@ var MongoRepository = class {
5438
5438
  this.model = model;
5439
5439
  }
5440
5440
  buildSelect(select) {
5441
- if (!select) return void 0;
5441
+ if (!select) {
5442
+ return void 0;
5443
+ }
5442
5444
  if (Array.isArray(select)) {
5443
5445
  return select.join(" ");
5444
5446
  }
@@ -5446,14 +5448,22 @@ var MongoRepository = class {
5446
5448
  }
5447
5449
  applyQueryOptions(query, options) {
5448
5450
  const select = this.buildSelect(options.select);
5449
- if (select) query = query.select(select);
5450
- if (options.sort) query = query.sort(options.sort);
5451
- if (options.limit) query = query.limit(options.limit);
5452
- if (options.skip) query = query.skip(options.skip);
5451
+ if (select) {
5452
+ query = query.select(select);
5453
+ }
5454
+ if (options.sort) {
5455
+ query = query.sort(options.sort);
5456
+ }
5457
+ if (options.limit) {
5458
+ query = query.limit(options.limit);
5459
+ }
5460
+ if (options.skip) {
5461
+ query = query.skip(options.skip);
5462
+ }
5453
5463
  if (options.populate) {
5454
5464
  if (Array.isArray(options.populate)) {
5455
- options.populate.forEach((p) => {
5456
- query = query.populate(p);
5465
+ options.populate.forEach((item) => {
5466
+ query = query.populate(item);
5457
5467
  });
5458
5468
  } else {
5459
5469
  query = query.populate(options.populate);
@@ -5462,7 +5472,13 @@ var MongoRepository = class {
5462
5472
  return query;
5463
5473
  }
5464
5474
  toPlain(doc) {
5465
- return doc.toObject();
5475
+ if (!doc) {
5476
+ return doc;
5477
+ }
5478
+ return typeof doc.toObject === "function" ? doc.toObject() : doc;
5479
+ }
5480
+ toPlainArray(docs) {
5481
+ return docs.map((doc) => this.toPlain(doc));
5466
5482
  }
5467
5483
  /**
5468
5484
  * Create
@@ -5473,7 +5489,7 @@ var MongoRepository = class {
5473
5489
  }
5474
5490
  async createMany(data) {
5475
5491
  const docs = await this.model.insertMany(data);
5476
- return docs.map((doc) => this.toPlain(doc));
5492
+ return this.toPlainArray(docs);
5477
5493
  }
5478
5494
  /**
5479
5495
  * Read
@@ -5481,22 +5497,26 @@ var MongoRepository = class {
5481
5497
  async findAll(filter = {}, options = {}) {
5482
5498
  let query = this.model.find(filter);
5483
5499
  query = this.applyQueryOptions(query, options);
5484
- return query.exec();
5500
+ return query.lean().exec();
5485
5501
  }
5486
5502
  async findById(id, options = {}) {
5487
5503
  let query = this.model.findById(id);
5488
5504
  query = this.applyQueryOptions(query, options);
5489
- return query.exec();
5505
+ return query.lean().exec();
5490
5506
  }
5491
5507
  async findOne(filter = {}, options = {}) {
5492
5508
  let query = this.model.findOne(filter);
5493
5509
  query = this.applyQueryOptions(query, options);
5494
- return query.exec();
5510
+ return query.lean().exec();
5495
5511
  }
5496
5512
  async findManyByIds(ids, options = {}) {
5497
- let query = this.model.find({ _id: { $in: ids } });
5513
+ let query = this.model.find({
5514
+ _id: {
5515
+ $in: ids
5516
+ }
5517
+ });
5498
5518
  query = this.applyQueryOptions(query, options);
5499
- return query.exec();
5519
+ return query.lean().exec();
5500
5520
  }
5501
5521
  async count(filter) {
5502
5522
  return this.model.countDocuments(filter).exec();
@@ -5509,12 +5529,22 @@ var MongoRepository = class {
5509
5529
  * Update
5510
5530
  */
5511
5531
  async updateById(id, data, options) {
5512
- const finalOptions = { new: true, runValidators: true, ...options };
5513
- return this.model.findByIdAndUpdate(id, data, finalOptions).exec();
5532
+ const finalOptions = {
5533
+ new: true,
5534
+ runValidators: true,
5535
+ ...options
5536
+ };
5537
+ const doc = await this.model.findByIdAndUpdate(id, data, finalOptions).exec();
5538
+ return doc ? this.toPlain(doc) : null;
5514
5539
  }
5515
5540
  async updateOne(filter, data, options) {
5516
- const finalOptions = { new: true, runValidators: true, ...options };
5517
- return this.model.findOneAndUpdate(filter, data, finalOptions).exec();
5541
+ const finalOptions = {
5542
+ new: true,
5543
+ runValidators: true,
5544
+ ...options
5545
+ };
5546
+ const doc = await this.model.findOneAndUpdate(filter, data, finalOptions).exec();
5547
+ return doc ? this.toPlain(doc) : null;
5518
5548
  }
5519
5549
  async updateMany(filter, data) {
5520
5550
  const result = await this.model.updateMany(filter, data).exec();
@@ -5524,10 +5554,12 @@ var MongoRepository = class {
5524
5554
  * Delete
5525
5555
  */
5526
5556
  async deleteById(id) {
5527
- return this.model.findByIdAndDelete(id).exec();
5557
+ const doc = await this.model.findByIdAndDelete(id).exec();
5558
+ return doc ? this.toPlain(doc) : null;
5528
5559
  }
5529
5560
  async deleteOne(filter) {
5530
- return this.model.findOneAndDelete(filter).exec();
5561
+ const doc = await this.model.findOneAndDelete(filter).exec();
5562
+ return doc ? this.toPlain(doc) : null;
5531
5563
  }
5532
5564
  async deleteMany(filter) {
5533
5565
  const result = await this.model.deleteMany(filter).exec();