Grails 2.4 - domain modelling -
i using grails 2.4 , working providing rest api. looking suggestions on domain/entity creation.
i have 4 simple tables:
1) projects: projectid (auto-increment, pk) name description (some other fields not of import question guess) 2) users: userid (auto-increment, pk) roleid (fk->roles/roleid) username (some other fields not of import question guess) 3) project_users (join table projects & users): projectid fk->projects/projectid) userid (fk->users/userid) 4) roles: roleid (auto-increment, pk) name
role can user or manager or admin. project can have many users (multiple users of role 'user' , 1 user of role 'manager').
the consumer of rest api (json) needs list(get)of projects names of users (username not userid) each projects (all users.. not matter if user manager not). want ability add together new users (put) project. create new project (post). requests json request/response should contain username) looking suggestions on how proceed.
to familiar grails/rest, have defined 3 domains (projects, users , roles) , & post json these domains individually working fine (not domain relationship done).
then play domain relations, have tried 'db-reverse-engineer' plugin , gave next domains relationships.
class projects { string name string description static hasmany = [userses: users] static mapping = { id column: "project_id" version false } static constraints = { name unique: true description nullable: true } } class users { string username roles roles static hasmany = [projectses: projects] static belongsto = [roles] static mapping = { id column: "user_id" version false } static constraints = { username maxsize: 32, unique: true } } class roles { string name static hasmany = [userses: users] static mapping = { id column: "role_id", generator: "assigned" version false } static constraints = { name maxsize: 16, unique: true } }
now not sure how proceed further. need create database view (db doesn't allow updates view) & corresponding controller , save write custom code. couple of days playing grails & rest, bear me basic question.
the general strategy grails development simple mvc pattern, have controller build model , pass view. rest api, don't need view unless need alter default way domain objects displayed. need configure mappings requests rest apis discussed here. beyond need controller , service if need in business logic beyond basic crud operations. domain layer, couple of recommendations:
1) it's mutual have domain classes , db tables matter singular avoid having have names userses , on. create tables user, role , project.
2) have many-to-many relationship users , projects you'll need specify that. can jointable in mapping though may want create separate domain class projectuser if querying relationship lot you'll run hibernate n+1 problem if you're not careful.
hope helps,
grails
No comments:
Post a Comment