-
Notifications
You must be signed in to change notification settings - Fork 137
Allow refType to be defined in @prop and @arrayProp #369
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when these are resolved, could you squash some of your commits?
i would recommend reverting 00ddb93 and then rebase, not merge :)
Sure will do. vscode merge somehow messed up things, will try another way, sorry |
i will review the changed in 60aa3ca tomorrow |
ok, you might want to look at ab91ee0 instead |
Btw though it’s working, I think Ref is now a bit too liberal. To be 100% correct, in Ref<R,T>, T should be R or the type of the designated id field of R. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i would request setting the types to all lowercase to keep everything in style and not "string" and "Buffer" just to write it easy
PS: sorry that it took so long, my Desktop Environment just kept crashing
PPS: could you please squash all your commits? (git rebase -i master
)
and try to reword your commits to comply with CONTRIBUTING.md (if not, i will at the merge squash & reword it)
How about now? |
yes, would be a better idea, only that "I merged Matt's changes" doesnt not seem to be actually merged (its still string) |
OK, so here c0ab190 is how I would do it. I think it's much cleaner this way. What do you think? |
I noticed after some testing, that if i do const test = new RefTestModel({ refFieldString: new RefTestStringModel() }); // just as an example
if (isDocument(test.refFieldString)) {
test.refFieldString // i mean here
} the type of |
got it fixed, please modify typeguards.ts: export function isDocument<T, S extends RefType>(doc: Ref<T, S>): doc is InstanceType<T> { and export function isDocumentArray<T, S extends RefType>(docs: Ref<T, S>[]): docs is InstanceType<T>[] { and if possible, add a test for it (just to check if the types are not brocken) (example for the tests: const test = new RefTestModel({ refArrayString: [new RefTestStringModel()] });
if (isDocumentArray(test.refArrayString)) {
test.refArrayString;
console.log('in if isDocument');
} else {
console.log('in else isDocument');
}
const test2 = new RefTestModel({ refFieldString: new RefTestStringModel() });
if (isDocument(test2.refFieldString)) {
test2.refFieldString;
console.log('in if isDocument');
} else {
console.log('in else isDocument');
} but please note that this only the thing i used to get it working, please modify it so that it uses expect and throw) |
I implemented all the changes and added the test. That actually brought up this: But would this change break compatibility? |
a mongoose issue explaining the difference (for further readers) Automattic/mongoose#1671 so yes, it would be more right to use so i would say, yes it would be good to keep it so |
This: |
ok, then lets keep it at |
Then its done ;-) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything is and looks good, just some small style changes
Could you check something for me? I am putting together the test you requested above. When running the below snippet, the created RefTestModel has both refFieldString and refFieldNumber fields created with the corresponding ID.
|
By using create Similarly strange:
puts both IDs to the field correctly but only creates the first document in the database so populate returns an array with 1 item. Should it even work? |
typegoose nowhere sets a race condition, at least not knowingly could you push it to another branch so i can check it out? |
sorry, cant confirm that, for me everything gets saved const { _id: populatedId } = await RefTestModel.create({
refField: new RefTestModel(), // gets saved
refArray: [new RefTestModel(), new RefTestModel()], // gets saved
refFieldString: new RefTestStringModel({ _id: 'test1' }), // gets saved
refArrayString: [
new RefTestStringModel({ _id: 'test2' }), // gets saved
new RefTestStringModel({ _id: 'test3' }) // gets saved
],
refFieldNumber: new RefTestNumberModel({ _id: 1234 }), // gets saved
refArrayNumber: [new RefTestNumberModel({ _id: 5678 }), new RefTestNumberModel({ _id: 9876 })], // gets saved
refFieldBuffer: new RefTestBufferModel({ _id: Buffer.from([1, 2, 3, 4]) }), // gets saved
refArrayBuffer: [
new RefTestBufferModel({ _id: Buffer.from([5, 6, 7, 8]) }), // gets saved
new RefTestBufferModel({ _id: Buffer.from([9, 8, 7, 6]) }) // gets saved
]
}); db document: {
"_id": "5d4733adb736993b51db6a45",
"refArray": [
"5d4733adb736993b51db6a43",
"5d4733adb736993b51db6a44"
],
"refArray2": [],
"refArrayString": [
"test2",
"test3"
],
"refArrayString2": [],
"refArrayNumber": [
5678,
9876
],
"refArrayNumber2": [],
"refArrayBuffer": [
"BQYHCA==",
"CQgHBg=="
],
"refArrayBuffer2": [],
"refField": "5d4733adb736993b51db6a42",
"refFieldString": "test1",
"refFieldNumber": 1234,
"refFieldBuffer": "AQIDBA==",
"__v": 0
} (Note: mongodb compass exported it as json, so example buffer got converted to string, but i assure you, everything is saved how it should) |
Do tests run without an error on the reftests branch for you? |
Oh yes, the root document is correctly created. But do the referenced documents (eg. the one with id 5678) all exist? |
no thests do not run, because not populated properly
i dont have any with the id with |
currently it fails at |
yes and that means populate can't find the foreign document with that id |
i can confirm that Field |
Ok, I will dig deeper tomorrow to see what’s going on. If the documents are there and the ids are there then populate should work. |
got it further with const pop = Object.keys(RefTestModel.schema.obj);
const foundPopulated = await RefTestModel.findById(populatedId).populate(pop).exec(); its still the same error, only for will investigate further Update: Update2: Update3: |
First thing I will try in the morning is the same with native mongoose (and a real mongodb instance) |
Update(4): TL;DR: what i said was wrong, no new refs are actually saved Edit: |
OK, so it is not supposed to work. Then await create() will do for tests, give me a minute |
|
i see no problem anymore, only resolve the last review, squash your commits, and i will approve 👍 |
…kodiakos#181) - added @prop({ refType }) - added @arrayProp({ itemsRefType }) - added Ref<R, T> to define Ref types with string, number of buffer id - Use mongoose.Schema.Types as refType - changed Ref<> value to mongoose.Types.ObjectId - added refType support to IsDocument and isDocumentArray typeguards - added tests
There you go 189a6ce And thanks for all :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything got resolved, everything conforms with the Coding Style, Commits are structured right 👍
@Ben305 (if you are back) i would want to know if this should be in release 6.0.0 or release 5.10.0? |
Hi! Sorry, I was out for a bit. Actually I would appreciate this before v6. I am planning to port some of our proprietary sw to Typegoose and this was one missing feature. (There might be more but then you’ll see a PR about them anyways :) ). Unfortunately this project is now iced for a few days but I’ll be back. |
which project? this pr is finished an ready to go :) (ben is out for a week) |
The one where I am porting stuff to run on Typegoose :) |
Merged it for now into 6.0.0(-19) 27f5d59 Edit / Update: |
this can be closed because it is included in v6.0.0, and will not be included a v5.x release |
Merged changes from pr #149, solved conflicts, added itemsRefType (works the same as refType) to @arrayProps and tests