@zenorm/generate 0.0.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3 -0
- package/README.md +2 -250
- package/dist/bin/zenorm-generate.js +6 -6
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -1,251 +1,3 @@
|
|
|
1
|
-
# ZenORM
|
|
1
|
+
# ZenORM Generate
|
|
2
2
|
|
|
3
|
-
[
|
|
4
|
-
|
|
5
|
-
## 安装
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install zenorm mysql-easy-query
|
|
9
|
-
npm install @zenorm/mysql --save-dev
|
|
10
|
-
```
|
|
11
|
-
|
|
12
|
-
## 配置
|
|
13
|
-
|
|
14
|
-
在 `package.json` 的 `scripts` 中增加如下代码,用于执行 `dbgen` 命令
|
|
15
|
-
|
|
16
|
-
```json title="package.json"
|
|
17
|
-
{
|
|
18
|
-
"scripts": {
|
|
19
|
-
"dbgen": "zenorm gen .dbgen.json"
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
创建文件 `.dbgen.json` 用于生成数据库结构代码时连接到指定数据库
|
|
25
|
-
|
|
26
|
-
*提示:运行时并不使用此配置*
|
|
27
|
-
|
|
28
|
-
```json title=".dbgen.json"
|
|
29
|
-
{
|
|
30
|
-
"host": "localhost",
|
|
31
|
-
"port": 3306,
|
|
32
|
-
"user": "root",
|
|
33
|
-
"password": "",
|
|
34
|
-
"database": "test"
|
|
35
|
-
}
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
## 演示
|
|
39
|
-
|
|
40
|
-
以下数据库结构为演示用,在数据中创建表结构
|
|
41
|
-
|
|
42
|
-
```sql
|
|
43
|
-
CREATE TABLE `user` (
|
|
44
|
-
`id` int(11) NOT NULL AUTO_INCREMENT,
|
|
45
|
-
`name` varchar(255) NOT NULL,
|
|
46
|
-
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
47
|
-
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
48
|
-
PRIMARY KEY (`id`)
|
|
49
|
-
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
|
50
|
-
|
|
51
|
-
CREATE TABLE `profile` (
|
|
52
|
-
`id` int(11) NOT NULL,
|
|
53
|
-
`edu` varchar(255) DEFAULT NULL,
|
|
54
|
-
`work` varchar(255) DEFAULT NULL,
|
|
55
|
-
PRIMARY KEY (`id`)
|
|
56
|
-
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
|
57
|
-
|
|
58
|
-
CREATE TABLE `message` (
|
|
59
|
-
`id` int(11) NOT NULL AUTO_INCREMENT,
|
|
60
|
-
`user_id` int(11) NOT NULL,
|
|
61
|
-
`content` varchar(255) DEFAULT NULL,
|
|
62
|
-
PRIMARY KEY (`id`),
|
|
63
|
-
KEY `fk1` (`user_id`)
|
|
64
|
-
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
运行命令开始生成数据库结构代码
|
|
68
|
-
|
|
69
|
-
```bash
|
|
70
|
-
npm run dbgen
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
### 编辑模型关系
|
|
74
|
-
|
|
75
|
-
编辑生成的模型文件 `src/model/user.ts`
|
|
76
|
-
|
|
77
|
-
```ts
|
|
78
|
-
import { model, createRepositoryQuery, join, many, data } from 'zenorm';
|
|
79
|
-
import { UserTable } from './_tables';
|
|
80
|
-
import { Profile } from './profile';
|
|
81
|
-
import { Message } from './message';
|
|
82
|
-
|
|
83
|
-
@model({
|
|
84
|
-
pk: 'id',
|
|
85
|
-
table: 'user',
|
|
86
|
-
})
|
|
87
|
-
export default class User extends UserTable {
|
|
88
|
-
static query = createRepositoryQuery<User, number>(User);
|
|
89
|
-
|
|
90
|
-
// 添加以下代码
|
|
91
|
-
|
|
92
|
-
// join 描述支持使用文件名,解决互相依赖问题
|
|
93
|
-
@join(__dirname + '/profile', { type: 'OneToMany', asList: false })
|
|
94
|
-
profile?: Profile;
|
|
95
|
-
|
|
96
|
-
@join(Message)
|
|
97
|
-
messages?: Message[];
|
|
98
|
-
|
|
99
|
-
@many(Message)
|
|
100
|
-
messageList?: Message[];
|
|
101
|
-
|
|
102
|
-
@data
|
|
103
|
-
get age() {
|
|
104
|
-
return this.birthday ? (new Date().getFullYear()) - this.birthday.getFullYear() : undefined;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
set age(v) {
|
|
108
|
-
if (v === undefined) throw new Error('age is undefined');
|
|
109
|
-
const date = new Date();
|
|
110
|
-
date.setFullYear(date.getFullYear() - v, 1, 1);
|
|
111
|
-
this.birthday = date;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
编辑生成的模型文件 `src/model/profile.ts`
|
|
117
|
-
|
|
118
|
-
```ts
|
|
119
|
-
import { model, createRepositoryQuery, join } from 'zenorm';
|
|
120
|
-
import { ProfileTable } from './_tables';
|
|
121
|
-
import User from './user';
|
|
122
|
-
|
|
123
|
-
@model({
|
|
124
|
-
pk: 'id',
|
|
125
|
-
table: 'profile',
|
|
126
|
-
})
|
|
127
|
-
export default class Profile extends ProfileTable {
|
|
128
|
-
static query = createRepositoryQuery<Profile, number>(Profile);
|
|
129
|
-
|
|
130
|
-
// 添加以下代码
|
|
131
|
-
|
|
132
|
-
@join(User)
|
|
133
|
-
user?: User;
|
|
134
|
-
}
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
### 初始化数据库访问层
|
|
138
|
-
|
|
139
|
-
创建代码 `src/db.ts`
|
|
140
|
-
|
|
141
|
-
```ts title="src/test.ts"
|
|
142
|
-
import { createPoolCompatible } from 'mysql-easy-query';
|
|
143
|
-
import { Repositories } from './model';
|
|
144
|
-
|
|
145
|
-
// 创建数据库连接池
|
|
146
|
-
export const pool = createPoolCompatible({
|
|
147
|
-
pools: {
|
|
148
|
-
// 主库
|
|
149
|
-
MASTER: {
|
|
150
|
-
host: '10.0.0.1',
|
|
151
|
-
user: 'root',
|
|
152
|
-
database: 'test',
|
|
153
|
-
password: '',
|
|
154
|
-
},
|
|
155
|
-
// 如果需要读写分离,创建命令规则为 SLAVE* 的只读配置
|
|
156
|
-
/*
|
|
157
|
-
SLAVE1: {
|
|
158
|
-
host: '10.0.0.2'
|
|
159
|
-
},
|
|
160
|
-
*/
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
// 创建仓库访问层
|
|
165
|
-
export const repositories = new Repositories(pool);
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
### 开始使用
|
|
169
|
-
|
|
170
|
-
#### 常规使用
|
|
171
|
-
|
|
172
|
-
```ts
|
|
173
|
-
import { repositories } from './db';
|
|
174
|
-
|
|
175
|
-
const {
|
|
176
|
-
UserRepository,
|
|
177
|
-
MessageRepository,
|
|
178
|
-
} = repositories;
|
|
179
|
-
|
|
180
|
-
async function test() {
|
|
181
|
-
// create
|
|
182
|
-
const id = await UserRepository.create({ name: 'yf' });
|
|
183
|
-
console.log(id); // 1
|
|
184
|
-
|
|
185
|
-
// get and update
|
|
186
|
-
const user = await UserRepository.findByPk(id);
|
|
187
|
-
user.name = 'yefei';
|
|
188
|
-
user.age = 20;
|
|
189
|
-
await UserRepository.save(user);
|
|
190
|
-
|
|
191
|
-
// find all
|
|
192
|
-
const users = await UserRepository.find().all();
|
|
193
|
-
|
|
194
|
-
// find limit
|
|
195
|
-
const users = await UserRepository.find().limit(10).all();
|
|
196
|
-
|
|
197
|
-
// find by where
|
|
198
|
-
const users = await UserRepository.find({ name: { $like: `%y%` } }).all();
|
|
199
|
-
|
|
200
|
-
// get all count
|
|
201
|
-
const count = await UserRepository.count();
|
|
202
|
-
|
|
203
|
-
// page
|
|
204
|
-
const page = await UserRepository.page();
|
|
205
|
-
|
|
206
|
-
// exists
|
|
207
|
-
const exists = await UserRepository.exists({ id: 1 });
|
|
208
|
-
// or
|
|
209
|
-
const exists = await UserRepository.find({ name: 'yf' }).exists();
|
|
210
|
-
|
|
211
|
-
// update
|
|
212
|
-
const updatedCount = await UserRepository.find({ id: 1 }).update({ name: 'yf', age: 11 });
|
|
213
|
-
|
|
214
|
-
// delete
|
|
215
|
-
const user = await UserRepository.findByPk(1);
|
|
216
|
-
const deletedCount = await UserRepository.delete(user);
|
|
217
|
-
|
|
218
|
-
await UserRepository.find({ name: 'aaa' }).delete();
|
|
219
|
-
|
|
220
|
-
// join 预定义
|
|
221
|
-
const user = await UserRepository.find().join("messages").get();
|
|
222
|
-
|
|
223
|
-
// join 模型(未定义的)
|
|
224
|
-
const user = await MessageRepository.find().join(User).all();
|
|
225
|
-
|
|
226
|
-
// many 独立查询功能
|
|
227
|
-
const userList = await UserRepository.find().many("messageList").all();
|
|
228
|
-
|
|
229
|
-
// 指定使用主从库
|
|
230
|
-
await UserRepository.find().of('MASTER').all();
|
|
231
|
-
await UserRepository.find().of('SLAVE*').all();
|
|
232
|
-
}
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
#### 事物支持
|
|
236
|
-
|
|
237
|
-
```ts
|
|
238
|
-
import { pool } from './db';
|
|
239
|
-
import { User, Message } from './model';
|
|
240
|
-
|
|
241
|
-
async function test() {
|
|
242
|
-
await pool.transaction(async tx => {
|
|
243
|
-
await User.query(tx).find().update({ some: 'data' });
|
|
244
|
-
await Message.query(tx).find().update({ some: 'data' });
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
```
|
|
248
|
-
|
|
249
|
-
## Related projects
|
|
250
|
-
[mysql-easy-query](https://www.npmjs.com/package/mysql-easy-query)
|
|
251
|
-
[sql-easy-builder](https://www.npmjs.com/package/sql-easy-builder)
|
|
3
|
+
[ZenORM](https://www.npmjs.com/package/zenorm) 表结构代码生成工具
|
|
@@ -3,15 +3,15 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
const path = require("path");
|
|
5
5
|
const generate_1 = require("../generate");
|
|
6
|
-
function getConfig() {
|
|
7
|
-
const configFile = path.join(process.cwd(),
|
|
6
|
+
function getConfig(filename) {
|
|
7
|
+
const configFile = path.join(process.cwd(), filename);
|
|
8
8
|
const config = require(configFile);
|
|
9
9
|
return Object.assign({
|
|
10
|
-
backend: '@zenorm/mysql',
|
|
10
|
+
backend: '@zenorm/generate-mysql',
|
|
11
11
|
}, config);
|
|
12
12
|
}
|
|
13
|
-
async function main() {
|
|
14
|
-
const config = await getConfig();
|
|
13
|
+
async function main(configFilename) {
|
|
14
|
+
const config = await getConfig(configFilename);
|
|
15
15
|
const call = require(config.backend).default;
|
|
16
16
|
await (0, generate_1.generate)(call()(config), config);
|
|
17
17
|
}
|
|
@@ -20,7 +20,7 @@ if (!process.argv[2]) {
|
|
|
20
20
|
process.exit(1);
|
|
21
21
|
}
|
|
22
22
|
else {
|
|
23
|
-
main().then(() => process.exit(), e => {
|
|
23
|
+
main(process.argv[2]).then(() => process.exit(), e => {
|
|
24
24
|
console.error(e);
|
|
25
25
|
process.exit(1);
|
|
26
26
|
});
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenorm/generate",
|
|
3
3
|
"description": "Easy ORM, easy query. easy typing! Auto generate typescript declaration.",
|
|
4
|
-
"version": "0.0
|
|
4
|
+
"version": "1.0.0",
|
|
5
5
|
"exports": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
|
-
"repository": "https://github.com/yefei/
|
|
7
|
+
"repository": "https://github.com/yefei/zenorm-generate",
|
|
8
8
|
"author": "YeFei <316606233@qq.com>",
|
|
9
9
|
"license": "MIT",
|
|
10
10
|
"bin": {
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"scripts": {
|
|
17
17
|
"build": "rimraf dist && tsc",
|
|
18
18
|
"prepublishOnly": "npm run build",
|
|
19
|
-
"gen": "
|
|
19
|
+
"gen": "cd test && ts-node ../src/bin/zenorm-generate.ts config.json"
|
|
20
20
|
},
|
|
21
21
|
"keywords": [
|
|
22
22
|
"mysql",
|
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
],
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@types/node": "^12.20.41",
|
|
31
|
-
"@zenorm/mysql": "^1.
|
|
32
|
-
"
|
|
31
|
+
"@zenorm/generate-mysql": "^1.2.0",
|
|
32
|
+
"ts-node": "^10.9.1",
|
|
33
33
|
"typescript": "^4.9.5"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|