elseware-nodejs 1.8.2 → 1.8.3

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
@@ -5175,81 +5175,98 @@ var validate = (schema) => (req, _res, next) => {
5175
5175
  next();
5176
5176
  };
5177
5177
 
5178
- // src/repositories/mongo.repository.ts
5178
+ // src/repositories/providers/mongo.repository.ts
5179
5179
  var BaseMongoRepository = class {
5180
5180
  model;
5181
5181
  constructor(model) {
5182
5182
  this.model = model;
5183
5183
  }
5184
- async findAll(filter = {}, options = {}) {
5185
- let query = this.model.find(filter);
5186
- if (options.select) query = query.select(options.select);
5184
+ buildSelect(select) {
5185
+ if (!select) return void 0;
5186
+ if (Array.isArray(select)) {
5187
+ return select.join(" ");
5188
+ }
5189
+ return select;
5190
+ }
5191
+ applyQueryOptions(query, options) {
5192
+ const select = this.buildSelect(options.select);
5193
+ if (select) query = query.select(select);
5187
5194
  if (options.sort) query = query.sort(options.sort);
5188
5195
  if (options.limit) query = query.limit(options.limit);
5189
5196
  if (options.skip) query = query.skip(options.skip);
5190
- return query.exec();
5197
+ return query;
5191
5198
  }
5192
- async findById(id) {
5193
- return this.model.findById(id).exec();
5194
- }
5195
- async findOne(filter) {
5196
- return this.model.findOne(filter).exec();
5199
+ toPlain(doc) {
5200
+ return doc.toObject();
5197
5201
  }
5202
+ /**
5203
+ * Create
5204
+ */
5198
5205
  async create(data) {
5199
- return this.model.create(data);
5206
+ const doc = await this.model.create(data);
5207
+ return this.toPlain(doc);
5200
5208
  }
5201
- async updateById(id, data) {
5202
- return this.model.findByIdAndUpdate(id, data, {
5203
- new: true,
5204
- runValidators: true
5205
- }).exec();
5209
+ async createMany(data) {
5210
+ const docs = await this.model.insertMany(data);
5211
+ return docs.map((doc) => this.toPlain(doc));
5206
5212
  }
5207
- async deleteById(id) {
5208
- return this.model.findByIdAndDelete(id).exec();
5213
+ /**
5214
+ * Read
5215
+ */
5216
+ async findAll(filter = {}, options = {}) {
5217
+ let query = this.model.find(filter);
5218
+ query = this.applyQueryOptions(query, options);
5219
+ return query.exec();
5209
5220
  }
5210
- };
5211
-
5212
- // src/repositories/postgres.repository.ts
5213
- var BasePostgresRepository = class {
5214
- model;
5215
- // prisma model (e.g. prisma.testPost)
5216
- constructor(model) {
5217
- this.model = model;
5221
+ async findById(id, options = {}) {
5222
+ let query = this.model.findById(id);
5223
+ query = this.applyQueryOptions(query, options);
5224
+ return query.exec();
5218
5225
  }
5219
- async findAll(filter = {}, options = {}) {
5220
- return this.model.findMany({
5221
- where: filter,
5222
- select: options.select,
5223
- orderBy: options.sort,
5224
- take: options.limit,
5225
- skip: options.skip
5226
- });
5226
+ async findOne(filter = {}, options = {}) {
5227
+ let query = this.model.findOne(filter);
5228
+ query = this.applyQueryOptions(query, options);
5229
+ return query.exec();
5227
5230
  }
5228
- async findById(id) {
5229
- return this.model.findUnique({
5230
- where: { id }
5231
- });
5231
+ async findManyByIds(ids, options = {}) {
5232
+ let query = this.model.find({ _id: { $in: ids } });
5233
+ query = this.applyQueryOptions(query, options);
5234
+ return query.exec();
5232
5235
  }
5233
- async findOne(filter) {
5234
- return this.model.findFirst({
5235
- where: filter
5236
- });
5236
+ async count(filter) {
5237
+ return this.model.countDocuments(filter).exec();
5237
5238
  }
5238
- async create(data) {
5239
- return this.model.create({
5240
- data
5241
- });
5239
+ async exists(filter) {
5240
+ const result = await this.model.exists(filter);
5241
+ return result !== null;
5242
5242
  }
5243
- async updateById(id, data) {
5244
- return this.model.update({
5245
- where: { id },
5246
- data
5247
- });
5243
+ /**
5244
+ * Update
5245
+ */
5246
+ async updateById(id, data, options) {
5247
+ const finalOptions = { new: true, runValidators: true, ...options };
5248
+ return this.model.findByIdAndUpdate(id, data, finalOptions).exec();
5249
+ }
5250
+ async updateOne(filter, data, options) {
5251
+ const finalOptions = { new: true, runValidators: true, ...options };
5252
+ return this.model.findOneAndUpdate(filter, data, finalOptions).exec();
5248
5253
  }
5254
+ async updateMany(filter, data) {
5255
+ const result = await this.model.updateMany(filter, data).exec();
5256
+ return result.modifiedCount;
5257
+ }
5258
+ /**
5259
+ * Delete
5260
+ */
5249
5261
  async deleteById(id) {
5250
- return this.model.delete({
5251
- where: { id }
5252
- });
5262
+ return this.model.findByIdAndDelete(id).exec();
5263
+ }
5264
+ async deleteOne(filter) {
5265
+ return this.model.findOneAndDelete(filter).exec();
5266
+ }
5267
+ async deleteMany(filter) {
5268
+ const result = await this.model.deleteMany(filter).exec();
5269
+ return result.deletedCount ?? 0;
5253
5270
  }
5254
5271
  };
5255
5272
  var AzureBlobStorageService = class {
@@ -5715,7 +5732,6 @@ exports.AzureBlobStorageService = AzureBlobStorageService;
5715
5732
  exports.BPlusTree = BPlusTree;
5716
5733
  exports.BTree = BTree;
5717
5734
  exports.BaseMongoRepository = BaseMongoRepository;
5718
- exports.BasePostgresRepository = BasePostgresRepository;
5719
5735
  exports.BinaryHeap = BinaryHeap;
5720
5736
  exports.BinarySearchTree = BinarySearchTree;
5721
5737
  exports.BinaryTree = BinaryTree;