Using the OneDB Rest Listener
Once the REST listener is started, you can use any HTTP client to access your data through REST. Here are some examples to get you started.
The following examples assume the REST wire listener is running on the localhost at port 8080.
Example 1: Get the list of databases on the server
Response:
[
"db1",
"db2",
"stores_demo"
]
Example 2: Get the list of collection tables in the “stores_demo” database
GET http://localhost:8080/stores_demo?options={includeRelational: true}
Response:
[
"call_type",
"catalog",
"classes",
"cust_calls",
"customer",
...
]
Example 3: Query for all rows the “customer” table
GET http://localhost:8080/stores_demo/customer
Response:
[
{
"customer_num": 101,
"fname": "Ludwig",
"lname": "Pauli",
"company": "All Sports Supplies",
"address1": "213 Erstwild Court",
"address2": null,
"city": "Sunnyvale",
"state": "CA",
"zipcode": "94086",
"phone": "408-789-8075"
},
...
]
Example 4: Query for all rows in the “customer” table, sorting by “customer_num” and only including certain fields in the result
Response:
[
{
"customer_num": 101,
"lname": "Pauli",
"state": "CA"
},
{
"customer_num": 102,
"lname": "Sadler",
"state": "CA"
},
…
]
Example 5: Query the “customer” table with query condition
Note that the syntax of the query condition is based on the MongoDB API.
GET http://localhost:8080/stores_demo/customer?query={"state": "CA", "customer_num":{"$gt": 110}}
Response:
[
{
"customer_num": 111,
"fname": "Frances",
"lname": "Keyes",
"company": "Sports Center",
"address1": "3199 Sterling Court",
"address2": null,
"city": "Sunnyvale",
"state": "CA",
"zipcode": "94085",
"phone": "408-277-7245"
},
…
]
Example 6: Insert one row into a table or JSON collection
POST http://localhost:8080/stores_demo/people
Body: { "firstName": "John", "lastName": "Doe", "age": 31 }
{ "n": 1, "ok": true }
Example 7: Insert multiple rows into a table or JSON collection
POST http://localhost:8080/stores_demo/people
Body:
[
{"firstName": "Jane", "lastName": "Doe", "age": 28},
{"firstName": "Joseph", "lastName": "Doe", "age": 32}
]
Response: { "n": 2, "ok": true }
Example 8: Update one or more rows or documents in a table or JSON collection
Note that the syntax of the query and update condition is based on the MongoDB API.
PUT http://localhost:8080/stores_demo/people?query={"firstName":"John"}
Body:
{"$set": {"age": 32} }
Response: { "n": 1, "ok": true }
Example 9: Delete one or more rows or documents in a table or JSON collection based on a query condition
DELETE http://localhost:8080/stores_demo/people?query={"age":32}
Response: { "n": 2, "ok": true }
Example 10: Run a SQL query directly through REST
Note that running SQL statements directly through REST is only support if security.sql.passthrough is set to true in the REST listener properties file.
GET http://localhost:8080/stores_demo/system.sql?query={"$sql":"select count(*) as count from customer"}
Response:
[
{
"count": 28
}
]
Example 11: Run a SQL query with host variables through REST
Response:
[
{
"count": 8
}
]
The full OneDB REST API is documented here.