chanjs 1.0.33 → 1.0.35

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,45 @@
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
+ }
@@ -0,0 +1,25 @@
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;
@@ -0,0 +1,78 @@
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
+ };
@@ -0,0 +1,41 @@
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
+ };
@@ -0,0 +1,38 @@
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
+ };
@@ -0,0 +1,190 @@
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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "chanjs",
3
- "version": "1.0.33",
4
- "description": "Chanjs is a lightweight MVC framework developed based on express MySQL and pure JavaScript.",
3
+ "version": "1.0.35",
4
+ "description": "chanjs基于express 纯js研发的轻量级mvc框架。",
5
5
  "main": "core/chan.js",
6
6
  "module": "core/chan.js",
7
7
  "keywords": [
@@ -26,8 +26,7 @@
26
26
  "mysql2": "^3.11.0",
27
27
  "dayjs": "^1.11.12"
28
28
  },
29
- "homepage": "https://www.npmjs.com/package/chanjs",
30
- "files": [
31
- "README.md"
32
- ]
29
+ "__npminstall_done": true,
30
+ "_from": "chanjs@1.0.22",
31
+ "_resolved": "https://registry.npmmirror.com/chanjs/-/chanjs-1.0.22.tgz"
33
32
  }
package/publish.bat ADDED
@@ -0,0 +1,4 @@
1
+ npm config set proxy null
2
+ npm cache clean --force
3
+ npm config set registry=https://registry.npmjs.org
4
+ npm publish