n a r w a l
alpha - do not use (yet)
narwal
(NodeJS) is a loosely coupled structure-data architecture for modeling and moving around your MySQL data.
Install
npm install narwal
Use
var narwal = ;
Overview
narwal
gives you model abstraction so you manipulate your MySQL data and structure easier.
// Create a model representation of a table called players// This table must exists with a matching structure as the one modelized var Player = 'Player' name: String score: Number joined: Date ;
SELECT name FROM players WHERE score > 100 LIMIT 10 ORDER BY joined DESC
Player // SELECT // column selection // WHERE score > 100 above 'score': 100 // LIMIT 10 // ORDER BY joined DESC // Do something with the results ;
INSERT INTO players (name) VALUES('Lara')
Player // INSERT INTO players (name) VALUES('Lara') // Do something with results ;
UPDATE players SET score=100 WHERE name='Lara'
Player
DELETE FROM players WHERE score > 100
Player // DELETE FROM players WHERE score = 100 ;
Model
constructor
Model Creates a new Narwal Model.
{Model} new narwal.Model(String name, Object? structure, Object? options);
Argument | Type | Required | Default | Description |
---|---|---|---|---|
name | String | ✔ | The name of the model | |
structure | Object | ✖ | {} |
The structure |
options | Object | ✖ | {} |
Model options |
'Player' name: String prefix: 'test_' ;
connect()
Model MySQL thread setter. Narwal models are connexion-agnostic. We use node-mysql default connection method for the moment. Future implementations to come.
{Model} Model.connect(String | Object);
var Player = ; Player; Player; // You can also a Narwal client var Client = Client; var client = 'mysql://user@localost/db'; Playerrows1000;
Events:
- error
Error
- connected
- disconnected
Helpers:
Name | Example | Description |
---|---|---|
connected | client().connected(Function connected) |
Listens on "connected" |
disconnected | client().disconnected(Function disconnected) |
Listens on "disconnected" |
create()
Model Create a table stucture from Model. Returns Query
.
{Query} Model.create(Object? options, Function? callback)
( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR NOT NULL, score INT NOT NULL DEFAULT 500) ENGINE=INNODB DEFAULT CHARSET utf-8
;
Events:
- error
Error
- success
Helpers:
Name | Example | Description |
---|---|---|
created | create().created(Function success) |
Listens on "success" |
filter()
Model Performs a filter query. Returns Query
.
{Query} Model.filter(Object filter)
SELECT FROM models WHERE field='value'
Model;
Events:
- error
Error
- success
[Row]
Helpers:
Name | Example | Description |
---|---|---|
found | find().found(Function success) |
Listens on "success" and [Row].length |
notFound | find().notFound(Function success) |
Listens on "success" and ! [Row].length |
forEach | find().forEach(function (model) { //... }}) |
Listens on "success" and for each [Row] |
find()
Model Performs a find query. Returns Find
.
{Query} Model.find(Mixed? filter)
// Find all Model; // Sugar for Model.find().filter(Object); Model; // Sugar for Model.find().limit(Number); Model;
Events:
- error
Error
- success
[Row]
Helpers:
Name | Example | Description |
---|---|---|
found | find().found(Function success) |
Listens on "success" and [Row].length |
notFound | find().notFound(Function success) |
Listens on "success" and ! [Row].length |
forEach | find().forEach(function (model) { //... }}) |
Listens on "success" and for each [Row] |
findById()
Model Performs a find query with a filter by id. Returns Query
.
Model.findById(Number)
// Find by id Model;
Events:
- error
Error
- success
[Row]
Helpers:
Name | Example | Description |
---|---|---|
found | findById().found(Function success) |
Listens on "success" and [Row].length |
notFound | findById().notFound(Function success) |
Listens on "success" and ! [Row].length |
findOne()
Model Performs a find query and returns first found. Returns Query. Success emits a Row object.
Model.findOne(Mixed? filter)
// Find one with no filter Model; // Sugar for Model.findOne().filter(Object); Model;
Events:
- error
Error
- success
Row
Helpers:
Name | Example | Description |
---|---|---|
found | findOne().found(Function success) |
Listens on "success" and Row |
notFound | findOne().notFound(Function success) |
Listens on "success" and ! Row |
limit()
Model Apply a limit filter. Returns Query. Success emits [Row]
.
{Query} Model.limit(Number limit).success([Row])
// Find 10 Model; // Find one (will return an array, even if it has only one row in it) Model;
Events:
- error
Error
- success
[Row]
Helpers:
Name | Example | Description |
---|---|---|
found | limit().found(Function success) |
Listens on "success" and [Row].length |
notFound | limit().notFound(Function success) |
Listens on "success" and ! [Row].length |
forEach | find().forEach(function (model) { //... }}) |
Listens on "success" and for each [Row] |