Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create custom field type: PrimaryKey #74

Open
mjovanc opened this issue May 3, 2024 · 0 comments
Open

Create custom field type: PrimaryKey #74

mjovanc opened this issue May 3, 2024 · 0 comments
Labels
feature New feature or request sqlite Issues related to SQLite

Comments

@mjovanc
Copy link
Owner

mjovanc commented May 3, 2024

Just thinking of creating a custom field type called PrimaryKey to add to a field for example id: https://github.com/njord-rs/njord/blob/master/njord/tests/sqlite_test.rs#L15

Some example implementation:

pub struct PrimaryKey<T>(Option<T>);

impl<T> PrimaryKey<T> {
    pub fn new(value: Option<T>) -> Self {
        PrimaryKey(value)
    }

    pub fn get(&self) -> Option<&T> {
        self.0.as_ref()
    }
}

impl<T> Default for PrimaryKey<T> {
    fn default() -> Self {
        PrimaryKey(None)
    }
}

Usage example:

let table_row: User = User {
    id: PrimaryKey::default(),
    username: "mjovanc".to_string(),
    email: "mjovanc@icloud.com".to_string(),
    address: "Some Random Address 1".to_string(),
};

When setting it to ::default we are saying that we want the database to increment the id field automatically using autoincrement.

@mjovanc mjovanc added feature New feature or request sqlite Issues related to SQLite labels May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request sqlite Issues related to SQLite
Projects
None yet
Development

No branches or pull requests

1 participant