Code Monkey home page Code Monkey logo

gen-mdl's Introduction

gen-mdl

Build status

What is it?

gen-mdl is a code generator that takes a data model definition as input and produces code in C# and TypeScript for that model.

What is it good for?

It's great at centralizing knowledge to keep data model classes in client side (browser) and server-side updated.

Our philosophy

We attempt to follow DRY and KISS principles.

Our core principles

  • Knowledge should be centered in a single place.
    • You shouldn't have to look at files in different languages to understand your model.
  • Data modeling should be simple.
    • You should only provide as much detail as is actually needed, but rely on the defaults to do the rest.

How does gen-mdl work?

  • You write model definitions in YAML.
  • You invoke gen-mdl with your model definition.
  • You get code. :)

What does a model definition look like?

A model definition for a blog application looks like this.

%YAML 1.1
---
targets : 
    csharp : 
        path         : Blog.Model\\Data
        namespace    : Blog.Model.Data
        type_aliases : 
            id_t : int 

    typescript : 
        path         : client\\app\\model
        type_aliases : 
            id_t : int

enums : 
    blog_post_status : [draft, final]

entities :
    author :
        members: 
            id    : id_t
            name  : string
            alias : string

    blog :
        members:
            id     : id_t
            title  : string
            posts  : 
                type          : blog_post
                is_collection : true
            author : author

    blog_post : 
        members:
            id             : id_t
            date_published : { type: datetime, is_nullable: true }
            description    : { type: string, is_nullable: true }
            comments       :
                type          : comment
                is_collection : true
            status         : blog_post_status

    comment :
        members:
            id   : id_t
            text : string
            shared_in_fb : { type: bool, exclude: [typescript] }

What do I get after invoking gen-mdl?

For the model above you would get the following files

  • Blog.Model\Data\Author.cs
namespace Blog.Model.Data
{
  using System.ComponentModel.DataAnnotations;

  public class Author
  {
    public int Id { get; set; }

    [Required(AllowEmptyStrings = true)]
    public string Name { get; set; }

    [Required(AllowEmptyStrings = true)]
    public string Alias { get; set; }
  }
}
  • Blog.Model\Data\Blog.cs
namespace Blog.Model.Data
{
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;

  public class Blog
  {
    public int Id { get; set; }

    [Required(AllowEmptyStrings = true)]
    public string Title { get; set; }

    [Required]
    public IList<BlogPost> Posts { get; set; }

    [Required]
    public Author Author { get; set; }
  }
}
  • Blog.Model\Data\BlogPost.cs
namespace Blog.Model.Data
{
  using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;

  public class BlogPost
  {
    public int Id { get; set; }

    public DateTimeOffset? DatePublished { get; set; }

    public string Description { get; set; }

    [Required]
    public IList<Comment> Comments { get; set; }

    public BlogPostStatus Status { get; set; }
  }
}
  • Blog.Model\Data\BlogPostStatus.cs
namespace Blog.Model.Data
{
  public enum BlogPostStatus
  {
    Draft,
    Final
  }
}
  • Blog.Model\Data\Comment.cs
namespace Blog.Model.Data
{
  using System.ComponentModel.DataAnnotations;

  public class Comment
  {
    public int Id { get; set; }

    [Required(AllowEmptyStrings = true)]
    public string Text { get; set; }

    public bool SharedInFb { get; set; }
  }
}
  • client\app\model\author.ts
export default class Author {
  id : number;
  name : string;
  alias : string;
}
  • client\app\model\blog-post-status.ts
enum BlogPostStatus {
  Draft,
  Final
}

export default BlogPostStatus;
  • client\app\model\blog-post.ts
import BlogPostStatus from './blog-post-status'
import Comment from './comment'

export default class BlogPost {
  id : number;
  datePublished : Date;
  description : string;
  comments : Comment[];
  status : BlogPostStatus;
}
  • client\app\model\blog.ts
import BlogPost from './blog-post'
import Author from './author'

export default class Blog {
  id : number;
  title : string;
  posts : BlogPost[];
  author : Author;
}
  • client\app\model\comment.ts
export default class Comment {
  id : number;
  text : string;
}
  • client\app\model\index.ts
export * from './blog-post-status';
export * from './author';
export * from './blog';
export * from './blog-post';
export * from './comment';

Requirements

gen-mdl's People

Contributors

angrifel avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

gen-mdl's Issues

Move entity members under a 'members' property under each entity

Currently entity members are listed just below the entity level.

They belong to a separate propery.

Current design

...
entities:
  entity_1:
    member_1_a: ...
    member_1_b: ...
    ...
    member_1_z: ...

  entity_2:
    member_2_a: ...
    member_2_b: ...
    ...
    member_2_z: ...

''''

expected design

...
entities:
  entity_1:
    members:
      member_1_a: ...
      member_1_b: ...
      ...
      member_1_z: ...

  entity_2:
    members:
      member_2_a: ...
      member_2_b: ...
      ...
      member_2_z: ...

...

Fix generation of datetime type

when a member with type 'datetime' is used it's type in CSharp is generated as follows.

System.datetimeoffset

when it should be generated as

System.DateTimeOffset

Add unit tests

  • CSharp

    • CSharpAmmendment
    • CSharpClass
    • CSharpClassMember
    • CSharpEnum
    • CSharpEnumMember
    • CSharpGenerator #16
    • CSharpNamespace
  • TypeScript

    • TypeScriptAmmendment
    • TypeScriptClass #17
    • TypeScriptClassMember #18
    • TypeScriptEnum #19
    • TypeScriptEnumMember #20
    • TypeScriptExportStatement #21
    • TypeScriptFile #22
    • TypeScriptGenerator #23
    • TypeScriptImportStatement #24
    • TypeScriptReExportStatement #25
  • Other

    • SpecFunctions #27

Add Validations to model

We should allow validations to occur on the model at different levels.

  • at the field level
entities:
  entity_1:
    ...
    members:
      field_1: ...
      field_2:
        type: ...
        validations: 
          validation_name_1 : [validation rule here]
          validation_name_2 : [validation rule here]
          #other validations here
      #other fields here
  entity_2: ...
  #other entities here
  • at the entity level
entities:
  entity_1:  ...
    members: ...
    validations: 
      validation_name_1 : [validation rule here]
      validation_name_2 : [validation rule here]
      #other validations here
  entity_2: ...
  #other entities here
  • at the model level.
enums: ...

entities: ...

validations: 
  validation_name_1 : [validation rule here]
  validation_name_2 : [validation rule here]
  #other validations here

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.