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 6a091558
authored
Sep 20, 2014
by
Sveinn Fannar Kristjansson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Example of postgres json data type
1 parent
259b754d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
72 additions
and
0 deletions
examples/postgres-json-datatype/app.js
examples/postgres-json-datatype/app.js
0 → 100644
View file @
6a09155
/*
Title: PostgreSQL JSON Data-Type
An example of using PostgreSQL's JSON data-type.
In this example we create a single table that can handle multiple types of different media and it's metadata.
This example uses the promise API preferred in 2.0 and above.
*/
var
Sequelize
=
require
(
__dirname
+
"/../../index"
)
,
config
=
require
(
__dirname
+
"/../../test/config/config"
)
,
sequelize
=
new
Sequelize
(
config
.
postgres
.
database
,
config
.
postgres
.
username
,
config
.
postgres
.
password
,
{
dialect
:
'postgres'
,
logging
:
false
});
var
Content
=
sequelize
.
define
(
'Content'
,
{
title
:
{
type
:
Sequelize
.
STRING
},
type
:
{
type
:
Sequelize
.
STRING
},
// ENUM('Movie', 'Episode', 'Music Video') },
metadata
:
{
type
:
Sequelize
.
JSON
}
})
,
movie
=
Content
.
build
({
title
:
'Grave of the Fireflies'
,
type
:
'Movie'
,
metadata
:
{
director
:
'Isao Takahata'
,
language
:
'Japanese'
,
year
:
1988
}
})
,
episode
=
Content
.
build
({
title
:
'Chapter 3'
,
metadata
:
{
season
:
1
,
episode
:
3
,
language
:
'English'
,
seriesTitle
:
'House of Cards'
,
genres
:
[
'Drama'
,
'Political thriller'
]
}
});
sequelize
.
sync
({
force
:
true
})
.
then
(
function
()
{
return
sequelize
.
Promise
.
all
([
movie
.
save
(),
episode
.
save
()
]);
})
.
then
(
function
()
{
console
.
log
(
'====================================='
);
console
.
log
(
'Searching for any content in Japanese'
);
console
.
log
(
'-------------------------------------'
);
return
Content
.
find
({
where
:
"metadata->>'language' = 'Japanese'"
})
.
then
(
function
(
content
)
{
console
.
log
(
'Result:'
,
content
.
dataValues
);
console
.
log
(
'====================================='
);
})
})
.
then
(
function
()
{
console
.
log
(
'====================================='
);
console
.
log
(
'Searching for any content in English'
);
console
.
log
(
'-------------------------------------'
);
return
Content
.
find
({
where
:
"metadata->>'language' = 'English'"
})
.
then
(
function
(
content
)
{
console
.
log
(
'Result:'
,
content
.
dataValues
);
console
.
log
(
'====================================='
);
})
})
.
error
(
function
(
error
)
{
console
.
log
(
error
)
});
\ 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