Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jamg44/mongoose-class
💥 A set of decorators to use Mongoose with ES6 classes in Typescript
https://github.com/jamg44/mongoose-class
class decorators es6 mongodb mongoose typescript
Last synced: 2 months ago
JSON representation
💥 A set of decorators to use Mongoose with ES6 classes in Typescript
- Host: GitHub
- URL: https://github.com/jamg44/mongoose-class
- Owner: jamg44
- License: mit
- Created: 2017-05-16T08:10:20.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-06-03T10:25:50.000Z (over 7 years ago)
- Last Synced: 2024-09-21T14:05:58.854Z (3 months ago)
- Topics: class, decorators, es6, mongodb, mongoose, typescript
- Language: TypeScript
- Homepage:
- Size: 24.4 KB
- Stars: 6
- Watchers: 3
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mongoose-class
## Install
```
npm install mongoose-class --save
``````
npm install @types/mongoose @types/mongodb --save-dev
```## Create a model
./models/User.ts
```
/**
* User model
* @module User
*/import { Model as MongooseModel } from 'mongoose';
import { Model, Column } from '../lib/mongoose-class';@Model({
indexes: [ { name: 1, age: -1} ],
options: { collection: 'user'},
beforeCreate: schema => {
schema.virtual('capitalizedName').get(function () {
return this.name.toUpperCase();
});
}
})
export class User extends MongooseModel {@Column({ type: String, index: true })
name: string;@Column(Number)
age: number;static list(callback?: Function) {
return this.find().exec(callback);
}greet() {
return 'Hello, my name is ' + this.name;
}}
```## Use the model
./index.ts
```import { User } from './models/User';
const user = new User({ name: 'Pepe', age: 34, nosale: 33});
User.find({}).exec((err, data) => {
console.log('find', err, data);
});User.list().then(users => {
console.log('list', users);
});console.log(user);
console.log(user.greet());
```