Skip to content

Commit a8cfc2c

Browse files
committed
Add Find Options
1 parent 076620a commit a8cfc2c

2 files changed

Lines changed: 22 additions & 17 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nosqlax",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "NoSQLax is a lightweight JavaScript library designed to simplify and streamline CRUD operations with CouchDB. NoSQLax provides a unified and intuitive repository pattern to handle your data effortlessly.",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/core/CouchRepository.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import Nano, { DocumentScope } from "nano";
1+
import Nano, { DocumentScope, MangoQuery, MangoSelector } from "nano";
22
import Validation from './Validation'; // Import the Validation class
33
import BaseEntity from './BaseEntity';
44
import { DocumentNotFoundError } from "./DocumentNotFoundError";
55

6+
type MangoOptions = Omit<MangoQuery, 'selector'>;
7+
68
// Type for the entity class constructor
79
type 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

Comments
 (0)