Skip to content

Commit

Permalink
Merge v0.10 into generators.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikermcneil committed Jan 16, 2014
2 parents 746e72b + 98188d3 commit 5b54e12
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 215 deletions.
243 changes: 38 additions & 205 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,249 +1,80 @@
# Sails.js
# [![Sails.js](http://balderdashy.github.io/sails/images/logo.png)](http://sailsjs.org)

[![NPM version](https://badge.fury.io/js/sails.png)](http://badge.fury.io/js/sails)


![[email protected]](http://i.imgur.com/RIvu9.png)
### [Website](http://sailsjs.org/)   [Getting Started](http://sailsjs.org/#!getStarted)   [Documentation](http://sailsjs.org/#!documentation)   [Submit Issue](https://github.com/balderdashy/sails/search?q=&type=Issues)

> Sails.js makes it easy to build custom, enterprise-grade Node.js apps. It is designed to resemble the MVC architecture from frameworks like Ruby on Rails, but with support for the more modern, data-oriented style of web app development. It's especially good for building realtime features like chat.
Sails.js is a web framework that makes it easy to build custom, enterprise-grade Node.js apps. It is designed to resemble the MVC architecture from frameworks like Ruby on Rails, but with support for the more modern, data-oriented style of web app development. It's especially good for building realtime features like chat.


## Installation

With [node](http://nodejs.org) installed:
**With [node](http://nodejs.org) [installed](http://sailsjs.com/#!documentation/new-to-nodejs):**
```sh
sudo npm install sails -g
# Get the latest stable release of Sails
$ sudo npm install sails -g
```


## Your First Sails Project

Create a new app:
**Create a new app:**
```sh
# Create the app
sails new testProject
$ sails new testProject
```

Start the server:
**Lift sails:**
```sh
# cd into the new folder
cd testProject

# Fire up the server
sails lift
```

<!--
The default port for Sails is 1337, so at this point, if you visit <a href="http://localhost:1337/">http://localhost:1337/</a>, you'll see the default home page.
Now, let's get Sails to do cool stuff.
## Creating a RESTful JSON API
Sails allows you to generate a powerful RESTful JSON API using the command line tool. This is exactly what you need for [AJAX web pages](http://irlnathan.github.io/sailscasts/blog/2013/10/10/building-a-sails-application-ep22-manipulating-the-dom-based-upon-changes-via-real-time-model-events/), [realtime apps](http://lanyrd.com/2013/nodepdx/video/), [SPAs](https://www.youtube.com/watch?v=Di50_eHqI7I), [Backbone apps](http://net.tutsplus.com/tutorials/javascript-ajax/working-with-data-in-sails-js/), [Angular apps](https://github.com/rdroro/tulipe-personal-todolist), [Cordova/PhoneGap apps](https://groups.google.com/forum/#!topic/sailsjs/o7HaB0rvSKU), [native mobile apps](https://github.com/aug2uag/SampleAppiOS), [refrigerators](https://www.youtube.com/watch?v=tisWSKMPIg8), [lamps](https://www.youtube.com/watch?v=OmcQZD_LIAE), etc.
Without writing any code, Sails supports:
+ filtering (`where`)
+ search (`or`, `and`, `in`, `startsWith`, `endsWith`, `contains`, `greaterThan`, `lessThan`, `not`)
+ sorting (`sort`)
+ pagination (`limit`, `skip`, `sort`)
+ JSONP
+ CORS
+ csrf protection
Best of all, all of these things work with both HTTP _and_ WebSockets, and work across any of the supported database adapters, including PostgreSQL, MongoDB, and MySQL. Authentication and access control are implemented using [policies](https://github.com/balderdashy/sails-docs/blob/0.9/policies.md). More on all that stuff here:
[![Creating a REST API with Sails.js](http://i.imgur.com/drtMlWH.png)](//www.youtube.com/embed/xlOolpwwGQg?feature=player_embedded) [![Original Sails.js Screencast from March 2013](http://balderdashy.github.com/sails/images/screenshot.png)](http://youtu.be/GK-tFvpIR7c)
---------------------------------------------------------------------------------
###### Enough talk! Let's generate a User API.
We'll need an empty model and controller:
```
sails generate user
```
If you check out your app, you'll notice that this created a file at **/api/models/User.js** and **/api/controllers/UserController.js**.
Now, if you send a POST request to `http://localhost:1337/user` or visit `http://localhost:1337/user/create`, you'll see:
```json
{
"createdAt": "2013-01-10T01:33:19.105Z",
"updatedAt": "2013-01-10T01:33:19.105Z",
"id": 1
}
```
That's it! You just created a model in the database! You can also `find`, `update`, and `destroy` users:
```bash
# List of all users
http://localhost:1337/user
# Find the user with id 1
http://localhost:1337/user/1
# Create a new user
http://localhost:1337/user/create?name=Fisslewick
(or send an HTTP POST to http://localhost:1337/user)
# Update the name of the user with id 1
http://localhost:1337/user/update/1?name=Gordo
(or send an HTTP PUT to http://localhost:1337/user/1)
# Destroy the user with id 1
http://localhost:1337/user/destroy/1
(or send an HTTP DELETE to http://localhost:1337/user/1)
```
$ cd testProject

> #### JSONP, CORS, CSRF?
> This built-in API bundles optional support for JSONP-- and in general, Sails has built-in support for CORS, and CSRF protection.
> See your project's `config/cors.js`, `config/csrf.js`, and `config/controllers.js` files for more options.
## Custom Controllers
These automatically generated URL routes are called "blueprints". Blueprints may be disabled, pluralized, or prefixed globally or on a per-controller basis.
But what if you need more customized logic?
Say, your `UserController.create` needs to also send a confirmation email-- no problem.
Just write a custom `create` method in your `UserController` and it will be available using the same blueprint routes (e.g. `POST /user`)
Custom controllers are just Express middleware, the de facto standard for writing server code in Node.js.
e.g.
```javascript
// api/controllers/UserController
module.exports = {
/**
* @param {String} email
* @param {String} name
*/
create: function (req, res) {
User.create({
name: req.param('name'),
email: req.param('email')
})
.exec(function userCreated(err, newUser) {
// Bail out if there's an error!
// (this will use the app-global logic in config/500.js)
if (err) return res.serverError(err);
// Send some email
require('my-favorite-email-module').send({
html: 'Well that\'s neat.',
to: newUser.email
});
sails.log('New user created successfully!');
sails.log.verbose('Confirmation email sent to', newUser.email);
// Send JSON response
return res.json(newUser);
})
}
};
# fire up the server
$ sails lift
```

**Generate a REST API:**

[![ScreenShot](http://i.imgur.com/Ii88jlhl.png)](https://www.youtube.com/watch?v=GK-tFvpIR7c)

> Worth noting is that the custom controller above still supports WebSockets out of the box, since Sails will actually simulate `req` and `res` objects when it receives properly-formatted messages from Socket.io.
> Check out `assets/js/app.js` in your project for an example of how to use Socket.io to talk to your Sails backend.
## Custom Routes
You can also define custom routes, controllers, and controller methods (aka "actions").

```sh
sails generate controller hello index
```
## Compatibility

This will generate a file called `HelloController.js` in your app's `api/controllers` directory with one action, `index()`.
Now let's edit that action to send back the string `'Hello World!'`.
[![NPM version](https://badge.fury.io/js/sails.png)](http://badge.fury.io/js/sails)

```javascript
// api/controllers/HelloController.js
module.exports = {
Sails is built on Node.js, Connect, Express, and Socket.io.

index: function(req, res) {
// Here, you can do all the Express/Connect things!
res.send('Hello World!');
}
};
```
Sails [controllers](http://sailsjs.org/#!documentation/controllers) are compatible with Connect middleware, so in most cases, you can paste code into Sails from an existing Express project and everything will work-- plus you'll be able to use WebSockets to talk to your API, and vice versa.

The ORM, [Waterline](github.com/balderdashy/waterline), has a well-defined adapter system for supporting all kinds of datastores. Officially supported databases include **[MySQL](https://github.com/balderdashy/sails-mysql)**, **[PostgreSQL](https://github.com/balderdashy/sails-postgresql)**, **[MongoDB](https://github.com/balderdashy/sails-mongo)**, **[Redis](https://github.com/balderdashy/sails-redis)**, local [disk](https://github.com/balderdashy/sails-disk), and local [memory](https://github.com/balderdashy/sails-memory). [Community adapters](https://github.com/balderdashy/sails-docs/blob/master/intro-to-custom-adapters.md#notable-community-adapters) exist for Riak, CouchDB, and ElasticSearch; for various 3rd-party REST APIs like Yelp and Twitter; plus some [eclectic projects](https://www.youtube.com/watch?v=OmcQZD_LIAE).

Let's say we want the application to display this hello response specifically when a request comes in for `http://localhost:1337/hi`.
Go into the **/config/routes.js** file and add a route like this:

```javascript
// config/routes.js
module.exports = {
'/hi': 'HelloController.index'
};
```

Finally, restart the server by going to your node terminal and pressing control+c. Then enter the following.

<!--
Generate a JSON API:
```sh
sails lift
```
Now when you visit <a href="http://localhost:1337/hi">http://localhost:1337/hi</a>, or send a Sails-formatted Socket.io message to `/hi`:
```
// Try this from the Chrome/Firebug javascript console on your app's home page:
socket.get('/hi', function (response) { console.log(response); });
```
You'll see:
```
Hello World!
# generate a user model + controller (i.e. a User API)
sails generate user
```
-->

<!--
> Sails provides "blueprint routes" (URL mappings) and "blueprint actions" (built-in CRUD and pubsub operations) for every controller+model in your app. These "blueprints" can be configured and/or completely disabled as needed. Notably, all logic in Sails (including blueprint actions) supports both WebSockets and HTTP out of the box.
## Documentation & Resources
#### Official Documentation
[Docs](http://github.com/balderdashy/sails-docs)
#### FAQ
https://github.com/balderdashy/sails/wiki
#### SailsCasts
Short screencasts that take you through the basics of building traditional websites, single-page/mobile apps, and APIs using Sails. Perfect for both novice and tenured developers, but does assume some background on MVC:
[SailsCasts](http://irlnathan.github.io/sailscasts/)
#### Google Group
If you have questions, ideas, or run into a problem, post it to our google group-- someone there might be able to help you.
[Sails.js Google Group](https://groups.google.com/forum/?fromgroups#!forum/sailsjs)
#### IRC
We're [#sailsjs on freenode](http://webchat.freenode.net/)
+ List all users ([http://localhost:1337/user](http://localhost:1337/user))
+ Find the user with id 1 ([http://localhost:1337/user/1](http://localhost:1337/user/1))
+ Create a new user ([http://localhost:1337/user/create?name=Fisslewick](http://localhost:1337/user/create?name=Fisslewick), or `POST to http://localhost:1337/user`)
+ Update the name of the user with id 1 ([http://localhost:1337/user/update/1?name=Gordo](http://localhost:1337/user/update/1?name=Gordo), or `PUT http://localhost:1337/user/1`)
+ Destroy the user with id 1 (visit [http://localhost:1337/user/destroy/1](http://localhost:1337/user/destroy/1), or `DELETE http://localhost:1337/user/1`)
-->


## Issue Submission
Make sure you've read the [issue submission guidelines](https://github.com/balderdashy/sails/blob/master/CONTRIBUTING.md#opening-issues) before opening a new issue.

Sails is composed of a number of different sub-projects, many of which have their own dedicated repository. If you are looking for a repo for a particular piece, you'll find it on the [organization](https://github.com/balderdashy) page.
Sails is composed of a [number of different sub-projects](https://npmjs.org/search?q=sails), many of which have [their own dedicated repository](https://github.com/search?q=sails+user%3Amikermcneil+user%3Abalderdashy+user%3Aparticlebanana&type=Repositories&ref=advsearch&l=). If you are looking for a repo for a particular piece, you'll usually find it on the [organization](https://github.com/balderdashy) page.

## Feature Requests
Feature requests should be submitted to the [repo](https://github.com/balderdashy) it concerns. Submit to [balderdashy/sails](https://github.com/balderdashy/sails) if you're unsure.
See the [Trello board](https://trello.com/b/cGzNVE0b/sails-js-feature-requests) to view/discuss our roadmap and [request features](https://github.com/balderdashy/sails/blob/master/CONTRIBUTING.md#requesting-features).

## Contribute
See the [contributing docs](https://github.com/balderdashy/sails/blob/master/CONTRIBUTING.md).
Expand All @@ -252,9 +83,10 @@ See the [contributing docs](https://github.com/balderdashy/sails/blob/master/CON
## Support
Need help or have a question?

- [Tutorials](https://github.com/balderdashy/sails-docs/blob/master/FAQ.md#where-do-i-get-help)
- [Stackoverflow](http://stackoverflow.com/questions/tagged/sails.js)
- [#sailsjs](http://webchat.freenode.net/) on Freenode (IRC channel)
- [Professional/Enterprise Options](https://github.com/balderdashy/sails/wiki#are-there-professional-support-options)
- [#sailsjs on Freenode](http://webchat.freenode.net/) (IRC channel)
- [Professional/Enterprise Options](https://github.com/balderdashy/sails-docs/blob/master/FAQ.md#are-there-professional-support-options)

_Please don't use the issue tracker for support/questions._

Expand All @@ -263,7 +95,7 @@ _Please don't use the issue tracker for support/questions._
- [Official Documentation](http://sailsjs.org/#!documentation)
- [Changelog](https://github.com/balderdashy/sails-docs/blob/0.9/changelog.md)
- [Roadmap](https://github.com/balderdashy/sails-wiki/blob/0.9/roadmap.md)
- [Google group](https://groups.google.com/forum/?fromgroups#!forum/sailsjs)
- [Google Group](https://groups.google.com/forum/?fromgroups#!forum/sailsjs)
- [Twitter](https://twitter.com/sailsjs)
- [SailsCasts](http://irlnathan.github.io/sailscasts/)

Expand All @@ -285,5 +117,6 @@ Balderdash designs/builds scalable Node.js apps for startups and enterprise cust

> Sails is built around so many great open-source technologies that it would never have crossed our minds to keep it proprietary. We owe huge gratitude and props to TJ Holowaychuk ([@visionmedia](https://github.com/visionmedia)) and Guillermo Rauch ([@guille](https://github.com/guille)) for the work they did, as well as the stewards of all the other open-source modules we use. Sails could never have been developed without your tremendous contributions to the node community.
![[email protected]](http://i.imgur.com/RIvu9.png)

[![githalytics.com alpha](https://cruel-carlota.pagodabox.com/8acf2fc2ca0aca8a3018e355ad776ed7 "githalytics.com")](http://githalytics.com/balderdashy/sails)
1 change: 0 additions & 1 deletion lib/hooks/pubsub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ module.exports = function(sails) {
});
}


function broadcast(room) {
sails.log.silly("Published ", message, " to ", room);
if (socketToOmit && socketToOmit.broadcast) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"lib": "lib"
},
"dependencies": {
"waterline": "git://github.com/balderdashy/waterline.git#associations",
"waterline": "git://github.com/balderdashy/waterline.git#v0.10",
"sails-disk": "git://github.com/balderdashy/sails-disk.git#associations",
"sails-util": "~0.10.0",
"captains-log": "~0.10.6",
Expand Down
23 changes: 15 additions & 8 deletions test/integration/load.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,23 @@ describe.skip('Configs', function() {
});
});

it('should load adapter configs', function() {
var conf = config.adapters;
assert(conf.
default.module === 'sails-disk');
assert(conf.custom && conf.custom.module === 'sails-disk');
assert(conf.sqlite.module === 'sails-sqlite');
assert(conf.sqlite.host === 'sqliteHOST');
assert(conf.sqlite.user === 'sqliteUSER');
it('should retain legacy `config.adapters` for backwards compat.', function() {
var legacyConfig = config.adapters;
assert(legacyConfig.custom && legacyConfig.custom.module === 'sails-disk');
assert(legacyConfig.sqlite.module === 'sails-sqlite');
assert(legacyConfig.sqlite.host === 'sqliteHOST');
assert(legacyConfig.sqlite.user === 'sqliteUSER');
});

// it('should load connection configs', function() {
// var connectionsConfig = config.connections;
// assert(config.model.adapter === 'sails-disk');
// assert(connectionsConfig.custom && connectionsConfig.custom.module === 'sails-disk');
// assert(connectionsConfig.sqlite.module === 'sails-sqlite');
// assert(connectionsConfig.sqlite.host === 'sqliteHOST');
// assert(connectionsConfig.sqlite.user === 'sqliteUSER');
// });



it('should load application configs', function() {
Expand Down

0 comments on commit 5b54e12

Please sign in to comment.