Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
feat(mongoose): Model DI (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiisol authored Feb 27, 2017
1 parent eb1d790 commit 81d884a
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 22 deletions.
3 changes: 3 additions & 0 deletions mongoose/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Mongoose#1.2.0
* Model DI

# Mongoose#1.1.2
* New `options` parameter for `Model` decorator to pass in Schema Type options
* `Set` and `Option` decorators are now deprecated and will be removed in a future release
Expand Down
23 changes: 13 additions & 10 deletions mongoose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,28 @@ npm install @decorators/mongoose --save

### API
#### Functions
* **bootstrapMongoose(MongooseModel)** - Function to generate model for class.
* **bootstrapMongoose(MongooseModel || Injectable)** - Function to generate model for class, where Injectable:
```typescript
{ provide: MongooseModel, deps: [UserService] }
```
* **ref(collectionRef)** - helper function to define reference to another collection/model
* **ModelClass** - interface provides all properties and functions to the class

#### Decorators
##### Class
* @Model(name: string, options?: SchemaTypeOpts)
* **@Model(name: string, options?: SchemaTypeOpts)** - registers model with defined name and options

##### Method
* @Static()
* @Query()
* @Instance()
* @Virtual()
* **@Static()** - registers static method
* **@Query()** - registers query
* **@Instance()** - registers instance method
* **@Virtual()** - registers virtual property

##### Property
* @SchemaField(schemaFieldDefinition)
* @Static()
* @Index()
* @Set() = @Option() (*deprecated* Use options parameter of Model decorator instead)
* **@SchemaField(schemaFieldDefinition)** - registers schema field
* **@Static()** - registers static property
* **@Index()** - registers index property
* **@Set()** = **@Option()** (*deprecated* Use options parameter of Model decorator instead)

### Example Mongoose Model
```typescript
Expand Down
2 changes: 1 addition & 1 deletion mongoose/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@decorators/mongoose",
"version": "1.1.2",
"version": "1.2.0",
"description": "node decorators",
"main": "index.js",
"dependencies": {
Expand Down
11 changes: 9 additions & 2 deletions types/mongoose.d.ts → mongoose/src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
interface MongooseMeta {
export interface MongooseMeta {
name: string;
schema: any;
statics: [ [string, Function] | string ];
Expand All @@ -9,6 +9,13 @@ interface MongooseMeta {
options: string[];
}

interface MongooseClass extends Object {
export interface MongooseClass extends Object {
__meta__: MongooseMeta;

new (...deps: any[]);
}

export interface Injectable {
provide: Function;
deps: any[];
}
2 changes: 2 additions & 0 deletions mongoose/src/meta.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { MongooseClass, MongooseMeta } from './interfaces';

export function getMongooseMeta(target: MongooseClass): MongooseMeta {
if (!target.__meta__) {
target.__meta__ = <MongooseMeta> {
Expand Down
19 changes: 12 additions & 7 deletions mongoose/src/mongoose.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Schema, model as Model, Model as IModel, Document } from 'mongoose';

import { MongooseClass, Injectable, MongooseMeta } from './interfaces';
import { getMongooseMeta, extend } from './meta';

/**
Expand All @@ -15,13 +17,16 @@ export function ref(collectionRef: string): { type: any, ref: string } {
* @param DecoratedClass
* @returns {Object} Mongoose model itself
*/
export function bootstrapMongoose<T extends Document>(DecoratedClass): IModel<T> {
let meta: MongooseMeta = getMongooseMeta(DecoratedClass.prototype),
classInstance = new DecoratedClass(),
schema: Schema = new Schema(meta.schema),
statics = {},
indexes = {},
model;
export function bootstrapMongoose<T extends Document>(injectable: Injectable | Function): IModel<T> {
let DecoratedModel: any = (<Injectable>injectable).provide || <MongooseClass>injectable;
let deps = (<Injectable>injectable).deps || [];

let meta: MongooseMeta = getMongooseMeta(DecoratedModel.prototype);
let classInstance = new DecoratedModel(...deps);
let schema: Schema = new Schema(meta.schema);
let statics = {};
let indexes = {};
let model;

meta.statics.forEach((stat: [string, Function] | string) => {
if (typeof stat[1] === 'function') {
Expand Down
11 changes: 9 additions & 2 deletions playground/mongoose/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ interface TestModelType extends mongoose.Model<TestInstance> {
}

@Model('Test')
class TestModelClass extends ModelClass {
class TestModelClass extends ModelClass {

@SchemaField(String)
testField: string;

constructor(...args) {
super();
console.log(args);
}

@Static()
staticMethod() {
console.log('static test method');
Expand All @@ -46,4 +51,6 @@ interface TestModelType extends mongoose.Model<TestInstance> {

}

export let TestModel = <TestModelType>bootstrapMongoose(TestModelClass);
export let TestModel = <TestModelType>bootstrapMongoose({
provide: TestModelClass, deps: [1, 2, 3]
});

0 comments on commit 81d884a

Please sign in to comment.