diff --git a/README.md b/README.md index e480cae..38a0b55 100644 --- a/README.md +++ b/README.md @@ -363,6 +363,18 @@ db.class.get('MyClass') }); ``` +### Updating an existing class + +```js +db.class.update({ + name: 'MyClass', + superClass: 'V' +}) +.then(function (MyClass) { + console.log('Updated class: ' + MyClass.name + ' that extends ' + MyClass.superClass); +}); +``` + ### Listing properties in a class ```js diff --git a/lib/db/class/index.js b/lib/db/class/index.js index 25d86db..0bcec77 100644 --- a/lib/db/class/index.js +++ b/lib/db/class/index.js @@ -217,7 +217,7 @@ exports.create = function (name, parentName, cluster, isAbstract) { if (cluster) { query += ' CLUSTER ' + cluster; } - + if(isAbstract) { query += ' ABSTRACT'; } @@ -232,6 +232,31 @@ exports.create = function (name, parentName, cluster, isAbstract) { }); }; +/** + * Update the given class. + * + * @param {Object} class The class settings. + * @param {Boolean} reload Whether to reload the class, default to true. + * @promise {Object} The updated class. + */ +exports.update = function (cls, reload) { + var promises = [], + prefix = 'ALTER CLASS ' + cls.name + ' '; + + if (reload == null) { + reload = true; + } + + if (cls.superClass !== undefined) { + promises.push(this.exec(prefix + 'SUPERCLASS ' + cls.superClass)); + } + + return Promise.all(promises) + .bind(this) + .then(function () { + return this.class.get(cls.name, reload); + }); +}; /** * Delete a class. @@ -250,7 +275,6 @@ exports.drop = function (name) { }); }; - /** * Get a class by name. * diff --git a/test/db/class-test.js b/test/db/class-test.js index ac08ed9..f482f0c 100644 --- a/test/db/class-test.js +++ b/test/db/class-test.js @@ -33,6 +33,29 @@ describe("Database API - Class", function () { return this.db.class.create('TestClass') .then(function (item) { item.name.should.equal('TestClass'); + item.should.have.property('superClass', null); + item.should.be.an.instanceOf(Class); + }); + }); + it('should create a class with the given name and a superClass', function () { + return this.db.class.create('TestClassExtended', 'V') + .then(function (item) { + item.name.should.equal('TestClassExtended'); + item.should.have.property('superClass', 'V'); + item.should.be.an.instanceOf(Class); + }); + }); + }); + + describe('Db::class.update()', function () { + it('should update a class with the given superClass', function () { + return this.db.class.update({ + name: 'TestClass', + superClass: 'V' + }) + .then(function (item) { + item.name.should.equal('TestClass'); + item.should.have.property('superClass', 'V'); item.should.be.an.instanceOf(Class); }); }); @@ -44,7 +67,6 @@ describe("Database API - Class", function () { }); }); - describe('Instance functions', function () { before(function () { return this.db.class.get('OUser')