bookManagement/models.js
knight 3de9f3d8ee Refactor book management system:
- Updated .gitignore to exclude node_modules directory.
- Enhanced index.js with new endpoints for book fetching and checkout requests, integrating nodemailer and express-rate-limit for email notifications and request management.
- Added functionality to confirm book presence in the library and improved error handling for external book sources.
- Updated package.json and package-lock.json to include new dependencies (nodemailer, express-rate-limit) and their respective versions.
- Modified public HTML and JavaScript files to support new features, including a confirm mode for book scanning and improved UI elements.
- Updated styles for better user experience in the library interface.
2024-12-11 09:41:22 -05:00

180 lines
3.7 KiB
JavaScript

const { Sequelize, DataTypes, Op } = require('sequelize');
// Initialize Sequelize
const sequelize = new Sequelize({
dialect: 'sqlite',
storage: './books.db',
});
// Test the connection
sequelize.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
const Location = sequelize.define('Location', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
allowNull: false,
},
shelf: {
type: DataTypes.TEXT,
allowNull: false,
}
}, {
tableName: 'locations',
timestamps: false, // If your table doesn't have `createdAt` and `updatedAt`
});
const Book = sequelize.define('Book', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
isbn: {
type: DataTypes.TEXT,
allowNull: false,
unique: true,
},
title: {
type: DataTypes.TEXT,
allowNull: false,
},
authors: {
type: DataTypes.TEXT,
},
publishedDate: {
type: DataTypes.TEXT,
},
description: {
type: DataTypes.TEXT,
},
url: {
type: DataTypes.TEXT,
},
number_of_pages: {
type: DataTypes.INTEGER,
},
identifiers: {
type: DataTypes.TEXT,
},
publishers: {
type: DataTypes.TEXT,
},
subjects: {
type: DataTypes.TEXT,
},
notes: {
type: DataTypes.TEXT,
},
cover_small: {
type: DataTypes.TEXT,
},
cover_medium: {
type: DataTypes.TEXT,
},
cover_large: {
type: DataTypes.TEXT,
},
location_id: {
type: DataTypes.INTEGER,
references: {
model: Location,
key: 'id'
}
},
status: { // Available, Checked Out, Lost, etc.
type: DataTypes.TEXT,
defaultValue: 'Available'
}
}, {
tableName: 'books',
timestamps: false, // If your table doesn't have `createdAt` and `updatedAt`
});
// Define a table for checkouts
const Checkout = sequelize.define('Checkout', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
book_id: {
type: DataTypes.INTEGER,
references: {
model: Book,
key: 'id'
}
},
user_id: {
type: DataTypes.INTEGER,
allowNull: false,
},
checkout_date: {
type: DataTypes.DATE,
defaultValue: Sequelize.NOW
},
return_date: {
type: DataTypes.DATE,
},
returned_date: {
type: DataTypes.DATE,
}
}, {
tableName: 'checkouts',
timestamps: false, // If your table doesn't have `createdAt` and `updatedAt`
});
// Define User table
const User = sequelize.define('User', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
allowNull: false,
},
email: {
type: DataTypes.TEXT,
allowNull: false,
},
role: {
type: DataTypes.TEXT,
defaultValue: 'user'
}
}, {
tableName: 'users',
timestamps: false, // If your table doesn't have `createdAt` and `updatedAt`
});
// Define the relationships
Book.belongsTo(Location, { foreignKey: 'location_id' });
Location.hasMany(Book, { foreignKey: 'location_id' });
Checkout.belongsTo(Book, { foreignKey: 'book_id' });
Book.hasOne(Checkout, { foreignKey: 'book_id' });
Checkout.belongsTo(User, { foreignKey: 'user_id' });
User.hasMany(Checkout, { foreignKey: 'user_id' });
sequelize.sync({ alter: true }).then(() => {
console.log('Database & tables synced!');
});
exports.Location = Location;
exports.Book = Book;
exports.Checkout = Checkout;
exports.User = User;
exports.sequelize = sequelize;
exports.Op = Op;