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 7361930c
authored
Mar 11, 2013
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial impl.
1 parent
e53fdf6c
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
100 additions
and
3 deletions
changelog.md
lib/dao.js
lib/query-interface.js
spec/dao.spec.js
changelog.md
View file @
7361930
# v2.0.0 #
-
[
DEPENDENCIES
]
upgrade mysql to alpha7. You
*MUST*
use this version or newer for DATETIMEs to work
# v1.6.0 #
# v1.6.0 #
-
[
DEPENDENCIES
]
upgrade mysql to alpha7. You
*MUST*
use this version or newer for DATETIMEs to work
-
[
DEPENDENCIES
]
upgraded most dependencies. most important: mysql was upgraded to 2.0.0-alpha-3
-
[
DEPENDENCIES
]
upgraded most dependencies. most important: mysql was upgraded to 2.0.0-alpha-3
-
[
DEPENDENCIES
]
mysql is now an optional dependency. #355 (thanks to clkao)
-
[
DEPENDENCIES
]
mysql is now an optional dependency. #355 (thanks to clkao)
-
[
REFACTORING
]
separated tests for dialects
-
[
REFACTORING
]
separated tests for dialects
...
@@ -40,6 +38,7 @@
...
@@ -40,6 +38,7 @@
-
[
FEATURE
]
add increment and decrement methods on dao. #408 (thanks to janmeier/innofluence)
-
[
FEATURE
]
add increment and decrement methods on dao. #408 (thanks to janmeier/innofluence)
-
[
FEATURE
]
unified the result of describeTable
-
[
FEATURE
]
unified the result of describeTable
-
[
FEATURE
]
add support for decimals (thanks to alexyoung)
-
[
FEATURE
]
add support for decimals (thanks to alexyoung)
-
[
FEATURE
]
added DAO.refresh(), which updates the attributes of the DAO in-place (as opposed to doing having to do a find() and returning a new model)
# v1.5.0 #
# v1.5.0 #
-
[
REFACTORING
]
use underscore functions for Utils.isHash (thanks to Mick-Hansen/innofluence)
-
[
REFACTORING
]
use underscore functions for Utils.isHash (thanks to Mick-Hansen/innofluence)
...
...
lib/dao.js
View file @
7361930
...
@@ -151,6 +151,17 @@ module.exports = (function() {
...
@@ -151,6 +151,17 @@ module.exports = (function() {
}
}
}
}
/*
* Refresh the current instance in-place, i.e. update the object with current data from the DB and return the same object.
* This is different from doing a `find(DAO.id)`, because that would create and return a new object. With this method,
* all references to the DAO are updated with the new data and no new objects are created.
*
* @return {Object} A promise which fires `success`, `error`, `complete` and `sql`.
*/
DAO
.
prototype
.
reload
=
function
()
{
return
this
.
QueryInterface
.
reload
(
this
,
this
.
__factory
.
tableName
);
}
/*
/*
* Validate this dao's attribute values according to validation rules set in the dao definition.
* Validate this dao's attribute values according to validation rules set in the dao definition.
*
*
...
...
lib/query-interface.js
View file @
7361930
...
@@ -225,6 +225,36 @@ module.exports = (function() {
...
@@ -225,6 +225,36 @@ module.exports = (function() {
return
queryAndEmit
.
call
(
this
,
[
sql
,
dao
],
'increment'
);
return
queryAndEmit
.
call
(
this
,
[
sql
,
dao
],
'increment'
);
}
}
QueryInterface
.
prototype
.
reload
=
function
(
dao
,
tableName
)
{
var
self
=
this
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
var
sql
=
self
.
QueryGenerator
.
selectQuery
(
tableName
,
{
id
:
dao
.
id
,
limit
:
1
})
,
calleeStub
=
{
build
:
function
(
values
)
{
// We cannot use setAttributes here, since a refresh might also update the read only attributes
Utils
.
_
.
each
(
values
,
function
(
value
,
attr
)
{
(
dao
.
attributes
.
indexOf
(
attr
)
>
-
1
)
&&
(
dao
[
attr
]
=
value
)
})
}
}
,
qry
=
self
.
sequelize
.
query
(
sql
,
calleeStub
,
{
plain
:
true
,
raw
:
false
,
type
:
'SELECT'
})
qry
.
success
(
function
()
{
self
.
emit
(
'refresh'
,
null
)
emitter
.
emit
(
'success'
,
dao
)
})
.
error
(
function
(
err
)
{
self
.
emit
(
'refresh'
,
err
)
emitter
.
emit
(
'error'
,
err
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitter
.
emit
(
'sql'
,
sql
)
})
}).
run
()
}
QueryInterface
.
prototype
.
rawSelect
=
function
(
tableName
,
options
,
attributeSelector
)
{
QueryInterface
.
prototype
.
rawSelect
=
function
(
tableName
,
options
,
attributeSelector
)
{
var
self
=
this
var
self
=
this
...
...
spec/dao.spec.js
View file @
7361930
...
@@ -216,6 +216,63 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
...
@@ -216,6 +216,63 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
});
});
});
});
describe
(
'refresh'
,
function
()
{
it
(
"should return a refrence to the same DAO instead of creating a new one"
,
function
(
done
)
{
this
.
User
.
create
({
username
:
'John Doe'
}).
done
(
function
(
err
,
originalUser
)
{
originalUser
.
updateAttributes
({
username
:
'Doe John'
}).
done
(
function
()
{
originalUser
.
reload
().
done
(
function
(
err
,
updatedUser
)
{
expect
(
originalUser
===
updatedUser
).
toBeTrue
()
done
();
})
})
})
})
it
(
"should update the values on all references to the DAO"
,
function
(
done
)
{
var
self
=
this
this
.
User
.
create
({
username
:
'John Doe'
}).
done
(
function
(
err
,
originalUser
)
{
self
.
User
.
find
(
originalUser
.
id
).
done
(
function
(
err
,
updater
)
{
updater
.
updateAttributes
({
username
:
'Doe John'
}).
done
(
function
()
{
// We used a different refernce when calling updateAttributes, so originalUser is now out of sync
expect
(
originalUser
.
username
).
toEqual
(
'John Doe'
)
originalUser
.
reload
().
done
(
function
(
err
,
updatedUser
)
{
expect
(
originalUser
.
username
).
toEqual
(
'Doe John'
)
expect
(
updatedUser
.
username
).
toEqual
(
'Doe John'
)
done
();
})
})
})
})
})
it
(
"should update read only attributes as well (updatedAt)"
,
function
(
done
)
{
var
self
=
this
this
.
timeout
=
2000
;
this
.
User
.
create
({
username
:
'John Doe'
}).
done
(
function
(
err
,
originalUser
)
{
var
originallyUpdatedAt
=
originalUser
.
updatedAt
// Wait for a second, so updatedAt will actually be different
setTimeout
(
function
()
{
self
.
User
.
find
(
originalUser
.
id
).
done
(
function
(
err
,
updater
)
{
updater
.
updateAttributes
({
username
:
'Doe John'
}).
done
(
function
()
{
originalUser
.
reload
().
done
(
function
(
err
,
updatedUser
)
{
expect
(
originalUser
.
updatedAt
).
toBeGreaterThan
(
originallyUpdatedAt
)
expect
(
updatedUser
.
updatedAt
).
toBeGreaterThan
(
originallyUpdatedAt
)
done
();
})
})
})
},
1000
)
})
})
});
describe
(
'default values'
,
function
()
{
describe
(
'default values'
,
function
()
{
describe
(
'current date'
,
function
()
{
describe
(
'current date'
,
function
()
{
it
(
'should store a date in touchedAt'
,
function
()
{
it
(
'should store a date in touchedAt'
,
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