Code Monkey home page Code Monkey logo

Comments (6)

timkolotov avatar timkolotov commented on August 20, 2024 1

@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.

timkolotov avatar timkolotov commented on August 20, 2024

@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.

kettanaito avatar kettanaito commented on August 20, 2024

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. via update etc).

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.

timkolotov avatar timkolotov commented on August 20, 2024

Yep, I'm in. Currently, I just patched the types to shut the TS compiler up, which isn't really safe...

from data.

harry-gocity avatar harry-gocity commented on August 20, 2024

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.

timkolotov avatar timkolotov commented on August 20, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.