Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import conf from './cfg';
- var Sequelize = require('sequelize-cockroachdb');
- var sequelize = new Sequelize('vendr', 'maxroach', '', {
- dialect: 'postgres',
- port: conf.get().database.port,
- host: conf.get().database.host,
- logging: (str) => {
- console.log(str)
- }
- })
- export const User = sequelize.define('users', {
- id: { type: Sequelize.STRING, primaryKey: true },
- identifier: { type: Sequelize.STRING },
- })
- export const Machine = sequelize.define('machines', {
- id: { type: Sequelize.STRING, primaryKey: true },
- name: { type: Sequelize.STRING, unique: 'compositeIndex' },
- description: { type: Sequelize.STRING },
- uri: { type: Sequelize.STRING },
- })
- export const Item = sequelize.define('items', {
- id: { type: Sequelize.STRING, primaryKey: true },
- name: { type: Sequelize.STRING, allowNull: false },
- description: { type: Sequelize.STRING, allowNull: false },
- quantity: { type: Sequelize.INTEGER, allowNull: false },
- }, {
- indexes: [{ unique: true, fields: ["id", "name"] }],
- }
- );
- export const ItemPurchase = sequelize.define('item_vend', {
- id: { type: Sequelize.STRING, primaryKey: true },
- itemId: {
- type: Sequelize.STRING,
- references: {
- model: Item,
- key: 'id',
- constraints: false,
- },
- },
- userId: {
- type: Sequelize.STRING,
- references: {
- model: User,
- key: 'id',
- constraints: false,
- }
- }
- });
- Machine.hasMany(Item, { constraints: false });
- Item.hasMany(ItemPurchase, { constraints: false });
- User.hasMany(ItemPurchase, { constraints: false });
- export function initDb(force = false) {
- User.sync({ force: force });
- Machine.sync({ force: force });
- Item.sync({ force: force });
- ItemPurchase.sync({ force: force });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement