Skip to content

Commit

Permalink
fix type
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Dec 3, 2021
1 parent 5dcc094 commit 747cdb7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 34 deletions.
43 changes: 28 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
"should": "^13.2.3",
"ts-node": "^8.10.2",
"typedoc": "^0.17.3",
"typescript": "^3.9.7"
"typescript": "^4.5.2"
},
"dependencies": {
"@sindresorhus/is": "^3.0.0",
"@sindresorhus/is": "^4.2.0",
"reflect-metadata": "^0.1.12",
"type-fest": "^2.8.0"
},
Expand Down
35 changes: 18 additions & 17 deletions src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {Schema, SchemaDefinition, SchemaTypeOptions, Types} from 'mongoose'
import {Constructor} from 'type-fest'
import {getSchema, validators} from './index'
import {Constructor, Fn, getMongooseMeta, IMongooseClass, mongooseMeta, Prototype} from './meta'
import {Fn, getMongooseMeta, IMongooseClass, mongooseMeta} from './meta'
import {getType} from './util'

type SchemaType<T> = Omit<SchemaTypeOptions<T>, 'type'> & {type?: T}

export function prop<T>(options: SchemaType<T> = {},
type?: SchemaDefinition['type']): PropertyDecorator {
return (target: Prototype, name: string) => {
return (target: unknown, name: string) => {
const pathSchema = getMongooseMeta(target).schema[name] || {}
type = type || pathSchema['type']
if (!type && !options.type) {
Expand All @@ -21,7 +22,7 @@ export function prop<T>(options: SchemaType<T> = {},
}

export function array<T extends unknown>(type?: T, options?: SchemaTypeOptions<T[]>) {
return (target: Prototype, name: string): void => {
return (target: unknown, name: string): void => {
let t
if (type?.['prototype']?.[mongooseMeta]) {
t = getSchema(type as unknown as IMongooseClass)
Expand All @@ -40,55 +41,55 @@ export function array<T extends unknown>(type?: T, options?: SchemaTypeOptions<T
}

export function id(): PropertyDecorator {
return (target: Prototype, name: string) => {/* empty */}
return (target: unknown, name: string) => {/* empty */}
}

export function required(): PropertyDecorator {
return (target: Prototype, name: string) => {
return (target: unknown, name: string) => {
getMongooseMeta(target).schema[name] = {...getMongooseMeta(target).schema[name], required: true}
}
}

export function indexed(): PropertyDecorator {
return (target: Prototype, name: string) => {
return (target: unknown, name: string) => {
getMongooseMeta(target).schema[name] = {...getMongooseMeta(target).schema[name], index: true}
}
}

export function hidden(): PropertyDecorator {
return (target: Prototype, name: string) => {
return (target: unknown, name: string) => {
getMongooseMeta(target).schema[name] = {...getMongooseMeta(target).schema[name], select: false}
}
}

export function unique(): PropertyDecorator {
return (target: Prototype, name: string) => {
return (target: unknown, name: string) => {
getMongooseMeta(target).schema[name] = {...getMongooseMeta(target).schema[name], unique: true}
}
}

export function defaults<T>(value: T): PropertyDecorator {
return (target: Prototype, name: string) => {
return (target: unknown, name: string) => {
getMongooseMeta(target).schema[name] = {...getMongooseMeta(target).schema[name], default: value}
}
}

export function type(type: Prototype): PropertyDecorator {
export function type(type: unknown): PropertyDecorator {
return (target: unknown, name: string) => {
getMongooseMeta(target).schema[name] = {...getMongooseMeta(target).schema[name], type}
}
}

export function enums(values: Array<string | number> | Record<string | number, string | number>): PropertyDecorator {
return (target: Prototype, name: string) => {
return (target: unknown, name: string) => {
if (!Array.isArray(values)) {
values = Object.values(values)
}
getMongooseMeta(target).schema[name] = {...getMongooseMeta(target).schema[name], enum: values}
}
}

type LazyClass = () => Constructor
type LazyClass = () => Constructor<unknown>

export function ref(nameOrClass: string | LazyClass, idType: unknown)
export function ref(nameOrClass: IMongooseClass, idType?: unknown)
Expand Down Expand Up @@ -119,7 +120,7 @@ export function ref(nameOrClass: string | IMongooseClass | LazyClass, idType?: u
getMongooseMeta(target).schema[name] = {...field, ref: getMongooseMeta(nameOrClass.prototype).name}
}
} else {
return (target: Prototype, name: string) => {
return (target: unknown, name: string) => {
const field = getMongooseMeta(target).schema[name] || {}
const isArray = Array.isArray(field['type'])
if (isArray && !Array.isArray(idType)) {
Expand Down Expand Up @@ -179,25 +180,25 @@ export function refArray(nameOrClass: string | LazyClass | IMongooseClass, eleme
}

export function statics(): PropertyDecorator {
return (target: Constructor, name: string) => {
return (target: Constructor<unknown>, name: string) => {
getMongooseMeta(target.prototype).statics[name] = target[name]
}
}

export function query(): PropertyDecorator {
return (target: Constructor, name: string) => {
return (target: Constructor<unknown>, name: string) => {
getMongooseMeta(target.prototype).queries[name] = target[name]
}
}

export function methods(): PropertyDecorator {
return (target: Prototype, name: string) => {
return (target: unknown, name: string) => {
getMongooseMeta(target).methods[name] = target[name] as Fn
}
}

export function virtual(): MethodDecorator {
return (target: Prototype, name: string, descriptor: PropertyDescriptor) => {
return (target: unknown, name: string, descriptor: PropertyDescriptor) => {
if (descriptor.value) {
if (typeof descriptor.value !== 'function') {
throw new TypeError('virtual can only used on class method or getter/setter')
Expand Down

0 comments on commit 747cdb7

Please sign in to comment.