Zeromagic introduces container, operators and properties, providing developers with a structured approach to perform operations on data containers and individual items within those containers. These properties enable efficient data manipulation and management within the Zeromagic platform.

Zeromagic Query Language supports CRUD operations:

  • ReadOne and ReadMany
  • CreateOne and CreateMany
  • UpdateOne and UpdateMany
  • DeleteOne and DeleteMany

Filter Property

The filter property within your Zeromagic queries provides a versatile set of operators to refine your data access. Here’s a breakdown of some commonly used operators:

"filter" : {} // fetchs all records
    (or) 
"filter" : {
    ...props
}

1. Logical Operators

@and, @or: Combine multiple filter conditions using the logical AND (and) or OR (or) operators within this property.

"@and" : [ {"key1" : "val1"},{"key2" : "val2"} ]
    (or)
"@or" : [ {"key1" : "val1"},{"key2" : "val2"} ]

2. Sorting Operators

@sort: Specify sorting criteria within this property. Define the key to sort by (key) and the desired order ("order": "asc" for ascending, "desc" for descending).

"@sort": { "key": "name", "order": "asc" }
    (or)
"@sort": { "key": "name", "order": "desc" }

3. Comparison Operators:

@eq: Exact equality check

// Returns records where the name is exactly "Max"
"name": { "@eq": "Max" }

@ne: Not equal check

// Returns records where the name is not "Max"
"name": { "@ne": "Max" }

@gt: Greater than comparison

// Returns records where the price is greater than 100
"price": { "@gt": 100 }

@lt: Less than comparison

// Returns records where the price is less than 100
"price": { "@lt": 100 }

@gte: Greater than or equal to comparison.

// Returns records where the price is greater than or equal to 120
"price": { "@gte": 120 }

@lte: Less than or equal to comparison.

// Returns records where the price is less than or equal to 120
"price": { "@lte": 120 }

4. String Matching:

@contains: Check if a string contains a specific substring.

// Returns records where the name contains "John Doe"
"name": { "@contains": "John Doe" }

@startswith: Check if a string starts with a specific prefix.

// Returns records where the name starts with "John"
"name": { "@startswith": "John" }

@in: Check if a value is within a provided list.

// Returns records where the status is in "active" or "pending"
"status": { "@in": ["active", "pending"] }

@nin: Check if a value is not within a provided list.

// Returns records where the status is not in "active" or "pending"
"status": { "@nin": ["active", "pending"] }

Sample Filter Query

Query-1
{
"container" : "ecommerce",
"filter" : {
        "@sort": { "key": "name", "order": "asc" },
        "id": "1",
        "price": {
            "@gt": 20,
            "@lt": 20,
            "@gte": 20,
            "@lte": 20
        },
        "name" : {
            "@contains": "John Doe" ,
            "@startswith": "John", 
            "@eq": "Max",
            "@ne": "Max",
        },
        "status"  : {
        "@in": ["active", "pending"],
        "@nin": ["active", "pending"] 
        }
    } 
}
Query-2
{
    "container" : "ecommerce",
    "fields" : ["id",{"name":"full_name"},{"price" : "cost_price"} , "status"],
    "filter" : {
            "@sort": { "key": "name", "order": "asc" },
            "@and" : [
                {
                    "id": "1"
                },
                {
                    "price": {
                        "@gt": 20
                    }
                }
            ],
            "@or" : [
                {
                    "name" : {
                        "@contains": "John Doe"
                    }
                },
                {
                    "status"  : {
                        "@in": ["active", "pending"]
                    }
                }
            ]
        } 
}

Query Property

1. Container Property: (Required)

container: Specifies the name of the container on which the operation will be performed. This property allows developers to target specific data containers within their Zeromagic projects.

"container" : "student"

2. Field Selection

fields: Define a list of fields ["<field_1>", "<field_2>", "<field_3>"] you want to retrieve from the records. To represent aliases for a return field, provide input field as [{"<field_1>":"alias"}].

//retrieve specific fields
"fields" : ["id","name","price","status"],

//retrieve specific fields with alias
"fields" : [{"name":"full_name"}]

Here, name is the original field and full_name is the alias name. So output will be like {"full_name":"Zeromagic"} instead of {"name":"Zeromagic"}. This is useful when you want to change the field name in the output.

3. Item Property: (Required for createOne method)

item: Represents a single item to be created within a data container. This property encapsulates the attributes and values of the item to be created, facilitating the creation of individual data records.

"item": {
  "name": "John Doe",
  "email": "john.doe@example.com"
} // single item to create
Create single item Query
//Query to create a single item in the employee container
{
    "container" : "employee",
    "item": {
        "name": "John Doe",
        "email": "john.doe@example.com"
    }
}

4. Items Property: (Required for createMany method)

items: Contains an array of multiple items to be created within a data container. This property allows developers to create multiple data records simultaneously, streamlining the data creation process.

"items" : [
 {
  "name": "John Doe",
  "email": "john.doe@example.com"
} , 
{
  "name": "Max Well",
  "email": "max.well@example.com"
}
]   // multiple item to create
Create multiple items Query
//Query to create multiple items in the employee container
{
    "container" : "employee",
    "items": [
        {
            "name": "John Doe",
            "email": "john.doe@example.com"
        } , 
        {
            "name": "Max Well",
            "email": "max.well@example.com"
        }       
    ]
}

5. Update Property: (Required for updateOne and updateMany method)

update: Defines the properties to be updated for a specific data record. This property specifies the attributes and new values to be applied to the selected record, enabling seamless data updates within Zeromagic containers.

"update": {
    "name": "Steve"
}