diff --git a/lib/cql/builders.js b/lib/cql/builders.js index 5ad8153..edc7071 100644 --- a/lib/cql/builders.js +++ b/lib/cql/builders.js @@ -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 () { diff --git a/lib/cql/queries/truncate.js b/lib/cql/queries/truncate.js new file mode 100644 index 0000000..d13a9ca --- /dev/null +++ b/lib/cql/queries/truncate.js @@ -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; diff --git a/lib/model/collection.js b/lib/model/collection.js index a475d20..ac7e47e 100644 --- a/lib/model/collection.js +++ b/lib/model/collection.js @@ -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. * diff --git a/spec/model/CollectionSpec.js b/spec/model/CollectionSpec.js index 0d579ad..e544a4c 100644 --- a/spec/model/CollectionSpec.js +++ b/spec/model/CollectionSpec.js @@ -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(); + }); + }); });