Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sisimomo committed Nov 5, 2021
1 parent f942358 commit 8ecbd39
Show file tree
Hide file tree
Showing 9 changed files with 387 additions and 0 deletions.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Spring Filter Query Builder

A simple Query Builder for [Spring Filter](https://github.com/turkraft/spring-filter). This Query Builder doesn't have any dependencies and can be used with any webpage.

### Usage:

Import index.js file in your javascript file:

```import * as SFQB from './src/index.js';```

Example:

```new SFQB.Or(new SFQB.And(new SFQB.Equal("test.test1", "testvalue1"), new SFQB.IsNotNull("test.test2"), ), new SFQB.NotEqual("test.test2", "testvalue2")).toString()```

Enjoy!

### Classes:
All classes have a toString methode. So you can convert any Object of these classes to string easily.

The library exposes all classes that you will need to create a query:

#### Operators
* **new SFQB.And(item, item ...)** - and's two or more expressions
* **new SFQB.Or(item, item ...)** - or's two or more expressions
* **new SFQB.Not(item)** - not's an expression
* **new SFQB.Exists(item)** - exists expression

#### Comparators
* **new SFQB.Like(selector, value)** - Like comparator
* **new SFQB.Equal(selector, value)** - Equal comparator
* **new SFQB.NotEqual(selector, value)** - Not equal comparator
* **new SFQB.Gt(selector, value)** - Greater than comparator
* **new SFQB.Ge(selector, value)** - Greater than or equal comparator
* **new SFQB.Lt(selector, value)** - Less than comparator
* **new SFQB.Le(selector, value)** - Less than or equal comparator
* **new SFQB.IsNull(selector)** - Is null comparator
* **new SFQB.IsNotNull(selector)** - Is not null comparator
* **new SFQB.IsEmpty(selector)** - Is empty comparator
* **new SFQB.IsNotEmpty(selector)** - Is not empty comparator
* **new SFQB.In(selector, value, value ...)** - In comparator

PS: selector parameter must be either String or Function type

#### Functions
* **new SFQB.Absolute(selector)** - Absolute function
* **new SFQB.Average(selector)** - Average function
* **new SFQB.Min(selector)** - Min function
* **new SFQB.Max(selector)** - Max function
* **new SFQB.Sum(selector)** - Sum function
* **new SFQB.CurrentDate()** - CurrentDate function
* **new SFQB.CurrentTime()** - CurrentTime function
* **new SFQB.CurrentTimestamp()** - CurrentTimestamp function
* **new SFQB.Length(selector)** - Length function
* **new SFQB.Upper(selector)** - Upper function
* **new SFQB.Lower(selector)** - Lower function
* **new SFQB.Concat(selector)** - Concat function
16 changes: 16 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Demo</title>
<script type="module">
import * as SFQB from './src/index.js';
console.log(new SFQB.Or(new SFQB.And(new SFQB.Equal("test.test1", "testvalue1"), new SFQB.IsNotNull("test.test2")), new SFQB.Equal("test.test2", "testvalue2")).toString());
</script>
</head>

<body>
</body>

</html>
13 changes: 13 additions & 0 deletions package-lock.json

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

23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "spring-filter-query-builder",
"version": "0.1.0",
"description": "A simple Query Builder for Spring Filter",
"main": "scr/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/sisimomo/Spring-Filter-Query-Builder.git"
},
"keywords": [
"Spring",
"Filter"
],
"author": "Sisimomo",
"license": "MIT",
"bugs": {
"url": "https://github.com/sisimomo/Spring-Filter-Query-Builder/issues"
},
"homepage": "https://github.com/sisimomo/Spring-Filter-Query-Builder#readme"
}
102 changes: 102 additions & 0 deletions src/comparators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { Item } from './item.js';

class Comparator extends Item { //Abstract
constructor(selector, comparatorKeyWord, value = undefined) {
super();
if (new.target === Comparator) {
throw new TypeError(`Cannot construct ${new.target.name} instances directly`);
}
if (!(selector instanceof Function || (typeof selector === 'string' || selector instanceof String))) {
throw `${new.target.name} parameter 'selector' must be a Function or a String!`;
}
if (value) {
if (!(typeof value === 'string' || value instanceof String)) {
throw `${new.target.name} parameter 'value' must be a String!`;
}
}
this.selector = selector;
this.comparatorKeyWord = comparatorKeyWord;
this.value = value;
}

toString() {
if (this.value) {
return `${this.selector} ${this.comparatorKeyWord} ${this.value}`;
} else {
return `${this.selector} ${this.comparatorKeyWord}`;
}
}
}

class Like extends Comparator {
constructor(selector, value) {
super(selector, "~",value);
}
}

class Equal extends Comparator {
constructor(selector, value) {
super(selector, ":",value);
}
}

class NotEqual extends Comparator {
constructor(selector, value) {
super(selector, "!",value);
}
}

class Gt extends Comparator {
constructor(selector, value) {
super(selector, ">",value);
}
}

class Ge extends Comparator {
constructor(selector, value) {
super(selector, ">:",value);
}
}

class Lt extends Comparator {
constructor(selector, value) {
super(selector, "<",value);
}
}
class Le extends Comparator {
constructor(selector, value) {
super(selector, "<:", value);
}
}

class IsNull extends Comparator {
constructor(selector) {
super(selector, "IS NULL");
}
}

class IsNotNull extends Comparator {
constructor(selector) {
super(selector, "IS NOT NULL");
}
}

class IsEmpty extends Comparator {
constructor(selector) {
super(selector, "IS EMPTY");
}
}

class IsNotEmpty extends Comparator {
constructor(selector) {
super(selector, "IS NOT EMPTY");
}
}

class In extends Comparator {
constructor(selector, ...values) {
super(selector, "IN", "('" + values.join("', '") + "')");
}
}

export { Comparator, Like, Equal, NotEqual, Gt, Ge, Lt, Le, IsNull, IsNotNull, IsEmpty, IsNotEmpty, In };
106 changes: 106 additions & 0 deletions src/functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { Item } from './item.js';
import { Comparator } from './comparators.js';

class Function extends Item { //Abstract
constructor(selector, functionKeyWord) {
if (new.target === Comparator) {
throw new TypeError("Cannot construct " + new.target.name + " instances directly");
}
if (!(typeof selector === 'string' || selector instanceof String)) {
throw new.target.name + " parameter 'selector' must be a String!";
}
super();
this.selector = selector;
this.functionKeyWord = functionKeyWord;
}

toString() {
return this.functionKeyWord + " (" + this.selector + ")";
}
}

class Absolute extends Function {
constructor(selector) {
super(selector, "ABSOLUTE");
}
}

class Average extends Function {
constructor(selector) {
super(selector, "AVERAGE");
}
}

class Min extends Function {
constructor(selector) {
super(selector, "MIN");
}
}

class Max extends Function {
constructor(selector) {
super(selector, "MAX");
}
}

class Sum extends Function {
constructor(selector) {
super(selector, "SUM");
}
}

class CurrentDate extends Function {
constructor() {
super("", "CURRENTDATE");
}
}

class CurrentTime extends Function {
constructor() {
super("", "CURRENTTIME");
}
}

class CurrentTimestamp extends Function {
constructor() {
super("", "CURRENTTIMESSTAMP");
}
}

class Size extends Function {
constructor(selector) {
super(selector, "SIZE");
}
}

class Length extends Function {
constructor(selector) {
super(selector, "LENGTH");
}
}

class Trim extends Function {
constructor(selector) {
super(selector, "TRIM");
}
}

class Upper extends Function {
constructor(selector) {
super(selector, "UPPER");
}
}

class Lower extends Function {
constructor(selector) {
super(selector, "LOWER");
}
}

class Concat extends Function {
constructor(selector) {
super(selector, "CONCAT");
}
}

export { Function, Absolute, Average, Min, Max, Sum, CurrentDate, CurrentTime, CurrentTimestamp, Size, Length, Trim, Upper, Lower, Concat };
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Like, Equal, NotEqual, Gt, Ge, Lt, Le, IsNull, IsNotNull, IsEmpty, IsNotEmpty, In } from './comparators.js';
import { And, Or, Not, Exists } from './operators.js';
import { Absolute, Average, Min, Max, Sum, CurrentDate, CurrentTime, CurrentTimestamp, Size, Length, Trim, Upper, Lower, Concat } from './functions.js';

export { Like, Equal, NotEqual, Gt, Ge, Lt, Le, IsNull, IsNotNull, IsEmpty, IsNotEmpty, In, And, Or, Not, Exists, Absolute, Average, Min, Max, Sum, CurrentDate, CurrentTime, CurrentTimestamp, Size, Length, Trim, Upper, Lower, Concat };
17 changes: 17 additions & 0 deletions src/item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Item { //Abstract
constructor() {
const mustOverwriteMethods = [ "toString" ];
if (new.target === Item) {
throw new TypeError(`Cannot construct ${new.target.name} instances directly`);
}
let protoLits = [ Object.getPrototypeOf(this) ];
while(Object.getPrototypeOf(protoLits[0]).constructor.name != "Object") { protoLits.unshift(Object.getPrototypeOf(protoLits[0])); }
const superProto = protoLits.shift();
let missing = mustOverwriteMethods.find(name => typeof superProto[name] === "function" && !protoLits.some(proto => proto.hasOwnProperty(name)));
if (missing) {
throw new TypeError(`${this.constructor.name} need to overwrite ${missing}`);
}
}
}

export { Item };
Loading

0 comments on commit 8ecbd39

Please sign in to comment.