jax-hono 1.0.11 → 1.0.12

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jax-hono",
3
- "version": "1.0.11",
3
+ "version": "1.0.12",
4
4
  "description": "Lightweight framework layer on top of Hono, built for Bun",
5
5
  "type": "module",
6
6
  "main": "./index.ts",
@@ -7,7 +7,7 @@ import mongoose, {
7
7
  type SchemaOptions,
8
8
  } from "mongoose";
9
9
  import config from "../../config";
10
- import { rankSetter } from "./schema";
10
+ import { rankSetter, rankedFields } from "./schema";
11
11
  import dayjs from "dayjs";
12
12
 
13
13
  export { rankSetter };
@@ -154,17 +154,35 @@ export function applyDefaultSchemaOptions(schema: Schema) {
154
154
  }
155
155
 
156
156
  export function installRankHooks(schema: Schema) {
157
+ const fields: Record<string, unknown> = {};
158
+
159
+ if (!schema.path("rank")) {
160
+ fields.rank = rankedFields.rank;
161
+ }
162
+
163
+ if (!schema.path("isTop")) {
164
+ fields.isTop = rankedFields.isTop;
165
+ }
166
+
167
+ if (Object.keys(fields).length) {
168
+ schema.add(fields as any);
169
+ }
170
+
157
171
  if ((schema as any)[rankHooksInstalled]) {
158
172
  return;
159
173
  }
160
174
 
161
175
  (schema as any)[rankHooksInstalled] = true;
162
176
 
177
+ // 修改
163
178
  schema.pre(["updateOne", "findOneAndUpdate", "updateMany"], function () {
179
+ console.log('updateOne');
164
180
  normalizeRank(this.getUpdate());
165
181
  });
166
182
 
183
+ // 新增
167
184
  schema.pre("save", function () {
185
+ console.log('save');
168
186
  normalizeRank(this);
169
187
  });
170
188
  }
@@ -294,14 +312,11 @@ function walkTree(items: any[], depth: number, list: any[]) {
294
312
  }
295
313
 
296
314
  function normalizeRank(data: any) {
297
- if (!data) {
298
- return;
299
- }
300
-
301
- const target = data.$set ?? data;
302
-
303
- if (target.rank !== undefined) {
304
- target.rank = rankSetter(target.rank);
305
- target.isTop = target.rank !== null;
315
+ if (data.rank != undefined) {
316
+ if (data.rank) {
317
+ data.isTop = true
318
+ } else {
319
+ data.isTop = false
320
+ }
306
321
  }
307
322
  }
@@ -21,7 +21,10 @@ export function rankSetter(value: unknown) {
21
21
  return value !== '' && value !== undefined && value !== null ? Number(value) : null;
22
22
  }
23
23
 
24
+
24
25
  export const rankedFields = {
25
- rank: { type: Number, set: rankSetter },
26
- isTop: { type: Boolean, default: false }
26
+ rank: { type: Number, set: (value: string) => (value != '' ? Number(value) : null) }, // 序号
27
+ isTop: { type: Boolean, default: false }, // 是否置顶,序号设置后自动置顶
28
+ // rank: { type: Number, set: rankSetter },
29
+ // isTop: { type: Boolean, default: false }
27
30
  };