jax-hono 1.0.11 → 1.0.13

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.
@@ -334,7 +334,7 @@ function generateModelsRegistry() {
334
334
 
335
335
  files.forEach((filePath) => {
336
336
  const source = readFileSync(filePath, 'utf8')
337
- const exportedModels = [...source.matchAll(/\bexport\s+const\s+([a-zA-Z_$][a-zA-Z0-9_$]*Model)\b/g)]
337
+ const exportedModels = [...source.matchAll(/\bexport\s+const\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*=\s*create(?:Loose)?Model\s*\(/g)]
338
338
  .map((match) => match[1])
339
339
  .sort((left, right) => left.localeCompare(right))
340
340
 
@@ -343,12 +343,19 @@ function generateModelsRegistry() {
343
343
  }
344
344
 
345
345
  const modelKey = source.match(/\bexport\s+const\s+modelKey\s*=\s*['"]([a-zA-Z_$][a-zA-Z0-9_$]*)['"]/)?.[1]
346
+ const fileModelName = toPascalCase(toModulePath(
347
+ filePath.includes(`${srcDir}/modules/`) ? 'modules' : basename(parse(filePath).dir),
348
+ filePath,
349
+ ['.model'],
350
+ ))
346
351
 
347
352
  imports.push(`import { ${exportedModels.join(', ')} } from '${toImportPath(filePath)}'`)
348
353
  entries.push(...exportedModels.map((name) => {
349
354
  const key = exportedModels.length === 1 && modelKey
350
355
  ? modelKey
351
- : name.slice(0, -'Model'.length)
356
+ : name === `${fileModelName}Model`
357
+ ? fileModelName
358
+ : name
352
359
 
353
360
  return ` ${key}: ${name},`
354
361
  }))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jax-hono",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
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
  };