We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Working with [email protected] on [email protected] and [email protected].
[email protected]
Four things here:
Date
Model.save
ObjectID
_id
Consider the following test:
import { ObjectId } from 'bson'; import { Collection, Core, Instance, Model, ObjectID, Property, Transform } from 'iridium'; import { expect } from 'chai'; import * as config from 'config'; import moment = require('moment'); process.env.NODE_ENV = 'test'; interface TestDocument { _id?:string; job:string|JobDocument; startDate:Date; } interface JobDocument { _id?:string; } @Collection('tests') class TestEntity extends Instance<TestDocument, TestEntity> implements TestDocument { @ObjectID _id:string; // No way to require this field // @Property(ObjectId, true) with a custom transform to/from ObjectId doesn't require it either @ObjectID job:string; // It'd also be nice if I didn't have to transform a date string @Property(Date) @Transform(d => d, d => moment(d).toDate()) startDate:Date; } describe('save/insert discrepancy', () => { const ctxt:Core = new Core(config.Database.connectionOptions.url), db:Model<TestDocument, TestEntity> = new Model<TestDocument, TestEntity>(ctxt, TestEntity); before(async () => { await ctxt.connect(); }); after((done) => { ctxt.db.dropDatabase().then(() => done()); }); describe('TestEntity', () => { it('does not run transforms using Model.save', async () => { try { const test = { job: '123123123123', startDate: '2018-05-05 22:39:50.362Z' }; const entity = new TestEntity(db, <any>test); await entity.save(); } catch(e) { const msg = [ 'Expected "123123123123" to be a valid MongoDB.ObjectID object', 'Expected startDate to be a date, but got "2018-05-05 22:39:50.362Z" instead' ].join('\n'); expect(e.message).to.eq(msg); } }); it('properly runs transforms using Model.insert', async () => { const test = { job: '123123123123', startDate: '2018-05-05 22:39:50.362Z' }; const res = (await db.insert(<any>test)).toJSON(); expect(res.job instanceof ObjectId).to.eq(true); expect(res.startDate instanceof Date).to.eq(true); }); it('does not convert ObjectID to string on retrieve', async () => { const test = { job: '123123123123', startDate: '2018-05-05 22:39:50.362Z' }; await db.insert(<any>test); const doc = (await db.findOne({ job: '123123123123' })).toJSON(); // Fails expect(typeof doc.job).to.eq('string'); }); it('does not require the job field, but I\'d sure like it to', async () => { const test = { startDate: '2018-05-05 22:39:50.362Z' }; const res = (await db.insert(<any>test)).toJSON(); expect(res.startDate instanceof Date).to.eq(true); }) }); });
The text was updated successfully, but these errors were encountered:
Regarding the @ObjectID decorator, there is actually a bug in their code.
@ObjectID
Here is where the bug is: https://github.com/SierraSoftworks/Iridium/blob/master/lib/Decorators.ts#L144
They simply need to change the order of the calls to the Property and Transform decorators and they will be good to go.
Property
Transform
Further, they might consider changing the decorator to something like below to allow the user to make the field required or not.
export function ObjectId(required = true) { return function(target, name) { Transform(DefaultTransforms.ObjectID.fromDB, DefaultTransforms.ObjectID.toDB)(target, name); Property(ObjectId, required)(target, name); }; }
Sorry, something went wrong.
It seems there is actually a larger issue with their @Property decorator not properly requiring an ObjectId but always allowing that to be optional...
@Property
ObjectId
No branches or pull requests
Working with
[email protected]
on[email protected]
and[email protected]
.Four things here:
Date
validator won't accept a valid date string. Is that intentional?Model.save
doesn't execute transforms. Is that also intentional?ObjectID
field other than_id
required.ObjectID
s are not converted to strings on retrievalConsider the following test:
The text was updated successfully, but these errors were encountered: