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 a8f6348f
authored
Aug 23, 2010
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added possibility to class and instance methods
1 parent
4feed050
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
77 additions
and
4 deletions
examples/MethodPassing/app.js
src/Sequelize.js
src/SequelizeTable.js
examples/MethodPassing/app.js
0 → 100644
View file @
a8f6348
var
Sequelize
=
require
(
__dirname
+
"/../../src/Sequelize"
).
Sequelize
,
sequelize
=
new
Sequelize
(
"sequelize_test"
,
"test"
,
"test"
,
{
disableLogging
:
true
})
// model definition
var
Task
=
sequelize
.
define
(
"Task"
,
{
name
:
Sequelize
.
STRING
,
deadline
:
Sequelize
.
DATE
,
importance
:
Sequelize
.
INTEGER
},
{
classMethods
:
{
setImportance
:
function
(
newImportance
,
callback
)
{
Task
.
findAll
(
function
(
allTasks
)
{
var
queries
=
[]
allTasks
.
forEach
(
function
(
task
)
{
queries
.
push
({
updateAttributes
:
task
,
params
:
[{
importance
:
newImportance
}]})
})
Sequelize
.
chainQueries
(
queries
,
callback
)
})
}
},
instanceMethods
:
{
hasDeadlinePassed
:
function
()
{
return
(
this
.
deadline
<
new
Date
())
}
}
})
// instance creation
var
task1
=
new
Task
({
name
:
'Choose a nice MySQL connector'
,
deadline
:
new
Date
(
Date
.
parse
(
"Jul 8, 2100"
)),
importance
:
10
})
var
task2
=
new
Task
({
name
:
'Build the rest'
,
deadline
:
new
Date
(
Date
.
parse
(
"Jul 8, 2005"
)),
importance
:
90
})
Task
.
drop
(
function
()
{
Task
.
sync
(
function
()
{
task1
.
save
(
function
()
{
task2
.
save
(
function
()
{
Sequelize
.
Helper
.
log
(
"should be false: "
+
task1
.
hasDeadlinePassed
())
Sequelize
.
Helper
.
log
(
"should be true: "
+
task2
.
hasDeadlinePassed
())
Sequelize
.
Helper
.
log
(
"should be 10: "
+
task1
.
importance
)
Task
.
setImportance
(
30
,
function
()
{
Task
.
findAll
(
function
(
tasks
)
{
tasks
.
forEach
(
function
(
task
)
{
Sequelize
.
Helper
.
log
(
"should be 30: "
+
task
.
importance
)
})
})
})
})
})
})
})
\ No newline at end of file
src/Sequelize.js
View file @
a8f6348
...
@@ -115,13 +115,13 @@ Sequelize.prototype = {
...
@@ -115,13 +115,13 @@ Sequelize.prototype = {
})
})
},
},
define
:
function
(
name
,
attributes
)
{
define
:
function
(
name
,
attributes
,
options
)
{
var
SequelizeTable
=
require
(
__dirname
+
"/SequelizeTable"
).
SequelizeTable
var
SequelizeTable
=
require
(
__dirname
+
"/SequelizeTable"
).
SequelizeTable
attributes
.
createdAt
=
'DATETIME NOT NULL'
attributes
.
createdAt
=
'DATETIME NOT NULL'
attributes
.
updatedAt
=
'DATETIME NOT NULL'
attributes
.
updatedAt
=
'DATETIME NOT NULL'
var
table
=
new
SequelizeTable
(
Sequelize
,
this
,
Sequelize
.
Helper
.
SQL
.
asTableName
(
name
),
attributes
)
var
table
=
new
SequelizeTable
(
Sequelize
,
this
,
Sequelize
.
Helper
.
SQL
.
asTableName
(
name
),
attributes
,
options
)
table
.
attributes
=
attributes
table
.
attributes
=
attributes
this
.
tables
[
name
]
=
{
klass
:
table
,
attributes
:
attributes
}
this
.
tables
[
name
]
=
{
klass
:
table
,
attributes
:
attributes
}
...
...
src/SequelizeTable.js
View file @
a8f6348
...
@@ -5,7 +5,11 @@
...
@@ -5,7 +5,11 @@
A.hasMany(B) + B.hasMany(A) => AB.aId + AB.bId
A.hasMany(B) + B.hasMany(A) => AB.aId + AB.bId
*/
*/
exports
.
SequelizeTable
=
function
(
Sequelize
,
sequelize
,
tableName
,
attributes
)
{
exports
.
SequelizeTable
=
function
(
Sequelize
,
sequelize
,
tableName
,
attributes
,
options
)
{
options
=
options
||
{}
options
.
classMethods
=
options
.
classMethods
||
{}
options
.
instanceMethods
=
options
.
instanceMethods
||
{}
var
table
=
function
(
values
)
{
var
table
=
function
(
values
)
{
var
self
=
this
var
self
=
this
Sequelize
.
Helper
.
Hash
.
forEach
(
values
,
function
(
value
,
key
)
{
Sequelize
.
Helper
.
Hash
.
forEach
(
values
,
function
(
value
,
key
)
{
...
@@ -352,9 +356,16 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes) {
...
@@ -352,9 +356,16 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes) {
}
}
}
}
Sequelize
.
Helper
.
Hash
.
forEach
(
classMethods
,
function
(
method
,
methodName
)
{
// merge classMethods + passed classMethods
Sequelize
.
Helper
.
Hash
.
merge
(
options
.
classMethods
,
classMethods
)
Sequelize
.
Helper
.
Hash
.
forEach
(
classMethods
,
function
(
method
,
methodName
)
{
table
[
methodName
]
=
method
table
[
methodName
]
=
method
})
})
// merge passed instanceMethods
Sequelize
.
Helper
.
Hash
.
forEach
(
options
.
instanceMethods
,
function
(
method
,
methodName
)
{
table
.
prototype
[
methodName
]
=
method
})
return
table
return
table
}
}
\ No newline at end of file
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