From e45183ceb8511eca6e8c2ff42afadd6f81604878 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Mon, 23 Sep 2019 16:07:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=80=E7=82=B9=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- History.md | 4 ++++ docs/3.x.md | 4 ++-- lib/api.js | 4 ++-- lib/method.js | 10 ++++------ lib/utils.js | 9 ++++++--- package.json | 2 +- 6 files changed, 19 insertions(+), 14 deletions(-) diff --git a/History.md b/History.md index 8de129d..c7976b1 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,7 @@ +# 3.0.9 / 2019-09-23 +* 优化创建数据库/表时的默认配置顶; +* 优化字段的默认值0的判断 + # 3.0.8 / 2019-09-19 * 修复tableCreate在没有索引时建表失败的bug * 更新文档 diff --git a/docs/3.x.md b/docs/3.x.md index 4c13371..29789f9 100644 --- a/docs/3.x.md +++ b/docs/3.x.md @@ -108,7 +108,7 @@ db.tableList().then(list => { ### dbCreate(name, options) * name `` [必传], 数据库名字 -* options ``, [必传], 数据库的配置 +* options ``, [选传], 数据库的配置 - charset `` 默认 utf8mb4 @@ -133,7 +133,7 @@ db.dbCreate('foo', { charset: 'utf8' }) // 默认是utf8mb4 - unique `` 是否为 唯一索引 - default `` 设置默认值 - update `` 是否自动更新(只有 datetime & timestamp 可以设) -* options ``, [必传], 表的配置 +* options ``, [选传], 表的配置 - charset `` 默认 utf8mb4 - engine `` 默认 InnoDB diff --git a/lib/api.js b/lib/api.js index adb8a0a..10b6285 100644 --- a/lib/api.js +++ b/lib/api.js @@ -124,7 +124,7 @@ class Api { } // 创建新的数据库 - dbCreate(name, { charset = 'utf8mb4' }) { + dbCreate(name, { charset = 'utf8mb4' } = {}) { if (!name) { return Promise.reject('Empty database name.') } @@ -146,7 +146,7 @@ class Api { } // 创建新的表, - tableCreate(name, fields, { charset = 'utf8mb4', engine = 'InnoDB' }) { + tableCreate(name, fields, { charset = 'utf8mb4', engine = 'InnoDB' } = {}) { if (!name) { return Promise.reject('Empty database name.') } diff --git a/lib/method.js b/lib/method.js index 9c1f584..a8fd1bd 100644 --- a/lib/method.js +++ b/lib/method.js @@ -56,9 +56,9 @@ class Method { * sql: .filter('name like "foo%" and age > 18') * func: .filter(function(){return 'name = "xiaoming"'}) * obj: .filter({ - * name: {$like: 'foo%'} - * age: {$gt: 18} - * }) + * name: {$like: 'foo%'} + * age: {$gt: 18} + * }) * obj形式的过滤, 支持多种多样, 详细请看Readme介绍 */ filter(val) { @@ -406,9 +406,7 @@ class Method { return this.connect().then(conn => { const defer = Promise.defer() - let sql = `ALTER TABLE ${ - this.cache.table - } ADD ${unique} INDEX \`${name}\` (${opt.field})` + let sql = `ALTER TABLE ${this.cache.table} ADD ${unique} INDEX \`${name}\` (${opt.field})` conn.query(sql, (err, result) => { conn.release() diff --git a/lib/utils.js b/lib/utils.js index d818547..bec2874 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -237,13 +237,14 @@ const parser = { } let notnull = it.notnull ? 'NOT NULL' : 'NULL' - if (/CHAR/.test(it.type)) { + if (~it.type.indexOf('CHAR')) { it.default = it.default ? escape(it.default) : '' } // 这几种类型,不允许设置默认值 if (['TEXT', 'BLOB', 'JSON', 'GEOMETRY'].includes(it.type)) { notnull = 'NULL' + delete it.default } // 这2种类型,如果设置了自动更新时间戳, 则默认值自动改为当前时间戳 @@ -257,10 +258,12 @@ const parser = { // 这3种时间类型,不允许设置默认值为 当前时间戳 if (['TIME', 'DATE', 'YEAR'].includes(it.type)) { if (it.default.toUpperCase() === 'CURRENT_TIMESTAMP') { - it.default = '' + delete it.default } } - defaultVal = it.default ? `DEFAULT ${it.default}` : '' + if (it.default || it.default === 0) { + defaultVal = 'DEFAULT ' + it.default + } // 非主键下, 设置了unique & index时,都为索引 if (!it.primary) { diff --git a/package.json b/package.json index 50a1a13..4223421 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mysqli", - "version": "3.0.8", + "version": "3.0.9", "description": "MySQL tool", "main": "index.js", "dependencies": {