continued
implemented HTMX implemented ORM (sequelize)
This commit is contained in:
parent
2a9bd4e81b
commit
d756a192e4
71 changed files with 3822 additions and 694 deletions
41
lib/database/connect.mjs
Normal file
41
lib/database/connect.mjs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
// models/index.js
|
||||
import { Sequelize } from 'sequelize';
|
||||
import { getConfig } from "../config.mjs";
|
||||
import { registerModels } from './models.mjs';
|
||||
|
||||
const configData = await getConfig();
|
||||
|
||||
// SQLite-Datenbank im data/-Verzeichnis
|
||||
const sequelize = new Sequelize({
|
||||
dialect: 'mysql',
|
||||
host: configData.database.host,
|
||||
port: configData.database.port,
|
||||
database: configData.database.database,
|
||||
username: configData.database.username,
|
||||
password: configData.database.password,
|
||||
logging: false,
|
||||
});
|
||||
|
||||
// Modelle initialisieren
|
||||
const {
|
||||
Group,
|
||||
User
|
||||
} = registerModels(sequelize);
|
||||
|
||||
// Datenbank synchronisieren
|
||||
(async () => {
|
||||
try {
|
||||
await sequelize.authenticate();
|
||||
console.log('✓ MySQL connection successfull');
|
||||
// Automatische Migration
|
||||
await sequelize.sync({ alter: true, force: false });
|
||||
console.log('✓ MySQL migration finished');
|
||||
} catch (error) {
|
||||
console.error('✕ MySQL Error:', error);
|
||||
}
|
||||
})()
|
||||
|
||||
export {
|
||||
Group,
|
||||
User
|
||||
};
|
||||
163
lib/database/models.mjs
Normal file
163
lib/database/models.mjs
Normal file
|
|
@ -0,0 +1,163 @@
|
|||
// models/MacPolicy.js
|
||||
import { DataTypes } from 'sequelize';
|
||||
|
||||
export const registerModels = (sequelize) => {
|
||||
const Group = sequelize.define('Group', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER(11),
|
||||
primaryKey: true,
|
||||
autoIncrement: true
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING(64),
|
||||
allowNull: false,
|
||||
unique: true
|
||||
},
|
||||
gidnumber: {
|
||||
type: DataTypes.INTEGER(11),
|
||||
allowNull: false,
|
||||
unique: true
|
||||
}
|
||||
}, {
|
||||
tableName: 'ldapgroups',
|
||||
timestamps: false,
|
||||
hooks: {
|
||||
afterCreate: (instance, options) => {
|
||||
console.log(`✓ saved Group: ${instance.name}`);
|
||||
},
|
||||
afterUpdate: (instance, options) => {
|
||||
console.log(`✓ updated Group: ${instance.name}`);
|
||||
},
|
||||
afterDestroy: (instance, options) => {
|
||||
console.log(`✗ deleted Group: ${instance.name}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const User = sequelize.define('User', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING(64),
|
||||
allowNull: false,
|
||||
unique: true
|
||||
},
|
||||
uidnumber: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false
|
||||
},
|
||||
primarygroup: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false
|
||||
},
|
||||
othergroups: {
|
||||
type: DataTypes.STRING(1024),
|
||||
allowNull: true,
|
||||
defaultValue: ''
|
||||
},
|
||||
givenname: {
|
||||
type: DataTypes.STRING(64),
|
||||
allowNull: true,
|
||||
defaultValue: ''
|
||||
},
|
||||
sn: {
|
||||
type: DataTypes.STRING(64),
|
||||
allowNull: true,
|
||||
defaultValue: ''
|
||||
},
|
||||
mail: {
|
||||
type: DataTypes.STRING(254),
|
||||
allowNull: true,
|
||||
defaultValue: ''
|
||||
},
|
||||
loginshell: {
|
||||
type: DataTypes.STRING(64),
|
||||
allowNull: true,
|
||||
defaultValue: ''
|
||||
},
|
||||
homedirectory: {
|
||||
type: DataTypes.STRING(64),
|
||||
allowNull: true,
|
||||
defaultValue: ''
|
||||
},
|
||||
disabled: {
|
||||
type: DataTypes.SMALLINT,
|
||||
allowNull: true,
|
||||
defaultValue: 0
|
||||
},
|
||||
passsha256: {
|
||||
type: DataTypes.STRING(64),
|
||||
allowNull: true,
|
||||
defaultValue: ''
|
||||
},
|
||||
passbcrypt: {
|
||||
type: DataTypes.STRING(64),
|
||||
allowNull: true,
|
||||
defaultValue: ''
|
||||
},
|
||||
otpsecret: {
|
||||
type: DataTypes.STRING(64),
|
||||
allowNull: true,
|
||||
defaultValue: ''
|
||||
},
|
||||
yubikey: {
|
||||
type: DataTypes.STRING(128),
|
||||
allowNull: true,
|
||||
defaultValue: ''
|
||||
},
|
||||
sshkeys: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: true,
|
||||
defaultValue: ''
|
||||
},
|
||||
custattr: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: true,
|
||||
defaultValue: '{}',
|
||||
get() {
|
||||
try {
|
||||
return JSON.parse(this.getDataValue('custattr'));
|
||||
} catch (e) {
|
||||
return {}; // Fallback, falls DB-Daten fehlerhaft sind
|
||||
}
|
||||
},
|
||||
set(value) {
|
||||
this.setDataValue('custattr', JSON.stringify(value ?? {}));
|
||||
}
|
||||
},
|
||||
}, {
|
||||
tableName: 'users',
|
||||
timestamps: false,
|
||||
hooks: {
|
||||
afterCreate: (instance, options) => {
|
||||
console.log(`✓ saved User: ${instance.name}`);
|
||||
},
|
||||
afterUpdate: (instance, options) => {
|
||||
console.log(`✓ updated User: ${instance.name}`);
|
||||
},
|
||||
afterDestroy: (instance, options) => {
|
||||
console.log(`✗ deleted User: ${instance.name}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Group.hasMany(User, {
|
||||
// targetKey: 'uidnumber',
|
||||
// foreignKey: 'primarygroup',
|
||||
// as: 'users'
|
||||
// });
|
||||
|
||||
User.belongsTo(Group, {
|
||||
targetKey: 'gidnumber',
|
||||
foreignKey: 'primarygroup',
|
||||
as: 'primarygroupObject'
|
||||
});
|
||||
|
||||
return {
|
||||
Group,
|
||||
User
|
||||
};
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue