-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
426ad5f
commit cc14f1a
Showing
7 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/// <reference path="../_references.d.ts" /> | ||
import MongoDB = require('mongodb'); | ||
|
||
import Instance from './Instance'; | ||
import {Index, IndexSpecification} from './Index'; | ||
|
||
export function Index(spec: IndexSpecification, options?: MongoDB.IndexOptions) { | ||
return function(target: typeof Instance) { | ||
target.indexes = target.indexes || []; | ||
|
||
if (options) target.indexes.push(<Index>{ spec: spec, options: options }); | ||
else target.indexes.push(<Index>{ spec: spec }); | ||
} | ||
} | ||
|
||
export function Identifier(fromDB: (value: any) => any, toDB: (value: any) => any) { | ||
return function(target: typeof Instance) { | ||
target.identifier = { | ||
apply: fromDB, | ||
reverse: toDB | ||
}; | ||
} | ||
} | ||
|
||
export function Validate(type: any) { | ||
return function(target: typeof Instance, name: string, descriptor: any) { | ||
descriptor.validateType = type; | ||
|
||
return descriptor; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/// <reference path="../_references.d.ts" /> | ||
import * as Iridium from '../index'; | ||
|
||
interface TestDocument { | ||
_id?: string; | ||
name: string; | ||
email: string; | ||
} | ||
|
||
@Iridium.Identifier(x => x / 10, x => x * 10) | ||
@Iridium.Index({ name: 1 }) | ||
@Iridium.Index({ email: 1 }, { background: true }) | ||
class Test extends Iridium.Instance<TestDocument, Test> implements TestDocument { | ||
_id: string; | ||
name: string; | ||
email: string; | ||
} | ||
|
||
describe("Decorators", () => { | ||
describe("Index", () => { | ||
it("should populate the constructor's indexes property with index objects", () => { | ||
chai.expect(Test.indexes).to.exist.and.have.length(2); | ||
}); | ||
|
||
it("should support just spec indexes", () => { | ||
chai.expect(Test.indexes).to.containOneLike({ spec: { name: 1 } }); | ||
}); | ||
|
||
it("should support indexes with both a spec and options", () => { | ||
chai.expect(Test.indexes).to.containOneLike({ spec: { email: 1 }, options: { background: true }}); | ||
}); | ||
}); | ||
|
||
describe("Identifier", () => { | ||
it("should populate the constructor's identifier object", () => { | ||
chai.expect(Test.identifier).to.exist.and.have.property('apply').which.is.a('function'); | ||
chai.expect(Test.identifier).to.exist.and.have.property('reverse').which.is.a('function'); | ||
}); | ||
|
||
it("should pass along the correct functions", () => { | ||
chai.expect(Test.identifier.apply(10)).to.eql(1); | ||
chai.expect(Test.identifier.reverse(10)).to.eql(100); | ||
}); | ||
}); | ||
}); |