1- import Nano , { DocumentScope } from "nano" ;
1+ import Nano , { DocumentScope , MangoQuery , MangoSelector } from "nano" ;
22import Validation from './Validation' ; // Import the Validation class
33import BaseEntity from './BaseEntity' ;
44import { DocumentNotFoundError } from "./DocumentNotFoundError" ;
55
6+ type MangoOptions = Omit < MangoQuery , 'selector' > ;
7+
68// Type for the entity class constructor
79type EntityClass = {
810 fieldMap : Record < string , string > ;
@@ -147,18 +149,16 @@ const inverseTransform = function (document: Record<string, any>, fieldMap: Reco
147149} ;
148150
149151
150- const findUsingMango = async function ( query : Nano . MangoQuery , entityClass : EntityClass , connection : DocumentScope < Nano . MaybeDocument > , fieldMap :Record < string , any > ) : Promise < Nano . MangoResponse < Nano . MaybeDocument > > {
152+ const findUsingMango = async function ( selector : Nano . MangoSelector = { } , options : MangoOptions = { } , entityClass : EntityClass , connection : DocumentScope < Nano . MaybeDocument > , fieldMap :Record < string , any > ) : Promise < Nano . MangoResponse < Nano . MaybeDocument > > {
151153 try {
152154
153- // Ensure this method is used only from CouchRepository
154- /* if (this.constructor !== CouchRepository) {
155- throw new Error('This method can only be used by CouchRepository, not children classes.');
156- } */
157155
158- // Add type to the selector if it's not already present
159- if ( ! query . selector ) {
160- query . selector = { } ;
161- }
156+ let query : MangoQuery = {
157+ ...options , // Spread the properties from MangoOptions
158+ selector, // Add the selector
159+ } ;
160+
161+ // add type
162162 query . selector [ CouchRepository . getFieldNameFromFieldMap ( fieldMap , 'type' ) ] = entityClass . type ;
163163
164164
@@ -217,10 +217,12 @@ abstract class CouchRepository {
217217
218218
219219 // 2. Find one document using a Mango selector
220- async findOne ( selector : any ) : Promise < BaseEntity > {
220+ async findOne ( selector : MangoSelector , options : MangoOptions = { } ) : Promise < BaseEntity > {
221221 try {
222+
222223 const res = await findUsingMango (
223- { "selector" : translateSelector ( selector , this . fieldMap ) } ,
224+ translateSelector ( selector , this . fieldMap ) ,
225+ options ,
224226 this . entityClass ,
225227 this . connection ,
226228 this . fieldMap ) ;
@@ -237,9 +239,12 @@ abstract class CouchRepository {
237239
238240
239241 // 4. Find many documents using a Mango selector
240- async findMany ( selector : any ) : Promise < BaseEntity [ ] > {
242+ async findMany ( selector : MangoSelector , options : MangoOptions = { } ) : Promise < BaseEntity [ ] > {
241243 try {
242- const res = await findUsingMango ( { "selector" : translateSelector ( selector , this . fieldMap ) } , this . entityClass , this . connection , this . fieldMap ) ;
244+ const res = await findUsingMango (
245+ translateSelector ( selector , this . fieldMap ) ,
246+ options ,
247+ this . entityClass , this . connection , this . fieldMap ) ;
243248
244249 return res . docs . map ( ( doc ) => new this . entityClass ( inverseTransform ( doc , this . fieldMap ) ) ) ;
245250 } catch ( err ) {
@@ -248,9 +253,9 @@ abstract class CouchRepository {
248253 }
249254
250255 // 5. Find all documents for the entity type
251- async findAll ( ) : Promise < BaseEntity [ ] > {
256+ async findAll ( options : MangoOptions = { } ) : Promise < BaseEntity [ ] > {
252257 try {
253- const res = await findUsingMango ( { selector : { } } , this . entityClass , this . connection , this . fieldMap ) ;
258+ const res = await findUsingMango ( { } , options , this . entityClass , this . connection , this . fieldMap ) ;
254259
255260 return res . docs . map ( ( doc ) => new this . entityClass ( inverseTransform ( doc , this . fieldMap ) ) ) ;
256261 } catch ( err ) {
0 commit comments