All Projects → goto-bus-stop → Mongoose Model Decorators

goto-bus-stop / Mongoose Model Decorators

Licence: mit
ES2016 decorator functions for building Mongoose models

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Mongoose Model Decorators

restgoose
Model-driven REST API framework using decorators
Stars: ✭ 28 (+154.55%)
Mutual labels:  mongoose, decorators
Tvrboreact
Dream starter project: React, Redux, React Router, Webpack
Stars: ✭ 13 (+18.18%)
Mutual labels:  mongoose, decorators
TvrboReact
Dream starter project: React, Redux, React Router, Webpack
Stars: ✭ 13 (+18.18%)
Mutual labels:  mongoose, decorators
Angular Cms
An flexiable, extendable, modular, single CMS app based on Angular, Express, MongoDB
Stars: ✭ 109 (+890.91%)
Mutual labels:  mongoose, decorators
typijs
The Angular CMS Framework for building fully-featured SPA sites powered by NodeJS and MongoDB with TypeScript
Stars: ✭ 141 (+1181.82%)
Mutual labels:  mongoose, decorators
Vue Blog
A single-user blog built with vue2, koa2 and mongodb which supports Server-Side Rendering
Stars: ✭ 586 (+5227.27%)
Mutual labels:  mongoose
Pwned
Simple C++ code for simple tasks
Stars: ✭ 16 (+45.45%)
Mutual labels:  mongoose
Draper
Decorators/View-Models for Rails Applications
Stars: ✭ 5,053 (+45836.36%)
Mutual labels:  decorators
Graphql Compose Mongoose
Mongoose model converter to GraphQL types with resolvers for graphql-compose https://github.com/nodkz/graphql-compose
Stars: ✭ 543 (+4836.36%)
Mutual labels:  mongoose
Node Auth
基于 Node Express Mongoose 实现的用户注册/登陆权限验证
Stars: ✭ 10 (-9.09%)
Mutual labels:  mongoose
Mongoosastic
Index Mongoose models into elasticsearch automatically. Looking for maintainers!
Stars: ✭ 942 (+8463.64%)
Mutual labels:  mongoose
Type Graphql
Create GraphQL schema and resolvers with TypeScript, using classes and decorators!
Stars: ✭ 6,864 (+62300%)
Mutual labels:  decorators
Node Express Mongodb Jwt Rest Api Skeleton
This is a basic API REST skeleton written on JavaScript using async/await. Great for building a starter web API for your front-end (Android, iOS, Vue, react, angular, or anything that can consume an API). Demo of frontend in VueJS here: https://github.com/davellanedam/vue-skeleton-mvp
Stars: ✭ 603 (+5381.82%)
Mutual labels:  mongoose
Node Express Boilerplate
A boilerplate for building production-ready RESTful APIs using Node.js, Express, and Mongoose
Stars: ✭ 890 (+7990.91%)
Mutual labels:  mongoose
Alosaur
Alosaur - Deno web framework with many decorators
Stars: ✭ 559 (+4981.82%)
Mutual labels:  decorators
Decorum
Tasteful decorators for Ruby.
Stars: ✭ 7 (-36.36%)
Mutual labels:  decorators
Node Express Mongoose Demo
A simple demo app using node and mongodb for beginners
Stars: ✭ 4,976 (+45136.36%)
Mutual labels:  mongoose
Mevn Cli
Light speed setup for MEVN(Mongo Express Vue Node) Apps
Stars: ✭ 696 (+6227.27%)
Mutual labels:  mongoose
Active decorator
ORM agnostic truly Object-Oriented view helper for Rails 4, 5, and 6
Stars: ✭ 928 (+8336.36%)
Mutual labels:  decorators
Class Validator
Decorator-based property validation for classes.
Stars: ✭ 6,941 (+63000%)
Mutual labels:  decorators

mongoose-model-decorators

ES2016 decorator functions for building Mongoose models.

As of Mongoose 4.7.0, Mongoose includes a loadClass function with which ES classes can be used to define Mongoose models. It's a bit different than this module, but it may suit your needs. See the docs for more.

Installation - Usage - API - Translations - Licence

Installation

npm install --save mongoose-model-decorators

Currently, there is no official Babel transformer for decorators. To use the @Model decorator syntax, you need to add the decorators-legacy transformer to your .babelrc or other Babel configuration.

npm install --save-dev babel-plugin-transform-decorators-legacy

Usage

import mongoose from 'mongoose'
import { Model } from 'mongoose-model-decorators'

@Model
class Channel {
  static schema = {
    channelName: { type: String, index: true },
    channelTopic: String,
    users: Array,
    favorited: { type: Boolean, default: false }
  }

  get summary () {
    const users = this.users.length
    return `${this.channelName} : ${this.channelTopic} (${users} active)`
  }
}

Channel.findOne({ channelName: '#mongoose' }).then(channel =>
  console.log(channel.summary)
  // → "#mongoose: Now with class syntax! (7 active)"
})

API

@Schema, @Schema()

Creates a Mongoose Schema from a Class definition.

Define your schema in a static schema property. The contents of that property will be passed to the Mongoose Schema constructor.

@Schema
class User {
  static schema = {
    name: String,
    age: Number,
    email: { type: String, required: true }
  }
}

You can also define a configureSchema method which will be called on the schema after it is instantiated, so you can do anything to it that may not be supported by mongoose-model-decorators otherwise:

@Schema
class User {
  static configureSchema (schema) {
    schema.query.byName = function (name) {
      return this.find({ username: name })
    }
    schema.index({ username: 1, joinedAt: 1 }, { unique: true })
  }
}

@Schema(options={})

Creates a Mongoose Schema from a Class definition.

The possible options are passed straight to the Mongoose Schema constructor.

Options defined in the options object take precedence over options that were defined as static properties on the Schema class. Thus:

@Schema({ collection: 'vip_users' })
class User {
  static autoIndex = false
  static collection = 'users'
}

…results in { autoIndex: false, collection: 'vip_users' } being passed to Mongoose.

@Model, @Model(), @Model(options={})

Creates a Mongoose schema from a class definition, and defines it on the global mongoose connection.

You can specify the Mongoose connection to attach the model to in the connection option (defaults to mongoose). Other options are passed straight through to @Schema.

@Model({ connection: myConnection, collection: 'best_users' })
class User {
  // …
}

is equivalent to:

@Schema({ collection: 'best_users' })
class UserSchema {
  // …
}
myConnection.model('User', new UserSchema)

And without a connection option:

@Model
class User { /* … */ }

is equivalent to:

@Schema
class UserSchema { /* … */ }
require('mongoose').model('User', new UserSchema)

createSchema(Class), createSchema(options={})(Class)

Alias to @Schema. This one reads a bit nicer if you're not using decorators:

const UserSchema = createSchema(UserClass)

createModel(Class), createModel(options={})(Class)

Alias to @Model. Reads a bit nicer if you're not using decorators:

const UserModel = createModel({ collection: 'best_users' })(UserClass)

Usage without decorators support

If your project configuration doesn't support decorators, you can still use most mongoose-model-decorators translations. Instead of using the @Decorator syntax, you can call the decorator as a function, passing the class definition:

import { createSchema, createModel } from 'mongoose-model-decorators'
class UserTemplate {
  static schema = {
    // ...
  }

  getFriends() {
    // ...
  }
}
// then use any of:
const UserSchema = createSchema(UserTemplate)
const UserSchema = createSchema()(UserTemplate)
const UserSchema = createSchema(options)(UserTemplate)
// or even:
const UserSchema = createSchema(class {
  // ...
})
// or for models:
const User = createModel(UserTemplate)
const User = createModel()(UserTemplate)
const User = createModel(options)(UserTemplate)
// or even:
createModel(class User {
  // ...
})

Translations

mongoose-model-decorators translates as many ES2015 class things to their Mongoose Schema and Model equivalents as possible, as transparently as possible.

Feature mongoose-model-decorators plain Mongoose
Instance methods
methodName () {
  console.log('hi!')
}
schema.method('methodName', function methodName () {
  console.log('hi!')
})
Instance getters
get propName () {
  return 10
}
schema.virtual('propName').get(function () {
  return 10
})
Instance setters
set propName (val) {
  console.log('set', val)
}
schema.virtual('propName').set(function (val) {
  console.log('set', val)
})
Pre/post hooks
@pre('validate')
makeSlug () {
  this.slug = slugify(this.username)
}
schema.pre('validate', function makeSlug () {
  this.slug = slugify(this.username)
})
Static methods
static methodName () {
  console.log('static!')
}
schema.static('methodName', function methodName () {
  console.log('static!')
})
Static properties
static prop = 'SOME_CONSTANT'
schema.on('init', function (ModelClass) {
  Object.defineProperty(ModelClass, 'prop', {
    value: 'SOME_CONSTANT'
  })
})
Static properties are a bit hacky, because Mongoose doesn't have a shorthand for them (only for static methods). They work well though :)
NB: static properties that are also Schema options are also copied.
Static getters and setters
static get propName () {
  return 20
}
static set propName () {
  throw new Error('Don\'t set this :(')
}
schema.on('init', function (ModelClass) {
  Object.defineProperty(ModelClass, 'propName', {
    get: function () {
      return 20
    },
    set: function () {
      throw new Error('Don\'t set this :(')
    }
  })
})

Licence

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].