Skip to content

Commit

Permalink
BASIC CRUD complete - delete record works - #9
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Dec 17, 2014
1 parent 2796bf3 commit ef7c3cd
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ esta
- [x] Create/Save a record (basic POST request)
- [x] Read a record
- [x] Update a record
- [ ] Delete (hard)
- [x] Delete (hard)

### Advanced

Expand Down
29 changes: 29 additions & 0 deletions lib/delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var http = require('http');
var config = require('../config');

function del(record, callback) {

var options = {
host: config.host,
port: config.port,
path: '/' + record.index + '/' + record.type + '/' + record.id,
method: 'DELETE'
};

var resStr = '';
// Set up the request
var req = http.request(options, function(res) {
res.setEncoding('utf8');
var resStr = '';
res.on('data', function (chunk) {
resStr += chunk;
}).on('end', function () {
callback(null, JSON.parse(resStr));
});
}).end();

}

module.exports = {
del : del
}
2 changes: 1 addition & 1 deletion test/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var test = require('tape');
var chalk = require('chalk');
var C = require('../lib/connection.js');

test(chalk.yellow('Check we are able to connect to ES on localhost:9200'), function (t) {
test(chalk.yellow('CONNECT to ES on localhost:9200'), function (t) {
C.checkConnection(function (err, res) {
t.equal(err, null, chalk.green("✓ No Errors Connecting"));
t.equal(res.status, 200, chalk.green("✓ Status 200 - OK"));
Expand Down
4 changes: 2 additions & 2 deletions test/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var faker = require('faker');

var C = require('../lib/create.js');

test(chalk.yellow('Create a record'), function (t) {
test(chalk.yellow('CREATE a record'), function (t) {
var record = {
type: 'tweet',
index: 'twitter',
Expand All @@ -19,7 +19,7 @@ test(chalk.yellow('Create a record'), function (t) {
});
});

test(chalk.yellow('Create a record without specifying index, type or id!'), function (t) {
test(chalk.yellow('CREATE a record without specifying index, type or id!'), function (t) {
var record = {
message: faker.hacker.phrase()
}
Expand Down
34 changes: 34 additions & 0 deletions test/delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var test = require('tape');
var chalk = require('chalk');
var faker = require('faker');

var C = require('../lib/create.js');
var R = require('../lib/read.js');
var D = require('../lib/delete.js');

test(chalk.yellow('DELETE a Record'), function (t) {
var record = {
type: 'tweet',
index: 'twitter',
id: Math.floor(Math.random() * (1000000)),
message: faker.hacker.phrase()
}

var rec = {}; // make a copy of rec for later.
for(var key in record) {
if(record.hasOwnProperty(key)) {
rec[key] = record[key];
}
}
C.create(record, function(err, res) {
D.del(rec, function(err3, res3) {
t.equal(res3.found, true, chalk.green("✓ Record Existed - So Delete it!"));
t.equal(err3, null, chalk.green("✓ No Errors Deleting"));
// attempt to read record - it should fail
R.read(rec, function(err4, res4){
t.equal(res4.found, false, chalk.green("✓ Record Deleted"));
t.end();
})
});
});
});
2 changes: 1 addition & 1 deletion test/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var faker = require('faker');
var C = require('../lib/create.js');
var R = require('../lib/read.js');

test(chalk.yellow('Read a record'), function (t) {
test(chalk.yellow('READ a record'), function (t) {
var record = {
type: 'tweet',
index: 'twitter',
Expand Down
2 changes: 1 addition & 1 deletion test/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var C = require('../lib/create.js');
var R = require('../lib/read.js');
var U = require('../lib/update.js');

test(chalk.yellow('Read a record'), function (t) {
test(chalk.yellow('UPDATE a record'), function (t) {
var record = {
type: 'tweet',
index: 'twitter',
Expand Down

0 comments on commit ef7c3cd

Please sign in to comment.