mongoose-typescript
TypeScript icon, indicating that this package has built-in type declarations

3.3.0 • Public • Published

mongoose-typescript

Build mongoose schema with typescript and decorator

Before Usage

make sure that both experimentalDecorators and emitDecoratorMetadata are set to true in tsconfig

Example

@subModel()
class Address {
  @prop() @required public country: string
  @prop() @required public province: string
  @prop() @required public city: string
  @prop() @required public address: string
}

@model('user')
class User extends Model<User> {
  @statics
  public static async findByName(name: string): Promise<User> {
    return this.findOne({username: name})
  }

  @id public readonly _id: mongoose.Types.ObjectId

  @prop() @unique @required public username: string
  @prop() @hidden public password: string
  @prop() @indexed public loginCount: number
  @array(Address) public addresses: Address[]

  @methods
  public addAddress(address: Address) {
    this.addresses.push(address)
    return this
  }
}

@model('organization')
@index({user: 1, name: 1}, {unique: true})
class Organization extends Model<Organization> {
  @statics
  public static async listByUser(userId: string) {
    return this.find({
      members: userId,
    })
  }

  @id public readonly _id: mongoose.Types.ObjectId

  @ref(User) @required public user: Ref<User>
  @prop() @unique @required public name: string
  @array() @ref(User) public members: Array<Ref<User>>

  @methods
  public async addMember(userId: mongoose.Types.ObjectId) {
    this.members.push(userId)
    return this.save()
  }
}

const UserModel: typeof User = getModel(User)

const user = new UserModel({
  username: 'abc',
  password: 'wow',
  addresses: [{
    country: 'china',
    provicne: 'zhejiang',
    city: 'hangzhou',
    address: 'xihu',
  }],
})

user.save()

Function

getSchema(model): Schema get Schema for custom config

getModel(model): Model get Model

Model level Decorators

@model(name: string, options?: SchemaOptions)

@index(fields: IIndexArgs['fields'], options?: IIndexArgs['options'])

@plugin<T>(plugin: IPluginType<T>, options?: T) registers a mongoose schema plugin

@subModel(options: SchemaOptions & {name?: string} = {})

Schema level Decorators

@prop(options: SchemaTypeOpts<T> = {}, type?: T) type is optional, mongoose-typescript will try to determined the type automatically

@array(type?: T, options?: SchemaTypeOpts<T>) because of typescript only mark the type as Array, so array field need set type manually, or using mixed

@id empty decorator, just for emit Reflect metadata for _id

@required set field {required: true}

@indexed set field {index: true}

@hidden set field {select: false}

@unique set field {unique: true}

@type(type) set field {type: type}

@defaults(value) set field {default: value}

@ref(nameOrClass: string | IMongooseClass) set field {ref: nameOrClass} if type isn't set, and the argument is class, mongoose-typescript will try to using the typeof class._id

@statics register static method

@methods register instance method

@query register query helper

@virtual register virtual field

APIDOC

https://bangbang93.github.io/mongoose-typescript

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.2.0-00next
3.3.013latest

Version History

VersionDownloads (Last 7 Days)Published
3.3.013
3.2.15
3.2.00
3.1.01
3.0.10
3.0.00
2.4.06
2.3.31
2.3.20
2.3.10
2.3.00
2.2.30
2.2.20
2.2.10
2.2.00
2.1.30
2.1.20
2.1.10
2.1.00
2.0.00
1.2.03
1.2.0-00
1.1.40
1.1.30
1.1.20
1.1.10
1.0.70
1.0.60
1.0.50
1.0.40
1.0.30
1.0.20
1.0.10
1.0.00
0.7.10
0.7.00
0.6.10
0.6.00
0.5.10
0.5.00
0.4.50
0.4.30
0.4.20
0.4.10
0.4.00
0.3.40
0.3.30
0.3.20
0.3.10
0.3.00
0.2.80
0.2.70
0.2.60
0.2.50
0.2.40
0.2.30
0.2.20
0.2.10
0.2.00
0.1.00
0.0.40
0.0.30
0.0.20
0.0.10

Package Sidebar

Install

npm i mongoose-typescript

Weekly Downloads

29

Version

3.3.0

License

MIT

Unpacked Size

46.8 kB

Total Files

25

Last publish

Collaborators

  • bangbang93