Permalink
Browse files

Initial code to populate Neo4J nodes from CSV

  • Loading branch information...
1 parent 75541e6 commit 8a88f45a538e3933541cde6153c2066bee824e8b skamarrudin committed Jul 5, 2012
View
4 app.js
@@ -45,5 +45,9 @@ app.del('/users/:id', routes.users.del);
app.post('/users/:id/follow', routes.users.follow);
app.post('/users/:id/unfollow', routes.users.unfollow);
+app.get('/datainit/business', routes.datainit.loadBusiness);
+
+app.get('/businesses', routes.businesses.list);
+
app.listen(process.env.PORT || 3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
View
BIN data/Business.csv
Binary file not shown.
View
12 data/BusinessTest.csv
@@ -0,0 +1,12 @@
+ Id , Name , ACN , Phone , Website , AddressLine1 , AddressLine2 , Postcode , City , State , Type
+ 1 , Admunaquax Holdings , 512888591 , 060-357-0479 , http://hjgqktbxthwd.com.au , 254 , New Freeway , 3004 , Hamilton , NSW , T
+ 2 , Klisapeficator International Company , 911268370 , 3237875959 , http://buwkjqyzcwfj.com.nz , 125 , White Clarendon Way , 2872 , Adelaide , NT , C
+ 3 , Rehupadar Direct , 296352667 , 834-6660209 , http://twdulvvmjv.com.au , 116 , West New Road , 4397 , Parramatta , TAS , T
+ 4 , Ciptinicator Holdings , 157361279 , 276-6711926 , http://hudmfqsslscnke.com.nz , 876 , Rocky Nobel Avenue , 6400 , Dubbo , VIC , T
+ 5 , Emtinicator WorldWide , 748572906 , 398602-6555 , http://hjkbefexdbrlvp.com.au , 247 , Green Second Street , 6867 , Sunshine Coast , TAS , C
+ 6 , Upnipommower International , 131293998 , 477-732-0140 , http://aejewqroqvkatqk.com.au , 814 , Oak Drive , 2164 , Busselton , NT , T
+ 7 , Grocadollan International , 369578957 , 740031-2445 , http://neakizbqfidyhxw.com.au , 981 , East Nobel Drive , 2463 , Port Lincoln , TAS , C
+ 8 , Winglibplazz International Corp. , 320328002 , 026-417-0740 , http://nfvnmrrwkjudenr.com.au , 254 , Rocky Cowley Freeway , 4890 , Cessnock , SA , C
+ 9 , Surwerpower WorldWide Group , 509931675 , 4512570658 , http://hkjqrehnqhopix.com.nz , 248 , First Blvd. , 3365 , Mount Isa , SA , T
+ 10 , Qwihupor Direct Group , 972211482 , 387-859-5665 , http://rcemqanjoyq.com.au , 309 , West Fabien Parkway , 6302 , Wollongong , SA , T
+
View
10,001 data/Business_TaxRecord.csv
10,001 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
BIN data/Employee.csv
Binary file not shown.
View
BIN data/FinancialYear.csv
Binary file not shown.
View
BIN data/Individual.csv
Binary file not shown.
View
BIN data/Individual_Relation.csv
Binary file not shown.
View
BIN data/Individual_Stock.csv
Binary file not shown.
View
BIN data/Individual_TaxRecord.csv
Binary file not shown.
View
BIN data/Partner.csv
Binary file not shown.
View
BIN data/Referral.csv
Binary file not shown.
View
BIN data/Relation.csv
Binary file not shown.
View
BIN data/Stock.csv
Binary file not shown.
View
105 models/business.js
@@ -0,0 +1,105 @@
+// Initialization
+
+var csv = require('csv');
+var neo4j = require('neo4j');
+var db = new neo4j.GraphDatabase(process.env.NEO4J_URL || 'http://localhost:7474');
+
+// Constants:
+
+var INDEX_NAME = 'nodes';
+var INDEX_KEY = 'type';
+var INDEX_VAL = 'business';
+
+// Private "constructors"
+
+var Business = module.exports = function Business(_node) {
+ this._node = _node;
+};
+
+// Pass-through Business properties:
+
+function proxyProperty(prop, isData) {
+ Object.defineProperty(Business.prototype, prop, {
+ get: function () {
+ if (isData) {
+ return this._node.data[prop];
+ } else {
+ return this._node[prop];
+ }
+ },
+ set: function (value) {
+ if (isData) {
+ this._node.data[prop] = value;
+ } else {
+ this._node[prop] = value;
+ }
+ }
+ });
+}
+
+proxyProperty('Id');
+proxyProperty('Name');
+proxyProperty('ACN');
+proxyProperty('Phone');
+proxyProperty('Website');
+proxyProperty('AddressLine1');
+proxyProperty('AddressLine2');
+proxyProperty('Postcode');
+proxyProperty('City');
+proxyProperty('State');
+proxyProperty('Type');
+
+// Private functions
+
+function loadBusiness(data, index) {
+ var business = data;
+ Business.create(business, handleCreated);
+}
+
+function handleSuccess(count) {
+ console.log('Number of businesses created: ' + count);
+}
+
+function handleLoadError(error) {
+ console.log(error.message);
+}
+
+function handleCreated(error, data) {
+ if (error) console.log(error.message);
+ console.log('Created: ' + data);
+}
+
+// Public functions
+
+Business.loadFromCSV = function() {
+ csv().fromPath('data/BusinessTest.csv', { columns: true, trim: true })
+ .on('data', loadBusiness)
+ .on('end', handleSuccess)
+ .on('error', handleLoadError);
+};
+
+Business.create = function (data, callback) {
+ var node = db.createNode(data);
+ var business = new Business(node);
+
+ node.save(function (err) {
+ if (err) return callback(err);
+
+ node.index(INDEX_NAME, INDEX_KEY, INDEX_VAL, function (err) {
+ if (err) return callback(err);
+ callback(null, business);
+ });
+ });
+};
+
+Business.getAll = function (callback) {
+ db.getIndexedNodes(INDEX_NAME, INDEX_KEY, INDEX_VAL, function (err, nodes) {
+ if (err) return callback(null, []);
+
+ var businesses = nodes.map(function (node) {
+ return new Business(node);
+ });
+
+ callback(null, businesses);
+ });
+};
View
57 readme.md
@@ -1,57 +0,0 @@
-# Node-Neo4j Template
-
-This is a template app showing the use of [Neo4j][] from Node.js. It uses the
-[node-neo4j][] library, available on npm as `neo4j`.
-
-The app is a simple social network manager: it lets you add and remove users
-and "follows" relationships between them.
-
-This app supports deploying to Heroku, and a demo is in fact running live at
-[http://nodeneo4jtemplate.herokuapp.com/](http://nodeneo4jtemplate.herokuapp.com/).
-
-So try it out, browse the code, and fork this project to get a head start on
-creating your own Node-Neo4j app. Enjoy!
-
-
-## Installation
-
-```bash
-# Install the required dependencies
-npm install
-
-# Install a local Neo4j instance
-curl "http://dist.neo4j.org/neo4j-community-1.6-unix.tar.gz" --O "db-unix.tar.gz"
-tar -zxvf db-unix.tar.gz 2> /dev/null
-rm db-unix.tar.gz
-```
-
-
-## Usage
-
-```bash
-# Start the local Neo4j instance
-neo4j-community-1.6/bin/neo4j start
-
-# Run the app!
-npm start
-```
-
-The app will now be accessible at [http://localhost:3000/](http://localhost:3000/).
-
-The UI is admittedly quite crappy, but hopefully it shows the functionality.
-(Anyway, this project is really about the code! =P)
-
-
-## Miscellany
-
-- MIT license.
-- Questions/comments/etc. are welcome.
-- As an exercise, I built this without using [CoffeeScript][coffeescript] or
- [Streamline][streamline]. What a gigantic pain! Never again. =P
-
-
-[Neo4j]: http://www.neo4j.org/
-[node-neo4j]: https://github.com/thingdom/node-neo4j
-
-[coffeescript]: http://www.coffeescript.org/
-[streamline]: https://github.com/Sage/streamlinejs
View
11 routes/businesses.js
@@ -0,0 +1,11 @@
+var Business = require('../models/business');
+
+// GET /businesses
+exports.list = function (req, res, next) {
+ Business.getAll(function (err, businesses) {
+ if (err) return next(err);
+ res.render('businesses', {
+ businesses: businesses
+ });
+ });
+};
View
6 routes/datainit.js
@@ -0,0 +1,6 @@
+var Business = require('../models/business');
+
+// GET /datainit/business
+exports.loadBusiness = function (req, res, next) {
+ Business.loadFromCSV();
+};
View
2 routes/index.js
@@ -1,3 +1,5 @@
// convenience wrapper around all other files:
exports.site = require('./site');
exports.users = require('./users');
+exports.datainit = require('./datainit');
+exports.businesses = require('./businesses');
View
15 views/businesses.jade
@@ -0,0 +1,15 @@
+h1 Businesses
+
+if businesses.length
+ p Here are the current businesses:
+ ul.businesses
+ for business in businesses
+ li.business
+ #{JSON.stringify(business)}
+else
+ p There are no businesses currently.
+
+form(action='', method='POST')
+ p Create a new business:
+ input(type='text', name='name', placeholder='Name', required)
+ input(type='submit', value='Create')
View
1 views/datainit.jade
@@ -0,0 +1 @@
+h1= Load CSV data
View
2 views/users.jade
@@ -5,7 +5,7 @@ if users.length
ul.users
for user in users
li.user
- a(href='/users/#{user.id}') #{user.name}
+ a(href='/users/#{user.id}') #{user.name} #{JSON.stringify(user)}
else
p There are no users currently.

0 comments on commit 8a88f45

Please sign in to comment.