. Sequalize, , . :)
posts: [
{ body: 'Hello everyone, my name is Timothy.' },
{ body: 'Today was a good day.'}
]
:
Posts: [
{ body: 'Hello everyone, my name is Timothy.' },
{ body: 'Today was a good day.'}
]
?
- , P, p.
:
testdb=
id | username | password | email | createdAt | updatedAt | deletedAt
----+--------------+------------+--------------------+----------------------------+----------------------------+-----------
1 | Jack Sparrow | plain-text | jacksparrow@msn.nl | 2016-11-17 22:38:06.493+01 | 2016-11-17 22:38:06.493+01 |
2 | Timothy | plain-text | x@msn.nl | 2016-11-17 22:38:06.492+01 | 2016-11-17 22:38:06.492+01 |
(2 rows)
testdb=
id | body | createdAt | updatedAt | deletedAt | userId
----+-------------------------------------+----------------------------+----------------------------+-----------+--------
1 | Hello everyone, my name is Timothy. | 2016-11-17 22:38:06.515+01 | 2016-11-17 22:38:06.515+01 | | 2
2 | Today was a good day. | 2016-11-17 22:38:06.515+01 | 2016-11-17 22:38:06.515+01 | | 2
(2 rows)
: , , .
:
const Post = masterdb.define('post', {
-, P.
, , Post Post, . , .
, docs of sequelize , . , ? "" t T.
, "" , . , - . , , . var Tag = this.sequelize.definte('tag', {.
var Tag = masterdb.define('tag', {.
, - this.sequelize?
. testdb , :
const Sequelize = require('sequelize')
const masterdb = new Sequelize('testdb', process.env.POSTGRES_USER, process.env.POSTGRES_PASSWORD, {
dialect: 'postgres',
})
const User = masterdb.define('user', {
username: {
type: Sequelize.STRING,
unique: true,
allowNull: false
},
password: {
type: Sequelize.STRING,
allowNull: false
},
email: {
type: Sequelize.STRING,
unique: true,
allowNull: false
}
}, {
paranoid: true
})
var Tag = masterdb.define('Tag', {
name: Sequelize.STRING
}, {
paranoid: true
})
User.hasMany(Tag)
masterdb.sync({force:true}).then( x => {
return Promise.all([
User.create({
username:'Timothy',
password:'plain-text',
email:'x@msn.nl',
Tags: [
{ name: 'Alpha'},
{ name: 'Beta'}
]
}, {
include: [ Tag ]
}),
User.create({
username:'Jack Sparrow',
password:'plain-text',
email:'jacksparrow@msn.nl'
}),
])
}).catch(x => console.log(x))