Fork me on GitHub

mysql orm

看到老雷在cnode社区发的node面试题,很好的引导。

实现一个简单 MySQL ORM 模块

主要参考并学习了azathothes这位jser的思路,同时用了es6的class类。

es5实现方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import mysql from 'mysql';
function Orm(options) {
this.pool = mysql.createPool(options);
console.log('initialize successful.database connected...');
}
function Query(table, pool) {
this.table = table;
this.pool = pool;
}
Orm.prototype.table = function (table) {
return new Query(table, this.pool);
};
Query.prototype.find = function (opt) {
if (!this.table) {
throw new Error('can not query data without a table set up.invoke table function first!');
}
this.sql = `select * from ${this.table} where 1=1 `;
this.opt = opt;
return this;
};
Query.prototype.update = function (opt, upt) {
if (!this.table) {
throw new Error('can not query data without a table set up.invoke table function first!');
}
this.sql = `update ${this.table} set `;
this.upt = upt;
this.opt = opt;
return this;
};
Query.prototype.insert = function (ipt) {
if (!this.table) {
throw new Error('can not query data without a table set up.invoke table function first!');
}
this.sql = `insert into ${this.table} set `;
this.ipt = ipt;
return this;
};
Query.prototype.limit = function (lpt) {
if (!this.table) {
throw new Error('can not query data without a table set up.invoke table function first!');
}
this.lpt = lpt;
return this;
};
Query.prototype.skip = function (spt) {
if (!this.table) {
throw new Error('can not query data without a table set up.invoke table function first!');
}
this.spt = spt;
return this;
};
Query.prototype.exec = function () {
if (!this.table || !this.sql) {
throw new Error('can not query data without a table set up.invoke table function first!');
}
return new Promise((res, rej) => {
this.pool.getConnection((err, conn) => {
if (err) {
rej(err);
} else {
if (this.ipt) {
const ip = Object.keys(this.ipt);
for (const key of ip) {
this.sql += `${key}=${this.ipt[key]}, `;
}
if (this.sql.endsWith(',')) {
this.sql = this.sql.substring(this.sql, this.sql.length);
}
}
if (this.upt) {
const up = Object.keys(this.upt);
for (const key of up) {
this.sql += `${key}='${this.upt[key]}' `;
this.sql += ',';
}
if (this.sql.endsWith(',')) {
this.sql = this.sql.substring(this.sql, this.sql.length - 1);
}
this.sql += ' where 1=1 ';
}
if (this.opt) {
const op = Object.keys(this.opt);
for (const key of op) {
this.sql += `and ${key}='${this.opt[key]}' `;
}
}
if (this.lpt) {
this.sql += `limit ${this.lpt}`;
}
if (this.spt) {
this.sql += `,${this.spt}`;
}
console.log(this.sql);
conn.query(this.sql, (error, result) => {
if (error) {
rej(error);
}
res(result);
});
conn.release();
}
});
});
};
module.exports = Orm;

es6实现方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import mysql from 'mysql';
function Rdm(options) {
this.pool = mysql.createPool(options);
console.log('initialize successful.database connected...');
}
Orm.prototype.table = function (table) {
return new Query(table, this.pool);
};
class Query {
constructor(table, pool) {
this.table = table;
this.pool = pool;
}
find(opt) {
if (!this.table) {
throw new Error('can not query data without a table set up.invoke table function first!');
}
this.sql = `select * from ${this.table} where 1=1 `;
this.opt = opt;
return this;
}
exec() {
if (!this.table || !this.sql) {
throw new Error('can not query data without a table set up.invoke table function first!');
}
return new Promise((res, rej) => {
this.pool.getConnection((err, conn) => {
if (err) {
rej(err);
} else {
if (this.ipt) {
const ip = Object.keys(this.ipt);
for (const key of ip) {
this.sql += `${key}=${this.ipt[key]}, `;
}
if (this.sql.endsWith(',')) {
this.sql = this.sql.substring(this.sql, this.sql.length);
}
}
if (this.upt) {
const up = Object.keys(this.upt);
for (const key of up) {
this.sql += `${key}='${this.upt[key]}' `;
this.sql += ',';
}
if (this.sql.endsWith(',')) {
this.sql = this.sql.substring(this.sql, this.sql.length - 1);
}
this.sql += ' where 1=1 ';
}
if (this.opt) {
const op = Object.keys(this.opt);
for (const key of op) {
this.sql += `and ${key}='${this.opt[key]}' `;
}
}
if (this.lpt) {
this.sql += `limit ${this.lpt}`;
}
if (this.spt) {
this.sql += `,${this.spt}`;
}
console.log(this.sql);
conn.query(this.sql, (error, result) => {
if (error) {
rej(error);
}
res(result);
});
conn.release();
}
});
});
}
...
...
}
module.exports = Orm;
1
2
3
4
5
6
7
8
(async() => {
const cn = new Orm({
host: '127.0.0.1',
user: 'root',
password: 'root'
});
console.log(await cn.table('test').find({name: 'aa'}).exec());
})();
-------------本文结束感谢您的阅读-------------
如果您觉得受益了,欢迎打赏鼓励。