https://github.com/treedomtrees/commercetools-where-string-builder
CommerceTools where string builder
https://github.com/treedomtrees/commercetools-where-string-builder
commercetools typescript
Last synced: 10 months ago
JSON representation
CommerceTools where string builder
- Host: GitHub
- URL: https://github.com/treedomtrees/commercetools-where-string-builder
- Owner: treedomtrees
- License: mit
- Created: 2023-02-03T08:46:57.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-11-06T09:08:56.000Z (over 1 year ago)
- Last Synced: 2024-11-29T16:43:50.387Z (over 1 year ago)
- Topics: commercetools, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@treedom/commercetools-where-string-builder
- Size: 15.6 KB
- Stars: 7
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @treedom/commercetools-where-string-builder
A simple programmatic builder for CommerceTools `where` [Query Predicates](https://docs.commercetools.com/api/predicates/query)
Performs stringification and quote escaping for string, boolean and numerical values
__Made with β€οΈ atΒ Β [
](#-join-us-in-making-a-difference-)__, [join us in making a difference](#-join-us-in-making-a-difference-)!
## Usage
```typescript
import { Field, Equals, And } from "@treedom/commercetools-where-string-builder";
Field(
"masterData",
"current",
"masterVariant",
And(
Equals("key", "my-variant-key"),
Field(
"attributes",
And(
Equals("name", "my-attribute-key"),
Equals("value", "my-attribute-value")
)
)
)
);
```
This snippet builds the following where query string
```
masterData(current(masterVariant((key = "my-variant-key") and (attributes((name = "my-attribute-key") and (value = "my-attribute-value"))))))
```
## Predicates
### Field: (...fields: Array<string>) => string
```typescript
Field("bar", "baz", "foo"); // bar(baz(foo)))
```
### And: (...fields: Array>) => string
```typescript
And("bar", "baz"); // (bar) and (baz)
```
### Or: (...fields: Array>) => string
```typescript
Or("bar", "baz"); // (bar) or (baz)
```
### Compare: (field: string, operator: "=" | "!=" | ">" | "<" | ">=" | "<=" | "<>", value: number | boolean | string) => string
```typescript
Compare("bar", "<=", 5); // bar <= 5
```
### Equals: (field: string, value: number | boolean | string) => string
```typescript
Equals("bar", 5); // bar = 5
```
### NotEquals: (field: string, value: number | boolean | string) => string
```typescript
NotEquals("bar", 5); // bar != 5
```
### GreaterThan: (field: string, value: number | string) => string
```typescript
GreaterThan("bar", 5); // bar > 5
```
### LessThan: (field: string, value: number | string) => string
```typescript
LessThan("bar", 5); // bar > 5
```
### GreaterOrEqualThan: (field: string, value: number | string) => string
```typescript
GreaterOrEqualThan("bar", 5); // bar >= 5
```
### LessOrEqualThan: (field: string, value: number | string) => string
```typescript
LessOrEqualThan("bar", 5); // bar <= 5
```
### Contains: (field: string, kind: "any" | "all", values: Array) => string
```typescript
Contains("bar", "any", ["str", 123, true]); // bar contains any ("str", 123, true)
```
### ContainsAll: (field: string, values: Array) => string
```typescript
ContainsAll("bar", ["str", 123, true]); // bar contains all ("str", 123, true)
```
### ContainsAny: (field: string, values: Array) => string
```typescript
ContainsAny("bar", ["str", 123, true]); // bar contains any ("str", 123, true)
```
### In: (field: string, values: Array) => string
```typescript
In("bar", ["str", 123, true]); // bar in ("str", 123, true)
```
### NotIn: (field: string, values: Array) => string
```typescript
NotIn("bar", ["str", 123, true]); // bar not in ("str", 123, true)
```
### IsDefined: (field: string) => string
```typescript
IsDefined("bar"); // bar is defined
```
### IsNotDefined: (field: string) => string
```typescript
IsNotDefined("bar"); // bar is defined
```
### HasChanged: (field: string) => string
```typescript
HasChanged("bar"); // bar has changed
```
### HasNotChanged: (field: string) => string
```typescript
HasNotChanged("bar"); // bar has not changed
```
### WithinCircle: (field: string, latitude: number, longitude: number, radius: number) => string
```typescript
WithinCircle("bar", 75.2345, 12.2345, 100); // bar within circle(75.2345, 12.2345, 100)
```
## π³ Join Us in Making a Difference! π³
We invite all developers who use Treedom's open-source code to support our mission of sustainability by planting a tree with us. By contributing to reforestation efforts, you help create a healthier planet and give back to the environment. Visit our [Treedom Open Source Forest](https://www.treedom.net/en/organization/treedom/event/treedom-open-source) to plant your tree today and join our community of eco-conscious developers.
Additionally, you can integrate the Treedom GitHub badge into your repository to showcase the number of trees in your Treedom forest and encourage others to plant new ones. Check out our [integration guide](https://github.com/treedomtrees/.github/blob/main/TREEDOM_BADGE.md) to get started.
Together, we can make a lasting impact! ππ
## Contributing
Contributions are welcome! Please read the contributing guidelines before submitting a pull request.
## License
This project is licensed under the MIT License.