moigagoo / norm

A Nim ORM for SQLite and Postgres
https://norm.nim.town
MIT License
381 stars 34 forks source link

Why `select` and `selectAll` wants non-empty seq? #176

Open inv2004 opened 1 year ago

inv2004 commented 1 year ago

Hello, was trying to use norm for the first time, was reading documentation and it was very unclear for me:

Why the select requires one element in seq? I suppose model (table and etc) goes from type, why the element is needed here?

var customersFoo = @[newCustomer()]
dbConn.select(customersFoo, "User.email = ?", "foo@foo.foo")

echo()
moigagoo commented 1 year ago

This is because in general, there is no universal way to instantiate a type in Nim. That means, you have just the type, you can't in general create its instance.

Therefore Norm requires an container instance to put the data into. At least one instance is required, it can be copied to fit multiple rows.

moigagoo commented 1 year ago

We've discussed this topic outside of GitHub. It seems that with the introduction of default field values for objects, we can finally reliably use types instead of instances. Also, DateTime type now has a default value.

This means new flavors of the existing procs can already be added, the flavors that take type instead of instance and create a necessary instance on the fly and then call the regular flavor.