You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-**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
19
19
20
-
- 基本数据模型 `MysqlNative` 自动实现基本的 CRUD 等操作
21
-
- 缓存数据模型 `MysqlCache` 在基本数据模型上接管数据缓存逻辑
22
-
- 分布式 ID `MysqlUuid` 超轻量的分布式 UUID
20
+
## Component
23
21
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
25
25
26
-
### 安装
26
+
## Quick Start
27
+
28
+
### Install
27
29
28
30
```shell
29
31
yarn add coa-mysql
30
32
```
31
33
32
-
### 实例配置
34
+
### Instance configuration
33
35
34
36
```typescript
35
37
import { MysqlBin } from'coa-mysql'
36
38
37
-
//MySQL配置
39
+
//MySQL configuration
38
40
const mysqlConfig = {
39
41
host: '127.0.0.1',
40
42
port: 3306,
@@ -49,61 +51,62 @@ const mysqlConfig = {
49
51
},
50
52
}
51
53
52
-
// 初始化Mysql基本连接,后续所有模型均依赖此实例
54
+
// Initialize MySQL basic connection,
55
+
// follow-up all models depend on this example
53
56
const mysqlBin =newMysqlBin(mysqlConfig)
54
57
```
55
58
56
-
### 基本 SQL 查询
59
+
### Basic SQL query
57
60
58
-
新建用户表`user`,表结构如下
61
+
New user table `user`, the table structure is as follows
59
62
60
63
```shell
61
64
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',
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
102
105
103
106
```typescript
104
107
import { MysqlBin, MysqlNative } from'coa-mysql'
105
108
106
-
//定义User默认结构
109
+
//Define the default structure of User
107
110
const userScheme = {
108
111
userId: ''asstring,
109
112
name: ''asstring,
@@ -115,150 +118,151 @@ const userScheme = {
115
118
created: 0asnumber,
116
119
updated: 0asnumber,
117
120
}
118
-
//定义User类型(通过默认结构自动生成)
121
+
//Define the User type (automatically generated by the default structure)
119
122
typeUserScheme=typeofuserScheme
120
123
121
-
//通过基类初始化
124
+
//Initialization by base class
122
125
const User =new (classextendsMysqlNative<UserScheme> {
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
181
185
182
186
```typescript
183
187
import { CoaMysql } from'coa-mysql'
184
188
185
-
//通过mysqlBin定义一个模型的基类,各个模型都可以使用这个基类
189
+
//Define the base class of a model by mysqlBin, each model can use this base class
//Both user model and manager model can call common method
218
222
awaitUser.commonMethod()
219
223
awaitManager.commonMethod()
220
224
221
-
//仅仅用户模型可以调用自定义方法
225
+
//Only user models can call custom method
222
226
awaitUser.customMethodForUser()
223
227
```
224
228
225
-
### 缓存数据模型
229
+
### Cache data model
226
230
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**
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