Created
June 28, 2014 21:56
-
-
Save KnowledgeGarden/bad28a47feed6015cea8 to your computer and use it in GitHub Desktop.
Testing TQPortal Mongo/Topic/Dataprovider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Really, three js files in one: the devtest, Topic, and Dataprovider | |
/** | |
* textsearchtest | |
*/ | |
var MongoClient = require('mongodb').MongoClient | |
, dp = require('../core/dataprovider') | |
, topic = require('../core/topic'); | |
var myCollection; | |
var myDB; | |
var colName = "MyCollection2"; | |
var begin = function() { | |
var DataProvider = new dp(myDB); | |
console.log('C '+DataProvider); | |
var Topic = new topic(); | |
Topic.setLocator('MyFirstNodexyz'); | |
Topic.setNodeType('SuperFancyNode'); | |
Topic.addLabel('My second label', 'en'); | |
Topic.addDetails('Now is the first time', 'en'); | |
console.log('D '+JSON.stringify(Topic.getData())); | |
DataProvider.putNode(Topic.getData(), function(err,result) { | |
console.log('E '+err+' '+result); | |
DataProvider.getNodeByLocator('MyFirstNodebbb', function(err,result) { | |
console.log('F '+err+' '+result); | |
console.log(JSON.stringify(result)); | |
DataProvider.listNodesByType('SuperFancyNode', function(err,result) { | |
console.log('G '+err+' '+result); | |
var len = result.length; | |
for (var i=0;i<len;i++) { | |
console.log(JSON.stringify(result[i])); | |
} | |
//NOW DO A TEXT SEARCH | |
//http://mongodb.github.io/node-mongodb-native/driver-articles/anintroductionto1_4_and_2_6.html | |
//http://docs.mongodb.org/manual/tutorial/create-text-index-on-multiple-fields/ | |
// find() returns a cursor | |
myCollection.find({ $text: { $search: "\"first time\"" } }, { score: { $meta: "textScore" } } ) | |
.sort({ score: { $meta: "textScore" } },function(err, items) { | |
console.log('I '+err+' '+items); | |
// Sort simply sorts the cursor | |
//http://docs.mongodb.org/manual/reference/method/cursor.sort/ | |
items.toArray(function(err, result) { | |
console.log('II '+err+' '+result); | |
console.log(result.length); | |
// fails because length = 0 | |
}); | |
}); | |
}); | |
}); | |
}); | |
} | |
//create the database | |
MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) { | |
myDB = db; | |
if(!err) { | |
console.log("We are connected"); | |
} | |
db.createCollection(colName, {strict:true}, function(err, collection) { | |
console.log('--'+err); | |
if (err) { | |
console.log('++'+err); | |
db.collection(colName, function(err, collection) { | |
console.log('+++'+err+" "+collection); | |
myCollection = collection; | |
console.log('A '+myCollection); | |
}); | |
} else { | |
myCollection = collection; | |
console.log('B '+myCollection); | |
} | |
//create an index for text search | |
//http://docs.mongodb.org/manual/tutorial/create-text-index-on-multiple-fields/ | |
myCollection.ensureIndex({"$**":'text'},{ name: "TextIndex" }, function(err, indexName) { | |
console.log('oo '+err+" "+indexName); | |
//fire up the program | |
begin(); | |
}); | |
}); | |
}); | |
=========================================== | |
/** | |
* Topic, the primary information artifact | |
*/ | |
var utils = require('./util/utils') | |
, struct = require('./util/relnstruct') | |
, dateFormat = require('dateformat') | |
, constants = require('./constants') | |
, properties = require('./properties'); | |
var Topic = module.exports = function(jsonObject) { | |
if (jsonObject) | |
this.data = jsonObject; | |
else | |
this.data = {}; | |
}; | |
Topic.prototype.getData = function() { | |
return this.data; | |
}; | |
/////////////////////////////////////////////// | |
//Subject Identity: | |
//Based on several components: | |
// A LOCATOR which is a unique identifier in the database | |
// Location in a taxonomy: | |
// Type (instanceOf) | |
// SuperClass (subclassOf) | |
// Relations (links to Tuples) | |
/////////////////////////////////////////////// | |
//A Tuple is a Topic which forms the equivalent | |
//of a TRIPLE: | |
// {subject, predicate, object} | |
/////////////////////////////////////////////// | |
/** | |
* A <code>locator</code> is a <em>UUID</em> for this | |
* object; it is an identifier. | |
* @param locator | |
*/ | |
Topic.prototype.setLocator =function(locator) { | |
this.data[properties.LOCATOR]=locator; | |
//mongo id | |
this.data._id = locator; | |
}; | |
Topic.prototype.getLocator = function() { | |
return this.data[properties.LOCATOR]; | |
}; | |
//////////////////////////////////////////////// | |
//Taxonomy | |
//Transitive Closure is a list of all parents "up the tree" | |
// from this topic. Maintaining that lest allows us to | |
// answer an "isA" question without multiple databaes calls | |
//TransitiveClosure is modeled as properties['transitiveClosure'] list | |
//////////////////////////////////////////////// | |
Topic.prototype.setNodeType = function(typeLocator) { | |
this.data[properties.INSTANCE_OF] = typeLocator; | |
//TODO: deal with transitive closure | |
}; | |
Topic.prototype.getNodeType = function() { | |
return this.data[properties.INSTANCE_OF]; | |
}; | |
Topic.prototype.addSuperClassLocator = function(superClassLocator) { | |
console.log('Topic.addSuperClassLocator '+superClassLocator+' '+this); | |
var supx = this.data[properties.SUB_OF]; | |
console.log('AAA '+supx); | |
if (!supx) { | |
supx = []; | |
} | |
var where = supx.indexOf(superClassLocator); | |
console.log('XXX '+where+ " | "+supx); | |
if (where < 0) { | |
supx.push(superClassLocator); | |
this.data[properties.SUB_OF] = supx; | |
} | |
//TODO: deal with transitive closure | |
}; | |
Topic.prototype.listSuperClassLocators = function () { | |
return this.data[properties.SUB_OF]; | |
}; | |
Topic.prototype.isA = function(locator) { | |
var tclist = this.data[properties.TRANSITIVE_CLOSURE]; | |
if (!tclist) { | |
return false; | |
} | |
var where = tclist.indexOf(locator); | |
return (where > -1); | |
}; | |
///////////////////////////////////////////////// | |
//Text data | |
///////////////////////////////////////////////// | |
function makeLanguageLabel(language) { | |
if (language === constants.ENGLISH) { | |
return properties.LABEL; | |
} | |
return properties.LABEL+language; | |
}; | |
function makeLanguageDetails(language) { | |
if (language === constants.ENGLISH) { | |
return properties.DETAILS; | |
} | |
return properties.DETAILS+language; | |
}; | |
Topic.prototype.addLabel = function(label, language) { | |
var lan = language; | |
if (!lan) { | |
lan = constants.ENGLISH; // default | |
} | |
var key = makeLanguageLabel(lan); | |
var lx = this.data[key]; | |
if (!lx) { | |
lx = []; | |
} | |
lx.push(label); | |
this.data[key] = lx; | |
}; | |
Topic.prototype.addDetails = function(details,language) { | |
var lan = language; | |
if (!lan) { | |
lan = properties.ENGLISH; // default | |
} | |
var key = makeLanguageDetails(lan); | |
var lx = this.data[key]; | |
if (!lx) { | |
lx = []; | |
} | |
lx.push(details); | |
this.data[key] = lx; | |
}; | |
Topic.prototype.listLabels = function(language) { | |
var lan = language; | |
if (!lan) { | |
lan = constants.ENGLISH; // default | |
} | |
return this.data[makeLanguageLabel(lan)]; | |
}; | |
Topic.prototype.listDetails = function(language) { | |
var lan = language; | |
if (!lan) { | |
lan = constants.ENGLISH; // default | |
} | |
return this.data[makeLanguageDetails(lan)]; | |
}; | |
//////////////////////////////////// | |
// Relations | |
//////////////////////////////////// | |
/** | |
* <p>Add a modeled relation to this topic.</p> | |
* <p>Relations are modeled as a JSON object, with fields used | |
* according to the type of relation.</p> | |
* <p>There are two model types: <em>SimpleRelationType</em> and | |
* <em>ComplexRelationType</em>.</p> | |
* | |
*/ | |
Topic.prototype.addRelation = function(relationType, relationLabel, documentType, | |
documentSmallIcon, | |
targetLocator, targetLabel) { | |
console.log('TOPIC.addRelation '+documentSmallIcon+' '+relationType+' '+relationLabel+ | |
targetLocator+' '+targetLabel); | |
var rx = struct(relationType,relationLabel,documentType, | |
documentSmallIcon, targetLocator, targetLabel); | |
if (!this.data.relations) | |
this.relations = []; | |
this.relations.push(rx); | |
}; | |
Topic.prototype.listRelations = function() { | |
return this.data.relations; | |
}; | |
/** | |
* Add a child node to this topic: this builds a conversation tree. | |
* @param childSmallIcon | |
* @param childLocator | |
* @param childLabel | |
*/ | |
Topic.prototype.addChildNode = function(childSmallIcon, childLocator,childLabel) { | |
console.log('TOPIC.addChildNode '+childLocator+' '+childLabel); | |
var rx = struct("","","", | |
childSmallIcon, childLocator, targetchildLabelLabel); | |
if (!this.data.children) | |
this.data.children = []; | |
this.data.children.push(rx); | |
}; | |
Topic.prototype.listChildNodes = function() { | |
return this.data.children; | |
}; | |
///////////////////////////////////////////////// | |
//Other properties | |
///////////////////////////////////////////////// | |
Topic.prototype.setResourceUrl = function(url) { | |
this.data['url'] = url; | |
}; | |
Topic.prototype.getResourceUrl = function() { | |
return this.data['url']; | |
}; | |
Topic.prototype.setCreatorId = function(creatorId) { | |
this.data['creatorId'] = creatorId; | |
}; | |
Topic.prototype.getCreatorId = function() { | |
return this.data['creatorId']; | |
}; | |
Topic.prototype.setImage = function(imagePath) { | |
this.data['largeIcon'] = imagePath; | |
}; | |
Topic.prototype.getImage = function() { | |
return this.data['largeIcon']; | |
}; | |
Topic.prototype.setSmallImage = function(imagePath) { | |
this.data['smallIcon'] = imagePath; | |
}; | |
Topic.prototype.getSmallImage = function() { | |
return this.data['smallIcon']; | |
}; | |
Topic.prototype.setDate = function(date) { | |
//@see https://github.com/litejs/date-format-lite | |
this.data['createdDate'] = dateFormat(date, 'yyyy-MM-dd HH:mm:ss'); | |
}; | |
Topic.prototype.getDate = function() { | |
//@see https://github.com/litejs/date-format-lite | |
return this.data['createdDate'].date(); | |
}; | |
Topic.prototype.setLastEditDate = function(date) { | |
this.data['lastEditDate'] = dateFormat(date, 'yyyy-MM-dd HH:mm:ss'); | |
}; | |
Topic.prototype.getLastEditDate = function() { | |
return this.data['lastEditDate'].Date(); | |
}; | |
Topic.prototype.setIsPrivate = function(isPrivate) { | |
var ip = 'false'; | |
if (isPrivate) { | |
ip = 'true'; | |
} | |
this.data['isPrivate'] = ip; | |
}; | |
Topic.prototype.getIsPrivate = function() { | |
var ip = this.data['isPrivate']; | |
if (!ip) { | |
return false; // not set = default false | |
} | |
if (ip === 'true') { | |
return true; | |
} | |
return false; | |
}; | |
============================================ | |
/** | |
* dataprovider: a structured database | |
*/ | |
var uuid = require('node-uuid') | |
, EventEmitter = require('events').EventEmitter | |
, tm = require('./topicmodel') | |
, util = require('util'); | |
var Dataprovider = module.exports = function(db) { | |
this.database = db; | |
this.uuid1 = uuid.v1(); | |
this.TopicModel = new tm(); | |
}; | |
Dataprovider.prototype.getTopicModel = function() { | |
return this.TopicModel; | |
}; | |
Dataprovider.prototype.getNodeByLocator = function(locator, callback) { | |
this.database.collection('proxies', function(err, collection) { | |
console.log('GETNODEBYLOCATOR '+collection.collectionName); | |
var q = {}; | |
q['_id'] = locator; | |
collection.findOne(q,function(err, result) { | |
console.log('Dataprovider.getNodeByLocator '+locator+' '+err+' '+result); | |
callback(err,result); | |
}); | |
}); | |
}; | |
/** | |
* Insert a <code>node</code> | |
* @param node: a JSON object | |
*/ | |
Dataprovider.prototype.putNode = function(node, callback) { | |
console.log('DataProvider.putNode- '+JSON.stringify(node)); | |
this.database.collection('proxies', function(err, collection) { | |
collection.save(node, {upsert:'true'}, function(err,result) { | |
console.log('Dataprovider.putNode '+node.locator+' '+err+' '+result); | |
callback(err,result); | |
}); | |
}); | |
}; | |
Dataprovider.prototype.listNodesByType = function(type, callback) { | |
this.database.collection('proxies', function(err, collection) { | |
var q = {}; | |
q['instanceOf'] = type; | |
collection.find(q).toArray(function(err, result) { | |
console.log('Dataprovider.listNodesByType '+type+' '+err+' '+result); | |
callback(err,result); | |
}); | |
}); | |
}; | |
//findNodeByKeyValue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment