A
A
Al2017-02-01 19:18:55
ORM
Al, 2017-02-01 19:18:55

ORM in nodeJS, how should it work?

Why do I need an ORM? - to simplify the process of saving / retrieving objects in the database. I googled a lot, looked at examples of different libraries, highlighted mainly Sequelize and Bookshelf. In general, they seemed to me to be some kind of query constructors, it doesn’t seem at all that any of them is designed to simplify my life and significantly reduce the SQL code in scripts, but just the opposite, in addition to SQL, I need to study the api of these libs.
For example, it’s easier for me to write SQL by hand than to write such a miracle:

users.findOne({
  order: [
    ['username', 'DESC'],
   sequelize.fn('max', sequelize.col('age')),
    [sequelize.fn('max', sequelize.col('age')), 'DESC'],
    [sequelize.fn('otherfunction', sequelize.col('col1'), 12, 'lalala'), 'DESC'],
    [User, 'name', 'DESC'],
    [{model: User, as: 'Friend'}, 'name', 'DESC'],
    [User, Company, 'name', 'DESC'],
  ]
  order: 'convert(user_name using gbk)'
  order: 'username DESC'
  order: sequelize.literal('convert(user_name using gbk)')
})

From the ORM I would like not the same as the libs given by me, but something like where I would describe an entity in some xml, and it generated a class with the necessary methods for me. Even if this doesn’t exist in nature or it’s not right, stupid (for example, I’m not particularly familiar with this issue), then push me to think about how to implement this myself for my project? Do people do anything like this at all? Let there be strict description rules or any restrictions, I'm more interested in the theoretical question of the solution. For example, how to correctly describe an entity, or how to analyze an existing base to generate this or that code .... etc.

Just looking at sequelize and libs like it makes me want to write SQL and not use them!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
X
xfg, 2017-02-01
@xfg

It's not clear what you want to get. But you can look at typeorm . This is the only orm in node.js that implements the data mapper pattern, all others implement the active record pattern. But the sampling conditions still have to be written.

Z
zergon321, 2017-02-01
@zergon321

Google metaprogramming and dynamic code generation and interpretation. Python, for example, has an exec() function that takes a string containing code to be interpreted and executed as input, and you can also provide it with dictionaries that replace the usual local and global scopes of variables. Maybe Node has something like that...

E
Eduard Vershinin, 2019-12-20
@noreduard

Lucid ORM is simple and convenient

A
Alexander, 2020-05-07
@Katyshin

Also asked this question. At the moment, I myself am inclined to believe that using ORM seems to be more convenient to work with links for models (1-1, 1-M, MM). Part of the work ORM takes over.
But personally, I did not understand how to properly embed models in my classes, how to correctly match all model fields with the corresponding fields of my classes in js or typescript.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question