Page tree
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 13 Next »

Use this strategy to select document from the database by UNID.

Allowed methods


  • databaseName(String databaseName): name of the database to read data from, use "server!!path/database.nsf" format; when omitted, current database is used
  • keyVariableName(String keyVariableName): name of the variable name in route to read value from; mandatory


GET method
router.GET('topics/{id}') {
   strategy(DOCUMENT_BY_UNID) {
   mapJson 'id', json:'id', type:'STRING', isformula:true, formula:'@DocumentUniqueID'
   mapJson 'date_created', json:'date_created', type:'DATETIME', isformula:true, formula:'@Created'
   mapJson 'topic', json:'topic', type:'STRING'
   mapJson 'author', json:'author', type:'STRING', isformula:true, formula:'@Name([CN]; @Author)'
   mapJson 'categories', json:'categories', type:'ARRAY_OF_STRING'


With route defined above, URL will return this JSON:

    "author": "Martin Jinoch",
    "categories": [
    "topic": "test"


POST method
Creating new document

WIth route defined below, you can create new document by POSTing to URL

router.POST('topics/{id}') {
   strategy(DOCUMENT_BY_UNID) {
   mapJson "id", json:'id', type:'STRING', isformula:true, formula:'@DocumentUniqueId', readonly:true
   mapJson "Subject", json:'topic', type:'STRING'
   mapJson "body", json:'content', type:'MIME'
   mapJson 'categories', json:'categories', type:'ARRAY_OF_STRING', writeonly:true
   mapJson "date", json:'date', type:'DATETIME', isformula:true, formula:'@Created', readonly:true
   mapJson "author", json:'author', type:'STRING', isformula:true, formula:'@Name([CN]; From)', readonly:true
   events PRE_SAVE_DOCUMENT: {
      context, document ->
      nsfHelp = context.getNSFHelper()

Let's have a detailed look at the definition above: note readonly:true part on the line 5. With this you can define the "output only" flag for fields. So SmartNSF doesn't expect and read field "id" from POSTed JSON data, but it includes it in the returned JSON.

Analogically you can define "input only" fields by specifying writeonly:true. Those are then not included in the output.

In the example above we are using another interesting feature: events. PRE_SAVE_DOCUMENT event, as name suggests, is executed before the document is saved. In this case we are simply grabbing the context and calling computeWithForm() method (provided by NSFHelper class) on the document.

So POSTing following JSON to URL

    "topic": "created from rest",
    "content": "<h3>test</h3>",
    "categories": [

should return something like this

    "content": "<h3>test<\/h3>",
    "author": "martin jinoch",
    "id": "EF09FEA7F9FF7265C12580E40065E4D6",
    "date": "2017-03-15T18:57+00:00",
    "topic": "created from rest"



  • No labels