Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. const Sequelize = require('sequelize')
  2.  
  3. const username = "user"
  4. const password = "pass"
  5. const dialect = 'sqlite'
  6.  
  7. const sequelize = new Sequelize('test', username, password, {
  8. dialect,
  9. pool: {
  10. max: 5,
  11. min: 0,
  12. idle: 10000
  13. },
  14. });
  15.  
  16. let clients = {
  17. id: {
  18. type: Sequelize.INTEGER,
  19. primaryKey: true,
  20. autoIncrement: true
  21. },
  22. login: {
  23. type: Sequelize.STRING(20),
  24. unique: true,
  25. allowNull: false,
  26. },
  27. password: {
  28. type: Sequelize.STRING(20),
  29. allowNull: false
  30. },
  31. name: {
  32. type: Sequelize.TEXT,
  33. allowNull: false
  34. },
  35. address: {
  36. type: Sequelize.TEXT,
  37. allowNull: false
  38. },
  39. email: {
  40. type: Sequelize.TEXT,
  41. allowNull: false,
  42. validate: {
  43. isEmail: true
  44. }
  45. },
  46. materials_version: Sequelize.INTEGER,
  47. prices_version: Sequelize.INTEGER
  48. }
  49.  
  50. let orders ={
  51. number: {
  52. type: Sequelize.INTEGER,
  53. primaryKey: true,
  54. autoIncrement: true
  55. },
  56. text: Sequelize.TEXT
  57. }
  58.  
  59. clients = sequelize.define('clients', clients)
  60. orders = sequelize.define('orders', orders)
  61.  
  62. let order = {
  63. text: "I want something"
  64. }
  65.  
  66. let client = {
  67. login: "Client1",
  68. password: "Password1",
  69. name: "Client's name",
  70. address: "Client's address",
  71. email: "client@mail.com",
  72. materials_version: 1,
  73. prices_version: 2
  74. }
  75.  
  76. let client2 = {
  77. login: "Client2",
  78. password: "Password2",
  79. name: "Client's name2",
  80. address: "Client's address2",
  81. email: "client2@mail.com",
  82. materials_version: 1,
  83. prices_version: 2
  84. }
  85. async function do_stuff() {
  86. const find_all_opts = {
  87. raw: true,
  88. attributes: {
  89. exclude: ['createdAt', 'updatedAt']
  90. }
  91. }
  92.  
  93. await orders.belongsToMany(clients, {through: "OrdersClients"})
  94. await clients.belongsToMany(orders, {through: "ClientsOrders"})
  95.  
  96. await sequelize.sync()
  97. order = await orders.create(order)
  98. client = await clients.create(client)
  99. client2 = await clients.create(client2)
  100.  
  101. await client.setOrders([order])
  102. await client2.setOrders([order])
  103. result = await clients.findAll(find_all_opts)
  104. console.log(result)
  105. }
  106.  
  107. do_stuff()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement