sn-persist
v0.1.4
Published
Persistence module for mysql & mongo.
Downloads
1
Readme
sn-persist
Easily persist node.js objects with MySQL, SQLite or Mongo.
Intro
Sn-persist adds an easy(ish) to use persistence tier to your node.js applications. It provides a regular interface between your application and common database drivers. Application developers use a simple JSON object to describe the schema of a database table or collection; sn-persist reads this description and creates a data access object to create, read, update and delete records in the collection.
Sn-persist is not an object-relational mapping package. It does not build a SQL table or Mongo collection from your object definitions. You still have to define the structure of tables or collections. But it does handle the mundane tasks of creating tables, building accessors and managing the connection to the database.
Though you still need to have knowledge of the underlying database technology, it does move database specific features into a declarative JSON initialization object. Application developers provide SQL or Mongo-specific setup data during initialization, but during steady state usage, a implementation-independent API is used to access data from the persistence tier.
Also, sn-persist binds records in a table (or collection) to a prototype object so database reads return objects with behavior, not blank maps of field-names to data.
Please note that sn-persist trades expressiblity for convenience. In reducing the complexity of dealing directly with the database, there are some complex database schemas which cannot be effectively modeled with this package. However, we have noted it effectively models the vast majority of database tables we have wanted to use. If you are building a simple or moderately complex web application, sn-persist is probably still a net win. In short, if you're not using a complex database schema, you won't miss what you aren't using.
Simple Example
What follows is a simple example of using sn-persist with MySQL. You might notice a few things about it. First off, it's kind of long for a "simple" example. Mea Culpa. But in my defense, let me remind you that a) there are a lot of comments, b) there's a fair amount of setup and c) it's not like database access has ever been a one or two line activity.
What we see in this example is:
- Setting up the persistence instance
- Building a model class to hold info we cribbed from the database and define it's behavior.
- How to use the DAO object returned from the init() call to access elements in the database.
- How to close down a database connection