SailsJS Cheat Sheet

A Programming › JavaScript cheat sheet in English by ProLoser

Quick reference for the Node.JS framework SailsJS

30,004 views 6,443 downloads 2 Jan 2015 History
3.7 avg · 0 ratings

Model Valida­tions

after: {Date object}
alpha: {boolean}
alphad­ashed: {boolean}
alphan­umeric: {boolean}
alphan­ume­ric­dashed: {boolean}
array: {boolean}
strings formatted as arrays don't pass
before: {Date object}
boolean: {boolean}
strings fail
contains: {strin­g?R­egEx?}
credit­card: {boolean}
date: {boolean}
datetime: {boolean}
decimal: {boolean}
email: {boolean}
empty: {boolean}
equals: {mixed}
=== comparison
falsey: {boolean}
finite: {boolean}
can be coerced to a finite number
float: {boolean}
hexade­cimal: {boolean}
hexColor: {boolean}
in: {array}
int: {boolean}
alias for integer
integer: {boolean}
ip: {boolean}
IPv4 or IPv6
ipv4: {boolean}
ipv6: {boolean}
is: {RegEx}
json: {boolean}
len: {?}
is integer > param1 && < param2. (Where are params defined?)
lowercase: {boolean}
max: {number}
minLength: {int}
not: {RegEx}
notCon­tains: {strin­g?R­egEx?}
notEmpty: {boolean}
notIn: {strin­g|a­rray}
notNull: {boolean}
notRegex: {boolean?}
null: {boolean}
number: {boolean}
NaN is considered a number
numeric: {boolean}
string contains only numbers
object: {boolean}
regex: {boolean?}
protected: {boolean}
remove attribute when toJSON is called on instance
required: {boolean}
during creation only
string: {boolean}
text: {boolean}
truthy: {boolean}
undefined: {boolean}
unique: {boolean}
uppercase: {boolean}
url: {boolean}
urlish: {boolean}
/^\s([­^\/­]+.)­+.+­\s*$/g
uuid: {boolean}
UUID v3, v4 or v5
uuidv3: {boolean}
uuidv4: {boolean}

Websocket PupSub Methods

Message: {id,ve­rb:­'­mes­sag­ed',­data}
Message: {id,ve­rb:­'­cre­ate­d',data}
Message: {id,ve­rb:­'­upd­ate­d',d­ata­:ob­jec­t,p­rev­iou­s:o­bject}
Message: {id,ve­rb:­'­add­edT­o',a­ttr­ibu­te,­add­edId}
Message: {id,ve­rb:­'­des­tro­yed­'­,pr­evious}
Message: {id,ve­rb:­'­rem­ove­dFr­om',­att­rib­ute­,re­mov­edId}
Subscribes to record(s) changes. Works for socket requests only.
Unsubs­cribes from record(s) changes. Works for socket requests only.
Subscribe to publis­hCr­eate(). Works for socket requests only.
Unsubs­cribe to publis­hCr­eate(). Works for socket requests only.

Model Lifecycle Callbacks

before­Val­idate: {fn(va­lues, next)}
create and update
afterV­ali­date: {fn(va­lues, next)}
create and update
before­Create: {fn(va­lues, next)}
afterC­reate: {fn(ne­wly­Ins­ert­edR­ecord, next)}
before­Update: {fn(va­lue­sTo­Update, next)}
afterU­pdate: {fn(up­dat­edR­ecord, next)}
before­Des­troy: {fn(cr­iteria, next)}
afterD­estroy: {fn(de­str­oye­dRe­cords, next)}

Waterline Model Methods

query(query, callba­ck(err, results))
Only works with PostgreSQL and mySQL
native(callb­ack­(err, collec­tion))
Only works with MongoDB. For low-level usage do requir­e('m­ong­odb­') directly.
count([crit­eria], [callb­ack]) {query}
find(criteria, [callb­ack]) {query}
findOne(criteria, [callb­ack]) {query}
findOr­Create(criteria, [callb­ack]) {query}
create(data, [callb­ack]) {query}
destroy(criteria, [callb­ack]) {query}
update(criteria, data, [callb­ack]) {query}
stream(criteria, [options]) {stream}
If a callback is not passed, most methods return chainable query object which ends with .exec(­cal­lback)

Waterline Record Methods

Add a many2many relati­onship
Remove a many2many relati­onship
Contains instance methods
Does not contain instance methods
persists any changes to the database. Required for add() / remove()

Model Settings

safe: never auto-m­igrate db. alter: auto-m­igrate db and attempt to keep existing. drop: drop all data on every lift.
only specified attributes are saved
lowercase version of filename
autoPK: {boolean}
autoCr­eat­edAt: {boolean}
autoUp­dat­edAt: {boolean}
tableName: {string}
table or collection to be used
attrib­utes: {object}

Model Attributes

type: {string}
 ­ ­string
 ­ text
 ­ ­integer
 ­ ­float
 ­ date
 ­ ­dat­etime
 ­ ­boolean
 ­ ­boolean
 ­ ­binary
 ­ ­array
 ­ json
 ­ ­email
defaul­tsTo: {mixed}
autoIn­cre­ment: {boolean}
requires type: '­int­ege­r'
unique: {boolean}
primar­yKey: {boolean}
enum: {array­[st­ring]}
size: {integer}
column­Name: {string}

Query Language

Where:
Mode.f­ind({ where: conditions })
 ­ { property: '­val­ue' }
 ­ { property: { 'comparison': '­val­ue' } }
Complex comparison options. Works for dates: { '­>': new Date('2­/4/­201­4') }
 ­ ­ { '­<' | '­les­sTh­an' : value }
 ­ ­ { '­<=' | '­les­sTh­anO­rEq­ual­' : value }
 ­ ­ { '­>' | '­gre­ate­rTh­an' : value }
 ­ ­ { '­>=' | '­gre­ate­rTh­anO­rEq­ual­' : value }
 ­ ­ { '­!' | '­not­' : value }
 ­ ­ { '­lik­e|c­ont­ain­s|s­tar­tsW­ith­|en­dsW­ith­': value }
like: '­sea­%rc­h' contains: '­%se­arc­h%' starts­With: '­sea­rch­%' endsWith: '­%se­arc­h'
 ­ { property: ['va­lue­1', '­val­ue2­'] }
Value in array
 ­ { property: { '­!' : ['va­lue­1', '­val­ue2­'] } }
Value not in array
 ­ { or: [ { prop1: '­val­1' }, { prop2: val2 } ] }
Limit:
Mode.f­ind({ limit: integer })
Skip:
Mode.f­ind({ skip: integer })
Sort:
Mode.f­ind({ sort: sortString })
 ­ { sort: '­pro­per­ty' | '­pro­perty ASC' }
 ­ { sort: '­pro­perty DESC' }
 ­ { sort: { '­pro­per­ty1­' : 1, '­pro­per­ty2­': 0 } }

Model Associ­ations

 ­ model: '­mod­el'
 ­ collec­tion: '­mod­el'
 ­ via: '­for­eig­nPr­ope­rty­Nam­e'
 ­ [dominant]: true

Badges

Useful Cheat Sheet Good Cheat Sheet Nice Cheat Sheet Downloaded from Unusual Locations × 2

Comments (2)

robertoandres24 09:29 12 Oct 2015

Thanks, very nice.

Reply
ProLoser 12:05 5 Jan 2015

@DaveChild do you know why these columns aren't distributing evenly? It's really messing things up.

Reply

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.