Skip to content

Commit 9fd5d5b

Browse files
committed
Merge pull request DefinitelyTyped#5755 from nodets/master
Node-mysql-wrapper update to 2.3.6
2 parents 32947c3 + cdffc42 commit 9fd5d5b

File tree

2 files changed

+330
-113
lines changed

2 files changed

+330
-113
lines changed

node-mysql-wrapper/node-mysql-wrapper-tests.ts

Lines changed: 137 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
/// <reference path="./node-mysql-wrapper.d.ts" />
2+
/// <reference path="../node/node.d.ts" />
3+
/// <reference path="../bluebird/bluebird.d.ts" />
4+
25
var express = require('express');
36
var app = express();
47
var server = require('http').createServer(app);
@@ -9,42 +12,37 @@ class User { //or interface
912
userId: number;
1013
username: string;
1114
mail: string;
15+
password:string;
1216
comments: Comment[];
17+
myComments: Comment[];
18+
info: UserInfo;
1319
}
1420

1521
interface Comment {
1622
commentId: number;
1723
content: string;
24+
likes: CommentLike[];
1825

1926
}
27+
interface CommentLike {
28+
commentLikeId: number;
29+
userId: number;
30+
commentId: number;
31+
}
32+
33+
interface UserInfo {
34+
userInfoId: number;
35+
userId: number;
36+
hometown: string;
37+
}
2038

2139
db.ready(() => {
2240

2341

2442
var usersDb = db.table<User>("users");
25-
usersDb.rules.orderBy("userId", true);
26-
27-
28-
usersDb.findAll().then(users=> {
29-
users.forEach(user=> {
30-
console.log(user.userId);
31-
});
32-
});
33-
34-
35-
//define new table rules: usersDb.rules.clear().orderBy....limit....groupBy...
36-
37-
//define rules but keep unchanged (table's rules) in find method:
38-
usersDb.find({ yearsOld: 22 }, (_users) => {
3943

40-
console.log("-------------------------------------------------------");
41-
_users.forEach(_user=> {
42-
console.log(_user.userId + " " + _user.username + " found with limit 3 but this doesnt...");
43-
44-
});
44+
//or var usersDb = db.table("users"); if you don't want intel auto complete from your ide/editor
4545

46-
}).limit(3).execute();
47-
4846
usersDb.findById(16, (_user) => {
4947

5048
console.log("TEST1: \n");
@@ -56,20 +54,27 @@ db.ready(() => {
5654
}, (err) => { console.log("ERROR ON FETCHING FINDBY ID: " + err) });
5755
*/
5856

59-
usersDb.find({ userId: 18, comments: { userId: '=' } }, _users=> {
60-
61-
var _user = _users[0];
62-
63-
console.log("TEST2: \n");
64-
console.log(_user.username + " with ");
65-
console.log(_user.comments.length + " comments ");
66-
_user.comments.forEach(_comment=> {
67-
console.log("--------------\n" + _comment.content);
57+
usersDb.findSingle(
58+
{
59+
userId: 18,
60+
myComments: {
61+
userId: '=',
62+
tableRules: { //NEW: SET rules to joined tables too!
63+
table: "comments", //NEW SET table name inside any property u want!
64+
limit: 50,
65+
orderByDesc: "commentId" //give me the first 50 comments ordered by -commentId (DESC) from table 'comments' and put them at 'myComments' property inside the result object.
66+
}
67+
}
68+
}).then(_user=> { // to get this promise use : .promise()
69+
console.log("\n-------------TEST 2 ------------\n");
70+
console.log(_user.username + " with ");
71+
console.log(_user.myComments.length + " comments ");
72+
_user.myComments.forEach(_comment=> {
73+
console.log("--------------\n" + _comment.content);
74+
});
6875
});
6976

70-
}).execute();
71-
72-
usersDb.safeRemove(5620, answer=> {
77+
usersDb.remove(5620, answer=> {
7378
console.log("TEST 3: \n");
7479
console.log(answer.affectedRows + ' (1) has removed from table: ' + answer.table);
7580

@@ -86,8 +91,107 @@ db.ready(() => {
8691
});
8792

8893

94+
95+
usersDb.find(
96+
{
97+
yearsOld: 22,
98+
comments: {
99+
userId: "=",
100+
tableRules: {
101+
limit: 2
102+
}
103+
}
104+
105+
}, (_users) => {
106+
107+
console.log("---------------TEST 6----------------------------------------");
108+
_users.forEach(_user=> {
109+
console.log(_user.userId + " " + _user.username + " found with " + _user.comments.length + " comments");
110+
111+
});
112+
113+
});
114+
//if no rules setted to find method it's uses the table's rules ( if exists)
115+
116+
117+
118+
let _criteriaFromBuilder = usersDb.criteria
119+
.except("password") // or .exclude(...columns). the only column you cannot except/exclude is the primary key (because it is used at where clause), be careful.
120+
.where("userId", 24)
121+
.joinAs("info", "userInfos", "userId")
122+
.at("info")
123+
.limit(1) //because we make it limit 1 it will return this result as object not as array.
124+
.parent()
125+
.joinAs("myComments", "comments", "userId")
126+
.at("myComments").limit(2)
127+
.joinAs("likes", "commentLikes", "commentId")
128+
.original().orderBy("userId", true).build();
129+
130+
/* console.dir(_criteriaFromBuilder);
131+
prints this object: ( of course you can create your own in order to pass it on .find table methods )
132+
{
133+
userId:23,
134+
135+
myComments:{
136+
userId: '=',
137+
138+
tableRules:{
139+
table: 'comments',
140+
limit:2
141+
142+
},
143+
144+
likes:{
145+
commentId: '=',
146+
147+
tableRules:{
148+
table: 'commentLikes'
149+
}
150+
151+
}
152+
},
153+
154+
tableRules:{
155+
orderByDesc: 'userId',
156+
except: ['password']
157+
}
158+
159+
}
160+
161+
162+
*/
163+
164+
165+
166+
usersDb.find(_criteriaFromBuilder).then(_users=> {
167+
console.log("\n----------------\nTEST ADVANCED 1\n-------------------\n ");
168+
_users.forEach(_user=> {
169+
console.log(_user.userId + " " + _user.username);
170+
171+
if (_user.info !== undefined) {
172+
console.log(' from ' + _user.info.hometown);
173+
//console.dir(_user.userInfos);
174+
}
175+
176+
if (_user.myComments !== undefined) {
177+
_user.myComments.forEach(_comment=> {
178+
console.log(_comment.commentId + " " + _comment.content);
179+
180+
if (_comment.likes !== undefined) {
181+
console.log(' with ' + _comment.likes.length + ' likes!');
182+
}
183+
});
184+
}
185+
});
186+
187+
});
188+
189+
89190
});
90191

192+
server.on('uncaughtException', function(err:any) {
193+
console.log(err);
194+
})
91195

92196
var httpPort = 1193;//config.get('Server.port') || 1193;
93197
server.listen(httpPort, function() {

0 commit comments

Comments
 (0)