Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rsql-query-builder-mongodb",
"version": "1.2.0",
"version": "1.3.0",
"description": "Library for building RSQL query strings for MongoDB.",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
48 changes: 46 additions & 2 deletions src/RSQLBuilderMongoDB.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,66 @@
import { describe, it, expect } from 'vitest';

import { RSQLBuilderMongoDB } from './';
import { MongoRegexOptions, RSQLBuilderMongoDB } from './';

describe('RSQLBuilderMongoDB', () => {
it("Test operator REGEX ('=regex=')", () => {
expect(new RSQLBuilderMongoDB().regex('name', 'Filip').toString()).toBe('name=regex="Filip"');
expect(new RSQLBuilderMongoDB().regex('name', 'Fil*').toString()).toBe('name=regex="Fil*"');
expect(new RSQLBuilderMongoDB().regex('name', 'Fil*', 'si').toString()).toBe('name=regex="Fil*"=si');
expect(
new RSQLBuilderMongoDB()
.regex('name', 'Fil*', [
MongoRegexOptions.DOTALL,
MongoRegexOptions.CASE_INSENSITIVE,
MongoRegexOptions.MULTILINE,
MongoRegexOptions.EXTENDED
])
.toString()
).toBe('name=regex="Fil*"=simx');
});

it("Test operator NOT REGEX ('=notregex=')", () => {
expect(new RSQLBuilderMongoDB().notRegex('name', 'Filip').toString()).toBe('name=notregex="Filip"');
expect(new RSQLBuilderMongoDB().notRegex('name', 'Fil*', 'si').toString()).toBe('name=notregex="Fil*"=si');
expect(
new RSQLBuilderMongoDB()
.notRegex('name', 'Fil*', [
MongoRegexOptions.DOTALL,
MongoRegexOptions.CASE_INSENSITIVE,
MongoRegexOptions.MULTILINE,
MongoRegexOptions.EXTENDED
])
.toString()
).toBe('name=notregex="Fil*"=simx');
});

it("Test operator LIKE ('=regex=')", () => {
expect(new RSQLBuilderMongoDB().like('name', 'Fil*').toString()).toBe('name=regex="Fil*"');
expect(new RSQLBuilderMongoDB().like('name', 'Fil*', 'si').toString()).toBe('name=regex="Fil*"=si');
expect(
new RSQLBuilderMongoDB()
.like('name', 'Fil*', [
MongoRegexOptions.DOTALL,
MongoRegexOptions.CASE_INSENSITIVE,
MongoRegexOptions.MULTILINE,
MongoRegexOptions.EXTENDED
])
.toString()
).toBe('name=regex="Fil*"=simx');
});

it("Test operator LIKE ('=notregex=')", () => {
expect(new RSQLBuilderMongoDB().notLike('name', 'Fil*').toString()).toBe('name=notregex="Fil*"');
expect(new RSQLBuilderMongoDB().notLike('name', 'Fil*', 'si').toString()).toBe('name=notregex="Fil*"=si');
expect(
new RSQLBuilderMongoDB()
.notLike('name', 'Fil*', [
MongoRegexOptions.DOTALL,
MongoRegexOptions.CASE_INSENSITIVE,
MongoRegexOptions.MULTILINE,
MongoRegexOptions.EXTENDED
])
.toString()
).toBe('name=notregex="Fil*"=simx');
});

it("Test operator EXISTS ('=exists=')", () => {
Expand Down
51 changes: 45 additions & 6 deletions src/RSQLBuilderMongoDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import { RSQLBuilderBase, RSQLBuilderOptions } from 'rsql-query-builder';

type ComparisonOperator = 'regex' | 'notRegex' | 'exists';

export enum MongoRegexOptions {
CASE_INSENSITIVE = 'i', // Ignore case
MULTILINE = 'm', // Multiline mode
DOTALL = 's', // Dot matches newline
EXTENDED = 'x' // Ignore whitespace and allow comments
}

/** RSQL Query Builder for MongoDB.
*
* @template Selector - The type of the selector. It is used to define the selector names and is a list of strings.
Expand All @@ -28,8 +35,16 @@ class RSQLBuilderMongoDB<TSelector extends string> extends RSQLBuilderBase<TSele
*
* @see https://www.mongodb.com/docs/manual/reference/operator/query/regex/
*/
public regex(selector: TSelector, regex: string, options?: string): RSQLBuilderMongoDB<TSelector> {
super.addComparison(selector, 'regex', regex);
public regex(
selector: TSelector,
regex: string,
options?: string | Array<MongoRegexOptions>
): RSQLBuilderMongoDB<TSelector> {
if (Array.isArray(options)) {
options = options.join('');
}

super.addComparison(selector, 'regex', regex, options);
return this;
}

Expand All @@ -42,8 +57,16 @@ class RSQLBuilderMongoDB<TSelector extends string> extends RSQLBuilderBase<TSele
*
* @see https://www.mongodb.com/docs/manual/reference/operator/query/regex/
*/
public notRegex(selector: TSelector, regex: string, options?: string): RSQLBuilderMongoDB<TSelector> {
super.addComparison(selector, 'notRegex', regex);
public notRegex(
selector: TSelector,
regex: string,
options?: string | Array<MongoRegexOptions>
): RSQLBuilderMongoDB<TSelector> {
if (Array.isArray(options)) {
options = options.join('');
}

super.addComparison(selector, 'notRegex', regex, options);
return this;
}

Expand All @@ -56,7 +79,15 @@ class RSQLBuilderMongoDB<TSelector extends string> extends RSQLBuilderBase<TSele
*
* @see https://www.mongodb.com/docs/manual/reference/operator/query/regex/
*/
public like(selector: TSelector, regex: string, options?: string): RSQLBuilderMongoDB<TSelector> {
public like(
selector: TSelector,
regex: string,
options?: string | Array<MongoRegexOptions>
): RSQLBuilderMongoDB<TSelector> {
if (Array.isArray(options)) {
options = options.join('');
}

return this.regex(selector, regex, options);
}

Expand All @@ -69,7 +100,15 @@ class RSQLBuilderMongoDB<TSelector extends string> extends RSQLBuilderBase<TSele
*
* @see https://www.mongodb.com/docs/manual/reference/operator/query/regex/
*/
public notLike(selector: TSelector, regex: string, options?: string): RSQLBuilderMongoDB<TSelector> {
public notLike(
selector: TSelector,
regex: string,
options?: string | Array<MongoRegexOptions>
): RSQLBuilderMongoDB<TSelector> {
if (Array.isArray(options)) {
options = options.join('');
}

return this.notRegex(selector, regex, options);
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {default as RSQLBuilderMongoDB} from './RSQLBuilderMongoDB';
export { default as RSQLBuilderMongoDB, MongoRegexOptions } from './RSQLBuilderMongoDB';
Loading