Skip to content

Commit

Permalink
Added the @transient decorator.
Browse files Browse the repository at this point in the history
  • Loading branch information
meirgottlieb committed Mar 29, 2016
1 parent 9e48098 commit af8113d
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs
Submodule docs updated 3 files
+1 −1 assets/js/search.js
+38 −0 globals.html
+3 −0 index.html
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hydrate-mongodb",
"description": "An Object Document Mapper (ODM) for MongoDB.",
"version": "0.2.22",
"version": "0.2.23",
"author": {
"name": "Artifact Health, LLC",
"url": "http://www.artifacthealth.com"
Expand Down
18 changes: 17 additions & 1 deletion src/mapping/providers/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,4 +796,20 @@ function getParentMapping(context: MappingBuilderContext, type: Type): MappingMo
return <MappingModel.ClassMapping>builder.mapping;
}
}
}
}

/**
* @hidden
*/
export class TransientAnnotation implements PropertyAnnotation {

toString(): string {
return "@Transient";
}

processPropertyAnnotation(context: MappingBuilderContext, mapping: MappingModel.ObjectMapping, property: MappingModel.Property, symbol: Property, annotation: Annotation): void {

property.field = null;
property.setFlags(MappingModel.PropertyFlags.Ignored);
}
}
24 changes: 23 additions & 1 deletion src/mapping/providers/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
PreUpdateAnnotation,
PostUpdateAnnotation,
PreRemoveAnnotation,
PostRemoveAnnotation
PostRemoveAnnotation, TransientAnnotation
} from "./annotations";

import {PropertyConverter} from "../mappingModel";
Expand Down Expand Up @@ -638,6 +638,27 @@ export declare function PreRemove(): PropertyDecorator;
*/
export declare function PostRemove(): PropertyDecorator;

/**
* Specifies that a property should not be persisted.
*
* Properties are mapped to persistent document fields on an opt-in basis. *All properties that are decorated are
* mapped.* However, if you wish to prevent a decorated property from being mapped, decorate that property with the
* Transient decorator as demonstrated below.
*
* ### Example
*
* ```typescript
*  @Entity()
* export class User {
*
*  @Transient()
*  @SomeOtherDecorator()
* private _something: string;
* }
* ```
*/
export declare function Transient(): PropertyDecorator;


exports.Entity = makeDecorator(EntityAnnotation);
exports.Embeddable = makeDecorator(EmbeddableAnnotation);
Expand All @@ -664,5 +685,6 @@ exports.PreUpdate = makeDecorator(PreUpdateAnnotation);
exports.PostUpdate = makeDecorator(PostUpdateAnnotation);
exports.PreRemove = makeDecorator(PreRemoveAnnotation);
exports.PostRemove = makeDecorator(PostRemoveAnnotation);
exports.Transient = makeDecorator(TransientAnnotation);


8 changes: 8 additions & 0 deletions tests/fixtures/annotations/transient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Entity, Transient } from "../../../src/mapping/providers/decorators";

@Entity()
export class A {

@Transient()
a: string;
}
24 changes: 24 additions & 0 deletions tests/mapping/providers/annotationMappingProvider.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,30 @@ describe('AnnotationMappingProvider', () => {
});
});
});

describe('@transient', () => {

it("sets property as ignored", (done) => {

getTransientFieldMapping(done, (property) => assert.isTrue(property.hasFlags(MappingModel.PropertyFlags.Ignored)));
});

it("sets field name to null.", (done) => {

getTransientFieldMapping(done, (property) => assert.isNull(property.field));
});

function getTransientFieldMapping(done: Callback, callback: (mapping: Property) => void): void {

processFixture("transient", done, (results) => {

var classMapping = findMapping(results, "A");
var property = classMapping.getProperty("a");
assert.ok(property, "Unable to get property 'a' on class 'A' in fixture 'transient'.");
callback(property);
});
}
});
});
});

Expand Down

0 comments on commit af8113d

Please sign in to comment.