ActiveRecordjs
Rails ActiveRecord inspired for Nodejs.
VERSION
0.1.6
Instalation
npm install active_record
Setup
Configure Connection:
var ActiveRecord = ; ActiveRecordBase;
or
ActiveRecordBaseconnection; // Set Enviroment; *Default: 'dev'ActiveRecordBase; /* JSON file example: */ "dev": "driver": "mysql" "hostname": "localhost" "port": "3306" "user": "root" "password": "" "database": "chat" "test": "driver": "sqlite3" "filename": "./database" "prod": "driver": "postgres" "user": "test" "password": "test" "hostname": "localhost" "database": "mydb"
Establish the connection:
ActiveRecordBase;
Usage
Model:
Equal to the RAILS each model is a separate file. But you can create a single connection file and configuration of models, as follows in the examples below. Takes also that the file names are singular.
Example 1
: For each model create a separate file.
var ActiveRecord = ;/* Exports the module */moduleexports = User/* Extends the module to ActiveRecord.Base */ActiveRecordBase/* Create the Class */{/* Initialize the instance variables */this;/** Validations */thisthis;this; /* Call this function after another validations */}/* Configure the model */Usertable_name = 'users';User; // Create dynamics finders: User.find_by_name, etc./* Configure the Associations */User;
Example 2
: Configuration in a single file
var ActiveRecord = ;ActiveRecordBase;ActiveRecordBase;/* Don't forget: Exports the models */exportsUser = User;exportsPhone = Phone; ActiveRecordBase;ActiveRecordBase; { this;} { this;} User;Phone; ActiveRecordBase;
CRUD:
/* Create, Update, Destroy */UserUserUser/* Finders */UserUser/* Calculations */User/** Associations* User.has_many('phones') */User/* Callbacks */User
API
ActiveRecord
ActiveRecord
Global config options
logger
:Not implemented
primary_key_prefix_type
:table_name_prefix
:table_name_suffix
:pluralize_table_names
:default_timezone
:record_timestamps
:cache_query
:max_number_of_listeners
:
Example
:
var ActiveRecord = ;ActiveRecord;
or
ActiveRecord;
ActiveSupport
Inflector
:@methods
:inflections
*plural
,singular
,uncountable
Example:
var ActiveSupport = ActiveSupport;ActiveSupportInflector;moduleexports = ActiveSupport;
Base
@alias ActiveRecord.Base
@methods
:
Base.configure_connection
alias ofBase.connection.config
Base.estabilish_connection
alias ofBase.connection.connect
Base.close_connection
alias ofBase.connection.disconnect
Base.extend
configure_connection(config)
Base
estabilish_connection()
Base
close_connection()
Base
extend(destination, [source])
moduleexports = UserBase{ // ...}
Class Methods
Finders
@methods
: find
, first
, last
, all
, exists
, where
, join
, find_by_sql
, find_by_id
, fields
.
find(id, [conditions], [callback])
@conditions
operators: is
, is_not
, like
, not_like
, gt
, gte
, lt
, lte
,
between
, exists
, not_exists
, some
, all
, in
, not_in
.
Example:
User;//> SELECT * FROM users WHERE `id` = 1 User;//> SELECT * FROM users WHERE `id` IN (1,2,3,4) User;//> SELECT `name`, `password` FROM users WHERE `name` LIKE '%foo%' LIMIT 1 User;//> SELECT * FROM users LIMIT 1 ORDER BY `id` ASC User;//> SELECT * FROM users WHERE `name` IS NOT 'foo' LIMIT 1 ORDER BY `id` DESC User;//> SELECT * FROM users WHERE `password` = 'bar' User;//> SELECT * FROM users WHERE `name` LIKE '%foo%'
first([conditions], [callback])
@alias find('first', [conditions], [callback])
User;//> SELECT * FROM users WHERE `id` > 10
last([conditions], [callback])
@alias find('last', [conditions], [callback])
User;//> SELECT * FROM users WHERE `name` LIKE '%foo%';
all([conditions], [callback])
@alias find('all', [conditions], [callback])
Userall{ ... };//> SELECT * FROM users
exists([conditions], [callback])
User;//> SELECT 1 FROM users WHERE `id` = 1 User;//> SELECT 1 FROM users WHERE `name` = 'foo'
join(table_name, [conditions], [callback])
User;//> SELECT * FROM users LEFT JOIN phones ON phones.user_id != users.id User;//> SELECT * FROM users INNER JOIN phones ON `phones`.`id` = `user`.`id` User;//> SELECT * FROM users INNER JOIN phones ON `phones`.`user_id` = `user`.`id` User//> SELECT * FROM users INNER JOIN phones ON phones.user_id = users.id WHERE `name` = 'foo'
find_by_sql(sql, [values], [callback])
User
find_by_id(id, [callback])
User//> SELECT * FROM users WHERE `id` = 1
field(*fields)
Create dynamic finders
User; User;//> SELECT * FROM users WHERE `name` = 'foo' User;//> SELECT * FROM users WHERE created_at = 'Thu Dec 11 2014 22:32:45 GMT-0300 (AMST)'
Calculations
@methods
: count
, average
alias avg
, minimum
alias min
, maximum
alias max
, sum
.
count([column_name], [conditions], [callback])
User;//> SELECT COUNT(*) FROM users User;//> SELECT COUNT(*) FROM users WHERE `name` LIKE '%foo%'
average([column_name], [conditions], [callback])
@alias avg
User;//> SELECT AVG(id) FROM users User;//> SELECT AVG(id) FROM users WHERE `name` LIKE '%foo%'
minimum([column_name], [conditions], [callback])
@alias min
User;//> SELECT MIN(id) FROM users User;//> SELECT MIN(id) FROM users WHERE `name` LIKE '%foo%'
maximum([column_name], [conditions], [callback])
@alias max
User;//> SELECT MAX(id) FROM users User;//> SELECT MAX(id) FROM users WHERE `name` LIKE '%foo%'
sum([column_name], [conditions], [callback]);
User;//> SELECT SUM(id) FROM users User;//> SELECT SUM(id) FROM users WHERE `name` LIKE '%foo%'
Actions
@methods
: create
, update
, update_all
, destroy
, destroy_all
, delete
, delete_all
.
create(params, [callback])
User;//> INSERT INTO users SET `name` = 'Akrata', `password` = '79a1a1b8ee1b831a27db58089cbf298dc38f3eec', `updated_at` = '2014-12-13 11:07:11', `created_at` = '2014-12-13 11:07:11'
update(id, params, [callback])
User;//> UPDATE users SET `name` = 'Fooo', `password` = '0f962118caa0122e7b7c9b1266cecf77918c9f65', `updated_at` = '2014-12-13 11:19:24' WHERE `id` = 1
update_all(conditions, params, [callback])
User;
destroy(id, [callback])
Find the record before destroy
User;//> SELECT * FROM users WHERE id = 1// if record//> DESTROY FROM users WHERE id = 1
destroy_all([conditions], [callback])
Find all record before destroy
User;//> SELECT * FROM users WHERE id > 10// if record > 0//> DESTROY FROM users WHERE id = @record.id
delete(id, [callback])
Like destroy() method, but don't find.
User;//> DELETE FROM users WHERE id = 1
delete_all([conditions], [callback])
Like destroy_all() method, but don't find.
User;//> DESTROY FROM users WHERE id > 10
Callbacks
@methods
: before_create
, before_update
, before_destroy
, before_find
.
Instance Methods
Attributes Methods
@methods
: initialize
, set
, get
, to_object
, to_json
, keys
, values
, attributes
, is_valid
,
changed
, toString
, save
, update_attributes
, destroy
, errors
: { add
, remove
, clear
, set
, get
, any
, size
, full_messages
}.
initialize(attributes)
Initialize instance variables
var user = ;user; // return ERROR// initialize variablesuser;user;
set(key, value)
var user = ;user;user;user; // return 'Foo'console; user;user; // return 'bar' //** DON'T USE **//username = 'Foo';
Validations
@methods
: validates
, validates_with
, validate_presence_of
, validate_uniqueness_of
, validate_numericality_of
, validate_length_of
, validate_format_of
, validate_exclusion_of
, validate_inclusion_of
, validate_confirmation_of
, has_secure_password
: { authenticate
}
{ this; this this}
Callbacks
@methods
: before_create
, after_create
,before_update
, after_update
, before_destroy
, after_destroy
, before_find
, after_find
.
before_create(record)
after_create(response, record)
before_update(record, new_values, old_values);
after_update(response, record)
before_destroy(record)
after_destroy(response, record)
There are two way to configure the callback. Config in class Example:
/* Before update */User /* After create */User
Config in instance
{ this; this; { if thisname == 'root' thiserrors; }}
License
GPLv3