wok-server 0.4.5 → 0.4.7

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.
@@ -122,12 +122,13 @@ class BaseMysqlManager {
122
122
  }
123
123
  /**
124
124
  * 按条件查询第一条记录.
125
- * @param table
126
- * @param criteria
125
+ * @param table 表信息
126
+ * @param criteria 查询条件
127
+ * @param orderBy 排序规则,按先后顺序放入,每个规则是一个元组,第一个元素是字段名称,第二个元素是顺序
127
128
  * @returns
128
129
  */
129
- findFirst(table, criteria) {
130
- return this.queryWithConnection(conn => (0, ops_1.findFirst)(this.opts.config, conn, table, criteria));
130
+ findFirst(table, criteria, orderBy) {
131
+ return this.queryWithConnection(conn => (0, ops_1.findFirst)(this.opts.config, conn, table, criteria, orderBy));
131
132
  }
132
133
  /**
133
134
  * 插入数据. 不支持自增加长id,id必须提前生成,请使用 uuid.
@@ -209,8 +209,9 @@ class MysqlCriteria {
209
209
  }
210
210
  if (typeof criterion.value !== 'number' &&
211
211
  typeof criterion.value !== 'string' &&
212
+ typeof criterion.value !== 'boolean' &&
212
213
  !(criterion.value instanceof Date)) {
213
- throw new exception_1.MysqlException('The value of the query criteria is invalid,only number,string and Date are supported,' +
214
+ throw new exception_1.MysqlException('The value of the query criteria is invalid,only number,string,boolean and Date are supported,' +
214
215
  `column name : ${criterion.key.toString()},value : ${criterion.value} .`);
215
216
  }
216
217
  }
@@ -97,14 +97,33 @@ exports.findByIdIn = findByIdIn;
97
97
  * @param criteria
98
98
  * @returns
99
99
  */
100
- async function findFirst(config, conn, table, criteria) {
100
+ async function findFirst(config, conn, table, criteria,
101
+ /**
102
+ * 排序规则,按先后顺序放入,每个规则是一个元组,第一个元素是字段名称,第二个元素是顺序
103
+ */
104
+ orderBy) {
101
105
  let query = criteria ? (0, criteria_1.buildQuery)(criteria) : undefined;
102
106
  let sql = `select * from ?? `;
107
+ const values = [table.tableName];
103
108
  if (query) {
104
109
  sql += ` where ${query.sql} `;
110
+ values.push(...query.values);
111
+ }
112
+ // 排序
113
+ if (orderBy && orderBy.length) {
114
+ orderBy.forEach((orderBy, idx) => {
115
+ const [field, sort] = orderBy;
116
+ if (idx == 0) {
117
+ sql += ` order by ?? ${sort} `;
118
+ }
119
+ else {
120
+ sql += ` , ?? ${sort} `;
121
+ }
122
+ values.push(field);
123
+ });
105
124
  }
106
125
  sql += ' limit 1';
107
- const res = await (0, utils_1.promiseQuery)(config, conn, sql, [table.tableName].concat(query ? query.values : []));
126
+ const res = await (0, utils_1.promiseQuery)(config, conn, sql, values);
108
127
  const list = res;
109
128
  return list.length ? list[0] : null;
110
129
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wok-server",
3
- "version": "0.4.5",
3
+ "version": "0.4.7",
4
4
  "packageManager": "pnpm@8.9.0",
5
5
  "description": "一个基于 NodeJs 和 TypeScript 的后端框架,轻量级、克制、简洁。A lightweight, restrained, and concise backend framework based on Node.js and TypeScript.",
6
6
  "scripts": {
@@ -78,11 +78,12 @@ export declare abstract class BaseMysqlManager {
78
78
  findAll<T>(table: Table<T>): Promise<T[]>;
79
79
  /**
80
80
  * 按条件查询第一条记录.
81
- * @param table
82
- * @param criteria
81
+ * @param table 表信息
82
+ * @param criteria 查询条件
83
+ * @param orderBy 排序规则,按先后顺序放入,每个规则是一个元组,第一个元素是字段名称,第二个元素是顺序
83
84
  * @returns
84
85
  */
85
- findFirst<T>(table: Table<T>, criteria?: MixCriteria<T>): Promise<T | null>;
86
+ findFirst<T>(table: Table<T>, criteria?: MixCriteria<T>, orderBy?: Array<[keyof T, 'asc' | 'desc']>): Promise<T | null>;
86
87
  /**
87
88
  * 插入数据. 不支持自增加长id,id必须提前生成,请使用 uuid.
88
89
  * @param table 表信息
@@ -63,4 +63,8 @@ export declare function findByIdIn<T>(config: MysqlConfig, connection: PoolConne
63
63
  * @param criteria
64
64
  * @returns
65
65
  */
66
- export declare function findFirst<T>(config: MysqlConfig, conn: PoolConnection, table: Table<T>, criteria?: MixCriteria<T>): Promise<T | null>;
66
+ export declare function findFirst<T>(config: MysqlConfig, conn: PoolConnection, table: Table<T>, criteria?: MixCriteria<T>,
67
+ /**
68
+ * 排序规则,按先后顺序放入,每个规则是一个元组,第一个元素是字段名称,第二个元素是顺序
69
+ */
70
+ orderBy?: Array<[keyof T, 'asc' | 'desc']>): Promise<T | null>;