Deleting Data

You can delete data in your app by simply calling db.delete on the frontend. Here's a code snippet to delete all documents matching a specific condition:

      
import { API, and, or, cond } from "space-api";

// Initialize api with the project name and url of the space cloud
const api = new API("todo-app", "http://localhost:8080");

// Initialize database(s) you intend to use
const db = api.Mongo();

// The condition to be matched
const condition = cond("userId", "==", "user1");

// Delete all todos of a particular user which match a particular condition
db.delete("todos").where(condition).apply()
  .then(res => {
    if (res.status === 200) {
      // Documents were deleted successfully
      return;
    }
  })
  .catch(ex => {
    // Exception occured while processing request
  });
      
    
      
// Java client coming soon!      
      
    
      
from space_api import API, COND

# Initialize api with the project name and url of the space cloud
api = API("books-app", "localhost:8081")

# Initialize database(s) you intend to use
db = api.my_sql()

# The condition to be matched
condition = COND("name", "==", "SomeAwesomeBook")

# Delete all books which match a particular condition
response = db.delete("books").where(condition).apply()
if response.status == 200:
    print("Success")
else:
    print(response.error)

api.close()
      
    

As you would have noticed, the delete method is asynchronous in nature. It takes the name of the concerned collection/table and deletes all the matching documents. The apply method actually triggers the given request to space-cloud and returns a promise.

Deleting a single document:

Note: deleteOne method is not available for SQL databases.

      
db.deleteOne('todos').where(cond('_id', '==', 1)).apply().then(res => ...).catch(ex => ...);      
      
    
      
// Java client coming soon!      
      
    
      
from space_api import API, COND

# Initialize api with the project name and url of the space cloud
api = API("books-app", "localhost:8081")

# Initialize database(s) you intend to use
db = api.my_sql()

# The condition to be matched
condition = COND("name", "==", "SomeAwesomeBook")

# Delete all books which match a particular condition
response = db.delete_one("books").where(condition).apply()
if response.status == 200:
    print("Success")
else:
    print(response.error)

api.close()
      
    

The deleteOne method takes the name of the concerned table/collection. It deletes a single document matching the condition. If no matching document is found, it returns an error (res.status - 400).

Delete documents selectively

You can selectively delete only a few documents which you desire and leave the rest by using where clause. The where method accepts a condition object. After validation, space-cloud generates a database specific query. The documents or rows which match this query are deleted by the update operations described above.

Specifying a single condition

The cond function is used to specify a single condition as shown below:

      
// Syntax
var op1 = 'field_name';
var operator = '== | != | > | < | >= | <= | in | notIn';
var op2 = 'value';
const condition = cond(op1, operator, op2);

// Example
const condition = cond('_id', '==', 1);
db.delete('todos').where(condition).apply().then(res => ...);   
      
    
      
// Java client coming soon!      
      
    
      
from space_api import API, COND

# Initialize api with the project name and url of the space cloud
api = API("books-app", "localhost:8081")

# Initialize database(s) you intend to use
db = api.my_sql()

# The condition to be matched
condition = COND("name", "==", "SomeAwesomeBook")

# Delete all books which match a particular condition
response = db.delete("books").where(condition).apply()
if response.status == 200:
    print("Success")
else:
    print(response.error)

api.close()
      
    

The operators allowed are:

Operator Description
== Passes if op1 is equal to op2
!= Passes if op1 is not equal to op2
> Passes if op1 is greater than op2
< Passes if op1 is lesser than op2
>= Passes if op1 is greater than or equal to op2
<= Passes if op1 is lesser than or equal to op2
in Passes if op1 is in op2
notIn Passes if op1 is not in op2

Combining multiple conditions

A single condition is often not enough to delete the data you desire. You might need to and / or several conditions together. For e.g., you want to delete only those posts which are of a particular author and of a particular category. The following code snippet shows how you can use and and or:

      
// AND syntax
const condition = and(...conditions)

// Example
const condition = and(cond('author', '==', 'some-author'), cond('category', '==', 'some-category'));
db.delete('todos').where(condition).apply().then(res => ...)

// OR syntax
const condition = or(...conditions)

// Example
const condition = or(cond('author', '==', 'some-author'), cond('category', '==', 'some-category'));
db.delete('todos').where(condition).apply().then(res => ...);      
      
    
      
// Java client coming soon!      
      
    
      
from space_api import API, COND, OR, AND

# Initialize api with the project name and url of the space cloud
api = API("books-app", "localhost:8081")

# Initialize database(s) you intend to use
db = api.my_sql()

# The condition to be matched
condition = AND(COND("name", "==", "SomeAwesomeBook"), COND("author", "==", "SomeAuthor"))

# Delete all books which match a particular condition
response = db.delete("books").where(condition).apply()
if response.status == 200:
    print("Success")
else:
    print(response.error)

api.close()
      
    

Response

On response from the server, the callback passed to the then method is called with the response object as described below:

{
  "status": "number" // Status of the operation
}

Next steps

Now you know how to perform all the basic operations of CRUD module. Now let's see how to perform batched operations and transactions.

Have a technical question?

Edit docs!