Code Monkey home page Code Monkey logo

Comments (2)

tanner0101 avatar tanner0101 commented on May 15, 2024

Transferring this to FluentKit since that's where the code would live for such a feature.

It should be possible to declare field defaults in Fluent 4, which could be an improvement to how we handle timestamps. Those types of properties could be made to only support reading. But, I'm not sure how write-once properties would work. If you need to be able to write the property ever (even if only once), then the property needs to be writable. There's not really a way to tell the compiler to only make it writable once.

To give some context, this is how setting a read only property w/ a default could work:

struct Planet: Model {
    let id = Field<Int>("id")
    let name = Field<String>("name")
    let createdAt = ReadOnlyField<Date>("createdAt", default: .currentDate)
}

let planet: Row<Planet> // from DB
try planet.get(\.createdAt) // Date
try planet.set(\.createdAt, to: ...) // Compiler error, ambiguous use of `set`

I'm not sure I love that API though since it's within the realm of possibility that you might need to update the createdAt field for whatever reason. It seems like it should be up to the developer whether to do that or not.

FWIW, ReadOnlyField could be declared entirely in user code without needing any internal Fluent support.

from fluent-kit.

MrMage avatar MrMage commented on May 15, 2024

In "regular" Swift, there is a way to declare write-once properties: let. My idea was similar; have a kind of property that doesn't expose a set method, with the only place to set its value in an initializer or when decoding from a database result set. However, I now see that set is (needs to, I guess) declared on the model itself, not on each individual field.

Explicit manual updates could still be done via a SQL UPDATE command, but by avoiding a set method we could avoid accidentally changing the corresponding property on the model and then calling save() with that changed property — again, similar to how let works.

from fluent-kit.

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.