Part 1 of 4. Basic Operations
1. Acquire a new Client ID and type it in textbox on top of the page
2. Add cde.js script to the page. It has already been added to this page
3. Initialize CDE:
// Initialize client
CdeClient =
new Cde(
'http://cndataengine2.cloudapp.net/cde/rest/',
$('#client_id').val());
4. Add code to insert some data. Car is chosen as type name. See inline comments for more explanation
// Create a Car object
var car = {
Id: CdeClient.newGuid(),
Producer: 'BMW',
Model: '645',
Color: 'Blue'
};
// Insert the car
CdeClient.insert('Car', car, function() {
log('Added successfully');
// When car is inserted, get list of all cars
CdeClient.find('Car', '', function (cars) {
// Show cars
log('Cars found:\n');
$.each(cars, function (i, car) {
dump(car);
});
});
});
Full code (just copy it to console box, enter client ID and click Run)
// Initialize client
CdeClient =
new Cde(
'http://cndataengine2.cloudapp.net/cde/rest/',
$('#client_id').val());
// Create a Car object
var car = {
Id: CdeClient.newGuid(),
Producer: 'BMW',
Model: '645',
Color: 'Blue'
};
// Insert the car
CdeClient.insert('Car', car, function() {
log('Added successfully');
// When car is inserted, get list of all cars
CdeClient.find('Car', '', function (cars) {
// Show cars
log('Cars found:\n');
$.each(cars, function (i, car) {
dump(car);
});
});
});
Any time click Run to see result. You may further experiment changing the code and clicking Run to see new result.
Part 2 of 4. Batch Insert
Batch insert 100 cars. See inline comments for explanation
// Initialize client
CdeClient =
new Cde(
'http://cndataengine2.cloudapp.net/cde/rest/',
$('#client_id').val());
// Generate array with 100 cars
var cars = [];
for (var i = 0; i < 100; i++) {
cars.push({
Id: CdeClient.newGuid(),
Producer: 'Prod' + ((i%3)+1),
Model: 'Model' + ((i%10)+1),
Color: 'Color' + ((i%5)+1)
});
}
// Prepare data for the service
var carsToInsert =
$.map(cars, function (car) {
return {
Id: car.Id,
JSon: JSON.stringify(car)
};
});
// Batch insert 100 cars
CdeClient.batchInsert(
'Car', carsToInsert,
function (numberOfCars) {
// When insert finished, show result
log(numberOfCars + ' cars were added');
});
Part 3 of 4. Index all Data
Iterate through all items and index.
It is recommended to index inserted records immediately after saving.
// Initialize client
CdeClient =
new Cde(
'http://cndataengine2.cloudapp.net/cde/rest/',
$('#client_id').val());
// Get all cars
CdeClient.find('Car', '', function (cars) {
// Iterate through cars
$.each(cars, function (i, car) {
if (typeof(car.Id) != 'undefined') {
// Index all fields of the car
CdeClient.index(
'Car', [
{
Id: car.Id,
Key: car.Producer.toLowerCase()
},
{
Id: car.Id,
Key: car.Model.toLowerCase()
},
{
Id: car.Id,
Key: car.Color.toLowerCase()
}
], function () { });
}
});
log('Indexed ' + cars.length + ' cars');
});
Part 4 of 4. Search for Data
Note that you can only find indexed data. Search return no results unless data is indexed.
// Initialize client
CdeClient =
new Cde(
'http://cndataengine2.cloudapp.net/cde/rest/',
$('#client_id').val());
// Search for cars
CdeClient.find('Car', 'Prod1', function (cars) {
// Show results
log('Search results:');
$.each(cars, function (i, car) {
dump(car);
});
});
To search for exact match, use quotes. In the following example "Model1" is returned but not "Model10":
// Initialize client
CdeClient =
new Cde(
'http://cndataengine2.cloudapp.net/cde/rest/',
$('#client_id').val());
// Search for cars
CdeClient.find('Car', '"Model1"', function (cars) {
// Show results
log('Search results:');
$.each(cars, function (i, car) {
dump(car);
});
});
To search for more than one keywork, use '+' separator:
// Initialize client
CdeClient =
new Cde(
'http://cndataengine2.cloudapp.net/cde/rest/',
$('#client_id').val());
// Search for cars
CdeClient.find('Car', 'Prod1+Model1', function (cars) {
// Show results
log('Search results:');
$.each(cars, function (i, car) {
dump(car);
});
});
To include only results having all keywords, use 'all:' prefix:
// Initialize client
CdeClient =
new Cde(
'http://cndataengine2.cloudapp.net/cde/rest/',
$('#client_id').val());
// Search for cars
CdeClient.find('Car', 'all:Prod1+Model1', function (cars) {
// Show results
log('Search results:');
$.each(cars, function (i, car) {
dump(car);
});
});