Skip to content

Commit c9ef000

Browse files
committed
mysql script lacking the first creation of db&tables, updated packages, not working with new express version?
1 parent 2baffe9 commit c9ef000

File tree

5 files changed

+138
-74
lines changed

5 files changed

+138
-74
lines changed

README.md

+24-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,30 @@ nodeblog
44

55
## About
66
[Obtvse] (https://github.com/natew/obtvse) is developed by [@natew](https://github.com/natew) with Ruby&Rails.
7-
[@gorekee](https://github.com/gorekee) has already [started](https://github.com/gorekee/obtvse-node.js) to convert it to nodejs, and I
8-
asked his permission to continue his work
7+
I am trying to convert it to nodejs.
98

109
## Configuration
10+
Configuration file is `config.json`. It looks as following:
11+
12+
{
13+
"user": "mysql_username",
14+
"password": "mysql_password",
15+
"DB": "mysql_db_name",
16+
"TABLE_POST": "posts",
17+
"TABLE_PUBLISH": "publish",
18+
"TABLE_COMMENT": "comment",
19+
"PORT": "3000",
20+
"env": "dev",
21+
"postPerPage": 10,
22+
"login": {
23+
"user": "obtvse_admin_username",
24+
"password": "obvtse_admin_password"
25+
}
26+
}
27+
28+
`user` & `password` are your database username & password. `DB` is the database that you will store your blog posts. Web site content
29+
is stored in `TABLE_*`. `PORT` is the port your node server is running. `postPerPage` is modifiable as well. Finally set your admin login and password.
30+
1131
Set MySql database and login information into the config.json under user/password.
1232
Set Admin module login information under login.user/login.password.
1333
First run `npm install` for dependencies, then run `node app.js` in order to start the server.
@@ -16,3 +36,5 @@ First run `npm install` for dependencies, then run `node app.js` in order to sta
1636
![Admin](http://i.imgur.com/hfnm9.png)
1737
![Draft](http://i.imgur.com/x4lXL.png)
1838
![Live](http://i.imgur.com/wbVJN.png)
39+
40+
Note: Not working with the bew updates of Jade and Express!

app.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ var app = module.exports = h5bp.server(express, {
1111
'root': './public'
1212
});
1313
app.configure(function() {
14-
app.set('views', __dirname + '/views');
15-
app.set('view engine', 'jade');
14+
app.set('views', __dirname + '/views'); // where view files are
15+
app.set('view engine', 'jade'); // jade
1616
app.use(express.bodyParser());
1717
app.use(express.methodOverride());
18-
app.use(express.cookieParser());
18+
app.use(express.cookieParser()); // cookie and session are for session tracking
1919
app.use(express.session({secret: '#Thij`YVv2OEz8UcqX'}));
2020
app.use(app.router);
2121
app.use(express.static(__dirname + '/public'));

lib/models/mysql.js

+92-58
Original file line numberDiff line numberDiff line change
@@ -10,117 +10,151 @@ var options = require('./options'),
1010
user: options.storageConfig.user,
1111
password: options.storageConfig.password
1212
},
13-
client = mysql.createConnection(loginData);
13+
client = mysql.createConnection(loginData);
1414

15-
client.connect();
16-
15+
client.connect();
16+
17+
// create the database if it is not there
18+
client.query('CREATE DATABASE ' + DB, function(err) {
19+
if (err && err.number !== mysql.ERROR_DB_CREATE_EXIST) {
20+
return;
21+
}
22+
});
23+
24+
// create posts table if it is not there
25+
client.query(
26+
'CREATE TABLE ' + TABLE_POST +
27+
'(id INT(11) AUTO_INCREMENT, ' +
28+
'title_html VARCHAR(255), ' +
29+
'title_markup VARCHAR(255), ' +
30+
'content_html TEXT, ' +
31+
'content_markup TEXT, ' +
32+
'created VARCHAR(255), ' +
33+
'PRIMARY KEY (id))', function(err) {
34+
if (err && err.number !== mysql.ER_TABLE_EXISTS_ERROR) {
35+
return;
36+
}
37+
});
38+
39+
// create publish table if it is not there
40+
client.query(
41+
'CREATE TABLE ' + TABLE_PUBLISH +
42+
'(id INT(11) AUTO_INCREMENT, ' +
43+
'post_id INT(11), ' +
44+
'PRIMARY KEY (id))', function(err) {
45+
if (err && err.number !== mysql.ER_TABLE_EXISTS_ERROR) {
46+
return;
47+
}
48+
});
49+
1750
// set db
1851
client.query('USE ' + DB);
1952

2053
// constructor
21-
this.MySql = function() {};
54+
this.MySql = function() {
55+
};
2256

2357
// add a new item
2458
this.MySql.prototype.add = function(obj, next) {
2559
client.query(
26-
'INSERT INTO ' + TABLE_POST + ' ' +
27-
'SET title_html = ?, title_markup = ?, content_markup = ?, content_html = ?, created = ?',
28-
[obj.titleHtml, obj.titleMarkup, obj.contentMarkup, obj.contentHtml, obj.created],
29-
next
30-
);
60+
'INSERT INTO ' + TABLE_POST + ' ' +
61+
'SET title_html = ?, title_markup = ?, content_markup = ?, content_html = ?, created = ?',
62+
[obj.titleHtml, obj.titleMarkup, obj.contentMarkup, obj.contentHtml, obj.created],
63+
next
64+
);
3165
};
3266

3367
// find all items
3468
this.MySql.prototype.getAll = function(next) {
3569
client.query(
36-
'SELECT * FROM ' + TABLE_POST + ' ' +
37-
'ORDER BY id DESC',
38-
function(err, result) {
39-
if(err)
40-
console.t.log(err);
41-
next(result);
42-
}
70+
'SELECT * FROM ' + TABLE_POST + ' ' +
71+
'ORDER BY id DESC',
72+
function(err, result) {
73+
if (err)
74+
console.t.log(err);
75+
next(result);
76+
}
4377
);
4478
};
4579

4680
// get all publish post ids
4781
this.MySql.prototype.getPubPostIds = function(next) {
4882
client.query(
49-
'SELECT * FROM ' + TABLE_PUBLISH + ' ',
50-
function(err, result) {
51-
if(err)
52-
return next(err);
53-
next(result);
54-
}
83+
'SELECT * FROM ' + TABLE_PUBLISH + ' ',
84+
function(err, result) {
85+
if (err)
86+
return next(err);
87+
next(result);
88+
}
5589
);
5690
};
5791

5892
//update post
5993
this.MySql.prototype.update = function(obj, next) {
6094
client.query(
61-
'UPDATE ' + TABLE_POST + ' ' +
62-
'SET title_html = ?, title_markup = ?, content_markup = ?, content_html = ?, created = ? ' +
63-
'WHERE id = ?',
64-
[obj.titleHtml, obj.titleMarkup, obj.contentMarkup, obj.contentHtml, obj.created, obj.id],
65-
next
66-
);
95+
'UPDATE ' + TABLE_POST + ' ' +
96+
'SET title_html = ?, title_markup = ?, content_markup = ?, content_html = ?, created = ? ' +
97+
'WHERE id = ?',
98+
[obj.titleHtml, obj.titleMarkup, obj.contentMarkup, obj.contentHtml, obj.created, obj.id],
99+
next
100+
);
67101
};
68102

69103
// remove an item
70104
this.MySql.prototype.remove = function(id, next) {
71105
client.query(
72-
'DELETE FROM ' + TABLE_POST + ' ' +
73-
'WHERE id = ?',
74-
[id],
75-
function(err) {
76-
if(err)
77-
throw err;
78-
next();
79-
}
106+
'DELETE FROM ' + TABLE_POST + ' ' +
107+
'WHERE id = ?',
108+
[id],
109+
function(err) {
110+
if (err)
111+
throw err;
112+
next();
113+
}
80114
);
81115
};
82116

83117
// search for a post
84118
this.MySql.prototype.lookup = function(id, next) {
85119
client.query(
86-
'SELECT post.*, pub.post_id ' +
87-
'FROM ' + TABLE_POST + ' post ' +
88-
'LEFT OUTER JOIN ' + TABLE_PUBLISH + ' pub ON post.id = pub.post_id ' +
89-
'WHERE post.id = ?',
90-
[id],
91-
function(err, obj) {
92-
next(err, obj[0]);
93-
}
120+
'SELECT post.*, pub.post_id ' +
121+
'FROM ' + TABLE_POST + ' post ' +
122+
'LEFT OUTER JOIN ' + TABLE_PUBLISH + ' pub ON post.id = pub.post_id ' +
123+
'WHERE post.id = ?',
124+
[id],
125+
function(err, obj) {
126+
next(err, obj[0]);
127+
}
94128
);
95129
};
96130

97131
// get last post by id
98132
this.MySql.prototype.getLast = function(next) {
99133
client.query(
100-
'SELECT * FROM ' + TABLE_POST + ' ' +
101-
'ORDER BY id DESC ' +
102-
'LIMIT 1',
103-
function(err, obj) {
104-
next(err, obj[0]);
105-
}
134+
'SELECT * FROM ' + TABLE_POST + ' ' +
135+
'ORDER BY id DESC ' +
136+
'LIMIT 1',
137+
function(err, obj) {
138+
next(err, obj[0]);
139+
}
106140
);
107141
};
108142

109143
// insert post id into publish table
110144
this.MySql.prototype.publishPost = function(id) {
111145
this.unpublishPost(id);
112146
client.query(
113-
'INSERT INTO ' + TABLE_PUBLISH + ' ' +
114-
'SET post_id = ?',
115-
[id]
116-
);
147+
'INSERT INTO ' + TABLE_PUBLISH + ' ' +
148+
'SET post_id = ?',
149+
[id]
150+
);
117151
};
118152

119153
// remove post id from publish table
120154
this.MySql.prototype.unpublishPost = function(id) {
121155
client.query(
122-
'DELETE FROM ' + TABLE_PUBLISH + ' ' +
123-
'WHERE post_id = ?',
124-
[id]
125-
);
156+
'DELETE FROM ' + TABLE_PUBLISH + ' ' +
157+
'WHERE post_id = ?',
158+
[id]
159+
);
126160
};

lib/models/postgre.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
7+

package.json

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
2-
"name": "obtvse-node.js",
3-
"version": "0.0.1",
4-
"dependencies": {
5-
"express": "2.5.8",
6-
"jade": ">= 0.0.1",
7-
"marked": ">= 0.2.3",
8-
"redis": ">= 0.7.1",
9-
"callsite": ">= 0.0.1",
10-
"mysql": ">= 2.0.0-rc1"
11-
}
12-
}
2+
"name": "obtvse-node.js",
3+
"version": "0.0.1",
4+
"dependencies": {
5+
"express": "~2.5.8",
6+
"jade": "~0.35.0",
7+
"marked": "~0.2.10",
8+
"redis": "~0.10.0",
9+
"callsite": "~1.0.0",
10+
"mysql": "~2.0.0-rc2",
11+
"pg": "~2.9.0"
12+
}
13+
}

0 commit comments

Comments
 (0)