Giter Club home page Giter Club logo

Comments (3)

tanner0101 avatar tanner0101 commented on May 13, 2024

@Joannis mentioned bringing the cursor to Fluent. I think that would be a great idea. I'm going to start working on it later today and getting some of the basics set up with Mongo and SQLite. I will be able to better answer this question once I get back into it.

from fluent.

Evertt avatar Evertt commented on May 13, 2024

Anther thing we need to think about is relationships between models. How to find out which models are related to which, how to make the syntax as readable and intuitive as possible and how do we retrieve the related entities.

At first I was thinking that we could probably manage that by just using reflection on any entity and checking if any of its properties is another entity or an array of other entities. And yes I've tried it and that does work to discover what kind of relationships an entity has.

BUT, the problem is that we want to make relationships load lazily. So I thought about it some more and these are the things I can think of:

We could create a Relationship protocol and their implementations OneToOne<T: Entity, U: Entity>, OneToMany<T: Entity, U: Entity>, ManyToOne<T: Entity, U: Entity>, ManyToMany<T: Entity, U: Entity>. Or something similar to that. And then define entities like so:

class Post: Entity {
    var id: String?
    var title: String
    var body: String
    lazy var user: ManyToOne<Post, User> = ManyToOne(self, key: "user_id")

    required init(unboxer: Unboxer) {
        id    = unboxer.unbox("id")
        title = unboxer.unbox("title")
        body  = unboxer.unbox("body")
    }
}

class User: Entity {
    var id: String?
    var name: String
    lazy var posts: OneToMany<User, Post> = OneToMany(self, reverseKey: "user_id")

    required init(unboxer: Unboxer) {
        id    = unboxer.unbox("id")
        name  = unboxer.unbox("name")
    }
}

Although I don't find this the prettiest solution. Another solution I could think of, which results in better syntax, but also goes back to a more Active-Record style is the following:

// Entity.swift
extension Entity {
    func belongsTo<T: Entity>(_ entityType: T.Type) -> T {
        let key = String(entityType) // let's just use convention for determining the key
        return Query(entityType).someQueryToRetrieveRelatedEntity()
    }
}

// Post.swift
class Post: Entity {
    var id: String?
    var title: String
    var body: String
    lazy var user: User = self.belongsTo(User)

    required init(unboxer: Unboxer) {
        id    = unboxer.unbox("id")
        title = unboxer.unbox("title")
        body  = unboxer.unbox("body")
    }
}

That second one ends up in prettier code in Post, but it seems to mess up the separation of concern. Any thoughts?

Edit

Oh and now I just realized we'd also need to support eager loading and I just did a test and this works:

class Post: Entity {
    var id: String?
    var title: String
    var body: String
    lazy var user: User = self.belongsTo(User)

    required init(unboxer: Unboxer) {
        id    = unboxer.unbox("id")
        title = unboxer.unbox("title")
        body  = unboxer.unbox("body")

        if let user: User = unboxer.unbox("user") {
            self.user = user
            // Because we initialized user here, the belongsTo() method is not executed.
            // Which means we can support eager loading and avoid the n+1 problem! :-D
        }
    }
}

from fluent.

tanner0101 avatar tanner0101 commented on May 13, 2024

Mapping was added with Node.

from fluent.

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.