Use value classes as ID
See original GitHub issueDescription
To prevent accidental using a wrong id from another entity, SQLDelight could generate an ID class for the primary key. For better performance, it should be a value class, if possible.
// before
val userID = 42
todos.find(id = userID) // compiles, but wrong ID
// after
val userID = User.ID(42)
todos.find(id = userID) // error: expected Todo.ID
With this definition, SQLDelight would generate this classes:
CREATE TABLE user (
id INT NOT NULL PRIMARY KEY
);
CREATE TABLE todo (
id INT NOT NULL PRIMARY KEY,
userID INT NOT NULL,
FOREIGN KEY (userID) REFERENCES user(id)
);
data class User(val id: User.ID) {
value class ID(val value: Int)
}
data class Todo(val id: Todo.ID, val userID: User.ID) {
value class ID(val value: Int)
}
BTW fluent-kit, an ORM framework written in Swift, provides this feature: http://github.com/vapor/fluent-kit/ Model file: https://github.com/vapor/fluent-kit/blob/main/Sources/FluentKit/Model/Model.swift
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (3 by maintainers)
Top Results From Across the Web
Effective Kotlin Item 49: Consider using inline value classes
This way, inline value classes allow us to introduce types where they were not allowed before; thanks to this, we have safer code...
Read more >How I use the new Inline Value Classes in Kotlin - Manu Sobles
Learn to apply the concept of Value Objects from Domain-Driven Design in Kotlin by using value classes.
Read more >Value Classes and Universal Traits - Scala Documentation
One use case for value classes is to combine them with implicit classes (SIP-13) ... the actual Meter instance must be created for...
Read more >Value-based Classes
Value -based Classes. Some classes, such as java.util.Optional and java.time.LocalDateTime , are value-based. Instances of a value-based class:.
Read more >Is there a basic id / value object in Java? - Stack Overflow
You want a class that constitutes a mutable int/String pair, and so your code is OK. but that's something for other languages.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
you can do composite primary keys, we have some in cash
You should be able to do this with custom column types.
@JvmInline value class
works.