GithubHelp home page GithubHelp logo

lgug2z / diesel-autoincrement-new-struct Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 4 KB

Generate NewStructs for all your tables with autoincrementing IDs

License: MIT License

Rust 100.00%
database-schema diesel-rs orm rust

diesel-autoincrement-new-struct's Introduction

diesel-autoincrement-new-struct

Stop copying and pasting structs with the id field removed just so you can insert objects into tables with autoincrementing primary keys.

Motivation

I mean really, who wants to do all that copying and pasting? Who wants to keep attributes and documentation in sync between two otherwise identical structs?

This whole idea came about after finding the excellent macro_rules_attribute crate by Daniel Henry-Mantilla, aka my personal Rust macro hero.

Example

#[macro_use]
extern crate diesel_autoincrement_new_struct;

// Alternatively, you can either import the prelude where needed:
//
// use diesel_increment_new_struct::prelude::*;
//
// Or even import apply and NewInsertable individually:
//
// use diesel_increment_new_struct::apply;
// use diesel_increment_new_struct::NewInsertable;

use diesel::prelude::*;

table! {
    users(id) {
        id -> Integer,
        name -> Text,
    }
}

#[apply(NewInsertable!)]
#[derive(Debug, Clone, Queryable, AsChangeset)]
#[diesel(table_name = users)]
/// This is a user
pub struct User {
    /// This is the ID of the user
    id: i32,
    /// This is the name of the user
    name: String
}

// The code below gets generated by `#[apply(NewInsertable!)]`

#[derive(Debug, Clone, Queryable, AsChangeset)]
#[derive(Insertable)]
#[diesel(table_name = users)]
/// This is a user
pub struct NewUser {
    /// This is the name of the user
    name: String
}

Notes

  • This crate doesn't re-export Diesel, so make sure you have use diesel::prelude::*; or use diesel::Insertable; in whichever files you use this macro
  • This crate requires at least whichever version or revision of Diesel where the #[diesel(table_name = ...)] attribute stopped taking a double quoted string

The #[apply] attribute should always be the topmost attribute above a struct, unless the struct that you want to use it on is also deriving Identifiable. If that is the case, you should have that derive been the topmost attribute above the struct so that it is excluded when generating the NewStruct, becuase obviously, without an id, it won't be Identifiable:

#[derive(Identifiable)]
#[apply(NewInsertable!)]
#[derive(Debug, Clone, Queryable, AsChangeset)]
#[diesel(table_name = users)]
/// This is a user
pub struct User {
    /// This is the ID of the user
    id: i32,
    /// This is the name of the user
    name: String
}

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.