Comments (6)
@kettanaito finally finished the implementation, please take a look (especially check if the changes in tests make sense to you, some cases I just removed, because they were testing the behavior that doesn't exist anymore).
from data.
@kettanaito, I would like to hear your thoughts about it. If this makes sense, I'd implement this and create PR.
A simple use case is: we have a model Employee
with fields id, name, email, etc., and also a company which is oneOf
relation to the Company
model. Employee
can never exist without Company
so employee.company
may never be undefined, otherwise we'd use nullable(oneOf('Company'))
.
from data.
Hi, @timkolotov. This is a great proposal. The consistency you describe is the way to go:
oneOf()
relationships must have initial value. If not provided, the library should throw. If provided, use the current logic as-is;- If the consumer wishes to have a nullable one-of relationship, describe it explicitly via
nullable(oneOf(modelName))
.- If marked as nullable and no initial value was provided to the relationship, set its value as
null
. - If marked as nullable and initial value was provided to the relationship, set its value to the initially provided value.
- If marked as nullable, allow updating this relational property to
null
(i.e. viaupdate
etc).
- If marked as nullable and no initial value was provided to the relationship, set its value as
Most of this is already covered by nullable()
. The change would be to treat oneOf
relationship as non-nullable by default.
Does this sound interesting to you to give it a try? I think the change itself should be rather scoped. I can always help with the code review.
from data.
Yep, I'm in. Currently, I just patched the types to shut the TS compiler up, which isn't really safe...
from data.
Any update on this issue? Really interested in this as I think I've run into a similar problem. Not sure of the best way around this, with strict mode due to undefined
:
// My MSW handler response type
type BasketResponse = {
basketId: string,
currency: {
code: string,
symbol: string
}
}
export const db = factory({
basket: {
basketId: primaryKey(String),
currency: oneOf('currency'),
},
currency: {
code: primaryKey(String),
symbol: String,
}
})
const currency = db.currency.create()
const basket = db.basket.create({ currency });
type currencyType = typeof currency; // PublicEntity< ... >
type basketCurrencyType = typeof basket.currency; // PublicEntity< ... > | undefined
const value: BasketResponse = basket // 'Type 'undefined' is not assignable to type '{ code: string; symbol: string; }'.
This makes it a bit awkward to use the basket
pulled directly from the db with a MSW handler typed as rest.post<CreateBasketRequest, PathParams, BasketResponse>
from data.
@harry-gocity it does look like the same issue indeed. I started working on it but didn't have much time so far. With enough luck I'll have a vacation next week and will be able to dedicate time for this.
from data.
Related Issues (20)
- It's impossible to use case insensitive search via "where" statement HOT 1
- Can `findFirst()` support the `where` parameter as function? HOT 1
- TypeError: Class constructor GraphQLList cannot be invoked without 'new' HOT 3
- Can we have models with multi-fields IDs? HOT 1
- Can we store a literal object to an entity's field? HOT 8
- Using zod to generate table schema
- comparator missing for find by id
- Using wildcard/parameters in baseUrl in toHandlers method HOT 4
- Poor performance when creating linked entities
- Database ID is not unique when extracted to a setup helper file (using Vitest) HOT 4
- Help Creating Type-Safe Models HOT 2
- Support transient attributes HOT 5
- Library's state and future HOT 13
- manyOf relationship in nullable objects HOT 1
- pnpm resolves msw optional dependency as v2 instead of v1 HOT 4
- Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'get') while generating handlers HOT 1
- Add toHandlers('json-api') support
- REST toHandlers() PUT and POST do not work with @mswjs/http-middleware due to req.body being a string
- Add the ability to set a foreign key on a model to create relationships HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from data.