dynamic-record
Advanced tools
Comparing version 0.4.1 to 0.4.2
@@ -147,4 +147,28 @@ "use strict"; | ||
/** | ||
* Add an index to the table's schema | ||
* Drop the table from the database. | ||
* | ||
* @method dropTable | ||
* @memberOf DynamicSchema | ||
* @instance | ||
* @return {Promise} | ||
*/ | ||
dropTable() { | ||
return connect.then((db) => { | ||
return db.collection("_schema").deleteOne({ "_$id": this.tableSlug }).then((result) => { | ||
return db.collection(this.tableSlug).drop(); | ||
}).then(() => { | ||
return db.collection("_counters").deleteOne({ "_$id": this.tableSlug }); | ||
}).then(() => { | ||
this.tableName = null; | ||
this.tableSlug = null; | ||
this.definition = {}; | ||
return Promise.resolve(); | ||
}); | ||
}).catch((err) => { | ||
return Promise.reject(err); | ||
}); | ||
} | ||
/** | ||
* Add an index to the table's schema. | ||
* | ||
* @method renameTable | ||
@@ -151,0 +175,0 @@ * @memberOf DynamicSchema |
{ | ||
"name": "dynamic-record", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"description": "A bare minimum Javascript implementation of the Active Record pattern", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -195,5 +195,31 @@ require("dotenv").config(); | ||
/** | ||
* Add an index to the table's schema | ||
* Drop the table from the database. | ||
* | ||
* @method dropTable | ||
* @memberOf DynamicSchema | ||
* @instance | ||
* @return {Promise} | ||
*/ | ||
dropTable(){ | ||
return connect.then((db) => { | ||
return db.collection("_schema").deleteOne({"_$id": this.tableSlug}).then((result) => { | ||
return db.collection(this.tableSlug).drop(); | ||
}).then(() => { | ||
return db.collection("_counters").deleteOne({"_$id": this.tableSlug}); | ||
}).then(() => { | ||
this.tableName = null; | ||
this.tableSlug = null; | ||
this.definition = {}; | ||
return Promise.resolve(); | ||
}); | ||
}).catch((err) => { | ||
return Promise.reject(err); | ||
}); | ||
} | ||
/** | ||
* Add an index to the table's schema. | ||
* | ||
* @method renameTable | ||
@@ -200,0 +226,0 @@ * @memberOf DynamicSchema |
@@ -106,2 +106,70 @@ // For setup and clean ups | ||
describe("dropTable()", function(){ | ||
let table; | ||
beforeEach(function(done){ | ||
utils.dropTestTable().then((reply) => { | ||
table = new DynamicSchema(); | ||
table.createTable(testSchema).then(() => { | ||
done(); | ||
}).catch((err) => { | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
after(function(){ | ||
return utils.dropTestTable(); | ||
}); | ||
it("should remove the table's entry in the _schema table", function(done){ | ||
connect.then((db) => { | ||
table.dropTable().then(() => { | ||
return db.collection("_schema").findOne({"_$id": testSchema.$id}); | ||
}).then((schema) => { | ||
assert.isNull(schema, "Entry under _schema table is deleted."); | ||
done(); | ||
}); | ||
}).catch((err) => { | ||
done(err); | ||
}); | ||
}); | ||
it("should drop the table itself", function(done){ | ||
connect.then((db) => { | ||
table.dropTable().then(() => { | ||
return db.listCollections({name: testSchema.$id}).toArray(); | ||
}).then((items) => { | ||
assert.isEmpty(items, "Table is dropped from the database"); | ||
done(); | ||
}); | ||
}).catch((err) => { | ||
done(err); | ||
}); | ||
}); | ||
it("should remove the table's entry in the _counters table", function(done){ | ||
connect.then((db) => { | ||
table.dropTable().then(() => { | ||
return db.collection("_counters").findOne({"_$id": testSchema.$id}); | ||
}).then((schema) => { | ||
assert.isNull(schema, "Entry under _counters table is deleted."); | ||
done(); | ||
}); | ||
}).catch((err) => { | ||
done(err); | ||
}); | ||
}); | ||
it("should remove existing data from the instance", function(done){ | ||
connect.then((db) => { | ||
table.dropTable().then(() => { | ||
assert.isNull(table.tableName, "Table name is set to null"); | ||
assert.isNull(table.tableSlug, "Table slug is set to null"); | ||
assert.isEmpty(table.definition, "Table definition is emptied"); | ||
done(); | ||
}); | ||
}).catch((err) => { | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
describe("renameTable()", function(){ | ||
@@ -108,0 +176,0 @@ // Instance of DynamicSchema used for testing |
125812
29
3720