Skip to content

Commit

Permalink
fix: 能用了
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Jul 14, 2023
1 parent a925e87 commit edcd713
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ export function forNestModule<T extends Constructor<object>>(modelClass: T): {na

function buildSchema<T>(meta: MongooseMeta): Schema<T> {
const schema = new Schema(meta.schema, meta.options)

Object.assign(schema.statics, meta.statics)
Object.assign(schema.methods, meta.methods)
Object.assign(schema.query, meta.queries)
Expand Down
16 changes: 8 additions & 8 deletions src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import lodash from 'lodash'
import {DefaultType, SchemaTypeOptions} from 'mongoose'
import {Constructor} from 'type-fest'
import {Class, Constructor} from 'type-fest'
import {getSchema, validators} from './index'
import {getMongooseMeta, hasMongooseMeta} from './meta'
import {getType} from './util'
Expand All @@ -16,9 +16,9 @@ export function prop<T>(options: SchemaTypeOptions<T> = {},
if (!type) {
type = getType(target, name) as object
}
if (typeof type === 'object' && type !== null && 'prototype' in type) {
if (hasMongooseMeta(type) && !pathSchema['type']) {
type = getSchema(type)
if (typeof type === 'function' && 'prototype' in type) {
if (hasMongooseMeta(type.prototype) && !pathSchema['type']) {
type = getSchema(type as Class<unknown>)
}
}
getMongooseMeta(target).schema[name] = {...pathSchema, ...options, ...type ? {type} : {}} as any
Expand All @@ -28,8 +28,8 @@ export function prop<T>(options: SchemaTypeOptions<T> = {},
export function array<T extends Constructor<unknown> | Array<unknown>>(type: T, options?: SchemaTypeOptions<T[]>) {
return (target: object, name: string): void => {
let t
if (typeof type === 'object' && type !== null) {
if (hasMongooseMeta(type)) {
if (typeof type === 'function') {
if (hasMongooseMeta(type.prototype)) {
t = getSchema(type)
}
}
Expand Down Expand Up @@ -191,8 +191,8 @@ export function refArray(nameOrClass: string | LazyClass | Constructor<unknown>,
}

export function statics() {
return (target: object, name: string, descriptor: PropertyDescriptor) => {
getMongooseMeta(target).statics[name] = descriptor.value
return (target: Class<unknown>, name: string, descriptor: PropertyDescriptor) => {
getMongooseMeta(target.prototype).statics[name] = descriptor.value
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {get} from 'lodash'
import * as mongoose from 'mongoose'
import mongoose from 'mongoose'
import should from 'should'
import {
array, DocumentType, getModel, getModelName, getSchema, hidden, id, index, indexed, methods, middleware, model,
Expand Down Expand Up @@ -153,8 +153,8 @@ describe('organization', () => {
it('getModel', () => {
const OrganizationSchema = getSchema(Organization)
OrganizationModel = getModel(Organization)
OrganizationModel.should.ownProperty('listByUser')
should(get(OrganizationSchema, 'paths.user.casterConstructor.name')).eql('ObjectID')
should(OrganizationModel.listByUser).not.undefined()
should(get(OrganizationSchema, 'paths.user.instance')).eql('ObjectID')
})
})

Expand Down

0 comments on commit edcd713

Please sign in to comment.