Skip to content

Commit 5b93b89

Browse files
committed
Add english readme
1 parent e59ba04 commit 5b93b89

File tree

2 files changed

+372
-101
lines changed

2 files changed

+372
-101
lines changed

README.md

Lines changed: 105 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,38 @@
55
[![npm downloads](https://img.shields.io/npm/dm/coa-mysql.svg?style=flat-square)](http://npm-stat.com/charts.html?package=coa-mysql)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://github.com/coajs/coa-mysql/pulls)
77

8-
COA 核心 MySQL 数据库组件,包含基本数据模型、缓存数据模型、分布式 ID 等
8+
English | [简体中文](README.zh-CN.md)
99

10-
## 特点
10+
MySQL database components for coajs, including basic data models, cache data models, distributed ID, etc.
1111

12-
- **功能齐全** 基础数据连接基于[mysql](https://github.com/mysqljs/mysql),SQL 查询基于[knex](https://github.com/knex/knex)库,注重性能,功能齐全包含原生库所有使用方式
13-
- **简单轻量** 不超过 1000 行代码,不依赖于其他第三方库
14-
- **快捷方便** 基本数据模型自带 CRUD 操作,无需额外代码
15-
- **自动缓存** 缓存数据模型能自动进行数据的缓存管理(缓存生成、缓存淘汰等逻辑),缓存基于[coa-redis](https://github.com/coajs/coa-redis)
16-
- **TypeScript** 全部使用 TypeScript 书写,类型约束、IDE 友好
12+
## Feature
1713

18-
## 组件
14+
- **Functional**: Basic data connection based on [mysql](https://github.com/mysqljs/mysql),SQL query based on [knex](https://github.com/knex/knex). Pay attention to performance, full-featured, including original library all use methods
15+
- **Lightweight**: No more than 1,000 lines of code, do not rely on other third-party libraries
16+
- **Fast and Convenient**: Basic data model comes with CRUD operation, no extra code
17+
- **Automatic Cache**: Cache data model automatically performs data cache management (cache generation, cache elimination, etc.), cache is based on[coa-redis](https://github.com/coajs/coa-redis)
18+
- **TypeScript**: All written in TypeScript, type constraint, IDE friendship
1919

20-
- 基本数据模型 `MysqlNative` 自动实现基本的 CRUD 等操作
21-
- 缓存数据模型 `MysqlCache` 在基本数据模型上接管数据缓存逻辑
22-
- 分布式 ID `MysqlUuid` 超轻量的分布式 UUID
20+
## Component
2321

24-
## 快速开始
22+
- Basic data model `MysqlNative`: Automatically implement basic CRUD
23+
- Cache data model `MysqlCache`: Take over data cache logic on the basic data model
24+
- Distributed ID `MysqlUuid`: Lightweight distributed UUID
2525

26-
### 安装
26+
## Quick Start
27+
28+
### Install
2729

2830
```shell
2931
yarn add coa-mysql
3032
```
3133

32-
### 实例配置
34+
### Instance configuration
3335

3436
```typescript
3537
import { MysqlBin } from 'coa-mysql'
3638

37-
// MySQL配置
39+
// MySQL configuration
3840
const mysqlConfig = {
3941
host: '127.0.0.1',
4042
port: 3306,
@@ -49,61 +51,62 @@ const mysqlConfig = {
4951
},
5052
}
5153

52-
// 初始化Mysql基本连接,后续所有模型均依赖此实例
54+
// Initialize MySQL basic connection,
55+
// follow-up all models depend on this example
5356
const mysqlBin = new MysqlBin(mysqlConfig)
5457
```
5558

56-
### 基本 SQL 查询
59+
### Basic SQL query
5760

58-
新建用户表`user`,表结构如下
61+
New user table `user`, the table structure is as follows
5962

6063
```shell
6164
CREATE TABLE `user` (
62-
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增长主键',
63-
`userId` varchar(32) NOT NULL DEFAULT '' COMMENT '用户ID',
64-
`name` varchar(64) NOT NULL DEFAULT '' COMMENT '姓名',
65-
`mobile` varchar(16) NOT NULL DEFAULT '' COMMENT '手机号',
66-
`avatar` varchar(256) NOT NULL DEFAULT '' COMMENT '头像',
67-
`gender` int(11) NOT NULL DEFAULT '0' COMMENT '性别,1男 2女',
68-
`language` varchar(16) NOT NULL DEFAULT '' COMMENT '语言',
69-
`status` int(1) NOT NULL DEFAULT '1' COMMENT '状态,1正常 2隐藏',
70-
`created` bigint(20) NOT NULL DEFAULT '0' COMMENT '创建时间',
71-
`updated` bigint(20) NOT NULL DEFAULT '0' COMMENT '更新时间',
65+
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Self-increased primary key',
66+
`userId` varchar(32) NOT NULL DEFAULT '' COMMENT 'user ID',
67+
`name` varchar(64) NOT NULL DEFAULT '' COMMENT 'name',
68+
`mobile` varchar(16) NOT NULL DEFAULT '' COMMENT 'mobile',
69+
`avatar` varchar(256) NOT NULL DEFAULT '' COMMENT 'avatar',
70+
`gender` int(11) NOT NULL DEFAULT '0' COMMENT 'gender, 1 male, 2 female',
71+
`language` varchar(16) NOT NULL DEFAULT '' COMMENT 'language',
72+
`status` int(1) NOT NULL DEFAULT '1' COMMENT 'status, 1 normal 2 hidden',
73+
`created` bigint(20) NOT NULL DEFAULT '0' COMMENT 'Create time',
74+
`updated` bigint(20) NOT NULL DEFAULT '0' COMMENT 'Update time',
7275
PRIMARY KEY (`id`) USING BTREE,
7376
UNIQUE KEY `user_userid_unique` (`userId`) USING BTREE
74-
) COMMENT='用户表';
77+
) COMMENT='User Table';
7578
```
7679

77-
对用户表进行 SQL 操作
80+
SQL operations on the user table
7881

7982
```typescript
80-
// 插入数据 https://knexjs.org/#Builder-insert
83+
// Insert data, see https://knexjs.org/#Builder-insert
8184
mysqlBin.io.table('user').insert({ userId: 'user-a', name: 'A', mobile: '15010001001', gender: 1, language: 'zh-CN', status: 1 })
8285

83-
// 查询全部数据,详见 https://knexjs.org/#Builder-select
86+
// Query all data, see https://knexjs.org/#Builder-select
8487
mysqlBin.io.table('user').select()
8588
mysqlBin.io.select('*').from('user')
8689

87-
// 带条件查询,详见 https://knexjs.org/#Builder-where
90+
// Conditional queries, see https://knexjs.org/#Builder-where
8891
mysqlBin.io.table('user').where('status', '=', 1)
8992

90-
// 修改数据,详见 http://knexjs.org/#Builder-update
93+
// Update data, see http://knexjs.org/#Builder-update
9194
mysqlBin.io.table('user').update({ name: 'AA', gender: 2 }).where({ userId: 'user-a' })
9295

93-
// 删除数据,详见 http://knexjs.org/#Builder-del%20/%20delete
96+
// Delete data, see http://knexjs.org/#Builder-del%20/%20delete
9497
mysqlBin.io.table('user').delete().where({ userId: 'user-a' })
9598
```
9699

97-
其中`io`是一个`Knex`对象,可以支持 [Knex.js](http://knexjs.org/#Builder)**全部**用法
100+
The `io` in this is a `Knex` object, can support **all the usage** of [Knex.js](http://knexjs.org/#Builder)
98101

99-
### 基本数据模型
102+
### Basic data model
100103

101-
在实际项目工程中,为了保证查询的高效、严谨,我们并不会直接操作 SQL 语句。基本的数据模块可以帮助我们实现 CURD 操作。 通过如下方式定义一个基本数据模型`User`
104+
In project engineering, in order to ensure the efficiency and rigor of the query, we will not directly operate the SQL statement. Basic data modules can help us implement CURD operations. Define a basic data model `User` by as follows
102105

103106
```typescript
104107
import { MysqlBin, MysqlNative } from 'coa-mysql'
105108

106-
// 定义User默认结构
109+
// Define the default structure of User
107110
const userScheme = {
108111
userId: '' as string,
109112
name: '' as string,
@@ -115,150 +118,151 @@ const userScheme = {
115118
created: 0 as number,
116119
updated: 0 as number,
117120
}
118-
// 定义User类型(通过默认结构自动生成)
121+
// Define the User type (automatically generated by the default structure)
119122
type UserScheme = typeof userScheme
120123

121-
// 通过基类初始化
124+
// Initialization by base class
122125
const User = new (class extends MysqlNative<UserScheme> {
123126
constructor() {
124127
super(
125128
{
126-
name: 'User', // 表名,默认会转化为下划线(snackCase)形式,如 User->user UserPhoto->user_photo
127-
title: '用户表', // 表的备注名称
128-
scheme: userScheme, // 表的默认结构
129-
pick: ['userId', 'name'], // 查询列表时显示的字段信息
129+
name: 'User', // Table name, default transformation into a `snackcase` format, such as User->user UserPhoto->user_photo
130+
title: 'User Table', // Table note name
131+
scheme: userScheme, // Default structure of the table
132+
pick: ['userId', 'name'], // Field information displayed when querying the list
130133
},
134+
// Binding configuration instance bin
131135
mysqlBin
132-
) // 绑定配置实例bin
136+
)
133137
}
134138

135-
// 自定义方法
139+
// Custom method
136140
async customMethod() {
137-
// 做一些事情
141+
// Do something
138142
}
139143
})()
140144
```
141145

142-
一般一个数据表对应一个模型,定义模型后,我们直接操作模型就可以对表进行操作
146+
Generally, a data sheet corresponds to a model, and after the model is defined, we can operate the model directly to operate the table
143147

144148
```typescript
145-
// 插入
146-
await User.insert({ name: '王小明', gender: 1 }) // 返回 'id001',即该条数据的 userId = 'id001'
149+
// Insert
150+
await User.insert({ name: 'Tom', gender: 1 }) // return 'id001', userId = 'id001' of this data
147151

148-
// 批量插入
152+
// Batch insert
149153
await User.mInsert([
150-
{ name: '王小明', gender: 1 },
151-
{ name: '宋小华', gender: 1 },
152-
]) // 返回 ['id002','id003']
154+
{ name: 'Tom', gender: 1 },
155+
{ name: 'Jerry', gender: 1 },
156+
]) // return ['id002','id003']
153157

154-
// 通过ID更新
155-
await User.updateById('id002', { name: '李四' }) // 返回 1
158+
// Update by ID
159+
await User.updateById('id002', { name: 'Lily' }) // return 1
156160

157-
// 通过ID批量更新
158-
await User.updateByIds(['id002', 'id003'], { status: 2 }) // 返回 2
161+
// Batch update by ID array
162+
await User.updateByIds(['id002', 'id003'], { status: 2 }) // return 2
159163

160-
// 通过ID更新或插入(如果id存在就更新,如果不存在就插入)
161-
await User.upsertById('id002', { name: '王小明', gender: 1 }) // 返回 1 ,更新了一条 userId = 'id02' 的数据
162-
await User.upsertById('id004', { name: '李四', gender: 1 }) // 返回 0 ,插入一条新数据,数据的 userId = 'id04'
164+
// Update or insert the ID (id exists if updated, if there is no insert)
165+
await User.upsertById('id002', { name: 'Tom', gender: 1 }) // return 1, update one data of userId = 'id02'
166+
await User.upsertById('id004', { name: 'Lily', gender: 1 }) // return 0, insert a new data of userId = 'id04'
163167

164-
// 通过ID删除多个
165-
await User.deleteByIds(['id003', 'id004']) // 返回 2
168+
// Delete by ID array
169+
await User.deleteByIds(['id003', 'id004']) // return 2
166170

167-
// 通过ID查询一个,第二个参数设置返回结果所包含的数据
168-
await User.getById('id001', ['name']) // 数据为{userId:'id001',name:'王小明',gender:1,status:1,...} 实际返回 {userId:'id001',name:'王小明'}
171+
// Query one by ID, the second parameter settings return the data contained in the result
172+
await User.getById('id001', ['name']) // data is {userId:'id001',name:'Tom',gender:1,status:1,...} return {userId:'id001',name:'Tom'}
169173

170-
// 通过ID获取多个
171-
await User.mGetByIds(['id001', 'id002'], ['name']) //返回 {id001:{userId:'id001',name:'王小明'},id002:{userId:'id002',name:'李四'}}
174+
// Get multiple data by ID array
175+
await User.mGetByIds(['id001', 'id002'], ['name']) // return {id001:{userId:'id001',name:'Tom'},id002:{userId:'id002',name:'Lily'}}
172176

173-
// 截断表
174-
await User.truncate() // 无返回值,不报错即成功截断整个表
177+
// Truncate table
178+
await User.truncate() // void, do not report an error is to operate successfully
175179

176-
// 自定义方法
177-
await User.customMethod() // 执行自定义方法
180+
// Custom method
181+
await User.customMethod() // call a custom method
178182
```
179183

180-
实际项目中,我们可能需要定义多个模型,每个模型上都有一些公共方法。这时,我们可以抽象一个基类模型,其他模型继承这个基类模型
184+
In the actual project, we may need to define multiple models, and there are some public methods on each model. At this time, we can abstract a base class model, other models inherit this base class model
181185

182186
```typescript
183187
import { CoaMysql } from 'coa-mysql'
184188

185-
// 通过mysqlBin定义一个模型的基类,各个模型都可以使用这个基类
189+
// Define the base class of a model by mysqlBin, each model can use this base class
186190
export class MysqlNativeModel<T> extends MysqlNative<T> {
187191
constructor(option: CoaMysql.ModelOption<T>) {
188-
// 将实例配置bin绑定
192+
// Configure the instance bin binding
189193
super(option, mysqlBin)
190194
}
191195

192-
// 也可以定义一些通用方法
196+
// You can also define some general methods
193197
commonMethod() {
194198
// do something
195199
}
196200
}
197201

198-
// 通过基类模型定义用户模型
202+
// Define user model by base model
199203
const User = new (class extends MysqlNativeModel<UserScheme> {
200204
constructor() {
201-
super({ name: 'User', title: '用户表', scheme: userScheme, pick: ['userId', 'name'] })
205+
super({ name: 'User', title: 'User Table', scheme: userScheme, pick: ['userId', 'name'] })
202206
}
203207

204-
// 自定义方法
208+
// Custom method
205209
async customMethodForUser() {
206-
// 做一些事情
210+
// Do something
207211
}
208212
})()
209213

210-
// 通过基类模型定义管理员模型
211-
const Manager = new (class extends MysqlNativeModel<UserScheme> {
214+
// Define Manager model by base model
215+
const Manager = new (class extends MysqlNativeModel<ManagerScheme> {
212216
constructor() {
213-
super({ name: 'Manager', title: '管理员表', scheme: userScheme, pick: ['userId', 'name'] })
217+
super({ name: 'Manager', title: 'Manager Table', scheme: managerScheme, pick: ['managerId', 'name'] })
214218
}
215219
})()
216220

217-
// 用户模型和管理员模型均可以调用公共方法
221+
// Both user model and manager model can call common method
218222
await User.commonMethod()
219223
await Manager.commonMethod()
220224

221-
// 仅仅用户模型可以调用自定义方法
225+
// Only user models can call custom method
222226
await User.customMethodForUser()
223227
```
224228

225-
### 缓存数据模型
229+
### Cache data model
226230

227-
基于 [coa-redis](https://www.npmjs.com/package/coa-redis) 实现快速高效的数据缓存逻辑,并**统一对缓存进行管理、维护缓存的生命周期、保证缓存与 MySQL 数据的一致性**
231+
Based on [coa-redis](https://www.npmjs.com/package/coa-redis) to achieve fast and efficient data cache logic, and **unify the cache, maintain the life cycle of the cache, to ensure the consistency of cache and mysql data**
228232

229-
使用之前需安装 `coa-redis` ,使用方法可查看 [这里](https://github.com/coajs/coa-redis)
233+
Need to install `coa-redis` before use, instructions for use to view [here](https://github.com/coajs/coa-redis)
230234

231-
缓存数据模型的使用方法和基本数据模型完全相同,仅需要将 `MysqlNative` 替换为 `MysqlCache`
235+
The method of use of cache data model is exactly the same as the basic data model. Only need to replace the `MysqlNative` to be `MysqlCache`
232236

233237
```typescript
234238
import { CoaMysql, MysqlCache } from 'coa-mysql'
235239
import { RedisBin, RedisCache } from 'coa-redis'
236240

237-
// 定义一个redis实例,详细用法详见 https://github.com/coajs/coa-redis
241+
// Define a Redis instance, detail usage see https://github.com/coajs/coa-redis
238242
const redisCache = new RedisCache(new RedisBin({ host: '127.0.0.1' }))
239243

240-
// 定义一个缓存数据模型的基类
244+
// Define the base class for a cache data model
241245
export class MysqlCacheModel<T> extends MysqlCache<T> {
242246
constructor(option: CoaMysql.ModelOption<T>) {
243-
// 将配置实例 和 redisCache实例 都绑定到这个基类上
247+
// Bind the configuration instance and the redisCache instance on this base class
244248
super(option, mysqlBin, redisCache)
245249
}
246250
}
247251

248-
// 通过缓存基类模型定义缓存用户模型
252+
// Define cache user model by cache base class
249253
const UserCached = new (class extends MysqlCacheModel<UserScheme> {
250254
constructor() {
251-
super({ name: 'User', title: '用户表', scheme: userScheme, pick: ['userId', 'name'] })
255+
super({ name: 'User', title: 'User Table', scheme: userScheme, pick: ['userId', 'name'] })
252256
}
253257
})()
254258

255-
// 查询数据
256-
await User.getById('id001') // 首次查询会先读取数据库
257-
await User.getById('id001') // 第二次调用会直接从缓存中读取数据
259+
// Query data
260+
await User.getById('id001') // First query will read the database
261+
await User.getById('id001') // The second call will read data directly from the cache
258262

259-
// 增删改操作和基本数据模型一直
260-
await User.insert({ name: '王小明', gender: 1 }) // 返回 'id001'
261-
await User.updateById('id001', { name: '李四' }) // 返回 1
263+
// Insert, delete, update, just like the basic data model
264+
await User.insert({ name: 'Tom', gender: 1 }) // return 'id001'
265+
await User.updateById('id001', { name: 'Lily' }) // return 1
262266
```
263267

264-
缓存模型会自动维护和管理缓存,如果缓存已经存在,接下来又调用 update 更新了数据,再次查询数据时自动从数据库中取出最新的数据。 实现原理可点击 这里(todo) 了解更多
268+
The cache model automatically maintains and manages caches. If the cache already exists, then call `updated` updated the data, and automatically remove the latest data from the database when querying the data again. Realization Principle Click here (todo) Learn more

0 commit comments

Comments
 (0)