Data Manipulation
As mentioned at the beginning of this section, SlashDB creates a REST-ful API for databases, so you can perform CRUD operations with any database.
To create a new record, send a POST request to a given endpoint containing required data i.e.
curl https://demo.slashdb.com/db/Chinook/Invoice.json -XPOST -i \
-H 'Content-Type: application/json' \
-d '{"BillingPostalCode": "789789",
"InvoiceDate": "2007-01-01T00:00:00",
"BillingAddress": "Theodor-Heuss-Straße 34",
"BillingCountry": "Germany",
"Total": 1.98,
"CustomerId": 2,
"BillingCity": "Stuttgart"}'
HTTP/1.1 201 Created
Server: nginx/1.4.6 (Ubuntu)
Date: Wed, 31 May 2017 10:54:12 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 33
Connection: keep-alive
Location: https://demo.slashdb.com/db/Chinook/Invoice/InvoiceId/456.json
or create multiple records in one go by sending an array of object:
curl https://demo.slashdb.com/db/Chinook/Invoice.json -XPOST -i \
-H 'Content-Type: application/json' \
-d '[{
"BillingPostalCode": "70174",
"InvoiceDate": "2007-01-01T00:00:00",
"BillingAddress": "Theodor-Heuss-Straße 34",
"BillingCountry": "FooLand",
"Total": 1.98,
"CustomerId": 2,
"BillingCity": "Stuttgart"
},{
"BillingPostalCode": "60316",
"InvoiceDate": "2007-01-19T00:00:00",
"BillingAddress": "Berger Straße 10",
"BillingCountry": "FooLand",
"Total": 0.99,
"CustomerId": 37,
"BillingCity": "Frankfurt"
},{
"BillingPostalCode": "10779",
"InvoiceDate": "2007-02-01T00:00:00",
"BillingAddress": "Barbarossastraße 19",
"BillingCountry": "FooLand",
"Total": 1.98,
"CustomerId": 38,
"BillingCity": "Berlin"
}]'
HTTP/1.1 201 Created
Server: nginx/1.4.6 (Ubuntu)
Date: Wed, 31 May 2017 10:59:55 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 101
Connection: keep-alive
/db/Chinook/Invoice/InvoiceId/426
/db/Chinook/Invoice/InvoiceId/427
/db/Chinook/Invoice/InvoiceId/428
You can also send data in XML:
curl https://demo.slashdb.com/db/Chinook/Invoice.xml -XPOST -i \
-H 'Content-Type: text/xml' \
-d '<SlashDB xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.vtenterprise.com/slashdb">
<Invoice>
<CustomerId>2</CustomerId>
<InvoiceDate>2007-01-01T00:00:00</InvoiceDate>
<BillingAddress>Theodor-Heuss-Straße 34</BillingAddress>
<BillingCity>Stuttgart</BillingCity>
<BillingCountry>FooLand</BillingCountry>
<BillingPostalCode>70174</BillingPostalCode>
<Total>1.98</Total>
</Invoice>
<Invoice>
<CustomerId>37</CustomerId>
<InvoiceDate>2007-01-19T00:00:00</InvoiceDate>
<BillingAddress>Berger Straße 10</BillingAddress>
<BillingCity>Frankfurt</BillingCity>
<BillingCountry>FooLand</BillingCountry>
<BillingPostalCode>60316</BillingPostalCode>
<Total>0.99</Total>
</Invoice>
<Invoice>
<CustomerId>38</CustomerId>
<InvoiceDate>2007-02-01T00:00:00</InvoiceDate>
<BillingAddress>Barbarossastraße 19</BillingAddress>
<BillingCity>Berlin</BillingCity>
<BillingCountry>FooLand</BillingCountry>
<BillingPostalCode>10779</BillingPostalCode>
<Total>1.98</Total>
</Invoice>
</SlashDB>'
HTTP/1.1 201 Created
Server: nginx/1.4.6 (Ubuntu)
Date: Wed, 31 May 2017 11:02:03 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 101
Connection: keep-alive
/db/Chinook/Invoice/InvoiceId/429
/db/Chinook/Invoice/InvoiceId/430
/db/Chinook/Invoice/InvoiceId/431
or even CSV:
curl https://demo.slashdb.com/db/Chinook/Invoice.csv -XPOST -i \
-H 'Content-Type: text/csv' \
-d 'CustomerId,InvoiceDate,BillingAddress,BillingCity,BillingState,BillingCountry,BillingPostalCode,Total
2,2007-01-01 00:00:00,Theodor-Heuss-Straße 34,Stuttgart,,FooLand,70174,1.98
37,2007-01-19 00:00:00,Berger Straße 10,Frankfurt,,FooLand,60316,0.99
38,2007-02-01 00:00:00,Barbarossastraße 19,Berlin,,FooLand,10779,1.98'
HTTP/1.1 201 Created
Server: nginx/1.4.6 (Ubuntu)
Date: Wed, 31 May 2017 11:05:16 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 101
Connection: keep-alive
/db/Chinook/Invoice/InvoiceId/432
/db/Chinook/Invoice/InvoiceId/433
/db/Chinook/Invoice/InvoiceId/434
Change the POST method to PUT and you'll be able to update existing records much like you created new entries, e.g.:
curl https://demo.slashdb.com/db/Chinook/Invoice/InvoiceId/1.json -XPUT -i \
-H 'Content-Type: application/json' -d '{"BillingPostalCode": "456"}'
HTTP/1.1 204 No Content
Server: nginx/1.4.6 (Ubuntu)
Date: Wed, 31 May 2017 11:20:48 GMT
Connection: keep-alive
Access-Control-Allow-Origin: *
Just as with POST you can use different formats i.e.
curl https://demo.slashdb.com/db/Chinook/Invoice/InvoiceId/2.csv -XPUT -i \
-H 'Content-Type: text/csv' \
-d 'BillingPostalCode
567'
HTTP/1.1 204 No Content
Server: nginx/1.4.6 (Ubuntu)
Date: Wed, 31 May 2017 11:20:06 GMT
Connection: keep-alive
Performing a DELETE request type will allow you to delete a record on demand. If you only want to delete a specific record, it's best to use a URL containing its Primary Key, e.g.:
# firstly, create a new MediaType record
curl https://demo.slashdb.com/db/Chinook/MediaType.json -XPOST -i -d '{"Name": "cat"}'
HTTP/1.1 201 Created
Server: nginx/1.4.6 (Ubuntu)
Date: Wed, 31 May 2017 11:34:36 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 35
Connection: keep-alive
Location: https://demo.slashdb.com/db/Chinook/MediaType/MediaTypeId/6.json
# and using the returned 'Location'
# let's issue an DELETE request on it using its primary key
curl https://demo.slashdb.com/db/Chinook/MediaType/MediaTypeId/6.json -XDELETE -i
HTTP/1.1 204 No Content
Server: nginx/1.4.6 (Ubuntu)
Date: Wed, 31 May 2017 11:24:34 GMT
Connection: keep-alive
This will delete a MediaType entry with its PK field MediaTypeId, of 6.
When deleting by a non-PK field, e.g:
# create two MediaType records with the 'Name': "Tape"
curl https://demo.slashdb.com/db/Chinook/MediaType.json -XPOST -i -d '{"Name": "Tape"}'
HTTP/1.1 201 Created
Server: nginx/1.4.6 (Ubuntu)
Date: Wed, 31 May 2017 11:34:36 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 35
Connection: keep-alive
Location: https://demo.slashdb.com/db/Chinook/MediaType/MediaTypeId/7.json
curl https://demo.slashdb.com/db/Chinook/MediaType.json -XPOST -i -d '{"Name": "Tape"}'
HTTP/1.1 201 Created
Server: nginx/1.4.6 (Ubuntu)
Date: Wed, 31 May 2017 11:34:36 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 35
Connection: keep-alive
Location: https://demo.slashdb.com/db/Chinook/MediaType/MediaTypeId/8.json
# issue a DELETE request
# deleting all MediaType records with "Name": "Tape"
curl https://demo.slashdb.com/db/Chinook/MediaType/Name/Tape.json -XDELETE -i
HTTP/1.1 204 No Content
Server: nginx/1.4.6 (Ubuntu)
Date: Wed, 31 May 2017 11:38:16 GMT
Connection: keep-alive
# now let's make sure that both records are properly erased
curl https://demo.slashdb.com/db/Chinook/MediaType/Name/Tape.json -i
HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Fri, 09 Jun 2017 11:52:39 GMT
Content-Type: application/json
Content-Length: 2
Connection: keep-alive
[]
be careful, as this will delete all MediaType entries where Name is Tape.
Also remember that if a given table has other tables referencing it, and ON DELETE CASCADE has not been set, a 409 status code will be returned.
validate
The validate
query string parameter will validate data in the HTTP request body against the database schema when sending POST or PUT requests. The validate
parameter is supported for JSON, XML, and CSV formats.
An HTTP 400
status code will be returned if validation fails, along with information about what caused the failure.
Usage:
?validate=true
Value type: boolean (True or False)
Default: False
Applicable to: POST or PUT requests in JSON, XML, CSV formats. Supports resources, vectors, arrays, scalars.
Examples:
Validation with valid JSON data
# request
curl -X POST "https://demo.slashdb.com/db/Chinook/Customer.json?validate=true" -d '{"FirstName": "Bruce", "LastName": "Wayne", "Email": "bat@man.com"}
# response
[{"Content-Location": "/db/Chinook/Customer/CustomerId/77.json"}]
Validation with invalid JSON data
# request
curl -X POST "https://demo.slashdb.com/db/Chinook/Customer.json?validate=true" -d '{"FirstName": 1, "LastName": "Wayne", "Email": "bat@man.com"}
# response
{"http_code": 400, "description": "Could not parse JSON: The \"FirstName\": error at node '/0/FirstName': 1 is not of type 'string'"}