Guest User

Untitled

a guest
Jun 11th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. const Sequelize = require('sequelize');
  2. const config = require('./config')();
  3.  
  4. const {DataTypes} = Sequelize;
  5.  
  6. const db = new Sequelize({
  7. database: config.postgres.database,
  8. username: config.postgres.user,
  9. password: config.postgres.password,
  10. host: config.postgres.host,
  11. port: config.postgres.port,
  12. dialect: 'postgres',
  13. logging: false
  14. });
  15.  
  16. db.parents = db.define('Parent', {
  17. _id: {
  18. type: DataTypes.INTEGER,
  19. primaryKey: true,
  20. autoIncrement: true
  21. },
  22. somefield: {type: DataTypes.TEXT, unique: true}
  23. }, {
  24. timestamps: false,
  25. tableName: 'parents'
  26. });
  27.  
  28. db.children = db.define('Child', {
  29. _id: {
  30. type: DataTypes.INTEGER,
  31. primaryKey: true,
  32. autoIncrement: true
  33. }
  34. }, {
  35. timestamps: false,
  36. tableName: 'children'
  37. });
  38.  
  39. db.parentToChild = db.define('parentToChild', {
  40. parentId: {type: DataTypes.INTEGER},
  41. childId: {type: DataTypes.INTEGER},
  42. additional: {type: DataTypes.TEXT}
  43. }, {
  44. timestamps: false,
  45. tableName: 'parentToChild'
  46. });
  47.  
  48. db.parents.belongsToMany(db.children, {
  49. as: 'children',
  50. through: 'parentToChild',
  51. foreignKey: 'parentId',
  52. otherKey: 'childId',
  53. timestamps: false
  54. });
  55.  
  56. db.parents.hasMany(db.parentToChild, {
  57. as: 'additionals',
  58. foreignKey: 'parentId'
  59. });
  60.  
  61. db.parentToChild.removeAttribute('id');
  62.  
  63. db.parentToChild.belongsTo(db.children, {
  64. as: 'child',
  65. foreignKey: 'childId',
  66. targetKey: '_id'
  67. });
  68.  
  69.  
  70. (async () => {
  71. try {
  72. await db.transaction(async (transaction) => {
  73. await db.parents.truncate({transaction});
  74. await db.parentToChild.truncate({transaction});
  75. await db.children.truncate({transaction});
  76.  
  77. const {_id: parentId} = await db.parents.create(
  78. {somefield: 'somevalue'},
  79. {transaction}
  80. );
  81. const {_id: childId} = await db.children.create({}, {transaction});
  82.  
  83. await db.parentToChild.create({
  84. parentId,
  85. childId,
  86. additional: 'some additional info'
  87. }, {transaction});
  88.  
  89. const parent = await db.parents.findOne({
  90. where: {somefield: 'somevalue'},
  91. include: [{
  92. model: db.parentToChild,
  93. as: 'additionals',
  94. required: true,
  95. include: [{
  96. model: db.children,
  97. as: 'child',
  98. required: true
  99. }]
  100. }],
  101. transaction,
  102. logging: console.log
  103. });
  104.  
  105. console.log(require('util').inspect(parent.toJSON(), false, null));
  106. });
  107. } catch (err) {
  108. console.log(err.stack);
  109. }
  110. })();
Add Comment
Please, Sign In to add comment