Chitika

Tuesday, November 12, 2013

my grails cheat-sheet

Environment

set $GRAILS_HOME=path to grails
set $JAVA_HOME=path to java
set $PATH=include $JAVA_HOME/bin and $GRAILS_HOME/bin

Init

grails create-app App1

Run

grails run-app

Create Domain

grails create-domain-class d1

path: grails-app/domain/d1.groovy

Data Constraints

class d1 {

String name
String address
Date dob
d2 linkedData

static constraints = {
   // order of fields in constraints are used in scaffolding
   name(size:1..48, unique:false)
   address(maxSize:128,blank:true)
   dob(blank:true)
   // linkedData of custom type nullable as in the case linked data doesn't exist,
    // then d1 can't be created
   linkedData(nullable:true)
}
}

Create controller

grails create-controller c1

path: grails-app/controller/c1.groovy

Scaffolding

class c1Controller {
    def scaffold = d1
}

Testing

grails test-app -unit
grails test-app -integration

Friday, November 1, 2013

mongo db

Setup:

run as root > mkdir -p /data/db

run as root > mongod
(--rest switch starts with rest interface)

create database abcdb:

> mongo abcdb
>_ (prompt will appear)
(db won't be persist unless data inserted)
insert data, into a collection coll:

> db.coll.insert( "lang": "english", "alphabets": ["a","b","c"...], "style":"ltr")
json object will be created in db as follows:
"book":{
                "coll":[
                              { "lang": "english", "alphabets": ["a","b","c"...], "style":"ltr" }
                            ]
              }

to see rows in collections:

> db.coll.find()

to see all the functions of any object use help() function

> db.help()
> db.coll.help()

to see all databases

> show dbs

to see the definition of function, enter function name without parenthesis

>db.coll.find

custom function
> function myFunctionLanguage(lang, style) {
... db.towns.insert(lang:lang, style:style);
... }

find function may take object Id as first argument or condition filter, and columns to show in result in second argument

find time taken by query to execute (need of index?)

> find(...).explain()

create index:

>db.coll.ensureIndex(...)

queries profiling:

> db.setProfilingLevel(2)
>db.system.profile.find()