-
-
Notifications
You must be signed in to change notification settings - Fork 518
New issue
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
Strict partial loading #3443
Comments
Just a note for anybody who would like to give this a shot - this needs to be about the FYI there was someone few months ago on slack trying to implement this, and ended up with quite breaking solution. We might need to wait for v6 with something like this. |
I managed to hack initial PoC for this, and to make the property access fail, we would have to get rid of the intersection with the entity type ( What I can do relatively easily is to type such not-selected properties to |
I think I found a way to make this work with actual property omission. It has its own quirks, but generally should work and should be worth it. The only real downside is that the partial loading will need to work the same as populate hints - as a simple array of relation names (with nested dot path support). I think that's a small feature to sacrifice when you compare it to the added strictness of the loaded result. Still have to crack the nested partial loading support, as well as fix many TS errors in the codebase caused by the changes, but I feel more confident this will land in v6. edit: last challenge - combination of partial loading and populate hints edit2: it is happening! |
The `Loaded` type is now improved to support the partial loading hints (`fields` option). When used, the returned type will only allow accessing selected properties. Primary keys are automatically selected. ```ts // book is typed to `Selected<Book, 'author', 'title' | 'author.email'>` const book = await em.findOneOrFail(Book, 1, { fields: ['title', 'author.email'], populate: ['author'], }); const id = book.id; // ok, PK is selected automatically const title = book.title; // ok, title is selected const publisher = book.publisher; // fail, not selected const author = book.author.id; // ok, PK is selected automatically const email = book.author.email; // ok, selected const name = book.author.name; // fail, not selected ``` BREAKING CHANGE: `FindOptions.fields` now accepts only array of strings, like `populate`. Closes #3443
The `Loaded` type is now improved to support the partial loading hints (`fields` option). When used, the returned type will only allow accessing selected properties. Primary keys are automatically selected. ```ts // book is typed to `Selected<Book, 'author', 'title' | 'author.email'>` const book = await em.findOneOrFail(Book, 1, { fields: ['title', 'author.email'], populate: ['author'], }); const id = book.id; // ok, PK is selected automatically const title = book.title; // ok, title is selected const publisher = book.publisher; // fail, not selected const author = book.author.id; // ok, PK is selected automatically const email = book.author.email; // ok, selected const name = book.author.name; // fail, not selected ``` BREAKING CHANGE: `FindOptions.fields` now accepts only array of strings, like `populate`. Closes #3443
Closing as implemented via #4092, let's test this! |
woot! thanks @B4nan, will test out soon. have another 50/month :) |
The `Loaded` type is now improved to support the partial loading hints (`fields` option). When used, the returned type will only allow accessing selected properties. Primary keys are automatically selected. ```ts // book is typed to `Selected<Book, 'author', 'title' | 'author.email'>` const book = await em.findOneOrFail(Book, 1, { fields: ['title', 'author.email'], populate: ['author'], }); const id = book.id; // ok, PK is selected automatically const title = book.title; // ok, title is selected const publisher = book.publisher; // fail, not selected const author = book.author.id; // ok, PK is selected automatically const email = book.author.email; // ok, selected const name = book.author.name; // fail, not selected ``` BREAKING CHANGE: `FindOptions.fields` now accepts only array of strings, like `populate`. Closes #3443
The `Loaded` type is now improved to support the partial loading hints (`fields` option). When used, the returned type will only allow accessing selected properties. Primary keys are automatically selected. ```ts // book is typed to `Selected<Book, 'author', 'title' | 'author.email'>` const book = await em.findOneOrFail(Book, 1, { fields: ['title', 'author.email'], populate: ['author'], }); const id = book.id; // ok, PK is selected automatically const title = book.title; // ok, title is selected const publisher = book.publisher; // fail, not selected const author = book.author.id; // ok, PK is selected automatically const email = book.author.email; // ok, selected const name = book.author.name; // fail, not selected ``` BREAKING CHANGE: `FindOptions.fields` now accepts only array of strings, like `populate`. Closes #3443
The `Loaded` type is now improved to support the partial loading hints (`fields` option). When used, the returned type will only allow accessing selected properties. Primary keys are automatically selected. ```ts // book is typed to `Selected<Book, 'author', 'title' | 'author.email'>` const book = await em.findOneOrFail(Book, 1, { fields: ['title', 'author.email'], populate: ['author'], }); const id = book.id; // ok, PK is selected automatically const title = book.title; // ok, title is selected const publisher = book.publisher; // fail, not selected const author = book.author.id; // ok, PK is selected automatically const email = book.author.email; // ok, selected const name = book.author.name; // fail, not selected ``` BREAKING CHANGE: `FindOptions.fields` now accepts only array of strings, like `populate`. Closes #3443
The `Loaded` type is now improved to support the partial loading hints (`fields` option). When used, the returned type will only allow accessing selected properties. Primary keys are automatically selected. ```ts // book is typed to `Selected<Book, 'author', 'title' | 'author.email'>` const book = await em.findOneOrFail(Book, 1, { fields: ['title', 'author.email'], populate: ['author'], }); const id = book.id; // ok, PK is selected automatically const title = book.title; // ok, title is selected const publisher = book.publisher; // fail, not selected const author = book.author.id; // ok, PK is selected automatically const email = book.author.email; // ok, selected const name = book.author.name; // fail, not selected ``` BREAKING CHANGE: `FindOptions.fields` now accepts only array of strings, like `populate`. Closes #3443
The `Loaded` type is now improved to support the partial loading hints (`fields` option). When used, the returned type will only allow accessing selected properties. Primary keys are automatically selected. ```ts // book is typed to `Selected<Book, 'author', 'title' | 'author.email'>` const book = await em.findOneOrFail(Book, 1, { fields: ['title', 'author.email'], populate: ['author'], }); const id = book.id; // ok, PK is selected automatically const title = book.title; // ok, title is selected const publisher = book.publisher; // fail, not selected const author = book.author.id; // ok, PK is selected automatically const email = book.author.email; // ok, selected const name = book.author.name; // fail, not selected ``` BREAKING CHANGE: `FindOptions.fields` now accepts only array of strings, like `populate`. Closes #3443
The `Loaded` type is now improved to support the partial loading hints (`fields` option). When used, the returned type will only allow accessing selected properties. Primary keys are automatically selected. ```ts // book is typed to `Selected<Book, 'author', 'title' | 'author.email'>` const book = await em.findOneOrFail(Book, 1, { fields: ['title', 'author.email'], populate: ['author'], }); const id = book.id; // ok, PK is selected automatically const title = book.title; // ok, title is selected const publisher = book.publisher; // fail, not selected const author = book.author.id; // ok, PK is selected automatically const email = book.author.email; // ok, selected const name = book.author.name; // fail, not selected ``` BREAKING CHANGE: `FindOptions.fields` now accepts only array of strings, like `populate`. Closes #3443
The `Loaded` type is now improved to support the partial loading hints (`fields` option). When used, the returned type will only allow accessing selected properties. Primary keys are automatically selected. ```ts // book is typed to `Selected<Book, 'author', 'title' | 'author.email'>` const book = await em.findOneOrFail(Book, 1, { fields: ['title', 'author.email'], populate: ['author'], }); const id = book.id; // ok, PK is selected automatically const title = book.title; // ok, title is selected const publisher = book.publisher; // fail, not selected const author = book.author.id; // ok, PK is selected automatically const email = book.author.email; // ok, selected const name = book.author.name; // fail, not selected ``` BREAKING CHANGE: `FindOptions.fields` now accepts only array of strings, like `populate`. Closes #3443
The `Loaded` type is now improved to support the partial loading hints (`fields` option). When used, the returned type will only allow accessing selected properties. Primary keys are automatically selected. ```ts // book is typed to `Selected<Book, 'author', 'title' | 'author.email'>` const book = await em.findOneOrFail(Book, 1, { fields: ['title', 'author.email'], populate: ['author'], }); const id = book.id; // ok, PK is selected automatically const title = book.title; // ok, title is selected const publisher = book.publisher; // fail, not selected const author = book.author.id; // ok, PK is selected automatically const email = book.author.email; // ok, selected const name = book.author.name; // fail, not selected ``` BREAKING CHANGE: `FindOptions.fields` now accepts only array of strings, like `populate`. Closes #3443
The `Loaded` type is now improved to support the partial loading hints (`fields` option). When used, the returned type will only allow accessing selected properties. Primary keys are automatically selected. ```ts // book is typed to `Selected<Book, 'author', 'title' | 'author.email'>` const book = await em.findOneOrFail(Book, 1, { fields: ['title', 'author.email'], populate: ['author'], }); const id = book.id; // ok, PK is selected automatically const title = book.title; // ok, title is selected const publisher = book.publisher; // fail, not selected const author = book.author.id; // ok, PK is selected automatically const email = book.author.email; // ok, selected const name = book.author.name; // fail, not selected ``` BREAKING CHANGE: `FindOptions.fields` now accepts only array of strings, like `populate`. Closes #3443
The `Loaded` type is now improved to support the partial loading hints (`fields` option). When used, the returned type will only allow accessing selected properties. Primary keys are automatically selected. ```ts // book is typed to `Selected<Book, 'author', 'title' | 'author.email'>` const book = await em.findOneOrFail(Book, 1, { fields: ['title', 'author.email'], populate: ['author'], }); const id = book.id; // ok, PK is selected automatically const title = book.title; // ok, title is selected const publisher = book.publisher; // fail, not selected const author = book.author.id; // ok, PK is selected automatically const email = book.author.email; // ok, selected const name = book.author.name; // fail, not selected ``` BREAKING CHANGE: `FindOptions.fields` now accepts only array of strings, like `populate`. Closes #3443
The `Loaded` type is now improved to support the partial loading hints (`fields` option). When used, the returned type will only allow accessing selected properties. Primary keys are automatically selected. ```ts // book is typed to `Selected<Book, 'author', 'title' | 'author.email'>` const book = await em.findOneOrFail(Book, 1, { fields: ['title', 'author.email'], populate: ['author'], }); const id = book.id; // ok, PK is selected automatically const title = book.title; // ok, title is selected const publisher = book.publisher; // fail, not selected const author = book.author.id; // ok, PK is selected automatically const email = book.author.email; // ok, selected const name = book.author.name; // fail, not selected ``` BREAKING CHANGE: `FindOptions.fields` now accepts only array of strings, like `populate`. Closes #3443
The `Loaded` type is now improved to support the partial loading hints (`fields` option). When used, the returned type will only allow accessing selected properties. Primary keys are automatically selected. ```ts // book is typed to `Selected<Book, 'author', 'title' | 'author.email'>` const book = await em.findOneOrFail(Book, 1, { fields: ['title', 'author.email'], populate: ['author'], }); const id = book.id; // ok, PK is selected automatically const title = book.title; // ok, title is selected const publisher = book.publisher; // fail, not selected const author = book.author.id; // ok, PK is selected automatically const email = book.author.email; // ok, selected const name = book.author.name; // fail, not selected ``` BREAKING CHANGE: `FindOptions.fields` now accepts only array of strings, like `populate`. Closes #3443
The `Loaded` type is now improved to support the partial loading hints (`fields` option). When used, the returned type will only allow accessing selected properties. Primary keys are automatically selected. ```ts // book is typed to `Selected<Book, 'author', 'title' | 'author.email'>` const book = await em.findOneOrFail(Book, 1, { fields: ['title', 'author.email'], populate: ['author'], }); const id = book.id; // ok, PK is selected automatically const title = book.title; // ok, title is selected const publisher = book.publisher; // fail, not selected const author = book.author.id; // ok, PK is selected automatically const email = book.author.email; // ok, selected const name = book.author.name; // fail, not selected ``` BREAKING CHANGE: `FindOptions.fields` now accepts only array of strings, like `populate`. Closes #3443
The `Loaded` type is now improved to support the partial loading hints (`fields` option). When used, the returned type will only allow accessing selected properties. Primary keys are automatically selected. ```ts // book is typed to `Selected<Book, 'author', 'title' | 'author.email'>` const book = await em.findOneOrFail(Book, 1, { fields: ['title', 'author.email'], populate: ['author'], }); const id = book.id; // ok, PK is selected automatically const title = book.title; // ok, title is selected const publisher = book.publisher; // fail, not selected const author = book.author.id; // ok, PK is selected automatically const email = book.author.email; // ok, selected const name = book.author.name; // fail, not selected ``` BREAKING CHANGE: `FindOptions.fields` now accepts only array of strings, like `populate`. Closes #3443
The `Loaded` type is now improved to support the partial loading hints (`fields` option). When used, the returned type will only allow accessing selected properties. Primary keys are automatically selected. ```ts // book is typed to `Selected<Book, 'author', 'title' | 'author.email'>` const book = await em.findOneOrFail(Book, 1, { fields: ['title', 'author.email'], populate: ['author'], }); const id = book.id; // ok, PK is selected automatically const title = book.title; // ok, title is selected const publisher = book.publisher; // fail, not selected const author = book.author.id; // ok, PK is selected automatically const email = book.author.email; // ok, selected const name = book.author.name; // fail, not selected ``` BREAKING CHANGE: `FindOptions.fields` now accepts only array of strings, like `populate`. Closes #3443
The `Loaded` type is now improved to support the partial loading hints (`fields` option). When used, the returned type will only allow accessing selected properties. Primary keys are automatically selected. ```ts // book is typed to `Selected<Book, 'author', 'title' | 'author.email'>` const book = await em.findOneOrFail(Book, 1, { fields: ['title', 'author.email'], populate: ['author'], }); const id = book.id; // ok, PK is selected automatically const title = book.title; // ok, title is selected const publisher = book.publisher; // fail, not selected const author = book.author.id; // ok, PK is selected automatically const email = book.author.email; // ok, selected const name = book.author.name; // fail, not selected ``` BREAKING CHANGE: `FindOptions.fields` now accepts only array of strings, like `populate`. Closes #3443
The `Loaded` type is now improved to support the partial loading hints (`fields` option). When used, the returned type will only allow accessing selected properties. Primary keys are automatically selected. ```ts // book is typed to `Selected<Book, 'author', 'title' | 'author.email'>` const book = await em.findOneOrFail(Book, 1, { fields: ['title', 'author.email'], populate: ['author'], }); const id = book.id; // ok, PK is selected automatically const title = book.title; // ok, title is selected const publisher = book.publisher; // fail, not selected const author = book.author.id; // ok, PK is selected automatically const email = book.author.email; // ok, selected const name = book.author.name; // fail, not selected ``` BREAKING CHANGE: `FindOptions.fields` now accepts only array of strings, like `populate`. Closes #3443
The `Loaded` type is now improved to support the partial loading hints (`fields` option). When used, the returned type will only allow accessing selected properties. Primary keys are automatically selected. ```ts // book is typed to `Selected<Book, 'author', 'title' | 'author.email'>` const book = await em.findOneOrFail(Book, 1, { fields: ['title', 'author.email'], populate: ['author'], }); const id = book.id; // ok, PK is selected automatically const title = book.title; // ok, title is selected const publisher = book.publisher; // fail, not selected const author = book.author.id; // ok, PK is selected automatically const email = book.author.email; // ok, selected const name = book.author.name; // fail, not selected ``` BREAKING CHANGE: `FindOptions.fields` now accepts only array of strings, like `populate`. Closes #3443
The `Loaded` type is now improved to support the partial loading hints (`fields` option). When used, the returned type will only allow accessing selected properties. Primary keys are automatically selected. ```ts // book is typed to `Selected<Book, 'author', 'title' | 'author.email'>` const book = await em.findOneOrFail(Book, 1, { fields: ['title', 'author.email'], populate: ['author'], }); const id = book.id; // ok, PK is selected automatically const title = book.title; // ok, title is selected const publisher = book.publisher; // fail, not selected const author = book.author.id; // ok, PK is selected automatically const email = book.author.email; // ok, selected const name = book.author.name; // fail, not selected ``` BREAKING CHANGE: `FindOptions.fields` now accepts only array of strings, like `populate`. Closes #3443
The `Loaded` type is now improved to support the partial loading hints (`fields` option). When used, the returned type will only allow accessing selected properties. Primary keys are automatically selected. ```ts // book is typed to `Selected<Book, 'author', 'title' | 'author.email'>` const book = await em.findOneOrFail(Book, 1, { fields: ['title', 'author.email'], populate: ['author'], }); const id = book.id; // ok, PK is selected automatically const title = book.title; // ok, title is selected const publisher = book.publisher; // fail, not selected const author = book.author.id; // ok, PK is selected automatically const email = book.author.email; // ok, selected const name = book.author.name; // fail, not selected ``` BREAKING CHANGE: `FindOptions.fields` now accepts only array of strings, like `populate`. Closes #3443
Is your feature request related to a problem? Please describe.
When using with mongo, it is possible to "select" certain fields and return them, however the return type ends up being
Loaded<Entity, 'field1' | 'field2'>
and still allowing the developer to access unselected fields.Describe the solution you'd like
I'd like to see a new
Selected
type which would effectively be an Omit to make sure no one is accessing unqueried data.The text was updated successfully, but these errors were encountered: