totaljs / framework

Node.js framework
http://www.totaljs.com
Other
4.36k stars 450 forks source link

How to set schema in rest builder in totaljs #564

Closed mahdi-gh33 closed 7 years ago

mahdi-gh33 commented 7 years ago

Hi @petersirka I have rest service with response like this : {"total_rows":69,"rows":[ {"id":"Service_3a287fd1-f80a-4624-9862-28409681dae0","key":["service"],"value":{"id":"Service_3a287fd1-f80a-4624-9862-28409681dae0","TimeStamp":"2017-09-26T14:06:54.894Z","Title":"NFC"}}, {"id":"Service_4c92371e-a642-4c1e-b59a-ed77f4dd7d7f","key":["service"],"value":{"id":"Service_4c92371e-a642-4c1e-b59a-ed77f4dd7d7f","TimeStamp":"2017-09-26T14:06:54.894Z","Title":"Wifi"}}, {"id":"Service_5d98aea5-7065-42c6-855e-2069faa9ef93","key":["service"],"value":{"id":"Service_5d98aea5-7065-42c6-855e-2069faa9ef93","TimeStamp":"2017-09-26T14:06:54.894Z","Title":"GPS"}} ] }

The real response is in value property of response . I have a rest builder like this :

` RESTBuilder.make(function (builder) { builder.url('http://example.com/_design/dev_customerId/_view/customerId? connection_timeout=60000&inclusive_end=true&key=%5B+%22' + customerId + '%22,+%22' + docType + '%22+%5D&limit=6&skip=0&stale=false');

    builder.get();

    //builder.schema('Service');

    builder.exec(function (err, response) {

        console.log("Error: " + err);
        var data = [];
        for (var i = 0; i < response.rows.length; i++) {

            data.push(response.rows[i].value)
        }
        self.json(data, true);

    });
});`

I have a array of json model (I call it service) in value property of response which I should write a for loop for returning that but I can't use schema for that because if I uncomment the //builder.schema('service'); I have e error : Cannot read property 'length' of undefined What should I do if I want to use schema features like validation for my rest builder that?

Thank you.

petersirka commented 7 years ago

Can I see your schema declaration?

mahdi-gh33 commented 7 years ago
const Fs = require('fs');

NEWSCHEMA('Service').make(function(schema) {
    schema.define('id', 'String(40)');
    schema.define('Title', 'String(30)');
    schema.define('TimeStamp', Date);
    schema.define('docType', 'String(40)');        
});

I don't write any validation yet because I can't use schema for that response.

petersirka commented 7 years ago

Yes, there is a problem. Because the schema is created from the whole response, not from rows only.

You need to update your schema:

NEWSCHEMA('ServiceRow').make(function(schema) {
    schema.define('id', 'String(40)');
    schema.define('Title', 'String(30)');
    schema.define('TimeStamp', Date);
    schema.define('docType', 'String(40)');
});

NEWSCHEMA('Service').make(function(schema) {
    schema.define('rows', '[ServiceRow]');
});

and now it should work:

    builder.schema('service');
    builder.exec(function (err, response) {
          console.log(response.rows);
          // each item should be ServiceRow
    });
mahdi-gh33 commented 7 years ago

Actually I my real reponse is in value property of each rows . Now the response is like this

[
    {
        "id": "Service_3a287fd1-f80a-4624-9862-28409681dae0",
        "key": [
            "service"
        ],
        "value": {
            "id": "Service_3a287fd1-f80a-4624-9862-28409681dae0",
            "TimeStamp": "2017-09-26T14:06:54.894Z",
            "Title": "NFC"
        }
    },
    {
        "id": "Service_4c92371e-a642-4c1e-b59a-ed77f4dd7d7f",
        "key": [
            "service"
        ],
        "value": {
            "id": "Service_4c92371e-a642-4c1e-b59a-ed77f4dd7d7f",
            "TimeStamp": "2017-09-26T14:06:54.894Z",
            "Title": "Wifi"
        }
    },
    {
        "id": "Service_5d98aea5-7065-42c6-855e-2069faa9ef93",
        "key": [
            "service"
        ],
        "value": {
            "id": "Service_5d98aea5-7065-42c6-855e-2069faa9ef93",
            "TimeStamp": "2017-09-26T14:06:54.894Z",
            "Title": "GPS"
        }
    }
]

But the value is my real schema data, So I change schema to :

`NEWSCHEMA('ServiceValue').make(function (schema) {
    schema.define('id', 'String(40)');
    schema.define('Title', 'String(30)');
    schema.define('TimeStamp', Date);
    schema.define('docType', 'String(40)');
});

NEWSCHEMA('ServiceRow').make(function (schema) {
    schema.define('value', '[ServiceValue]');
});

NEWSCHEMA('Service').make(function (schema) {
    schema.define('rows', '[ServiceRow]');
});`

and in ctrl:

        builder.schema('service');
        builder.exec(function (err, response) {
            console.log(response.rows.value);
            // each item should be ServiceRow
        });

But it doesn't work.

petersirka commented 7 years ago

Can you send me a small example on my email?

mahdi-gh33 commented 7 years ago

Sorry beacuse of this delay I'm working on my web service until I can send example.

petersirka commented 7 years ago

No problem.

mahdi-gh33 commented 7 years ago

example-schema.zip Hi peter this is my example and I send you some information with email for you to use that.

Thank you

petersirka commented 7 years ago

Something new?

mahdi-gh33 commented 7 years ago

I sent an email to you about my problem.

petersirka commented 7 years ago

Sorry for delay. This is solution:

NEWSCHEMA('ServiceRowValue').make(function(schema) {
    schema.define('id', 'String');
    schema.define('TimeStamp', 'Date');
    schema.define('Title', 'String');
});

NEWSCHEMA('ServiceRow').make(function (schema) {
   schema.define('id', 'String(40)');
   schema.define('key', '[String]');

   schema.define('value', 'ServiceRowValue');
   // Or just use Object:
   // schema.define('value', 'Object');

   schema.setSave(function($) {
    console.log($.model);
    $.callback(SUCCESS(true));
   });
});

NEWSCHEMA('Service').make(function (schema) {
   schema.define('rows', '[ServiceRow]');
});
mahdi-gh33 commented 7 years ago

Thank you it's working now . You are the best :-)