Skip to content

Commit 81e8bd4

Browse files
committed
findByAny and findOneByAny or search added
1 parent 8392b84 commit 81e8bd4

File tree

1 file changed

+49
-12
lines changed

1 file changed

+49
-12
lines changed

moose.js

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,57 @@ class Model {
7272
);
7373
};
7474

75-
findByAny = (options, cb) => {
76-
db.collection(this.collection)
77-
.find(options)
78-
.toArray((err, docs) => {
79-
if (err) console.log(err);
80-
else cb(docs);
81-
});
75+
findByAny = (options, strict = true, cb) => {
76+
let col = db.collection(this.collection);
77+
if (strict) {
78+
if (typeof options == 'object') {
79+
col.find(options).toArray((err, docs) => {
80+
if (err) console.log(err);
81+
else cb(docs);
82+
});
83+
} else {
84+
cb({ error: 'incorrect input given, object expected' });
85+
}
86+
} else {
87+
if (Array.isArray(options)) {
88+
col.find({
89+
$or: options
90+
}).toArray((err, docs) => {
91+
if (err) console.log(err);
92+
else cb(docs);
93+
});
94+
} else {
95+
cb({ error: 'incorrect input given, array expected' });
96+
}
97+
}
8298
};
8399

84-
findOneByAny = (options, cb) => {
85-
db.collection(this.collection).findOne(options, (err, result) => {
86-
if (err) console.log(err);
87-
else cb(result);
88-
});
100+
findOneByAny = (options, strict = true, cb) => {
101+
let col = db.collection(this.collection);
102+
if (strict) {
103+
if (typeof options == 'object') {
104+
col.findOne(options, (err, result) => {
105+
if (err) console.log(err);
106+
else cb(result);
107+
});
108+
} else {
109+
cb({ error: 'incorrect input given, object expected' });
110+
}
111+
} else {
112+
if (Array.isArray(options)) {
113+
col.findOne(
114+
{
115+
$or: options
116+
},
117+
(err, result) => {
118+
if (err) console.log(err);
119+
else cb(result);
120+
}
121+
);
122+
} else {
123+
cb({ error: 'incorrect input given, array expected' });
124+
}
125+
}
89126
};
90127

91128
// * =================

0 commit comments

Comments
 (0)