Tuesday 9 February 2016

I think I finally created nice ActiveRecord for Node. Not production-, but at least already pet-project-ready.

Hi Reddit. I think I created something good and I hope on feedback from you :3I call it Active-Graph-Record as it works over graph DB and is an Active Record pattern implementation. I know I'm bad on namingFirst of all, github and npm.In brief:Actual ActiveRecord: create an instance, assign properties, call #save() and it's reflected.neo4j as DB (reasons below)no schemanice relations (even many-to-many-through out of the box)fully promise-based (well, actually, it's async/await-based, which is, actually, same)At glance- just a code sample. Some stuff like DB connection omitted. It's really simple too, I promise. It's just a short example.Well, why neo4j and why everything is looking slightly weird?I've started to look for something to work with any DB. Something simple, something that does not require a schema - so I can just pass object and it will be stored, I can query anything and I'll get it.I found nothing, so I started working over it. Actually, I did it first over Redis (even with relations), but then I understood I'm doing slightly crazy (due to too much Lua code auto-generation magic) so I started searching next.I had following criteria:string, number and bool native types (storing bools and numbers as strings in redis seemed like a bad idea)transactions (and ACID in general for consistency)no schema (schema is nice, but for pet project it's only slowing everything down, bringing its own pseudo-syntax and making you build whole infrastructure inside the app, all schema-based solutions smells nearly like yeoman - it's working, but it brings you bad feelings. Auto-generation from console is always less obvious than actual code)nice relations so I would not have to create them from scratchNeo4j was funny solution I've used for dumping some social data, and I tried to give it a chance. Pros werenice type system: strings, numbers, bools, and (!!!) typed arrays of every kindawesome relations engine (how about to simple syntax for query of relation depth of 5 to 8 edges? On SQL it would be slightly crazy)But there are consIt's actually slower. But not very much (1.8 times or so if properly configured), and it's linearly slower (not O(log n) or O(n * 2)), which is not very bad.No scaling - you are limited to single server if not paying for enterprise solutionSingle-threaded (critical for long transactions)I've worked a lot on it and now it has a lot of features. Being something like Rails's ActiveRecord is very far, but I have all kinds of relations, querying, transactions and handy API.However, I have a feeling it's a great solution for rather small projects. I've tried it on my own and I was happy, as it really brings comfort and peace of mind for me.It's definitely beta now, I'm pretty sure there are some bugs, so if you will create a PR, an issue for writing some tests or propose a functionality, it would be great. So, to conclude, one more time - http://ift.tt/1KDBeA1! :)

Submitted February 09, 2016 at 08:19PM by _jabher

No comments:

Post a Comment