feat: database
This commit is contained in:
66
models/Link.js
Normal file
66
models/Link.js
Normal file
@@ -0,0 +1,66 @@
|
||||
const { DataTypes } = require('sequelize');
|
||||
const { v4: uuidv4 } = require('uuid');
|
||||
|
||||
module.exports = (sequelize) => {
|
||||
const Link = sequelize.define('Link', {
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true
|
||||
},
|
||||
url: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: false,
|
||||
unique: true,
|
||||
validate: {
|
||||
isUrl: true
|
||||
}
|
||||
},
|
||||
title: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: true
|
||||
},
|
||||
description: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: true
|
||||
},
|
||||
image: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: true
|
||||
},
|
||||
created_at: {
|
||||
type: DataTypes.DATE,
|
||||
defaultValue: DataTypes.NOW,
|
||||
allowNull: false
|
||||
},
|
||||
created_by: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: true
|
||||
},
|
||||
modified_at: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: true
|
||||
},
|
||||
modified_by: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: true
|
||||
},
|
||||
archived: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
defaultValue: false,
|
||||
allowNull: false
|
||||
}
|
||||
}, {
|
||||
tableName: 'links',
|
||||
timestamps: false, // We're using created_at and modified_at manually
|
||||
underscored: true
|
||||
});
|
||||
|
||||
// Hook to set modified_at before update
|
||||
Link.beforeUpdate((link) => {
|
||||
link.modified_at = new Date();
|
||||
});
|
||||
|
||||
return Link;
|
||||
};
|
||||
|
||||
33
models/LinkList.js
Normal file
33
models/LinkList.js
Normal file
@@ -0,0 +1,33 @@
|
||||
const { DataTypes } = require('sequelize');
|
||||
|
||||
module.exports = (sequelize) => {
|
||||
const LinkList = sequelize.define('LinkList', {
|
||||
link_id: {
|
||||
type: DataTypes.UUID,
|
||||
primaryKey: true,
|
||||
references: {
|
||||
model: 'links',
|
||||
key: 'id'
|
||||
},
|
||||
onDelete: 'CASCADE',
|
||||
onUpdate: 'CASCADE'
|
||||
},
|
||||
list_id: {
|
||||
type: DataTypes.UUID,
|
||||
primaryKey: true,
|
||||
references: {
|
||||
model: 'lists',
|
||||
key: 'id'
|
||||
},
|
||||
onDelete: 'CASCADE',
|
||||
onUpdate: 'CASCADE'
|
||||
}
|
||||
}, {
|
||||
tableName: 'link_lists',
|
||||
timestamps: false, // No timestamps on junction table
|
||||
underscored: true
|
||||
});
|
||||
|
||||
return LinkList;
|
||||
};
|
||||
|
||||
53
models/List.js
Normal file
53
models/List.js
Normal file
@@ -0,0 +1,53 @@
|
||||
const { DataTypes } = require('sequelize');
|
||||
const { v4: uuidv4 } = require('uuid');
|
||||
|
||||
module.exports = (sequelize) => {
|
||||
const List = sequelize.define('List', {
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: false,
|
||||
validate: {
|
||||
notEmpty: true
|
||||
}
|
||||
},
|
||||
created_at: {
|
||||
type: DataTypes.DATE,
|
||||
defaultValue: DataTypes.NOW,
|
||||
allowNull: false
|
||||
},
|
||||
created_by: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: true
|
||||
},
|
||||
modified_at: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: true
|
||||
},
|
||||
modified_by: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: true
|
||||
},
|
||||
public: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
defaultValue: false,
|
||||
allowNull: false
|
||||
}
|
||||
}, {
|
||||
tableName: 'lists',
|
||||
timestamps: false, // We're using created_at and modified_at manually
|
||||
underscored: true
|
||||
});
|
||||
|
||||
// Hook to set modified_at before update
|
||||
List.beforeUpdate((list) => {
|
||||
list.modified_at = new Date();
|
||||
});
|
||||
|
||||
return List;
|
||||
};
|
||||
|
||||
73
models/index.js
Normal file
73
models/index.js
Normal file
@@ -0,0 +1,73 @@
|
||||
const { Sequelize } = require('sequelize');
|
||||
const Link = require('./Link');
|
||||
const List = require('./List');
|
||||
const LinkList = require('./LinkList');
|
||||
|
||||
// Database connection configuration
|
||||
const getDatabaseConfig = () => {
|
||||
// Support DATABASE_URL or individual connection parameters
|
||||
if (process.env.DATABASE_URL) {
|
||||
return {
|
||||
url: process.env.DATABASE_URL,
|
||||
dialect: 'postgres',
|
||||
dialectOptions: {
|
||||
ssl: process.env.DATABASE_SSL === 'true' ? {
|
||||
require: true,
|
||||
rejectUnauthorized: false
|
||||
} : false
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
host: process.env.DB_HOST || 'localhost',
|
||||
port: process.env.DB_PORT || 5432,
|
||||
database: process.env.DB_NAME || 'linkding',
|
||||
username: process.env.DB_USER || 'postgres',
|
||||
password: process.env.DB_PASSWORD || 'postgres',
|
||||
dialect: 'postgres',
|
||||
logging: process.env.NODE_ENV === 'development' ? console.log : false
|
||||
};
|
||||
};
|
||||
|
||||
// Initialize Sequelize
|
||||
const config = getDatabaseConfig();
|
||||
const sequelize = config.url
|
||||
? new Sequelize(config.url, {
|
||||
dialect: 'postgres',
|
||||
dialectOptions: config.dialectOptions,
|
||||
logging: config.logging
|
||||
})
|
||||
: new Sequelize(config.database, config.username, config.password, {
|
||||
host: config.host,
|
||||
port: config.port,
|
||||
dialect: config.dialect,
|
||||
logging: config.logging
|
||||
});
|
||||
|
||||
// Initialize models
|
||||
const db = {
|
||||
sequelize,
|
||||
Sequelize,
|
||||
Link: Link(sequelize),
|
||||
List: List(sequelize),
|
||||
LinkList: LinkList(sequelize)
|
||||
};
|
||||
|
||||
// Set up associations
|
||||
db.Link.belongsToMany(db.List, {
|
||||
through: db.LinkList,
|
||||
foreignKey: 'link_id',
|
||||
otherKey: 'list_id',
|
||||
as: 'lists'
|
||||
});
|
||||
|
||||
db.List.belongsToMany(db.Link, {
|
||||
through: db.LinkList,
|
||||
foreignKey: 'list_id',
|
||||
otherKey: 'link_id',
|
||||
as: 'links'
|
||||
});
|
||||
|
||||
module.exports = db;
|
||||
|
||||
Reference in New Issue
Block a user