chanjs 1.0.29 → 1.0.31

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.
package/README.md CHANGED
@@ -1,96 +1,99 @@
1
- # <center>Chan.js mvc框架</center>
2
1
 
3
- Chan.js 基于express 纯js研发的轻量级mvc框架。基于函数式编程思想,性能优越,代码清晰,流程易读,可持续维护高。
2
+ # Chan.js
4
3
 
5
- ## 特性
4
+ Chan.js is a lightweight MVC framework developed in pure JavaScript based on Express. It embodies functional programming concepts, offering superior performance, clear code, and easy-to-follow processes that ensure high maintainability.
6
5
 
7
- - 基于express
8
- - 支持es6语法
9
- - 模块化
10
- - 支持多模块路由
11
- - 支持多模块视图
12
- - 支持多模块控制器
13
- - 支持多模块服务
14
- - 插件化
15
- - 轻量级(核心代码300行内)
16
- - 长期维护
6
+ ## features
17
7
 
18
- ## 规范 遵循约定优于配置
8
+ - Built on top of Express
9
+ - Supports ES6 syntax
10
+ - Modular Design
11
+ - Supports multiple module routes
12
+ - Supports multiple module views
13
+ - Supports multiple module controllers
14
+ - Supports multiple module services
15
+ - Plugin-based architecture
16
+ - Lightweight (core code within 300 lines)
17
+ - Long-term maintenance
18
+
19
+ ## Conventions Over Configuration
19
20
 
20
21
  ```code
21
22
  |- app
22
- |- config 配置
23
- |- module 模块1
24
- |- module1 模块1
25
- |- controller 控制器
26
- |- service 服务模型
27
- |- view 视图模板
28
- |- router.js 路由
29
- |- module2 模块2
30
- |- controller 控制器
31
- |- service 服务模型
32
- |- view 视图模板
33
- |- router.js路由
34
- |- extend 扩展
35
- |- middleware 中间件
36
- |- plugin 插件
37
- |- plus-module1 插件1
38
- |- controller 控制器
39
- |- service 服务模型
40
- |- view 视图模板
41
- |- router.js 路由
42
- |- module2 插件2
43
- |- controller 控制器
44
- |- service 服务模型
45
- |- view 视图模板
46
- |- router.js路由
47
- |- public 静态文件
23
+ |- config
24
+ |- module
25
+ |- module1
26
+ |- controller
27
+ |- service
28
+ |- view
29
+ |- router.js
30
+ |- module2
31
+ |- controller
32
+ |- service
33
+ |- view
34
+ |- router.js
35
+ |- extend
36
+ |- middleware
37
+ |- plugin
38
+ |- plus-module1
39
+ |- controller
40
+ |- service
41
+ |- view
42
+ |- router.js
43
+ |- module2
44
+ |- controller
45
+ |- service
46
+ |- view
47
+ |- router.js
48
+ |- public
48
49
  |- index.js
49
50
  ```
50
51
 
51
- ### 初始化流程
52
+ ### Initialization Process
52
53
 
53
- - 初始化
54
- - 加载配置
55
- - 加载模块
56
- - 加载service
57
- - 加载controller
58
- - 加载router
59
- - 加载extend
60
- - 加载plugin
61
- - 加载service
62
- - 加载controller
63
- - 加载router
64
- - beforeStart() 挂在从数据库获取的配置合并到配置文件中
65
- - run() 启动服务
54
+ - Initialization
55
+ - Load configuration
56
+ - Load modules
57
+ - Load services
58
+ - Load controllers
59
+ - Load routers
60
+ - Load extensions
61
+ - Load plugins
62
+ - Load services
63
+ - Load controllers
64
+ - Load routers
65
+ - beforeStart() Hook to merge configurations from the database into the configuration
66
+ - run() Start the server
66
67
 
67
- ### 官网
68
+ ### Official Website
68
69
 
69
- 基于Chanjs开发的cms系统
70
- 网址:<https://www.chancms.top>
70
+ A CMS system developed with Chan.js
71
+ Website:<https://www.chancms.top>
71
72
 
72
- ### 特点
73
+ ### Highlights
73
74
 
74
- - 配置文件
75
- - 多模块mvc
76
- - 插件mvc
77
- - 支持cors跨域配置
78
- - mysql数据库支持
79
- - 路由控制
80
- - art-template模板
81
- - 静态资源
82
- - cookie
83
- - 日志功能
75
+ - Configuration files
76
+ - Multi-module MVC structure
77
+ - Plugin MVC support
78
+ - CORS cross-origin configuration support
79
+ - MySQL database support
80
+ - Route control
81
+ - Art-template template engine
82
+ - Static resources management
83
+ - Cookie handling
84
+ - Logging capabilities
84
85
 
85
- ### 运行
86
+ ### run
86
87
 
87
88
  ```javascript
88
89
  const Chanjs = require("chanjs");
89
90
  const chan = new Chanjs();
90
- //加载中间件
91
+ // Load middleware
91
92
  chan.beforeStart(fn);
92
- //扫描模块
93
+ // Scan modules
93
94
  chan.start();
94
- //启动服务
95
+ // Start the server
95
96
  chan.run();
96
97
  ```
98
+
99
+ This framework is designed for developers who seek a balance between simplicity and functionality, providing a robust foundation for building web applications with ease.
package/package.json CHANGED
@@ -1,12 +1,14 @@
1
1
  {
2
2
  "name": "chanjs",
3
- "version": "1.0.29",
4
- "description": "chanjs基于express 纯js研发的轻量级mvc框架。",
3
+ "version": "1.0.31",
4
+ "description": "Chanjs is a lightweight MVC framework developed based on express MySQL and pure JavaScript.",
5
5
  "main": "core/chan.js",
6
6
  "module": "core/chan.js",
7
7
  "keywords": [
8
8
  "\"chanjs\"",
9
9
  "\"express mvc\"",
10
+ "\"express mysql\"",
11
+ "\"express knex\"",
10
12
  "\"chancms\"",
11
13
  "\"nodejs\""
12
14
  ],
@@ -24,7 +26,8 @@
24
26
  "mysql2": "^3.11.0",
25
27
  "dayjs": "^1.11.12"
26
28
  },
27
- "__npminstall_done": true,
28
- "_from": "chanjs@1.0.22",
29
- "_resolved": "https://registry.npmmirror.com/chanjs/-/chanjs-1.0.22.tgz"
29
+ "homepage": "https://www.npmjs.com/package/chanjs",
30
+ "files": [
31
+ "README.md"
32
+ ]
30
33
  }
@@ -1,45 +0,0 @@
1
- const path = require('path');
2
-
3
- /**
4
- * @description 根目录
5
- */
6
- const ROOT_PATH = process.cwd();
7
- // 读取package.json文件
8
- const {version='1.0.0',author='明空'} = require(path.join(ROOT_PATH, 'package.json'));
9
-
10
- /**
11
- * @description 程序目录
12
- */
13
- const APP_PATH = path.join(ROOT_PATH, 'app');
14
-
15
- let config = {
16
- JSON_LIMIT:"100kb",
17
- logger : {
18
- level: 'dev',
19
- },
20
- version,
21
- author,
22
- env:'dev',
23
- template:'default',
24
- views:[], //模板路径
25
- static:[{ //静态文件路径
26
- prefix: "/public/",
27
- dir: ["app/public"],
28
- maxAge: 0,
29
- }],
30
- database:{
31
- client: "mysql2",
32
- host: "localhost",
33
- port: "3306",
34
- user: "root",
35
- password: "123456",
36
- database: "chancms",
37
- charset: "utf8mb4",
38
- }
39
- }
40
-
41
- module.exports = {
42
- ROOT_PATH,
43
- APP_PATH,
44
- ...config
45
- }
@@ -1,25 +0,0 @@
1
- /**
2
- * @description
3
- * 控制器基类
4
- */
5
- class Controller {
6
- constructor() {}
7
-
8
- download(res, file) {
9
- res.download(file);
10
- }
11
-
12
- success(res, data = null) {
13
- res.json({ code: 200, msg: "success", data: data });
14
- }
15
-
16
- fail(res, msg = "操作失败,请稍后再试") {
17
- res.json({ code: 400, msg: msg });
18
- }
19
-
20
- error(res, msg) {
21
- res.json({ code: 500, msg: msg });
22
- }
23
- }
24
-
25
- module.exports = Controller;
@@ -1,78 +0,0 @@
1
- const knex = require('knex');
2
-
3
- /**
4
- * @description 实例化一个类,并将该类的所有方法绑定到一个新的对象上。
5
- * @param {Function} className - 需要实例化的类。
6
- *@returns {Object} 包含绑定方法的对象。
7
- */
8
- function bindClass(className) {
9
- let obj = {};
10
- const cls = new className();
11
- Object.getOwnPropertyNames(cls.constructor.prototype).forEach(
12
- (methodName) => {
13
- if (
14
- methodName !== "constructor" &&
15
- typeof cls[methodName] === "function"
16
- ) {
17
- obj[methodName] = cls[methodName].bind(cls);
18
- }
19
- }
20
- );
21
- return obj;
22
- }
23
-
24
-
25
- function createKnex(opt) {
26
- let config = {
27
- host:"localhost",
28
- port:"3306",
29
- client:"mysql2",
30
- charset:"utf8mb4",
31
- debug:false,
32
- ...opt
33
- };
34
- return knex({
35
- client: config.client,
36
- connection: {
37
- host:config.host,
38
- port:config.port,
39
- user:config.user,
40
- password:config.password,
41
- database:config.database,
42
- charset:config.charset,
43
- },
44
- debug: config.debug,
45
- //默认为{min: 2, max: 10}
46
- pool: {
47
- min: 0,
48
- max: 2,
49
- },
50
- log: {
51
- warn(message) {
52
- console.error("[knex warn]", message);
53
- },
54
- error(message) {
55
- console.error("[knex error]", message);
56
- },
57
- },
58
- });
59
- }
60
- /**
61
- *
62
- * @param {*} module 模块目录
63
- * @returns Array
64
- * @description 将web模块放到最后加载
65
- */
66
- function loadWebToEnd(module=[]){
67
- const index = module.indexOf('web');
68
- if (index !== -1) {
69
- const web = module.splice(index, 1);
70
- module.push(web[0]);
71
- }
72
- return module;
73
- }
74
- module.exports = {
75
- bindClass,
76
- createKnex,
77
- loadWebToEnd
78
- };
package/core/lib/index.js DELETED
@@ -1,41 +0,0 @@
1
- const express = require("express");
2
- const cookieParser = require("cookie-parser");
3
- const favicon = require("serve-favicon");
4
- const morgan = require("morgan");
5
- const path = require("path");
6
- const template = require("./middleware/template.js");
7
- module.exports = async function (app, config) {
8
- const { logger, APP_PATH, cookieKey, static, JSON_LIMIT, appName, version } =
9
- config;
10
- //日志
11
- app.use(morgan(logger.level));
12
-
13
- // favicon 图标
14
- app.use(favicon(path.join(APP_PATH, "public/favicon.ico")));
15
-
16
- //cookie
17
- app.use(cookieParser(cookieKey));
18
-
19
- //解析接口 json & url
20
- app.use(express.json({ limit: JSON_LIMIT }));
21
- app.use(express.urlencoded({ extended: false }));
22
-
23
- //配置模板引擎
24
- template(app, config);
25
-
26
- //使用静态资源 ,
27
- if (static.length > 0) {
28
- static.forEach((item) => {
29
- const { prefix, dir, maxAge } = item;
30
- app.use(prefix, express.static(dir, { maxAge: maxAge || 0 }));
31
- });
32
- }
33
-
34
- //设置头信息
35
- app.use((req, res, next) => {
36
- res.setHeader("Create-By", "Chanjs");
37
- res.setHeader("X-Powered-By", appName);
38
- res.setHeader(appName, version);
39
- next();
40
- });
41
- };
@@ -1,38 +0,0 @@
1
- const path = require("path");
2
- const template = require("art-template");
3
- const dayjs = require("dayjs");
4
-
5
- // 注册 dateFormat 函数
6
- template.defaults.imports.dateFormat = function (date, format) {
7
- if (!date) {
8
- return "";
9
- }
10
- // 如果传入的是一个 Date 对象,转换为 dayjs 对象
11
- if (
12
- date instanceof Date ||
13
- typeof date === "string" ||
14
- typeof date === "number"
15
- ) {
16
- date = dayjs(date);
17
- } else {
18
- return "";
19
- }
20
- return date.format(format);
21
- };
22
-
23
- module.exports = (app, config) => {
24
- const { APP_PATH, views, env } = config;
25
- //默认home view
26
- const home = path.join(APP_PATH, `modules/web/view`);
27
- //合并插件中的view
28
- const all = [...views, home];
29
-
30
- app.set("view options", {
31
- debug: env === "dev",
32
- cache: env === "prd",
33
- minimize: true,
34
- });
35
- app.set("view engine", "html");
36
- app.set("views", all);
37
- app.engine(".html", require("express-art-template"));
38
- };
@@ -1,190 +0,0 @@
1
- class BaseService {
2
- /**
3
- * @description 构造函数
4
- * @param {*} knex - knex实例
5
- * @param {*} model - 模型名称
6
- */
7
- constructor(knex, model = "") {
8
- this.knex = knex;
9
- this.model = model;
10
- }
11
-
12
- /**
13
- * @description 查找所有符合条件的记录
14
- * @param {*} query - 查询条件,例如:{name: 'test', age: 18}
15
- * @returns {Promise} 返回符合条件的记录
16
- */
17
- query(query) {
18
- return this.knex(this.model).where(query);
19
- }
20
-
21
- /**
22
- * @description 查询表所有记录,慎用
23
- * @returns {Promise} 返回所有记录
24
- */
25
- all() {
26
- return this.knex(this.model).select();
27
- }
28
-
29
- /**
30
- * @description 查询指定id的记录
31
- * @param {*} id
32
- * @returns {Promise} 返回指定id的记录
33
- */
34
- findById(id) {
35
- return this.knex(this.model).where("id", id).first();
36
- }
37
-
38
- /**
39
- * @description 查询分页数据
40
- * @param {*} current
41
- * @param {*} pageSize
42
- * @param {*} query
43
- * @returns {Promise} 返回分页数据
44
- */
45
- async findListAndQuery(current = 1, pageSize = 10, query = {}) {
46
- const offset = (current - 1) * pageSize;
47
-
48
- let countQuery = this.knex(this.model).count("* as total");
49
- let dataQuery = this.knex(this.model);
50
-
51
- // 应用查询条件
52
- for (const key in query) {
53
- dataQuery = dataQuery.where(key, query[key]);
54
- countQuery = countQuery.where(key, query[key]);
55
- }
56
-
57
- // 获取总记录数
58
- const total = await countQuery.first();
59
-
60
- // 获取分页数据
61
- const data = await dataQuery.offset(offset).limit(pageSize);
62
-
63
- return { data, total: parseInt(total.total, 10) };
64
- }
65
-
66
- /**
67
- * @description 查询指定id的记录
68
- * @param {*} id - 记录id
69
- * @returns {Promise} 返回指定id的记录
70
- */
71
- findOneById(id) {
72
- return this.knex(this.model).where("id", id).select();
73
- }
74
-
75
- /**
76
- * @description 查询指定条件的记录
77
- * @param {Object} query - {<key>:<value>}
78
- * @returns {Promise} 返回指定条件的记录
79
- */
80
- findOne(query) {
81
- return this.knex(this.model).where(query).first();
82
- }
83
-
84
- /**
85
- * @description 更新指定id的记录
86
- * @param {*} id - 记录id
87
- * @param {*} params - {<key>:<value>}
88
- * @returns {Promise} 返回更新后的记录
89
- */
90
- findOneAndUpdate(id, params) {
91
- return this.knex(this.model).where("id", id).update(params).returning("*");
92
- }
93
-
94
- /**
95
- * @description 查询指定条件的记录数量
96
- * @param {*} query - {<key>:<value>}
97
- * @returns {Promise} 返回指定条件的记录数量
98
- */
99
- count(query) {
100
- return this.knex(this.model).where(query).count("* as total").first();
101
- }
102
-
103
- /**
104
- * @description 插入一条记录
105
- * @param {*} params - {<key>:<value>}
106
- * @returns {Promise} 返回插入后的记录
107
- */
108
- insert(params) {
109
- return this.knex(this.model).insert(params);
110
- }
111
-
112
- /**
113
- * @description 插入多条记录
114
- * @param {*} records - [{<key>:<value>}, {<key>:<value>}, ...]
115
- * @returns {Promise} 返回插入后的记录
116
- */
117
- insertMany(records) {
118
- return this.knex(this.model).insert(records);
119
- }
120
-
121
- /**
122
- * @description 更新指定id的记录
123
- * @param {*} id - 记录id
124
- * @param {*} params - {<key>:<value>}
125
- * @returns {Promise} 返回更新后的记录
126
- */
127
- updateById(id, params) {
128
- return this.knex(this.model).where("id", id).update(params);
129
- }
130
-
131
- /**
132
- * @description 更新数据
133
- * @param {*} query - {<key>:<value>}
134
- * @param {*} update - {<key>:<value>}
135
- * @returns {Promise<*>} - 返回更新后的数据
136
- */
137
- update(query, update) {
138
- return this.knex(this.model).where(query).update(update);
139
- }
140
-
141
- /**
142
- * @description 查询并更新
143
- * @param {*} query - {<key>:<value>}
144
- * @param {*} update - {<key>:<value>}
145
- * @returns {Promise<*>} - 返回更新后的记录
146
- */
147
- findAndModify(query, update) {
148
- return this.knex(this.model).where(query).update(update);
149
- }
150
-
151
- /**
152
- *
153
- * @param {*} id - id
154
- * @param {*} update - {<key>:<value>}
155
- * @returns {Promise<*>} - 返回更新后的记录
156
- */
157
- findByIdAndUpdate(id, update) {
158
- return this.knex(this.model).where("id", id).update(update);
159
- }
160
-
161
- /**
162
- * @description 根据条件更新记录
163
- * @param {*} query - {<key>:<value>}
164
- * @param {*} update - {<key>:<value>}
165
- * @returns {Promise} - 返回更新后的记录
166
- */
167
- findOneAndUpdate(query, update) {
168
- return this.knex(this.model).where(query).update(update);
169
- }
170
-
171
- /**
172
- * @description 根据id删除一条记录
173
- * @param {*} id - 记录id
174
- * @returns {Promise} - 返回删除的记录
175
- */
176
- findByIdAndRemove(id) {
177
- return this.knex(this.model).where("id", id).del();
178
- }
179
-
180
- /**
181
- * @description 根据条件删除一条记录
182
- * @param {*} query
183
- * @returns {Promise} - 返回删除的记录
184
- */
185
- findOneAndRemove(query) {
186
- return this.knex(this.model).where(query).del();
187
- }
188
- }
189
-
190
- module.exports = BaseService;
package/publish.bat DELETED
@@ -1,4 +0,0 @@
1
- npm config set proxy null
2
- npm cache clean --force
3
- npm config set registry http://registry.npmmirror.com
4
- npm publish