Skip to content

Commit 3b10820

Browse files
Harrison IfeanyichukwuHarrison Ifeanyichukwu
authored andcommitted
feat: default nosql query field to id if field type is objectId
1 parent 3953bce commit 3b10820

3 files changed

Lines changed: 148 additions & 110 deletions

File tree

src/DBChecker.ts

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Common from './Common';
2-
import { DataValue, ModelDBCheck } from './@types';
2+
import { DataValue, ModelDBCheck, ResolvedRules, DataType } from './@types';
33
import { DB_MODELS } from './Constants';
44
import { pickValue, applyCase } from '@forensic-js/utils';
55

@@ -22,7 +22,14 @@ export default class DBChecker<F extends string = string> extends Common<F> {
2222
/**
2323
* resets the db checker, and checks if the check call should proceed
2424
*/
25-
protected setup(required: boolean, field: string, value: DataValue, check: ModelDBCheck, index: number) {
25+
protected setup(
26+
type: DataType,
27+
required: boolean,
28+
field: string,
29+
value: DataValue,
30+
check: ModelDBCheck,
31+
index: number
32+
) {
2633
this.reset(field, check, index);
2734

2835
if (value === '' && !required) {
@@ -32,8 +39,16 @@ export default class DBChecker<F extends string = string> extends Common<F> {
3239
if (check.query) {
3340
this.query = check.query;
3441
} else {
42+
let givenField = check.field;
43+
if (
44+
typeof givenField === 'undefined' &&
45+
this.dbModel === DB_MODELS.NOSQL &&
46+
type === 'objectId'
47+
) {
48+
givenField = '_id';
49+
}
3550
this.query = {
36-
[applyCase(check.field || field, this.dbCaseStyle)]: value,
51+
[givenField || applyCase(field, this.dbCaseStyle)]: value
3752
};
3853
}
3954
}
@@ -63,17 +78,28 @@ export default class DBChecker<F extends string = string> extends Common<F> {
6378
/**
6479
* checks if database field value exists, setting error if it does
6580
*
81+
* @param type field data type
6682
* @param required boolean indicating if field is required
6783
* @param field current field under check
6884
* @param value current field value under check
6985
* @param check check options
7086
* @param index current field value index under check
7187
*/
72-
async checkIfExists(required: boolean, field: string, value: string, check: ModelDBCheck, index: number) {
73-
if (this.setup(required, field, value, check, index)) {
88+
async checkIfExists(
89+
type: DataType,
90+
required: boolean,
91+
field: string,
92+
value: string,
93+
check: ModelDBCheck,
94+
index: number
95+
) {
96+
if (this.setup(type, required, field, value, check, index)) {
7497
const count = await this.execute(check.model, this.query);
7598
if (count > 0) {
76-
this.setError(pickValue('err', check, '{_this}:{this} already exists'), value);
99+
this.setError(
100+
pickValue('err', check, '{_this}:{this} already exists'),
101+
value
102+
);
77103
}
78104
}
79105
return this.succeeds();
@@ -82,17 +108,28 @@ export default class DBChecker<F extends string = string> extends Common<F> {
82108
/**
83109
* checks if database field value does not exist, setting error if it does not
84110
*
111+
* @param type field data type
85112
* @param required boolean indicating if field is required
86113
* @param field current field under check
87114
* @param value current field value under check
88115
* @param check check options
89116
* @param index current field value index under check
90117
*/
91-
async checkIfNotExists(required: boolean, field: string, value: string, check: ModelDBCheck, index: number) {
92-
if (this.setup(required, field, value, check, index)) {
118+
async checkIfNotExists(
119+
type: DataType,
120+
required: boolean,
121+
field: string,
122+
value: string,
123+
check: ModelDBCheck,
124+
index: number
125+
) {
126+
if (this.setup(type, required, field, value, check, index)) {
93127
const count = await this.execute(check.model, this.query);
94128
if (count === 0) {
95-
this.setError(pickValue('err', check, '{_this}:{this} does not exist'), value);
129+
this.setError(
130+
pickValue('err', check, '{_this}:{this} does not exist'),
131+
value
132+
);
96133
}
97134
}
98135
return this.succeeds();

0 commit comments

Comments
 (0)