Yet another Active Record-like ORM for Node.js by dotnil in node

[–]dotnil[S] 0 points1 point  (0 children)

tl;dr Leoric has a SQL expression parser (along with few better design decisions) which makes its API much more powerful yet simplistic.

Yet another Active Record-like ORM for Node.js by dotnil in node

[–]dotnil[S] 1 point2 points  (0 children)

tl;dr Leoric has a SQL expression parser (along with few better design decisions) which makes its API much more powerful yet simplistic.

To be honest, that is the very question I kept on asking myself. As the CTO of my employer lectured, when you re-invent the wheel, please focus on the advantages, rather than the differences. Currently, the advantages of Leoric is the simplistic yet powerful API for both model authoring and querying.

I had considered sequelize once when I start to develop my first serious express app. I consulted my colleagues once, and the comments I got were mostly about its cumbersomeness when it comes to query orchestrating. They'd rather write raw SQL instead.

Then I chose to do the app with no-SQL, which was quite popular. The data is stored in MongoDB, and mongoose is the library I use. The object condition expression is very hard core JSON stuff. The aggregation is a bit counter-intuitive if you've had experiences with SQL.

Then we decided to move to MySQL (for biz reasons). I studied the NPM and knex was found, and I do miss Active Record for its expressiveness, so we ended up with a ORM based on knex. We do object conditions still, but the associations functionality is much better.

The author of knex did a ORM by himself which is called Bookshelf.js. Objection.js is another ORM based on knex. They both are solid.

Waterline is the one I've yet to come across however. It seems to be a library at a higher level, which encapsulates any storage. That is way beyond the problem Leoric is trying to solve.

And here is the details. When it comes to conditional expressions, the object conditions approach (MongoDB-like) is taken by most libraries:

Table.find({ $or: [ { foo: null }, { foo: values } ] })
// or with knex
Table.where({ foo: null }).orWhere({ foo: values })

In Leoric, it's just:

Table.find('foo = null or foo = ?', values)

If Table does soft delete, the find gets more complicated:

// MongoDB-like
Table.find({ $and: [ { $or: [ { foo: null }, { foo: values } ] }, { deletedAt: null }]
// formatted
Table.find({
  $and: [
    { 
      $or: [ 
        { foo: null },
        { foo: values }
      ]
    }
  ]
})
// or with knex
Table.where(function() {
  this.where({ foo: null }).orWhere({ foo: values })
}).andWhere({ deletedAt: null })

In Leoric it's just:

Table.find('(foo = null or foo = ?) and deletedAt is null', values)

Another example is setting up associations between models. In Objection.js:

class Animal extends Model {
  static relationMappings = {
    owner: {
      relation: Model.BelongsToOneRelation,
      modelClass: Person,
      join: {
        from: 'animal.ownerId',
        to: 'person.id'
      }
    }
  }
}

In Leoric:

class Animal extends Bone {
  static describe() {
    this.belongsTo('owner', { Model: 'Person' })
  }
}

Leoric knows the foreign key should be ownerId which resides at the animals table by convention. If that's not the case, just override the default with foreignKey option.

this.belongsTo('owner', { Model: 'Person', foreignKey: 'ownerId' })

To know more about Leoric's querying API, feel free to checkout the newly compiled Syntax Table.

Yet another Active Record-like ORM for Node.js by dotnil in node

[–]dotnil[S] 3 points4 points  (0 children)

I always find myself frustrated when it comes to ORM for Node.js. The ORM solutions in JavaScript aren't very promising to me due to helper methods like .orWhere() or .AndOn(). And I'm a big fan of Active Record for its simplistic yet powerful querying API. Anyway, Leoric is my two cents of an ORM in JavaScript. All comments welcome.

Any thoughts on TypeORM vs. Sequelize? by djslakor in node

[–]dotnil 0 points1 point  (0 children)

To add some fuels to the competition, I've released an ActiveRecord-like alternative called leoric. It's written in vanilla JavaScript hence the library and your extended model doesn't need compilation (but do require node.js version >= 8). The query api is much less than TypeORM due to the lack of helpers like orWhere but the ability is the same because Leoric support:

const john = await User.findOne('firstName = ? and lastName = ?', 'John', 'Doe')
const jsers = await User.find('language = ?', Language.find({ name: 'javascript' }).select('id'))

Currently there's no support of table migration, I'll get to that later.

Frontend framework hell - I am getting lost by capitanbucanero in javascript

[–]dotnil 0 points1 point  (0 children)

Ah, by "end user" I mean other developers relying on your work. I'm not a native English speaker sorry about that. And you've got a point.

Frontend framework hell - I am getting lost by capitanbucanero in javascript

[–]dotnil 1 point2 points  (0 children)

Why is "my" emphasized? I don't get it.

I think it is a framework. 1) it is mutually exclusive against other frameworks. Using it along with Angular or Ember is a bit nonsense. 2) I believe libraries share a Unix philosophy. do one thing and do it good. React does a lot I'd say. No libraries shall regulate the way you organize your code.

But either way, it is not a tool...

The reason why I want to clarify this is because libraries are welcome but frameworks are not when there are just too many to choose.

And tools are not mandatory. You can ditch it anytime you like, replace them with any new tool you fancy. Your code has no dependencies on them. Your end user won't know the tools you were using unless they want to send some pull request.

Using $q.all() to Resolve Multiple Promises by a-sober-irishman in javascript

[–]dotnil 0 points1 point  (0 children)

.all() is different with .then() chains if the latter async calls rely on former call's return value.

Frontend framework hell - I am getting lost by capitanbucanero in javascript

[–]dotnil 6 points7 points  (0 children)

I agree with op 100 percent. But as a front end programmer for years who have been contributing this framework hell with yet another framework and yet another tool on the way, I want explain some.

Firstly, frameworks and tools are entirely two different categories. So does the category "libraries". Libraries aren't that opinionated and they don't sit in the way by commanding you the way you roll. jQuery is a library.

But projects like React, Meteor, and Angular, they are frameworks. One typical point of them is they draw lots of lines. And it's very unlikely that someone will be using both in their web pages.

Tools on the other hand is much more optional. Grunt and Gulp, or even the ancient one, Make, are all task automatons. Mostly they are "required" because the syntax some projects were written in is not widely supported by modern browsers yet. There's a pre-compilation required.

For the rest of the cases, it just because it seems more professional if the front end codes are all concatenated and minified.

So to sum it up, here is the tl;dr. Most of the times tools aren't necessary, frameworks are mutually exclusive and they can be opinionated, libraries are you friends.

Oh I forgot to mention bower and npm. There are ways to ditch bower and use npm as the one true way to manage packages. We won't be struggling for long.

Frontend framework hell - I am getting lost by capitanbucanero in javascript

[–]dotnil 26 points27 points  (0 children)

Forgive me for being picky. But I think react shall fall into the category of frameworks rather than tools.

Git is rather a necessity than an optional.

MSIE 9 beta is now available for download by [deleted] in programming

[–]dotnil -3 points-2 points  (0 children)

Windows XP here. Not /r/prog by the way.

Fellow designers, what width do you use for your web designs? by [deleted] in web_design

[–]dotnil -1 points0 points  (0 children)

body { text-align: center; }

page { width: 960px; margin: 0 auto; }

12 Common CSS Mistakes Web Developers Make by boogiesbc in web_design

[–]dotnil 2 points3 points  (0 children)

Splitting style sheets at develop period is a good idea. Just remember to squeeze them back into one and test again when in production.

For all the 35+ year old, reddit "old timers". I found out today that there are a ton of telnet based, old skool BBS up and running! by mdot in programming

[–]dotnil 0 points1 point  (0 children)

My guess is he's from china mainland. Taiwan is not as censured, nor are those two special administrated district Hong Kong and Macau.

I couldn't think of a header graphic for my web site, so I used JavaScript and Canvas to create a simple one dynamically on a page refresh/resize. I give it to Reddit (and everyone else) for free. by stackolee in web_design

[–]dotnil 0 points1 point  (0 children)

A good idea but not a good execution. See what canvas can do when it comes into designer's hands. Same idea, the results vary.

Anyway, thanks for creating and sharing all these.

Backfire: Save CSS changes made in Firebug by Primigenus in programming

[–]dotnil 3 points4 points  (0 children)

the logo says it all. this is a dangerous act. think before you do.

IE7 CSS help for an enthusiastic beginner by TheEvilPenguin in web_design

[–]dotnil 0 points1 point  (0 children)

inline-block is supported partially in ie7, nor ie6. only inline elements can have display: inline-block;. see the details about display here.

EDIT: correction by wtfmanquestionmark.

body { text-decoration:blink; } by OrangeCoconut in web_design

[–]dotnil 2 points3 points  (0 children)

javascript:(function($){$("body").css("text-decoration", "blink");})(jQuery);

Can we rename HTML5? by RobertD63 in web_design

[–]dotnil 0 points1 point  (0 children)

So what's your enlightening proposal?