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 2d298a3c
authored
Apr 11, 2013
by
Jochem Maas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add tests for getters/setters, bugfix, refactor - its WIP
1 parent
770c10c9
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
119 additions
and
35 deletions
lib/dao-factory.js
lib/dao.js
spec/dao-factory.spec.js
lib/dao-factory.js
View file @
2d298a3
...
...
@@ -78,11 +78,29 @@ module.exports = (function() {
Util
.
inherits
(
this
.
DAO
,
DAO
);
this
.
DAO
.
prototype
.
rawAttributes
=
this
.
rawAttributes
;
if
(
this
.
options
.
instanceMethods
)
{
Utils
.
_
.
each
(
this
.
options
.
instanceMethods
,
function
(
fct
,
name
)
{
self
.
DAO
.
prototype
[
name
]
=
fct
})
}
Utils
.
_
.
each
([
'Get'
,
'Set'
],
function
(
type
)
{
var
opt
=
type
.
toLowerCase
()
+
'terMethods'
,
meth
=
'__define'
+
type
+
'ter__'
;
if
(
self
.
options
[
opt
])
{
Utils
.
_
.
each
(
self
.
options
[
opt
],
function
(
fct
,
name
)
{
//var def = {};
if
(
!
Utils
.
_
.
isFunction
(
fct
))
throw
new
Error
(
type
+
'ter for "'
+
name
+
'" is not a function.'
)
self
.
DAO
.
prototype
[
meth
](
name
,
fct
);
})
}
})
this
.
DAO
.
prototype
.
attributes
=
Object
.
keys
(
this
.
DAO
.
prototype
.
rawAttributes
);
this
.
DAO
.
prototype
.
booleanValues
=
[];
...
...
lib/dao.js
View file @
2d298a3
...
...
@@ -14,13 +14,8 @@ module.exports = (function() {
this
.
__eagerlyLoadedAssociations
=
[]
initAttributes
.
call
(
this
,
values
,
isNewRecord
)
if
(
this
.
hasDefaultValues
)
{
Utils
.
_
.
each
(
this
.
defaultValues
,
function
(
value
,
name
)
{
self
.
addAttribute
(
name
,
value
());
})
}
}
Utils
.
_
.
extend
(
DAO
.
prototype
,
Mixin
.
prototype
)
Object
.
defineProperty
(
DAO
.
prototype
,
'sequelize'
,
{
...
...
@@ -87,6 +82,17 @@ module.exports = (function() {
}
})
DAO
.
prototype
.
getDataValue
=
function
(
name
)
{
return
this
.
dataValues
&&
this
.
dataValues
.
hasOwnProperty
(
name
)
?
this
.
dataValues
[
name
]
:
this
[
name
]
}
DAO
.
prototype
.
setDataValue
=
function
(
name
,
value
)
{
if
(
this
.
dataValues
&&
this
.
dataValues
.
hasOwnProperty
(
name
))
this
.
dataValues
[
name
]
=
value
else
this
[
name
]
=
value
}
// if an array with field names is passed to save()
// only those fields will be updated
DAO
.
prototype
.
save
=
function
(
fields
)
{
...
...
@@ -128,7 +134,7 @@ module.exports = (function() {
}
}
if
(
this
.
__options
.
timestamps
&&
this
.
hasOwnProperty
(
updatedAtAttr
))
{
if
(
this
.
__options
.
timestamps
&&
this
.
dataValues
.
hasOwnProperty
(
updatedAtAttr
))
{
this
.
dataValues
[
updatedAtAttr
]
=
values
[
updatedAtAttr
]
=
Utils
.
now
()
}
...
...
@@ -338,24 +344,34 @@ module.exports = (function() {
if
(
typeof
this
.
dataValues
[
attribute
]
!==
'undefined'
)
return
;
var
def
=
{},
predef
=
Object
.
getOwnPropertyDescriptor
(
this
,
attribute
),
isBool
=
this
.
booleanValues
.
length
&&
this
.
booleanValues
.
indexOf
(
attribute
)
!==
-
1
;
if
(
isBool
)
// transform integer 0,1 into boolean
if
(
this
.
booleanValues
.
length
&&
this
.
booleanValues
.
indexOf
(
attribute
)
!==
-
1
)
// transform integer 0,1 into boolean
value
=
!!
value
;
var
hasnot
=
function
(
which
)
{
return
!
predef
||
(
!
predef
.
hasOwnProperty
(
'value'
)
&&
!
predef
.
hasOwnProperty
(
which
));
};
if
(
hasnot
(
'get'
))
def
.
get
=
function
()
{
return
this
.
dataValues
[
attribute
];
};
if
(
hasnot
(
'set'
))
def
.
set
=
function
(
v
)
{
this
.
dataValues
[
attribute
]
=
v
;
};
if
(
Utils
.
_
.
size
(
def
))
Object
.
defineProperty
(
this
,
attribute
,
def
);
var
has
=
(
function
(
o
)
{
var
predef
=
Object
.
getOwnPropertyDescriptor
(
o
,
attribute
);
if
(
predef
&&
predef
.
hasOwnProperty
(
'value'
))
return
true
;
// true here means 'this property exist as a simple value property, do not place setters or getters at all'
return
{
get
:
(
predef
&&
predef
.
hasOwnProperty
(
'get'
)
?
predef
.
get
:
null
)
||
o
.
__lookupGetter__
(
attribute
),
set
:
(
predef
&&
predef
.
hasOwnProperty
(
'set'
)
?
predef
.
set
:
null
)
||
o
.
__lookupSetter__
(
attribute
)
};
})(
this
);
// node-v0.8.19:
// calling either __defineGetter__ any previously defined setters for the attribute in
// question *if* that property setter was defined on the object's prototype (which is what
// we do in dao-factory) ... therefore we need to [re]define both the setter and getter
// here with either the function that already existed OR the default/automatic definition
//
// (the same is true for __defineSetter and 'prototype' getters)
if
(
has
!==
true
)
{
this
.
__defineGetter__
(
attribute
,
has
.
get
||
function
()
{
return
this
.
dataValues
[
attribute
];
});
this
.
__defineSetter__
(
attribute
,
has
.
set
||
function
(
v
)
{
this
.
dataValues
[
attribute
]
=
v
;
});
}
this
.
dataValues
[
attribute
]
=
value
;
this
[
attribute
]
=
value
;
}
DAO
.
prototype
.
setValidators
=
function
(
attribute
,
validators
)
{
...
...
@@ -369,16 +385,10 @@ module.exports = (function() {
// private
var
initAttributes
=
function
(
values
,
isNewRecord
)
{
// add all passed values to the dao and store the attribute names in this.attributes
for
(
var
key
in
values
)
{
if
(
values
.
hasOwnProperty
(
key
))
{
this
.
addAttribute
(
key
,
values
[
key
])
}
}
// set id to null if not passed as value
// a newly created dao has no id
var
defaults
=
this
.
hasPrimaryKeys
?
{}
:
{
id
:
null
}
// set id to null if not passed as value, a newly created dao has no id
var
defaults
=
this
.
hasPrimaryKeys
?
{}
:
{
id
:
null
},
attrs
=
{},
key
;
if
(
this
.
__options
.
timestamps
&&
isNewRecord
)
{
defaults
[
this
.
__options
.
underscored
?
'created_at'
:
'createdAt'
]
=
Utils
.
now
()
...
...
@@ -387,12 +397,37 @@ module.exports = (function() {
if
(
this
.
__options
.
paranoid
)
{
defaults
[
this
.
__options
.
underscored
?
'deleted_at'
:
'deletedAt'
]
=
null
}
if
(
this
.
hasDefaultValues
)
{
Utils
.
_
.
each
(
this
.
defaultValues
,
function
(
valueFn
,
key
)
{
if
(
!
defaults
.
hasOwnProperty
(
key
))
defaults
[
key
]
=
valueFn
()
})
}
}
if
(
Utils
.
_
.
size
(
defaults
))
{
for
(
var
attr
in
defaults
)
{
this
.
addAttribute
(
attr
,
Utils
.
toDefaultValue
(
defaults
[
attr
]))
for
(
key
in
defaults
)
{
attrs
[
key
]
=
Utils
.
toDefaultValue
(
defaults
[
key
])
}
}
Utils
.
_
.
each
(
this
.
attributes
,
function
(
key
)
{
if
(
!
attrs
.
hasOwnProperty
(
key
))
{
attrs
[
key
]
=
undefined
}
})
if
(
values
)
{
for
(
key
in
values
)
{
if
(
values
.
hasOwnProperty
(
key
))
{
attrs
[
key
]
=
values
[
key
]
}
}
}
for
(
key
in
attrs
)
{
this
.
addAttribute
(
key
,
attrs
[
key
])
}
}
...
...
spec/dao-factory.spec.js
View file @
2d298a3
...
...
@@ -40,7 +40,7 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
var
User
=
this
.
sequelize
.
define
(
'SuperUser'
,
{},
{
freezeTableName
:
false
})
var
factorySize
=
this
.
sequelize
.
daoFactoryManager
.
all
.
length
var
User2
=
this
.
sequelize
.
define
(
'SuperUser'
,
{},
{
freezeTableName
:
false
})
var
User2
=
this
.
sequelize
.
define
(
'SuperUser'
,
{},
{
freezeTableName
:
false
})
var
factorySize2
=
this
.
sequelize
.
daoFactoryManager
.
all
.
length
expect
(
factorySize
).
toEqual
(
factorySize2
)
...
...
@@ -99,6 +99,37 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
var
user
=
this
.
User
.
build
({
username
:
'John Wayne'
})
expect
(
user
.
selectedValues
).
toEqual
({
username
:
'John Wayne'
})
})
it
(
"attaches getter and setter methods"
,
function
()
{
var
Product
=
this
.
sequelize
.
define
(
'ProductWithSettersAndGetters'
,
{
priceInCents
:
{
type
:
Sequelize
.
INTEGER
}
},{
instanceMethods
:
{
foo
:
function
()
{
console
.
log
(
'woot'
)
}
},
setterMethods
:
{
price
:
function
(
value
)
{
this
.
dataValues
.
priceInCents
=
value
*
100
;
}
},
getterMethods
:
{
price
:
function
()
{
return
'$'
+
(
this
.
getDataValue
(
'priceInCents'
)
/
100
);
},
priceInCents
:
function
()
{
return
this
.
dataValues
.
priceInCents
;
}
}
});
expect
(
Product
.
build
({
price
:
20
}).
priceInCents
).
toEqual
(
20
*
100
);
expect
(
Product
.
build
({
priceInCents
:
30
*
100
}).
price
).
toEqual
(
'$'
+
30
);
})
})
describe
(
'findOrCreate'
,
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