Updating Data
SlashDB creates a REST-ful API for databases, so you can perform CRUD operations with any database. Single data records can also be updated or deleted using a web browser.
Updating with the Browser
Updating a Record
Using the Track/TrackId/1 record in the demo Chinook database as a guide:
/db/Chinook/Track/TrackId/1.html
Navigate to the URL of a single record within a database and click the Edit button on the top right side of the page to modify the data.
The record data will appear in a form.
Nullable fields will have a NULL button, which can be used to toggle NULL
values.
To close the editor without saving any changes, click the Cancel button.
Click the Save button to write changes to the database.
Upon saving, if a record's primary key has changed, you will be redirected to the resource's new URL.
If an error occurs while attempting to save a record, a message will be displayed and no changes will be made.
For example, if you try to update a record's primary key value to a value that is already in use by another record (e.g. modify the TrackId to a value that is already taken), an error message will appear.
Deleting a Record
Using the PlaylistTrack/PlaylistId/1/TrackId/3402 record in the demo Chinook database as a guide:
/db/Chinook/PlaylistTrack/PlaylistId/1/TrackId/3402.html
Navigate to the URL of a single record within a database and click the Delete button on the top right side of the page to remove the record.
On clicking, a confirmation dialog will appear.
On clicking Yes, the record will be removed by the database and the URL for the record will no longer work. You will be redirected to the record's parent table and a confirmation message will appear.
If an error occurs when attempting to delete a record, a message will appear.
For example, trying to delete the /db/Chinook/Invoice/InvoiceId/1.html record will display an error. This is expected since the record is referenced by another table via a foreign key, so the database engine prevented deletion of the record.
Updating with the REST API
Updates to data are performed by making requests to an API endpoint using HTTP POST, PUT, or DELETE methods. All update operations work with single or multiple records.
Creating Records (POST)
To create new records, send a POST request to a given endpoint containing the required data.
To create a single record:
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
To create multiple records simultaneously, send an array of objects:
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 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
Updating Records (PUT)
To update existing records, send a PUT request to a given endpoint containing the required data.
To update a single record, use an endpoint with the target record's Primary Key:
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: *
You can also use other formats like XML or CSV:
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
You can update multiple records:
curl https://demo.slashdb.com/db/Chinook/Invoice/BillingCity/Boston.json -XPUT -i \
-H 'Content-Type: application/json' -d '{"BillingPostalCode": "02108"}'
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: *
This will update all Invoice records where the BillingCity = "Boston"
Deleting Records (DELETE)
To delete existing records, send a DELETE request to a given endpoint. If you only want to delete a specific record, use an endpoint that contains the Primary Key.
To delete a single record:
# first create a new record to delete
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 the MediaType record with the PK field MediaTypeId = 6.
When deleting by a non-PK field:
# first 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
[]
Both MediaType records with the Name field of "Tape" will be deleted.
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.
Data Validation
Data that is sent to an endpoint for creating or updating a resource is validated against the database schema by default. Validation can be turned off by default in the configuration file.
If the validation parameter is set to False
in the SlashDB configuration, you can validate an individual request by using the validate
URL query string parameter (modifier). It 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=false
?validate=true
Value type: boolean (true or false)
Default: true
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" -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" -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'"}
Skip validation with invalid JSON data
# request
curl -X POST "https://demo.slashdb.com/db/Chinook/Customer.json?validate=false" -d '{"FirstName": 1, "LastName": "Wayne", "Email": "bat@man.com"}
# response
[{"Content-Location": "/db/sqlite-chinook/Customer/CustomerId/73.json"}]
Here, the value 1 for the FirstName attribute in the payload will be cast to a string value "1" which is the expected data type for FirstName in the database.