Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
danielstocks committed Jan 26, 2016
1 parent cc6807f commit b657511
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 14 deletions.
37 changes: 31 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,37 @@ Inspired by [Backbone.js](http://backbonejs.org/) and [Her](http://www.her-rb.or
Yak requires Node.js >= 4.0. To run in a web browser environment you'll need native or polyfilled support for
ES6 Promises, Object.assign, Fat Arrow Syntax, and the window.fetch API.

Run `npm install yak-orm` and then require it in your project:
Run `npm install yak-orm` and then simply require it in your project.


## Example

```js
var Yak = require('yak-orm');
var yak = new Yak({
host: "http://localhost:8080/"
});
```

You should now be ready to go!
var Fruit = yak.model({
name: "fruits"
});

// GET http://localhost:8000/fruits?color=red
// returns { fruits: [{ name: "apple", name: "pomegranate"}] }
Fruit.all({
where: {
color: 'red'
}
}).then(fruits => {
fruits.forEach(fruit => {
console.log(fruit.attrs.name);
});
});
```

To run Yak in the web browser it's recommended that you bundle it with Browserify or Webpack.

## Yak is a work in progress
### Yak is a work in progress

Features that are on the roadmap but *not yet* currently implemented include:

Expand All @@ -52,20 +70,27 @@ Create a new Yak instance
var yak = new Yak({
host: "http://localhost:8080/"
});

```

### yakInstance.model

Create a new model based on a Yak instance

#### Arguments
- url (String) | The URL of specified model on API server
- name (String) | The name of the resource specified on API server
- url (String, optional) | Resource URI is generated based on name but can be overridden by passing this argument
- parse (Function(attrs (Object) ), optional) | This method will be called every time a new instance of the model is created. It will receive the model attributes as an argument, allowing you to mutate them before model is created.

#### Example
```js
// Model defintion
var User = yak.model({
url: "users"
name: "users",
parse: function(attrs) {
attrs.fullName = attrs.firstName + " " + attrs.lastName;
return attrs;
}
});
```

Expand Down
36 changes: 33 additions & 3 deletions examples/basic-usage.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,53 @@
var Yak = require('../index.js');


process.on('unhandledRejection', function(reason, p) {
console.log("Unhandled Rejection:", reason.stack);
process.exit(1);
});


// Create new Yak instance
var yak = new Yak({
host: "http://localhost:8080/"
})
});


// Model defintion
var User = yak.model({
url: "users"
name: "users",
parse: function(attrs) {
if(attrs.comments) {
attrs.comments = attrs.comments.map(function(comment) {
return new Comment({ attrs: comment});
});
}
return attrs;
}
});

var Comment = yak.model({
name: "comments",
parse: function(attrs) {
attrs.yearsAgo = Math.abs(
new Date(Date.parse(attrs.postedAt)).getFullYear() - new Date().getFullYear()
);
return attrs;
}
})

// Get existing user
var user = User.get({
id: 1,
headers: {
'Accept-Language' : 'fa',
}
}).then(user => {
console.log("Retrieved user:", user.attrs);
console.log("Retrieved user:", user.attrs.name);
console.log("-- comments --");
user.attrs.comments.map(function(comment) {
console.log(comment.attrs.body, "| posted", comment.attrs.yearsAgo, "years ago")
});
}).catch(error => {
console.log(error);
});
Expand Down
22 changes: 19 additions & 3 deletions examples/mock-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,27 @@ server.use(restify.queryParser());
server.use(restify.bodyParser());

server.pre(function (req, res, next) {
console.log("\n-- ", req.method, "---", req.url);
console.log("\n-- ", req.method, "--", req.url);
console.log(req.headers);
next();
});

var id = 2;
var users = [{ id: 1, name: "John Doe", email: "john@doe.com" }]
var users = [{
id: 1,
name: "John Doe",
email: "john@doe.com",
comments: [
{
postedAt: "1945-12-20",
body: "hey ho let's go"
},
{
postedAt: "1946-04-17",
body: "you're out of your mind"
}
]
}]

function getUser(id) {
return users.find(function(user) {
Expand All @@ -39,7 +53,9 @@ server.post('/users', function (req, res, next) {

// Get users
server.get('/users', (req, res, next) => {
res.send(users);
res.send({
users: users
});
return next();
});

Expand Down
6 changes: 4 additions & 2 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ var request = require('./request');
function model(options) {
var self = this;
var host = this.host;
var endpoint = host + options.url;
var endpoint = host + (options.url || options.name);
this.errorHandler = function(reject, error, status) {
return reject(error);
}
var fn = function(obj) {
var attrs = options.parse ? options.parse(obj.attrs) : obj.attrs;
this.errorHandler = self.errorHandler;
_.extend(this, {
host: host,
attrs: _.cloneDeep(obj.attrs) || {},
attrs: _.cloneDeep(attrs) || {},
headers: obj.headers || {},
endpoint: endpoint,
request: request,
Expand All @@ -32,6 +33,7 @@ function model(options) {
var qs = 'where' in obj ? '?' + toQueryString(obj.where) : '';
return request('GET', endpoint + qs, {
success: function(json) {
json = options.name ? json[options.name] : json;
return json.map(attrs => {
return new fn({ attrs: attrs });
});
Expand Down

0 comments on commit b657511

Please sign in to comment.