Skip to content

Commit

Permalink
Truncating functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
connor4312 committed Apr 17, 2015
1 parent a4f7c5d commit 2f7164f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/cql/builders.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require('lodash').forIn({
delete: require('../cql/queries/delete'),
insert: require('../cql/queries/insert'),
update: require('../cql/queries/update'),
truncate: require('../cql/queries/truncate'),
alterTable: require('../cql/queries/alterTable')
}, function (Builder, name) {
builders[name] = function () {
Expand Down
36 changes: 36 additions & 0 deletions lib/cql/queries/truncate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var Columns = require('../stmt/columns');
var Where = require('../stmt/where');
var Conditionals = require('../stmt/conditionals');
var Options = require('../stmt/options');
var Base = require('./base');
var util = require('../../util');

function Truncate () {
Base.apply(this, arguments);

this.parts = { table: null };
}

Truncate.prototype = new Base();

/**
* Sets the table to truncate.
* @param {String|Table} table
* @return {Truncate}
*/
Truncate.prototype.table = function (table) {
this.parts.table = util.resolveName(table);
return this;
};

/**
* Parameterizes the query, returning an array of parameters followed
* by the string representation.
*
* @return {[Array, String]}
*/
Truncate.prototype.parameterize = function () {
return [[], 'TRUNCATE ' + this.parts.table + ';'];
};

module.exports = Truncate;
8 changes: 8 additions & 0 deletions lib/model/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ Collection.prototype.update = function () {
return this.connection.update().table(this.table.getName());
};

/**
* Truncates data in the collection's table.
* @return {Promise}
*/
Collection.prototype.truncate = function () {
return this.connection.truncate().table(this.table.getName());
};

/**
* Checks out a new Model from the collection.
*
Expand Down
9 changes: 9 additions & 0 deletions spec/model/CollectionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,13 @@ describe('collection', function () {
done();
});
});

it('truncates', function (done) {
collection.truncate().table('tbl').then(function () {
expect(connection.queryLog).toEqual([
['TRUNCATE tbl;', [], {}]
]);
done();
});
});
});

0 comments on commit 2f7164f

Please sign in to comment.