dborm-mysql 2.1.1 → 2.1.3

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.
@@ -0,0 +1,165 @@
1
+ // Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
2
+ // Project: [~dborm-mysql~]
3
+ // Definitions by: [~zhangxiang~] <[~A URL FOR YOU~]>
4
+
5
+ /*~ This is the module template file for function modules.
6
+ *~ You should rename it to index.d.ts and place it in a folder with the same name as the module.
7
+ *~ For example, if you were writing a file for "super-greeter", this
8
+ *~ file should be 'super-greeter/index.d.ts'
9
+ */
10
+
11
+ /*~ Note that ES6 modules cannot directly export callable functions.
12
+ *~ This file should be imported using the CommonJS-style:
13
+ *~ import x = require('someLibrary');
14
+ *~
15
+ *~ Refer to the documentation to understand common
16
+ *~ workarounds for this limitation of ES6 modules.
17
+ */
18
+
19
+ /*~ If this module is a UMD module that exposes a global variable 'myFuncLib' when
20
+ *~ loaded outside a module loader environment, declare that global here.
21
+ *~ Otherwise, delete this declaration.
22
+ */
23
+ // export as namespace myFuncLib;
24
+
25
+ /*~ This declaration specifies that the function
26
+ *~ is the exported object from the file
27
+ */
28
+ import mysql = require('mysql');
29
+
30
+ export = dbORM;
31
+
32
+ /*~ This example shows how to have multiple overloads for your function */
33
+ // declare function dbORM(name: string): MyFunction.NamedReturnType;
34
+ // declare function dbORM(length: number): MyFunction.LengthReturnType;
35
+ interface dbConfig {
36
+ connectionLimit: number;
37
+ database: string;
38
+ host: string;
39
+ port: number;
40
+ user: string;
41
+ password: string;
42
+ multipleStatements: boolean;
43
+ }
44
+
45
+ interface db2ramFieldMap {}
46
+
47
+ interface textDbFieldsMap {}
48
+
49
+ interface options {
50
+ log: boolean;
51
+ dbCode: number;
52
+ noConvertDbCodes: Array<number>,
53
+ logExecuteTime?: boolean,
54
+ }
55
+
56
+
57
+ interface dbORMParams {
58
+ dbConfig: dbConfig,
59
+ db2ramFieldMap: db2ramFieldMap,
60
+ textDbFieldsMap: textDbFieldsMap,
61
+ options: options
62
+ }
63
+
64
+ /**
65
+ *
66
+ * @param params
67
+ */
68
+ declare function dbORM(params: dbORMParams): dbORM.ORMTableInstanceConstructor
69
+
70
+ /*~ If you want to expose types from your module as well, you can
71
+ *~ place them in this block. Often you will want to describe the
72
+ *~ shape of the return type of the function; that type should
73
+ *~ be declared in here, as this example shows.
74
+ */
75
+ declare namespace dbORM {
76
+ interface ORM_DB {
77
+ pool: mysql.Pool,
78
+ getConnection(): Promise<mysql.Connection>,
79
+ wrapTransaction(fn: Function, nth: number, timeout?: number): (...args: any[]) => Promise<any>,
80
+ query(sql: string, params: Array<any>, connection?: mysql.Connection): Promise<any>,
81
+ beginTransaction(): Promise<mysql.Connection>,
82
+ commitTransaction(conn: mysql.Connection): Promise<any>,
83
+ rollbackTransaction(conn: mysql.Connection): Promise<any>
84
+ }
85
+
86
+ interface ORM_DBUtil {
87
+ getFieldNames(tableName: string, extra?: boolean): Array<string>,
88
+ parseKeyword(keyword: string): string,
89
+ createKeywordSql(tableName: string, keywordRes: string|object): { params: Array<string>, sql: string },
90
+ getWhereFields(tableName: string, field: string, insertFieldNameMap?: object): string,
91
+ getRam2dbFieldMaps(db2ramFieldMaps: object): object,
92
+ getTextRamFields(textDbFields: object): object,
93
+ /**
94
+ * 将data中key为数据库的字段名称转换为内存的字段名称
95
+ */
96
+ convert2RamFieldName(tableName: string, data: object|Array<any>): any,
97
+ /**
98
+ * 将data中key为内存中的字段名称转换为数据库中的名称
99
+ */
100
+ convert2DbFieldName(tableName: string, data: object|Array<any>): any,
101
+ /**
102
+ * 将fieldNames转换为数据库对应的名字, insertFieldNames表示有些自定义的字段不需要转换
103
+ */
104
+ toDbFieldNames(tableName: string, fieldNames: Array<any>, insertFieldNames?: Array<any>): string,
105
+ /**
106
+ * 创建一次插入一条数据的sql相关信息
107
+ */
108
+ createInsertSql(tableName: string, obj: any): { params: Array<Array<any>>, sql: string },
109
+ /**
110
+ * 创建一次性插入多条数据的sql相关信息
111
+ */
112
+ createBulkInsertSql(tableName: string, obj: Array<any>): { params: Array<Array<any>>, sql: string },
113
+ /**
114
+ * 从给定的fieldNames中选择where字段,其中kwFieldName表示关键字查询对应的字段
115
+ */
116
+ createWhereQuery(obj: any, tableName: string, insertFieldNameMap?: any): { params: Array<any>, sql: string },
117
+ /**
118
+ * 从给定的fieldNames中查找要更新的字段,创建更新sql语句
119
+ */
120
+ createUpdateQuery(tableName: string, obj: any): { params: Array<any>, sql: string },
121
+ createBulkUpdateSql(tableName: string, objs: Array<any>): { params: Array<any>, sql: string },
122
+ /**
123
+ * mode = 1或者mode = 2 按照中文升降排序,mode = 3 或者 mode = 4按照英文升降排序
124
+ */
125
+ getOrderBySql(field: string, mode: number, tableName: string, insertFieldNames?: Array<any>): string,
126
+ createSelectSql(tableName: string, selectFields?: Array<string>): { sql: string, insertFieldNames: Array<any>, insertFieldNameMap: any },
127
+ convertSort(sort: string, strFields: Array<any>): string
128
+ }
129
+
130
+ export interface ORMTableInstanceConstructor {
131
+ db: ORM_DB;
132
+ dbUtil: ORM_DBUtil;
133
+ (tableName: string): {
134
+ db: ORM_DB,
135
+ dbUtil: ORM_DBUtil,
136
+ // 这里原生 mysql 是 any 类型,但是由于我们 orm 框架的实现,我可以理解为返回数组
137
+ getList(query: any, connection?: mysql.Connection): Promise<Array<any>>,
138
+ findOne(query: any, connection?: mysql.Connection): Promise<any>,
139
+ getMapByField(query: any, connection?: mysql.Connection): Promise<Map<string, Array<any>>>,
140
+ getGroupByField(query: any, connection?: mysql.Connection): Promise<Map<string, any>>,
141
+ getListByIds(ids: Array<number>, connection?: mysql.Connection): Promise<Array<any>>,
142
+ getCount(query: any, connection?: mysql.Connection): Promise<number>,
143
+ pageQuery(query: any, connection?: mysql.Connection): Promise<{ list: Array<any>, count: number }>,
144
+ add(data: any, connection?: mysql.Connection): Promise<number>,
145
+ delete(data: any, connection?: mysql.Connection): Promise<any>,
146
+ updateByIds(data: any, ids?: Array<number>, connection?: mysql.Connection): Promise<any>,
147
+ batchUpdateByIds(data: any, ids?: Array<number>, options: any, connection?: mysql.Connection): Promise<any>,
148
+ update(data: any, id: number | string, connection?: mysql.Connection): Promise<any>,
149
+ updateByQuery(data: any, query: any, connection?: mysql.Connection): Promise<any>,
150
+ get(id: number | string, connection?: mysql.Connection): Promise<any>,
151
+ createBulk(objs?: Array<any>, connection?: mysql.Connection): Promise<any>,
152
+ updateBulk(objs?: Array<any>, connection?: mysql.Connection): Promise<any>,
153
+ deleteByIds(ids?: Array<number>, connection?: mysql.Connection): Promise<any>,
154
+ [key: string]: any
155
+ };
156
+ }
157
+
158
+ /*~ If the module also has properties, declare them here. For example,
159
+ *~ this declaration says that this code is legal:
160
+ *~ import f = require('myFuncLibrary');
161
+ *~ console.log(f.defaultName);
162
+ */
163
+ // export const defaultName: string;
164
+ // export let defaultLength: number;
165
+ }
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "dborm-mysql",
3
+ "version": "2.1.2",
4
+ "description": "a NodeJs ORM for mysql",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "dependencies": {
8
+ "@types/node": "^14.14.13",
9
+ "co": "^4.6.0",
10
+ "lodash": "^4.17.4",
11
+ "moment": "^2.24.0",
12
+ "mysql": "^2.14.1",
13
+ "short-uuid": "^2.2.0",
14
+ "util": "^0.10.3"
15
+ },
16
+ "devDependencies": {
17
+ "chai": "^3.5.0",
18
+ "istanbul": "^1.1.0-alpha.1",
19
+ "mocha": "^3.3.0",
20
+ "should": "~3.3.1"
21
+ },
22
+ "scripts": {
23
+ "test": "istanbul cover node_modules/mocha/bin/_mocha test.js"
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "https://github.com/ZhangDianPeng/dborm"
28
+ },
29
+ "keywords": [
30
+ "nodejs",
31
+ "orm",
32
+ "mysql"
33
+ ],
34
+ "author": "zhangdianp@163.com",
35
+ "license": "ISC"
36
+ }
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "dborm-mysql",
3
+ "version": "2.1.3",
4
+ "description": "a NodeJs ORM for mysql",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "dependencies": {
8
+ "@types/node": "^14.14.13",
9
+ "co": "^4.6.0",
10
+ "lodash": "^4.17.4",
11
+ "moment": "^2.24.0",
12
+ "mysql": "^2.14.1",
13
+ "short-uuid": "^2.2.0",
14
+ "util": "^0.10.3"
15
+ },
16
+ "devDependencies": {
17
+ "chai": "^3.5.0",
18
+ "istanbul": "^1.1.0-alpha.1",
19
+ "mocha": "^3.3.0",
20
+ "should": "~3.3.1"
21
+ },
22
+ "scripts": {
23
+ "test": "istanbul cover node_modules/mocha/bin/_mocha test.js"
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "https://github.com/ZhangDianPeng/dborm"
28
+ },
29
+ "keywords": [
30
+ "nodejs",
31
+ "orm",
32
+ "mysql"
33
+ ],
34
+ "author": "zhangdianp@163.com",
35
+ "license": "ISC"
36
+ }
package/base/dbORM.js CHANGED
@@ -228,6 +228,19 @@ let dbFunss = (config) => {
228
228
  return 'ok';
229
229
  };
230
230
 
231
+ exportsObj.batchUpdateByIds = async function (tableName, data, ids, options = {}, connection) {
232
+ if (!ids || !ids.length) {
233
+ return await Promise.resolve('ok');
234
+ }
235
+ let {batchSize = 20} = options;
236
+ let offset = 0;
237
+ while (offset < ids.length) {
238
+ await exportsObj.updateByIds(tableName, data, ids.slice(offset, offset + batchSize), connection);
239
+ offset += batchSize;
240
+ }
241
+ return 'ok';
242
+ };
243
+
231
244
  exportsObj.update = async function (tableName, data, id, connection) {
232
245
  await exportsObj.updateByIds(tableName, data, [id], connection);
233
246
  return 'ok';
package/base/dbUtil.js CHANGED
@@ -198,7 +198,7 @@ module.exports = (config, {dbCode = 733, ignoreDataError = false}) => {
198
198
  addFieldNames.push('modify_time');
199
199
  }
200
200
 
201
- let sql = `INSERT INTO ${tableName} (${addFieldNames.join(',')}) VALUES(?)`;
201
+ let sql = `INSERT INTO ${tableName} (${addFieldNames.map(field => '`' + field + '`').join(',')}) VALUES(?)`;
202
202
  let params = addFieldNames.map(fieldName => obj[fieldName]);
203
203
  return {
204
204
  params: [params],
package/index.d.ts CHANGED
@@ -144,6 +144,7 @@ declare namespace dbORM {
144
144
  add(data: any, connection?: mysql.Connection): Promise<number>,
145
145
  delete(data: any, connection?: mysql.Connection): Promise<any>,
146
146
  updateByIds(data: any, ids?: Array<number>, connection?: mysql.Connection): Promise<any>,
147
+ batchUpdateByIds(data: any, ids?: Array<number>, options: any, connection?: mysql.Connection): Promise<any>,
147
148
  update(data: any, id: number | string, connection?: mysql.Connection): Promise<any>,
148
149
  updateByQuery(data: any, query: any, connection?: mysql.Connection): Promise<any>,
149
150
  get(id: number | string, connection?: mysql.Connection): Promise<any>,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dborm-mysql",
3
- "version": "2.1.1",
3
+ "version": "2.1.3",
4
4
  "description": "a NodeJs ORM for mysql",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",