How to create own function to model, or what is right way to do this..

43 views
Skip to first unread message

triple....@gmail.com

unread,
Jun 1, 2015, 12:54:41 PM6/1/15
to tot...@googlegroups.com
If have next functions in use:
setQuery - to get list all sql rows for controller
setGet - to get one Id/row for controller (with id param)
setRemove - ...
setSave ... to update/insert stuff to DB.

Then ie. I need to get special row/rows from DB where field is X (like select * from car where roof = 'true')

What is right way to get these rows?

I need to add new function to model like:

Car.setQuery ( ....)
Car.setGet ( ....)

Car.getAllCarsWhichHaveRoof = function(......)

exports.Car = Car;

OR

i need to modifield setGet-function and give special params somhow?

Thanks.

Peter Širka

unread,
Jun 1, 2015, 1:03:14 PM6/1/15
to tot...@googlegroups.com, triple....@gmail.com
Hi Jani,
yes...

setQuery(error, options, callback) -> yes
setGet(error, model, options, callback) -> yes
setRemove(error, options, callback) -> yes
setSave(error, model, options, callback) -> yes

You can use options/helper argument for special cases in all methods:

Schema.setQuery(function(error, options, callback) {
   
var sql = DATABASE();
   
var select = sql.select('cars', 'tbl_car');
   
if (options.roof)
       
select.where('roof', true);
    sql
.exec(callback, 'cars');
});

Schema.setGet(function(error, model, options, callback) {
   
var sql = DATABASE();
   
var select = sql.select('car', 'tbl_car');
   
if (options.roof)
       
select.where('roof', true);
   
select.first();
    sql
.exec(callback, 'car');
});

// ... ... some controller actions
Schema.query({ roof: true }, controller.callback());
Schema.get({ id: 123, roof: true }, controller.callback());

Do you understand?
Thanks :-)


Jani Juusola

unread,
Jun 2, 2015, 10:45:16 AM6/2/15
to tot...@googlegroups.com, triple....@gmail.com
Yes I got it, thank you.

Of course that's the way to do it =)

Reply all
Reply to author
Forward
0 new messages