jax-hono 1.0.9 → 1.0.11
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 +1 -1
- package/plugins/mongoose/model.ts +15 -0
package/package.json
CHANGED
|
@@ -62,6 +62,7 @@ export function createSchemaModel<TSchema extends Schema>(
|
|
|
62
62
|
|
|
63
63
|
applyDefaultSchemaOptions(schema);
|
|
64
64
|
installCommonFields(schema);
|
|
65
|
+
installRankHooksIfNeeded(schema);
|
|
65
66
|
installModelStatics(schema);
|
|
66
67
|
|
|
67
68
|
return (mongoose.models[name] ??
|
|
@@ -74,6 +75,8 @@ export function createSchemaModel<TSchema extends Schema>(
|
|
|
74
75
|
|
|
75
76
|
export const createModel = createSchemaModel;
|
|
76
77
|
|
|
78
|
+
const rankHooksInstalled = Symbol.for("jax-hono.mongoose.rankHooksInstalled");
|
|
79
|
+
|
|
77
80
|
export function getModelName(filePathOrName: string) {
|
|
78
81
|
const baseName =
|
|
79
82
|
filePathOrName.includes("/") || filePathOrName.includes("\\")
|
|
@@ -151,6 +154,12 @@ export function applyDefaultSchemaOptions(schema: Schema) {
|
|
|
151
154
|
}
|
|
152
155
|
|
|
153
156
|
export function installRankHooks(schema: Schema) {
|
|
157
|
+
if ((schema as any)[rankHooksInstalled]) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
(schema as any)[rankHooksInstalled] = true;
|
|
162
|
+
|
|
154
163
|
schema.pre(["updateOne", "findOneAndUpdate", "updateMany"], function () {
|
|
155
164
|
normalizeRank(this.getUpdate());
|
|
156
165
|
});
|
|
@@ -160,6 +169,12 @@ export function installRankHooks(schema: Schema) {
|
|
|
160
169
|
});
|
|
161
170
|
}
|
|
162
171
|
|
|
172
|
+
function installRankHooksIfNeeded(schema: Schema) {
|
|
173
|
+
if (schema.path("rank") && schema.path("isTop")) {
|
|
174
|
+
installRankHooks(schema);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
163
178
|
export function installAutoNo(schema: Schema, field: string, start = 0) {
|
|
164
179
|
schema.pre("save", async function () {
|
|
165
180
|
if (!this.isNew || this.get(field)) {
|