Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails JS: How to create model objects from the result of .query()?

Tags:

sails.js

I need to retrieve a set of records that require a somewhat complex criteria with multiple joins. I can get the records I want by doing a .query() with my own SQL. However, I am having trouble figuring out how to turn those records into actual model instances with instance methods, etc. I'm sure there's some kind of an internal method available to create a model instance from a record that's already been retrieved that I just need to map my results to, I just don't know what it is.

Some help?

like image 823
Eugene Avatar asked Nov 01 '25 17:11

Eugene


1 Answers

You should be able to do this with Collection._model(jsObject).

I know there was talk of an effort to make this an official public api, rather than a "hidden" option using a supposedly private method.

Example:

new Pet._model({name: "fluffy"});

See this issue for details. Btw, particlebanana (Cody Stoltman) who recommended this option in that issue is the lead developer on waterline ORM. Hopefully that gives you some confidence.

like image 124
arcseldon Avatar answered Nov 04 '25 14:11

arcseldon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!