Comments (3)
@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.
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.
Mapping was added with Node.
from fluent.
Related Issues (20)
- How to export data to a file HOT 1
- ID Field (UUID) not automatically generating HOT 1
- How to compare Date? HOT 21
- jknnnn
- Extend ModelCredentialsAuthenticatable with async authenticator
- Model middleware lifecycle event delete not called on siblings detach and detachAll HOT 2
- Error on delete when joining a table due to missing join operation in query
- Does Fluent need to depend on Vapor itself? HOT 2
- Unable to specify which field to filter on within a self-relating sibling HOT 2
- Concurrency Swift 5.7 - Any plans to include AsyncMap HOT 3
- Missing Inline Documentation
- OptionalParent is not so optional HOT 2
- Where is .alsoDecode ? HOT 1
- Support iOS platform missing
- Support reverting a migration on a specific database
- server: column "id" specified more than once (checkInsertTargets) HOT 3
- Fluent Session driver doesn't provide any way to remove old sessions HOT 3
- receive different sql statements, for equal valid fluent ones HOT 2
- fluent psql updated constraint is not accessible HOT 1
- contains filter on ModelAlias does result in error
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 fluent.