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

Commit

Permalink
Mongoose 1.2.1 (#41)
Browse files Browse the repository at this point in the history
* fix(mongoose): Correct context for the model

* fix(mongoose): Changelog
  • Loading branch information
serhiisol authored Mar 7, 2017
1 parent 4ebd13f commit 4747393
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 46 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.1
* Fixed model ctx for DI

# Mongoose#1.2.0
* Model DI

Expand Down
21 changes: 11 additions & 10 deletions mongoose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,27 @@ npm install @decorators/mongoose --save
* **@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
import {
SchemaField, Model, bootstrapMongoose,
Static, Instance
} from 'node-decorators/mongoose';

@Model('Test')
class TestModelClass {
@Model('Animal')
class Animal extends ModelClass {
@SchemaField(String)
testField: string;
name: string;

@Static()
testMethod() {
spawn() {
}

@Instance()
instanceMethod() {
scream() {
}

@Query()
byName(name: string): Promise<Animal> {
return this.find({ name });
}
}

Expand Down
1 change: 0 additions & 1 deletion mongoose/index.ts

This file was deleted.

8 changes: 6 additions & 2 deletions mongoose/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{
"name": "@decorators/mongoose",
"version": "1.2.0",
"version": "1.2.1",
"description": "node decorators",
"main": "index.js",
"main": "src/index.js",
"dependencies": {
"mongoose": "^4.7.0"
},
"devDependencies": {
"@types/mongoose": "4.7.2",
"typescript": "2.1.1"
},
"keywords": [
"nodejs",
"mongoose",
Expand Down
11 changes: 10 additions & 1 deletion mongoose/src/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
export * from './schema';
export {
SchemaField,
Static,
Query,
Instance,
Virtual,
Index,
Set,
Option
} from './schema';
export { Model } from './model';
26 changes: 13 additions & 13 deletions mongoose/src/decorators/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@ import { getMongooseMeta } from '../meta';
* @param type Field type
*/
export function SchemaField(type: any): PropertyDecorator {
return (target: any, name:string) => {
return (target: any, name: string) => {
Object.assign(getMongooseMeta(target).schema, {[name]: type});
}
};
}

/**
* Defines static method or property
*/
export function Static() {
return (target:any, name:string, descriptor?:TypedPropertyDescriptor<any>) => {
return (target: any, name: string, descriptor?: TypedPropertyDescriptor<any>) => {
getMongooseMeta(target).statics.push(descriptor ? [name, target[name]] : name);
}
};
}

/**
* Defines query method
*/
export function Query() {
return (target: any, name:string, descriptor:TypedPropertyDescriptor<any>) => {
return (target: any, name: string, descriptor: TypedPropertyDescriptor<any>) => {
getMongooseMeta(target).queries.push([name, target[name]]);
}
};
}

/**
* Defines instance method
*/
export function Instance() {
return (target: any, name:string, descriptor:TypedPropertyDescriptor<any>) => {
return (target: any, name: string, descriptor: TypedPropertyDescriptor<any>) => {
getMongooseMeta(target).instances.push([name, target[name]]);
}
};
}

/**
Expand All @@ -43,27 +43,27 @@ export function Instance() {
export function Virtual() {
return (target: any, name: string, descriptor: PropertyDescriptor) => {
getMongooseMeta(target).virtuals.push([name, descriptor]);
}
};
}

/**
* Defines index
*/
export function Index() {
return (target: any, name:string) => {
return (target: any, name: string) => {
getMongooseMeta(target).indexes.push(name);
}
};
}

/**
* Defines set method - options for model
*/
export function Set() {
console.warn(`Deprecated: This decorator will be removed in a future release.
console.warn(`Deprecated: This decorator will be removed in a future release.
Use the options parameter of the Model decorator instead.`);
return (target: any, name: string) => {
getMongooseMeta(target).options.push(name);
}
};
}
/**
* Alias of Set
Expand Down
12 changes: 11 additions & 1 deletion mongoose/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
export * from './decorators';
export {
Model,
SchemaField,
Static,
Query,
Instance,
Virtual,
Index,
Set,
Option
} from './decorators';
export { bootstrapMongoose, ref } from './mongoose';
export { ModelClass } from './model-class';
41 changes: 28 additions & 13 deletions mongoose/src/mongoose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ export function ref(collectionRef: string): { type: any, ref: string } {
return { type: Schema.Types.ObjectId, ref: collectionRef };
}

/**
* Wrap function with correct context
*
* @param {Function} fn
* @param {any} instance
* @returns {Function}
*/
function wrapFunction(fn: Function, instance): Function {
return function(...args) {
const fullCtx = Object.assign({}, instance, this);
Object.setPrototypeOf(fullCtx, Object.getPrototypeOf(this));

return fn.apply(fullCtx, args);
};
}

/**
* Bootstrap decorated class to native mongoose
* @param DecoratedClass
Expand All @@ -30,27 +46,26 @@ export function bootstrapMongoose<T extends Document>(injectable: Injectable | F

meta.statics.forEach((stat: [string, Function] | string) => {
if (typeof stat[1] === 'function') {
schema.statics[<string>stat[0]] = <Function>stat[1]
} else {
statics[<string>stat] = classInstance[<string>stat];
return schema.statics[<string>stat[0]] = wrapFunction(<Function>stat[1], classInstance);
}
statics[<string>stat] = classInstance[<string>stat];
});

meta.queries.forEach((query: [string, Function]) => {
schema['query'][query[0]] = query[1];
meta.queries.forEach(([name, fn]: [string, Function]) => {
schema['query'][name] = wrapFunction(fn, classInstance);
});

meta.instances.forEach((instance: [string, Function]) => {
schema.methods[instance[0]] = instance[1];
meta.instances.forEach(([name, fn]: [string, Function]) => {
schema.methods[name] = wrapFunction(fn, classInstance);
});

meta.virtuals.forEach((virtual: [string, PropertyDescriptor]) => {
let v = schema.virtual(virtual[0]);
if (virtual[1].get) {
v.get(virtual[1].get);
meta.virtuals.forEach(([name, descriptor]: [string, PropertyDescriptor]) => {
let v = schema.virtual(name);
if (descriptor.get) {
v.get(wrapFunction(descriptor.get, classInstance));
}
if (virtual[1].set) {
v.set(virtual[1].set);
if (descriptor.set) {
v.set(wrapFunction(descriptor.set, classInstance));
}
});

Expand Down
22 changes: 22 additions & 0 deletions mongoose/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compileOnSave": false,
"compilerOptions": {
"declaration": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"module": "commonjs",
"moduleResolution": "node",
"noEmitOnError": true,
"noImplicitAny": false,
"rootDir": ".",
"sourceMap": true,
"target": "es6",
"inlineSources": true,
"typeRoots": ["node_modules/@types"],
"pretty": true
},
"exclude": ["node_modules"],
"files": [
"src/index.ts"
]
}
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
"dependencies": {
"co": "4.6.0",
"express": "4.14.0",
"mongoose": "4.7.0",
"socket.io": "1.7.2"
},
"devDependencies": {
"@types/express": "4.0.34",
"@types/mongoose": "4.7.2",
"@types/socket.io": "1.4.27",
"typescript": "2.1.1"
},
Expand Down
5 changes: 5 additions & 0 deletions playground/mongoose/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { TestModel } from './model';
TestModel.staticMethod();
let test = new TestModel({testField: "Hello World"});
console.log('Model = ' + test.toString());
test.setField();

test.instanceMethod();

console.log('Model = ' + test.testField);

process.exit();

14 changes: 12 additions & 2 deletions playground/mongoose/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mongoose.connect('192.168.99.100:27017/test', {
interface TestInstance extends mongoose.Document {
testField: string;
instanceMethod();
setField();
}

interface TestModelType extends mongoose.Model<TestInstance> {
Expand All @@ -33,19 +34,28 @@ class TestModelClass extends ModelClass {
@SchemaField(String)
testField: string;

args: any;

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

@Static()
staticMethod() {
console.log('static test method');
}

@Instance()
setField() {
this.testField = 'World';
console.log(this.testField, this.args);
}

@Instance()
instanceMethod() {
console.log(this.testField);
console.log(this.testField, this.args);

this.save();
}

Expand Down
3 changes: 3 additions & 0 deletions playground/mongoose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"dependencies": {
"@decorators/mongoose": "../../mongoose"
},
"devDependencies": {
"@types/mongoose": "4.7.2"
},
"keywords": [
"nodejs",
"socket.io",
Expand Down
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"files": [
"co/index.ts",
"express/index.ts",
"mongoose/index.ts",
"socket/index.ts"
]
}

0 comments on commit 4747393

Please sign in to comment.