Skip to content

Commit

Permalink
Making the close function accessible
Browse files Browse the repository at this point in the history
  • Loading branch information
masumsoft committed Oct 4, 2015
1 parent 02ef47f commit a96fb44
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 80 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ What does the above code means?
+ `complete_name` the default values is calculated from others field. When the orm processes your model instances, the `complete_name` will be the result of the function you defined. In the function `this` is bound to the current model instance.
+ `age` no default is provided and we could write it just as `"age": "int"`.
+ `created`, like uuid(), will be evaluated from cassandra using the `now()` function.
- `key`: here is where you define the key of your table. As you can imagine, the first value of the array is the `partition key` and the others are the `clustering keys`. The `partition key` can be an array defining a `compound key`. Read more about keys on the <a href="http://www.datastax.com/documentation/cql/3.1/cql/cql_using/use_compound_keys_t.html" target="_blank">documentation</a>
- `key`: here is where you define the key of your table. As you can imagine, the array defines a `compound primary key` and the first value of the array is the `partition key` and the others are the `clustering keys`. The `partition key` itself can be an array with multiple fields making it a `composite key`. Read more about compound keys on the <a href="http://docs.datastax.com/en/cql/3.1/cql/ddl/ddl_compound_keys_c.html" target="_blank">documentation</a>
- `indexes` are the index of your table. It's always an array of field names. You can read more on the <a href="http://www.datastax.com/documentation/cql/3.1/cql/ddl/ddl_primary_index_c.html" target="_blank">documentation</a>
- `custom_index` provides the ability to define custom indexes with cassandra. Cassandra upto version 2.1.x supports only one custom index per table.

Expand Down Expand Up @@ -163,6 +163,8 @@ Express cassandra exposes some node driver methods for convenience. To generate
returns a type 3 (random) uuid, suitable for Cassandra `uuid` fields, as a string
* `models.timeuuid()`
returns a type 1 (time-based) uuid, suitable for Cassandra `timeuuid` fields, as a string
* `models.consistencies`
this object contains all the available consistency enums defined by cassandra driver, so you can for example use models.consistencies.one, models.consistencies.quorum etc.


### Support for Composite Data Types
Expand Down Expand Up @@ -578,6 +580,16 @@ models.instance.Person.get_cql_client(function(err, client){

```

## Closing connections to cassandra

You can close all orm connections to cassandra by using the following function:

```js
models.close(function(err){
if(err) throw err;
});
```

## Note

All queries except schema definition related queries (i.e. create table etc.) are prepared by default. If you don't want to prepare queries, just set `prepare=false` in the options object.
Expand Down
162 changes: 83 additions & 79 deletions lib/expressCassandra.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,93 +7,97 @@ var orm = require('./orm/apollo');

module.exports = {

setDirectory : function(directory) {
this.directory = directory;
this.modelInstance = {};
return this;
},

bind : function(options, cb) {
var self = this;

self.orm = new orm(options.clientOptions, options.ormOptions);
self.orm.connect(function(err){
if(err) {
console.error(err);
}

fs.readdir(self.directory, function(err, list) {
if(err) {
if(cb) cb(err);
return;
}

async.each(list, function(file, callback) {

var fileName = self.directory + '/' + file;
if(fileName.indexOf('Model') == -1) {
callback();
return;
}

var modelName = self._translateFileNameToModelName(file);

if(modelName) {
var modelSchema = require(fileName);
self.modelInstance[modelName] = self.orm.add_model(
modelName.toLowerCase(),
modelSchema,
function(err, result){
if(err) {
console.error(err);
}
callback();
}
);
}
else {
callback();
}
}, function(err){
if(err) {
if(cb) cb(err);
}
else {
if(cb) cb();
}

});
});

});
},

timeuuid: function() {
var timeuuid = cql.types.TimeUuid.now();
setDirectory : function(directory) {
this.directory = directory;
this.modelInstance = {};
return this;
},

bind : function(options, cb) {
var self = this;

self.orm = new orm(options.clientOptions, options.ormOptions);
self.orm.connect(function(err){
if(err) {
console.error(err);
}

fs.readdir(self.directory, function(err, list) {
if(err) {
if(cb) cb(err);
return;
}

async.each(list, function(file, callback) {

var fileName = self.directory + '/' + file;
if(fileName.indexOf('Model') == -1) {
callback();
return;
}

var modelName = self._translateFileNameToModelName(file);

if(modelName) {
var modelSchema = require(fileName);
self.modelInstance[modelName] = self.orm.add_model(
modelName.toLowerCase(),
modelSchema,
function(err, result){
if(err) {
console.error(err);
}
callback();
}
);
}
else {
callback();
}

}, function(err){

if(err) {
if(cb) cb(err);
}
else {
if(cb) cb();
}

});

});

});
},

timeuuid: function() {
var timeuuid = cql.types.TimeUuid.now();
return timeuuid.toString();
},
},

uuid: function() {
var uuid = cql.types.Uuid.random();
uuid: function() {
var uuid = cql.types.Uuid.random();
return uuid.toString();
},
},

get instance() {
get instance() {
return this.modelInstance;
},

get close() {
return this.orm.close;
},

get consistencies() {
return cql.types.consistencies;
return cql.types.consistencies;
},

_translateFileNameToModelName : function(fileName) {
return fileName
.slice( 0,
//Get everything before the last dot
fileName.lastIndexOf('.'))
.replace('Model', '');
}
_translateFileNameToModelName : function(fileName) {
return fileName
.slice( 0,
//Get everything before the last dot
fileName.lastIndexOf('.'))
.replace('Model', '');
}
};
9 changes: 9 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,13 @@ describe('Unit Tests', function(){
});
});
});

describe('#close cassandra connection',function(){
it('should close connection to cassandra without errors', function(done) {
models.close(function(err){
if(err) throw err;
done();
});
});
});
});

0 comments on commit a96fb44

Please sign in to comment.