chanjs 1.1.3 → 1.1.4

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.
@@ -5,6 +5,9 @@ export default (app)=>{
5
5
  app.use(prefix, express.static(dir, { maxAge: maxAge || 0 }));
6
6
  }
7
7
  }
8
+
9
+
8
10
  }
9
11
 
12
+
10
13
 
@@ -3,8 +3,9 @@ class BaseService {
3
3
  * @description 构造函数
4
4
  * @param {*} knex - knex实例
5
5
  */
6
- constructor() {
7
- this.knex = Chan.knex || null;
6
+ constructor(DB=Chan.knex) {
7
+ this.knex = DB;
8
+ this.DB = DB;
8
9
  this.model = ''; // 默认为空字符串
9
10
  }
10
11
 
@@ -15,9 +16,9 @@ class BaseService {
15
16
  */
16
17
  all(query={}) {
17
18
  if(Object.keys(query).length === 0) {
18
- return this.knex(this.model).select();
19
+ return this.DB(this.model).select();
19
20
  }
20
- return this.knex(this.model).where(query).select();
21
+ return this.DB(this.model).where(query).select();
21
22
  }
22
23
 
23
24
  /**
@@ -30,7 +31,7 @@ class BaseService {
30
31
  * @returns {Promise} 返回匹配条件的记录或记录列表
31
32
  */
32
33
  async findById({ query, field = [], len = 1 }) {
33
- let dataQuery = this.knex(this.model).where(query);
34
+ let dataQuery = this.DB(this.model).where(query);
34
35
 
35
36
  // 字段筛选
36
37
  if (field.length > 0) {
@@ -58,7 +59,7 @@ async insert(data = {}) {
58
59
  if (Object.keys(data).length === 0) {
59
60
  return false;
60
61
  }
61
- const result = await this.knex(this.model).insert(data);
62
+ const result = await this.DB(this.model).insert(data);
62
63
  return result?.length > 0 || !!result;
63
64
  }
64
65
 
@@ -75,7 +76,7 @@ async insert(data = {}) {
75
76
  }
76
77
 
77
78
  // 执行插入操作并获取结果
78
- const result = await this.knex(this.model).insert(records);
79
+ const result = await this.DB(this.model).insert(records);
79
80
 
80
81
  // 返回插入的结果
81
82
  return result;
@@ -90,7 +91,7 @@ async insert(data = {}) {
90
91
  if (Object.keys(query).length === 0) {
91
92
  return false;
92
93
  }
93
- const affectedRows = await this.knex(this.model).where(query).del();
94
+ const affectedRows = await this.DB(this.model).where(query).del();
94
95
  return affectedRows > 0;
95
96
  }
96
97
  /**
@@ -100,7 +101,7 @@ async insert(data = {}) {
100
101
  * @returns {Promise<boolean>} 返回操作是否成功
101
102
  */
102
103
  async update({query, params} = {}) {
103
- const result = await this.knex(this.model).where(query).update(params);
104
+ const result = await this.DB(this.model).where(query).update(params);
104
105
  return { affectedRows: result };
105
106
  }
106
107
 
@@ -117,7 +118,7 @@ async updateMany(updates = []) {
117
118
  }
118
119
 
119
120
  // 获取事务对象
120
- const trx = await this.knex.transaction();
121
+ const trx = await this.DB.transaction();
121
122
 
122
123
  try {
123
124
  const affectedRows = [];
@@ -155,8 +156,8 @@ async updateMany(updates = []) {
155
156
  */
156
157
  async query({current = 1, pageSize = 10, query = {},field = []}) {
157
158
  const offset = (current - 1) * pageSize;
158
- let countQuery = this.knex(this.model).count("* as total");
159
- let dataQuery = this.knex(this.model);
159
+ let countQuery = this.DB(this.model).count("* as total");
160
+ let dataQuery = this.DB(this.model);
160
161
  // 应用查询条件
161
162
  if(Object.keys(query).length > 0){
162
163
  Object.entries(query).forEach(([key, value]) => {
@@ -185,7 +186,7 @@ async updateMany(updates = []) {
185
186
  * @returns {Promise<number>} 返回符合条件的记录数量
186
187
  */
187
188
  async count(query=[]) {
188
- let dataQuery = this.knex(this.model);
189
+ let dataQuery = this.DB(this.model);
189
190
  if(query.length >0){
190
191
  query.forEach((condition) => {
191
192
  dataQuery = dataQuery.where(condition);
package/core/lib/view.js CHANGED
@@ -1,7 +1,6 @@
1
1
  import path from "path";
2
2
  import dayjs from 'dayjs';
3
3
  import { createRequire } from 'module';
4
-
5
4
  const require = createRequire(import.meta.url);
6
5
 
7
6
  const template = require("art-template");
package/index.js CHANGED
@@ -6,7 +6,7 @@ import { pathToFileURL } from 'url'; // 新增顶部导入
6
6
  import config from "./core/config.js";
7
7
  import {bindClass,createKnex,loadWebToEnd} from "./core/helper.js";
8
8
 
9
- import Controller from "./core/lib/controller.js";
9
+
10
10
 
11
11
  import Service from "./core/lib/service.js";
12
12
  import cache from './core/lib/cache.js';
@@ -21,7 +21,7 @@ class Chan {
21
21
  static plugins = {};
22
22
  static config = config;
23
23
  static cache = cache;
24
- static Controller = Controller;
24
+
25
25
 
26
26
  constructor() {
27
27
  this.app = express();
@@ -33,10 +33,14 @@ class Chan {
33
33
  await this.loadConfig();
34
34
  await this.loadExtends();
35
35
  this.loadCore();
36
- this.loadKnex();
36
+ this.loadDB();
37
37
  this.loadCors();
38
38
  }
39
39
 
40
+ extend(name, fn) {
41
+ this.app.plus[name] = ()=>fn;
42
+ }
43
+
40
44
 
41
45
  async loadConfig() {
42
46
  const configPath = path.join(Chan.config.APP_PATH, "config/index.js");
@@ -68,7 +72,7 @@ class Chan {
68
72
  }
69
73
 
70
74
  //数据库操作
71
- loadKnex() {
75
+ loadDB() {
72
76
  if(Chan.config?.db?.length > 0){
73
77
  Chan.config.db.map((item,index) => {
74
78
  if(index ==0){
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "chanjs",
4
- "version": "1.1.3",
4
+ "version": "1.1.4",
5
5
  "description": "chanjs基于express5 纯js研发的轻量级mvc框架。",
6
6
  "main": "index.js",
7
7
  "module": "index.js",
@@ -1,68 +0,0 @@
1
- /**
2
- * @description
3
- * 控制器基类
4
- */
5
- class Controller {
6
-
7
- status = {
8
- 200:"操作成功",
9
- 201:"操作失败",
10
- //错误状态码
11
- 401:"未授权",
12
- 403:"禁止访问",
13
- 404:"找不到资源",
14
- 500:"服务器内部错误",
15
- //业务状态码
16
- 1001:"参数错误",
17
- 1002:"用户名或密码错误",
18
- 1003:"用户名已存在",
19
- 1004:"邮箱已存在",
20
- 1005:"手机号已存在",
21
- 1006:"验证码错误",
22
- 1007:"验证码已过期",
23
- 1008:"验证码发送失败",
24
- 1009:"验证码发送成功",
25
- 1010:"用户未登录",
26
- 1011:"用户未注册",
27
- 1012:"用户已注册",
28
- //资源验证码
29
- 1013:"上传文件超过限制",
30
- 1014:"操作不允许",
31
- 1015:"资源不存在",
32
- //其他状态码
33
- 9999:"系统内部错误",
34
-
35
- };
36
-
37
- constructor() {
38
-
39
- }
40
-
41
- // 获取状态
42
- getStatus() {
43
- return this.status
44
- }
45
-
46
- setTatus(key, val) {
47
- this.status[key] = val
48
- }
49
-
50
- success(res,data = {}) {
51
- res.json({
52
- code: 200,
53
- msg: this.status[200],
54
- data
55
- })
56
- }
57
-
58
- // 定义一个fail函数,用于返回错误信息
59
- fail(res,code,data, msg='操作失败') {
60
- res.json({
61
- code: code,
62
- msg: this.status[code] || msg,
63
- data
64
- })
65
- }
66
- }
67
-
68
- export default Controller;