Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Sequelize = require('sequelize')
- const username = "user"
- const password = "pass"
- const dialect = 'sqlite'
- const sequelize = new Sequelize('test', username, password, {
- dialect,
- pool: {
- max: 5,
- min: 0,
- idle: 10000
- },
- });
- let clients = {
- id: {
- type: Sequelize.INTEGER,
- primaryKey: true,
- autoIncrement: true
- },
- login: {
- type: Sequelize.STRING(20),
- unique: true,
- allowNull: false,
- },
- password: {
- type: Sequelize.STRING(20),
- allowNull: false
- },
- name: {
- type: Sequelize.TEXT,
- allowNull: false
- },
- address: {
- type: Sequelize.TEXT,
- allowNull: false
- },
- email: {
- type: Sequelize.TEXT,
- allowNull: false,
- validate: {
- isEmail: true
- }
- },
- materials_version: Sequelize.INTEGER,
- prices_version: Sequelize.INTEGER
- }
- let orders ={
- number: {
- type: Sequelize.INTEGER,
- primaryKey: true,
- autoIncrement: true
- },
- text: Sequelize.TEXT
- }
- clients = sequelize.define('clients', clients)
- orders = sequelize.define('orders', orders)
- let order = {
- text: "I want something"
- }
- let client = {
- login: "Client1",
- password: "Password1",
- name: "Client's name",
- address: "Client's address",
- email: "client@mail.com",
- materials_version: 1,
- prices_version: 2
- }
- let client2 = {
- login: "Client2",
- password: "Password2",
- name: "Client's name2",
- address: "Client's address2",
- email: "client2@mail.com",
- materials_version: 1,
- prices_version: 2
- }
- async function do_stuff() {
- const find_all_opts = {
- raw: true,
- attributes: {
- exclude: ['createdAt', 'updatedAt']
- }
- }
- await orders.belongsToMany(clients, {through: "OrdersClients"})
- await clients.belongsToMany(orders, {through: "ClientsOrders"})
- await sequelize.sync()
- order = await orders.create(order)
- client = await clients.create(client)
- client2 = await clients.create(client2)
- await client.setOrders([order])
- await client2.setOrders([order])
- result = await clients.findAll(find_all_opts)
- console.log(result)
- }
- do_stuff()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement