Advertisement
zykes

Untitled

May 24th, 2017
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import conf from './cfg';
  2.  
  3. var Sequelize = require('sequelize-cockroachdb');
  4.  
  5. var sequelize = new Sequelize('vendr', 'maxroach', '', {
  6.   dialect: 'postgres',
  7.   port: conf.get().database.port,
  8.   host: conf.get().database.host,
  9.   logging: (str) => {
  10.     console.log(str)
  11.   }
  12. })
  13.  
  14. export const User = sequelize.define('users', {
  15.   id: { type: Sequelize.STRING, primaryKey: true },
  16.   identifier: { type: Sequelize.STRING },
  17. })
  18.  
  19. export const Machine = sequelize.define('machines', {
  20.   id: { type: Sequelize.STRING, primaryKey: true },
  21.   name: { type: Sequelize.STRING, unique: 'compositeIndex' },
  22.   description: { type: Sequelize.STRING },
  23.   uri: { type: Sequelize.STRING },
  24. })
  25.  
  26.  
  27. export const Item = sequelize.define('items', {
  28.   id: { type: Sequelize.STRING, primaryKey: true },
  29.   name: { type: Sequelize.STRING, allowNull: false },
  30.   description: { type: Sequelize.STRING, allowNull: false },
  31.   quantity: { type: Sequelize.INTEGER, allowNull: false },
  32. }, {
  33.     indexes: [{ unique: true, fields: ["id", "name"] }],
  34.   }
  35. );
  36.  
  37.  
  38. export const ItemPurchase = sequelize.define('item_vend', {
  39.   id: { type: Sequelize.STRING, primaryKey: true },
  40.   itemId: {
  41.     type: Sequelize.STRING,
  42.     references: {
  43.       model: Item,
  44.       key: 'id',
  45.       constraints: false,
  46.     },
  47.   },
  48.   userId: {
  49.     type: Sequelize.STRING,
  50.     references: {
  51.       model: User,
  52.       key: 'id',
  53.       constraints: false,
  54.     }
  55.   }
  56. });
  57.  
  58. Machine.hasMany(Item, { constraints: false });
  59. Item.hasMany(ItemPurchase, { constraints: false });
  60. User.hasMany(ItemPurchase, { constraints: false });
  61.  
  62. export function initDb(force = false) {
  63.   User.sync({ force: force });
  64.   Machine.sync({ force: force });
  65.   Item.sync({ force: force });
  66.   ItemPurchase.sync({ force: force });
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement