Skip to content

SimpleQL requests

François Billioud edited this page May 5, 2019 · 3 revisions

Requesting your database

Simple requests

You can now get information from the database by sending this kind of request body:

{
  User : {
    name : 'John Doe',
    get: ['contacts'],
  }
}

This will give you all the contacts of the user named 'John Doe':

{
  User : [{
   name : 'John Doe',
   contacts : [
    {
      name : 'Jane Doe',
    },
    {
      name : 'Ben Kenobi',
    },
  ]
}

Only the fields containing primitive values are retrieved. If you want to go deeper, do this :

{
  User : {
    name : 'John Doe',
    contacts : {
      get: ['name', 'mentor', 'contacts']
    }
  }
}

More complex research

You can use not, like, gt, ge, lt, le, <, >, <=, >=, ~, ! properties to make more complex researches in the database (gt, ge, lt and le respectively stand for greater than, greater or equal, less than and less or equal. ~ stands for like and ! stands for not).

{
  User : {
    age,
    name : {
      like : 'John%',
    }
  }
}

This might give you this:

[
  {
    name : 'John Doe',
    age : 18,
  },
  {
    name : 'John Snow',
    age : 28,
  },
]

You can mix them, and not can be combined with any of them:

{
  User : {
    name : {
      like : 'John',
      not : '%Doe',
    },
    age : {
      not : 18,
    }
  }
}

You can also use limit and offset to control the results.

{
  User : {
    name : 'John Doe',
    contacts : {
      limit: 10,
      offset: 10,
      get: ['name'],
    }
  }
}

Updating the database

Updating data

To update data, you just need to use the set property:

{
  User : {
    name : {
      like : 'John%',
    }
    set : {
      age : 20,
    }
  }
}

Inserting data

To create a new object into a table, use the create keyword.

{
  User : {
    name : 'John Doe',
    age : 18,
    create : true,
  }
}

Linking entities

To link entities between tables, use the add property.

This will add 2 new contacts to John Doe.

{
  User : {
    name : 'John Doe',
    contacts : {
      add : [
        {
          name : 'Jane Doe',
          age : 17,
        },
        {
          name : 'Mummy',
          age : 48,
        },
      ]
    }
  }
}

Unlinking entities

To unlink entities, use the remove property.

This will remove 2 contacts from John Doe's contacts:

{
  User : {
    name : 'John Doe',
    contacts : {
      remove : [
        {
          name : 'Jane Doe',
        },
        {
          name : 'Mummy',
        },
      ]
    }
  }
}

Deleting elements

To remove an object from a table, use the delete keyword.

{
  User : {
    name : 'John Doe',
    age : 18,
    delete : true,
  }
}

Warning : Delete cascade

Deleting an element from a table like User will remove it from all the array properties it appears in, like contacts. If it appeared in a property like mentor the object will too be deleted in cascade. To prevent this, you should unlink any object referencing the deleted element by setting its value to null for instance.

For instance, before removing John Doe, you should execute:

{
  User : {
    mentor : {
      name : 'John Doe'
    }
    set : {
      mentor : null
    }
  }
}

This could be made in a custom plugin

Combine instructions

You can combine all kind of instructions in one single request:

{
  User : [{
   name : 'John Doe',
   contacts : {
     add: [
       {
          name : 'Jane Doe',
          create : true,
          contacts : {
            add : {
               name : 'John Doe',
            }
          }
       },
       {
         name : 'Ben Kenobi',
          create : true,
          contacts : {
            add : {
               name : 'John Doe',
            }
          }
       },
  ]
}

This will create the users Ben Kenobi and Jane Doe and add them both to John Doe's contacts. While doing so, it will add John Doe to their own contacts.

Clone this wiki locally