Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 200802ba
authored
Nov 02, 2013
by
Sascha Depold
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' into milestones/2.0.0
Conflicts: package.json
2 parents
518baccc
eda3d59e
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
85 additions
and
10 deletions
README.md
lib/dao-factory.js
lib/dao.js
lib/dialects/mysql/query-generator.js
lib/dialects/postgres/query-generator.js
lib/dialects/sqlite/query-generator.js
lib/emitters/custom-event-emitter.js
lib/utils.js
package.json
test/dao-factory.test.js
test/dao.test.js
README.md
View file @
200802b
# Sequelize [](http://travis-ci.org/sequelize/sequelize) [](https://david-dm
.org/sequelize/sequelize) [](https://david-dm.org/sequelize/sequelize) [](http://flattr.com/thing/1259407/Sequelize) #
# Sequelize [](https://bitdeli.com/free "Bitdeli Badge") [](http://travis-ci
.org/sequelize/sequelize) [](https://david-dm.org/sequelize/sequelize) [](http://flattr.com/thing/1259407/Sequelize) #
MySQL, MariaDB, PostgresSQL, and SQLite Object Relational Mapper (ORM) for
[
node
](
http://nodejs.org
)
.
MySQL, MariaDB, PostgresSQL, and SQLite Object Relational Mapper (ORM) for
[
node
](
http://nodejs.org
)
.
...
...
lib/dao-factory.js
View file @
200802b
...
@@ -172,9 +172,8 @@ module.exports = (function() {
...
@@ -172,9 +172,8 @@ module.exports = (function() {
self
.
DAO
.
prototype
.
booleanValues
.
push
(
name
);
self
.
DAO
.
prototype
.
booleanValues
.
push
(
name
);
}
}
if
(
definition
.
hasOwnProperty
(
'defaultValue'
))
{
if
(
definition
.
hasOwnProperty
(
'defaultValue'
))
{
self
.
DAO
.
prototype
.
defaultValues
[
name
]
=
function
()
{
self
.
DAO
.
prototype
.
defaultValues
[
name
]
=
Utils
.
_
.
partial
(
return
Utils
.
toDefaultValue
(
definition
.
defaultValue
)
Utils
.
toDefaultValue
,
definition
.
defaultValue
)
}
}
}
if
(
definition
.
hasOwnProperty
(
'validate'
))
{
if
(
definition
.
hasOwnProperty
(
'validate'
))
{
...
...
lib/dao.js
View file @
200802b
...
@@ -407,7 +407,7 @@ module.exports = (function() {
...
@@ -407,7 +407,7 @@ module.exports = (function() {
}
}
DAO
.
prototype
.
addAttribute
=
function
(
attribute
,
value
)
{
DAO
.
prototype
.
addAttribute
=
function
(
attribute
,
value
)
{
if
(
this
.
booleanValues
.
length
&&
this
.
booleanValues
.
indexOf
(
attribute
)
!==
-
1
&&
value
!=
=
undefined
)
{
// transform integer 0,1 into boolean
if
(
this
.
booleanValues
.
length
&&
this
.
booleanValues
.
indexOf
(
attribute
)
!==
-
1
&&
value
!=
null
)
{
// transform integer 0,1 into boolean
value
=
!!
value
value
=
!!
value
}
}
...
...
lib/dialects/mysql/query-generator.js
View file @
200802b
...
@@ -357,7 +357,7 @@ module.exports = (function() {
...
@@ -357,7 +357,7 @@ module.exports = (function() {
}
}
// Blobs/texts cannot have a defaultValue
// Blobs/texts cannot have a defaultValue
if
(
dataType
.
type
!==
"TEXT"
&&
dataType
.
type
.
_binary
!==
true
&&
(
dataType
.
defaultValue
!==
undefined
)
&&
(
dataType
.
defaultValue
!=
DataTypes
.
NOW
))
{
if
(
dataType
.
type
!==
"TEXT"
&&
dataType
.
type
.
_binary
!==
true
&&
Utils
.
defaultValueSchemable
(
dataType
.
defaultValue
))
{
template
+=
" DEFAULT "
+
this
.
escape
(
dataType
.
defaultValue
)
template
+=
" DEFAULT "
+
this
.
escape
(
dataType
.
defaultValue
)
}
}
...
...
lib/dialects/postgres/query-generator.js
View file @
200802b
...
@@ -499,7 +499,10 @@ module.exports = (function() {
...
@@ -499,7 +499,10 @@ module.exports = (function() {
template
+=
" SERIAL"
template
+=
" SERIAL"
}
}
if
(
dataType
.
defaultValue
!==
undefined
)
{
if
(
Utils
.
defaultValueSchemable
(
dataType
.
defaultValue
))
{
// TODO thoroughly check that DataTypes.NOW will properly
// get populated on all databases as DEFAULT value
// i.e. mysql requires: DEFAULT CURRENT_TIMESTAMP
template
+=
" DEFAULT <%= defaultValue %>"
template
+=
" DEFAULT <%= defaultValue %>"
replacements
.
defaultValue
=
this
.
escape
(
dataType
.
defaultValue
)
replacements
.
defaultValue
=
this
.
escape
(
dataType
.
defaultValue
)
}
}
...
...
lib/dialects/sqlite/query-generator.js
View file @
200802b
...
@@ -256,7 +256,10 @@ module.exports = (function() {
...
@@ -256,7 +256,10 @@ module.exports = (function() {
template
+=
" NOT NULL"
template
+=
" NOT NULL"
}
}
if
(
dataType
.
defaultValue
!==
undefined
)
{
if
(
Utils
.
defaultValueSchemable
(
dataType
.
defaultValue
))
{
// TODO thoroughly check that DataTypes.NOW will properly
// get populated on all databases as DEFAULT value
// i.e. mysql requires: DEFAULT CURRENT_TIMESTAMP
template
+=
" DEFAULT <%= defaultValue %>"
template
+=
" DEFAULT <%= defaultValue %>"
replacements
.
defaultValue
=
this
.
escape
(
dataType
.
defaultValue
)
replacements
.
defaultValue
=
this
.
escape
(
dataType
.
defaultValue
)
}
}
...
...
lib/emitters/custom-event-emitter.js
View file @
200802b
...
@@ -49,7 +49,11 @@ module.exports = (function() {
...
@@ -49,7 +49,11 @@ module.exports = (function() {
function
(
fct
)
{
function
(
fct
)
{
fct
=
bindToProcess
(
fct
);
fct
=
bindToProcess
(
fct
);
this
.
on
(
'error'
,
function
(
err
)
{
fct
(
err
,
null
)
})
this
.
on
(
'error'
,
function
(
err
)
{
fct
(
err
,
null
)
})
.
on
(
'success'
,
function
(
arg0
,
arg1
)
{
fct
(
null
,
arg0
,
arg1
)
})
.
on
(
'success'
,
function
()
{
var
args
=
Array
.
prototype
.
slice
.
call
(
arguments
);
args
.
unshift
(
null
);
fct
.
apply
(
fct
,
args
);
})
return
this
return
this
}
}
...
...
lib/utils.js
View file @
200802b
...
@@ -373,7 +373,32 @@ var Utils = module.exports = {
...
@@ -373,7 +373,32 @@ var Utils = module.exports = {
},
},
toDefaultValue
:
function
(
value
)
{
toDefaultValue
:
function
(
value
)
{
if
(
lodash
.
isFunction
(
value
))
{
return
value
()
}
else
{
return
(
value
===
DataTypes
.
NOW
)
?
Utils
.
now
()
:
value
return
(
value
===
DataTypes
.
NOW
)
?
Utils
.
now
()
:
value
}
},
/**
* Determine if the default value provided exists and can be described
* in a db schema using the DEFAULT directive.
*
* @param {*} value Any default value.
* @return {boolean} yes / no.
*/
defaultValueSchemable
:
function
(
value
)
{
if
(
typeof
value
===
'undefined'
)
{
return
false
}
// TODO this will be schemable when all supported db
// have been normalized for this case
if
(
value
===
DataTypes
.
NOW
)
{
return
false
}
if
(
lodash
.
isFunction
(
value
))
{
return
false
}
return
true
},
},
setAttributes
:
function
(
hash
,
identifier
,
instance
,
prefix
)
{
setAttributes
:
function
(
hash
,
identifier
,
instance
,
prefix
)
{
...
...
package.json
View file @
200802b
{
{
"name"
:
"sequelize"
,
"name"
:
"sequelize"
,
"description"
:
"Multi dialect ORM for Node.JS"
,
"description"
:
"Multi dialect ORM for Node.JS"
,
"version"
:
"2.0.0-beta.
0
"
,
"version"
:
"2.0.0-beta.
1
"
,
"author"
:
"Sascha Depold <sascha@depold.com>"
,
"author"
:
"Sascha Depold <sascha@depold.com>"
,
"contributors"
:
[
"contributors"
:
[
{
{
...
...
test/dao-factory.test.js
View file @
200802b
/* jshint camelcase: false */
/* jshint camelcase: false */
/* jshint expr: true */
var
chai
=
require
(
'chai'
)
var
chai
=
require
(
'chai'
)
,
Sequelize
=
require
(
'../index'
)
,
Sequelize
=
require
(
'../index'
)
,
expect
=
chai
.
expect
,
expect
=
chai
.
expect
...
@@ -158,6 +159,27 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
...
@@ -158,6 +159,27 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
})
})
it
(
'should allow me to set a function as default value'
,
function
(
done
)
{
var
defaultFunction
=
sinon
.
stub
().
returns
(
5
)
var
UserTable
=
this
.
sequelize
.
define
(
'UserCol'
,
{
aNumber
:
{
type
:
Sequelize
.
INTEGER
,
defaultValue
:
defaultFunction
}
},
{
timestamps
:
true
})
UserTable
.
sync
({
force
:
true
}).
success
(
function
()
{
UserTable
.
create
().
success
(
function
(
user
)
{
UserTable
.
create
().
success
(
function
(
user2
)
{
expect
(
user
.
aNumber
).
to
.
equal
(
5
)
expect
(
user2
.
aNumber
).
to
.
equal
(
5
)
expect
(
defaultFunction
.
callCount
).
to
.
equal
(
2
)
done
()
})
})
})
})
it
(
'should allow me to override updatedAt, createdAt, and deletedAt fields'
,
function
(
done
)
{
it
(
'should allow me to override updatedAt, createdAt, and deletedAt fields'
,
function
(
done
)
{
var
UserTable
=
this
.
sequelize
.
define
(
'UserCol'
,
{
var
UserTable
=
this
.
sequelize
.
define
(
'UserCol'
,
{
aNumber
:
Sequelize
.
INTEGER
aNumber
:
Sequelize
.
INTEGER
...
...
test/dao.test.js
View file @
200802b
...
@@ -1114,6 +1114,25 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
...
@@ -1114,6 +1114,25 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
})
})
})
})
})
})
it
(
"returns null for null, undefined, and unset boolean values"
,
function
(
done
)
{
var
Setting
=
this
.
sequelize
.
define
(
'SettingHelper'
,
{
setting_key
:
DataTypes
.
STRING
,
bool_value
:
{
type
:
DataTypes
.
BOOLEAN
,
allowNull
:
true
},
bool_value2
:
{
type
:
DataTypes
.
BOOLEAN
,
allowNull
:
true
},
bool_value3
:
{
type
:
DataTypes
.
BOOLEAN
,
allowNull
:
true
}
},
{
timestamps
:
false
,
logging
:
false
})
Setting
.
sync
({
force
:
true
}).
success
(
function
()
{
Setting
.
create
({
setting_key
:
'test'
,
bool_value
:
null
,
bool_value2
:
undefined
}).
success
(
function
()
{
Setting
.
find
({
where
:
{
setting_key
:
'test'
}
}).
success
(
function
(
setting
)
{
expect
(
setting
.
bool_value
).
to
.
equal
(
null
)
expect
(
setting
.
bool_value2
).
to
.
equal
(
null
)
expect
(
setting
.
bool_value3
).
to
.
equal
(
null
)
done
()
})
})
})
})
})
})
describe
(
'equals'
,
function
()
{
describe
(
'equals'
,
function
()
{
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment