Skip to content
This repository has been archived by the owner on Oct 25, 2018. It is now read-only.

Commit

Permalink
Merge pull request #16 from webcc/v0.4.0
Browse files Browse the repository at this point in the history
V0.4.0
  • Loading branch information
Evangelos Vlachogiannis committed Oct 3, 2015
2 parents fd8c4c2 + d3b14dc commit 074b5fa
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 21 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*.log
*.csv
*.dat
*.out
*.pid
*.gz
pids
logs
.idea
npm-debug.log
node_modules
21 changes: 21 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.git*
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz
*.tgz

pids
logs/
doc/
examples/
test/
.DS_Store
.idea

npm-debug.log
node_modules
1 change: 1 addition & 0 deletions examples/FooMetaModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = class FooMetaModel extends m.MetaModel {
this.clusteringColumns = new Map([["name", "ASC"]]);
this.secondaryIndexes = ["name"];
this.entityClass = Foo;
this.ttl = 5000; // if not, set undefined
}

toRow(entity)
Expand Down
12 changes: 10 additions & 2 deletions lib/CriteriaQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ module.exports = class CriteriaQuery {
pairsQ = pairsQ + "\"" + key + "\"" + "=? ";
}
}
let ttlQ = "";
if(this.metaModel.ttl){
ttlQ = " USING TTL " + this.metaModel.ttl + " ";
}
return {
query: "UPDATE " + this.getFullTableName() + " SET " + pairsQ + criteriaQuery + ";",
query: "UPDATE " + this.getFullTableName() + ttlQ + " SET " + pairsQ + criteriaQuery + ";",
params: params
};
}
Expand Down Expand Up @@ -93,8 +97,12 @@ module.exports = class CriteriaQuery {
paramsKeys = " (" + paramsKeys.substring(0, paramsKeys.length - 1) + ") ";
let paramsQ = JSON.stringify(qq).toString().replace(/"/g, '').substring(1);
paramsQ = " (" + paramsQ.substring(0, paramsQ.length - 1) + ") ";
let ttlQ = "";
if(this.metaModel.ttl){
ttlQ = " USING TTL " + this.metaModel.ttl + " ";
}
let queryObject = {
query: "INSERT INTO " + this.getFullTableName() + paramsKeys + " VALUES " + paramsQ + " IF NOT EXISTS;",
query: "INSERT INTO " + this.getFullTableName() + paramsKeys + " VALUES " + paramsQ + " IF NOT EXISTS " + ttlQ + ";",
params: params
};
return (queryObject);
Expand Down
24 changes: 16 additions & 8 deletions lib/EntityManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,13 @@ module.exports = class EntityManager {
{
JPAConfiguration.validate(config);
this.metaModel = metaModel;
this.config = config;
if (this.metaModel)
{
this.criteriaBuilder = new CriteriaBuilder(metaModel);
this.criteriaQuery = this.criteriaBuilder.createQuery();
}
if (typeof config.client === "undefined" || config.client === null)
{
this.client = CassandraClientFactory.getClient(config.cassandra);
}
else
{
this.client = config.client;
}
this.client = CassandraClientFactory.getClient(config.cassandra);
if (config.logger)
{
this.logger = config.logger;
Expand Down Expand Up @@ -122,6 +116,7 @@ module.exports = class EntityManager {

query(queryObject, callback)
{
this.logQueryObject(queryObject);
let self = this;
this.client.execute(queryObject.query, queryObject.params, this.client.options.queryOptions,
function (error, result)
Expand Down Expand Up @@ -150,6 +145,7 @@ module.exports = class EntityManager {
{
self.client.batch(query, self.client.options.queryOptions, function (error, result)
{
this.logQueryObject(queryObject);
if (error)
{
Logger.error(JSON.stringify(error, null, 0));
Expand Down Expand Up @@ -356,4 +352,16 @@ module.exports = class EntityManager {
return callback(error, result);
});
}
logQueryObject(q)
{
let logQueryObject = false;
if(this.config.logQueryObject !== "undefined")
{
logQueryObject = this.config.logQueryObject;
}
if(logQueryObject)
{
Logger.info(JSON.stringify(q));
}
}
};
1 change: 1 addition & 0 deletions lib/JPAConfiguration.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module.exports = class JPAConfiguration {
pooling.coreConnectionsPerHost[distance.local] = 4;
pooling.coreConnectionsPerHost[distance.remote] = 1;
config.logger = logger;
config.logQueryObject = false;
config.cassandra = {
contactPoints: ["localhost"],
keyspace: "tests",
Expand Down
1 change: 1 addition & 0 deletions lib/MetaModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = class MetaModel {
this.secondaryIndexes = config.secondaryIndexes || [];
this.entityClass = config.entityClass || Entity;
this.rowInterceptor = config.rowInterceptor || new DefaultRowInterceptor();
this.ttl = undefined;
}

fromRow(row)
Expand Down
72 changes: 66 additions & 6 deletions lib/PersistenceUtils.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
"use strict";
let err = require("./errors");
let t = require("typly").instance();
let typly = require("typly").instance();
let TimeUuid = require('cassandra-driver').types.TimeUuid;
module.exports = class PersistenceUtils {
static toTimeUuid(value)
{
t.assertNotNull(value);
if (t.isObject(value))
typly.assertNotNull(value);
if (typly.isObject(value))
{
if (value instanceof TimeUuid)
{
return value;
}
if (t.isDate(value))
if (typly.isDate(value))
{
return TimeUuid.fromDate(value);
}
Expand All @@ -21,9 +21,9 @@ module.exports = class PersistenceUtils {
throw new RangeError("Invalid object");
}
}
else if (t.isString(value))
else if (typly.isString(value))
{
if (t.isUUID(value))
if (typly.isUUID(value))
{
return TimeUuid.fromString(value);
}
Expand All @@ -37,4 +37,64 @@ module.exports = class PersistenceUtils {
throw new TypeError("Invalid type");
}
}

static clone(entity)
{
return new entity.constructor(entity);
}

static bindToJSON(entity)
{
if (entity.toJSON !== "function")
{
entity.toJSON = function toJSON()
{
let obj = {};
Object.keys(this).map(key =>
{
if (key.charAt(0) === "_")
{
let newKey = key.substring(1);
let property = this[key];
if (property == null)
{
obj[newKey] = null;
}
else if (typeof property === "object")
{
obj[newKey] = property;
}
else if (property instanceof Map)
{
let arr = [];
for (let pair of property)
{
let o = [pair[0], pair[1]];
arr.push(o);
}
obj[newKey] = arr;
}
else if (Array.isArray(property))
{
obj[newKey] = this[key];
}
else if (property[Symbol.iterator] && typeof property === "object")
{
throw new RangeError("Unknown collection to toJSON for " + property);
}
else
{
obj[newKey] = this[key];
}
}
else
{
obj[key] = this[key];
}
});
return obj;
};
}
return entity;
}
};
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cassandra-jpa",
"version": "0.1.0",
"version": "0.4.0",
"description": "Imergo Node.js Javascript Persistence API for Apache Cassandra.",
"main": "index.js",
"repository": {
Expand All @@ -11,6 +11,16 @@
"name": "Web Compliance Center",
"email": "[email protected]"
},
"keywords": [
"cassandra",
"cql",
"cql3",
"imergo",
"webcc",
"database",
"jpa",
"persistence"
],
"dependencies": {
"async": "^1.4.2",
"cassandra-driver": "^2.2.1",
Expand All @@ -27,7 +37,7 @@
}
},
"scripts": {
"test": "mocha -R spec -t 10000 test/**/*.test.js",
"test": "mocha -R spec -t 7000 test/**/*.test.js",
"jpa:cover": "istanbul cover -x *.test.js _mocha -- -R spec test/*.js"
},
"license": {
Expand All @@ -37,5 +47,5 @@
"engines": {
"node": ">=4.1.0"
},
"homepage": "https://github.com/webcc/typly"
"homepage": "https://github.com/webcc/cassandra-jpa"
}
2 changes: 1 addition & 1 deletion test/CassandraClientFactory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("cassandra-persistence", function ()
{
describe("#CassandraClientFactory", function ()
{
let config = (new JPAConfiguration()).cassandra;
let config = require("./config/config.js").cassandra;
config.contactPoints = [ process.env.DBHOST || config.contactPoints[0] ];
it("should create a client", function (done)
{
Expand Down
1 change: 0 additions & 1 deletion test/EntityManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ describe("cassandra-persistence", function ()
describe("#EntityManager", function ()
{
let config = require("./config/config.js");
config.cassandra.contactPoints = [ process.env.DBHOST || config.cassandra.contactPoints[0] ];
let fooMetaModel = new FooMetaModel(config.cassandra.keyspace);
let entityManager;
let foo = new Foo({
Expand Down
19 changes: 19 additions & 0 deletions test/PersistenceUtils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,24 @@ describe("cassandra-persistence", function ()
}, RangeError);
});
});
describe("#bindToJSON", function ()
{
let entity =
{
_id: "id",
_name: "name",
echo: function echo (s)
{
return s;
}
};
it("should bind toJSON", function ()
{
let newEntity = m.PersistenceUtils.bindToJSON(entity);
let o = JSON.parse(JSON.stringify(newEntity));
assert(typeof newEntity.toJSON === "function");
assert.equal(o.id, newEntity._id);
});
});
});
});
3 changes: 3 additions & 0 deletions test/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ function initConfig()
configuration = new jpa.JPAConfiguration();
configuration.cassandra.contactPoints = ["localhost"];
configuration.cassandra.keyspace = "tests";
configuration.logQueryObject = false;
configuration.cassandra.contactPoints = [ process.env.JPA_HOST || configuration.cassandra.contactPoints[0] ];
configuration.cassandra.keyspace = [ process.env.JPA_KEYSPACE || configuration.cassandra.keyspace ];
return configuration;
}

0 comments on commit 074b5fa

Please sign in to comment.