mirror of
https://github.com/vastxie/99AI.git
synced 2025-11-09 10:13:43 +08:00
v3.5.0
This commit is contained in:
20
dist/modules/database/database.module.js
vendored
20
dist/modules/database/database.module.js
vendored
@@ -13,13 +13,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.DatabaseModule = void 0;
|
||||
const common_1 = require("@nestjs/common");
|
||||
const typeorm_1 = require("@nestjs/typeorm");
|
||||
const nestjs_config_1 = require("nestjs-config");
|
||||
const typeorm_2 = require("typeorm");
|
||||
const database_service_1 = require("./database.service");
|
||||
let DatabaseModule = DatabaseModule_1 = class DatabaseModule {
|
||||
constructor(connection, config) {
|
||||
constructor(connection) {
|
||||
this.connection = connection;
|
||||
this.config = config;
|
||||
this.logger = new common_1.Logger(DatabaseModule_1.name);
|
||||
}
|
||||
onModuleInit() {
|
||||
@@ -31,12 +29,22 @@ DatabaseModule = DatabaseModule_1 = __decorate([
|
||||
(0, common_1.Module)({
|
||||
imports: [
|
||||
typeorm_1.TypeOrmModule.forRootAsync({
|
||||
useFactory: (config) => config.get('database'),
|
||||
inject: [nestjs_config_1.ConfigService],
|
||||
useFactory: () => ({
|
||||
type: 'mysql',
|
||||
host: process.env.DB_HOST,
|
||||
port: parseInt(process.env.DB_PORT, 10),
|
||||
username: process.env.DB_USER,
|
||||
password: process.env.DB_PASS,
|
||||
database: process.env.DB_DATABASE,
|
||||
entities: [__dirname + '/../**/*.entity{.ts,.js}'],
|
||||
logging: false,
|
||||
charset: 'utf8mb4',
|
||||
timezone: '+08:00',
|
||||
}),
|
||||
}),
|
||||
],
|
||||
providers: [database_service_1.DatabaseService],
|
||||
}),
|
||||
__metadata("design:paramtypes", [typeorm_2.Connection, nestjs_config_1.ConfigService])
|
||||
__metadata("design:paramtypes", [typeorm_2.DataSource])
|
||||
], DatabaseModule);
|
||||
exports.DatabaseModule = DatabaseModule;
|
||||
|
||||
181
dist/modules/database/database.service.js
vendored
181
dist/modules/database/database.service.js
vendored
@@ -29,8 +29,22 @@ let DatabaseService = class DatabaseService {
|
||||
const adminPassword = bcrypt.hashSync('123456', 10);
|
||||
const superEmail = 'super';
|
||||
const adminEmail = 'admin';
|
||||
const superUserinfo = { username: 'super', password: superPassword, status: 1, email: superEmail, sex: 1, role: 'super' };
|
||||
const adminUserinfo = { username: 'admin', password: adminPassword, status: 0, email: adminEmail, sex: 1, role: 'admin' };
|
||||
const superUserinfo = {
|
||||
username: 'super',
|
||||
password: superPassword,
|
||||
status: 1,
|
||||
email: superEmail,
|
||||
sex: 1,
|
||||
role: 'super',
|
||||
};
|
||||
const adminUserinfo = {
|
||||
username: 'admin',
|
||||
password: adminPassword,
|
||||
status: 0,
|
||||
email: adminEmail,
|
||||
sex: 1,
|
||||
role: 'admin',
|
||||
};
|
||||
await this.createDefaultUser(superUserinfo);
|
||||
await this.createDefaultUser(adminUserinfo);
|
||||
}
|
||||
@@ -41,7 +55,7 @@ let DatabaseService = class DatabaseService {
|
||||
const user = await this.connection.query(`INSERT INTO users (username, password, status, email, role) VALUES ('${username}', '${password}', '${status}', '${email}', '${role}')`);
|
||||
const userId = user.insertId;
|
||||
const balance = await this.connection.query(`INSERT INTO balance (userId, balance, usesLeft, paintCount) VALUES ('${userId}', 0, 1000, 100)`);
|
||||
common_1.Logger.log(`初始化创建${role}用户成功、用户名为[${username}]、初始密码为[${username === 'super' ? 'nine-super' : '123456'}] ==============> 请注意查阅`, 'DatabaseService');
|
||||
common_1.Logger.log(`初始化创建${role}用户成功、用户名为[${username}]、初始密码为[${username === 'super' ? 'super' : '123456'}] ==============> 请注意查阅`, 'DatabaseService');
|
||||
}
|
||||
catch (error) {
|
||||
console.log('error: ', error);
|
||||
@@ -49,9 +63,17 @@ let DatabaseService = class DatabaseService {
|
||||
}
|
||||
}
|
||||
async checkSiteBaseConfig() {
|
||||
const keys = ['siteName', 'qqNumber', 'vxNumber', 'robotAvatar', 'userDefautlAvatar'];
|
||||
const keys = [
|
||||
'siteName',
|
||||
'qqNumber',
|
||||
'vxNumber',
|
||||
'robotAvatar',
|
||||
'userDefautlAvatar',
|
||||
];
|
||||
const result = await this.connection.query(`
|
||||
SELECT COUNT(*) AS count FROM config WHERE \`configKey\` IN (${keys.map((k) => `'${k}'`).join(',')})
|
||||
SELECT COUNT(*) AS count FROM config WHERE \`configKey\` IN (${keys
|
||||
.map((k) => `'${k}'`)
|
||||
.join(',')})
|
||||
`);
|
||||
const count = parseInt(result[0].count);
|
||||
if (count === 0) {
|
||||
@@ -87,27 +109,112 @@ let DatabaseService = class DatabaseService {
|
||||
encry: 0,
|
||||
},
|
||||
{ configKey: 'buyCramiAddress', configVal: '', public: 1, encry: 0 },
|
||||
{ configKey: 'openaiBaseUrl', configVal: 'https://api.lightai.io', public: 0, encry: 0 },
|
||||
{
|
||||
configKey: 'openaiBaseUrl',
|
||||
configVal: 'https://api.lightai.io',
|
||||
public: 0,
|
||||
encry: 0,
|
||||
},
|
||||
{ configKey: 'openaiTimeout', configVal: '300', public: 0, encry: 0 },
|
||||
{ configKey: 'openaiBaseKey', configVal: 'sk-', public: 0, encry: 0 },
|
||||
{ configKey: 'mjTranslatePrompt', configVal: `Translate any given phrase from any language into English. For instance, when I input '{可爱的熊猫}', you should output '{cute panda}', with no period at the end.`, public: 0, encry: 0 },
|
||||
{
|
||||
configKey: 'mjTranslatePrompt',
|
||||
configVal: `Translate any given phrase from any language into English. For instance, when I input '{可爱的熊猫}', you should output '{cute panda}', with no period at the end.`,
|
||||
public: 0,
|
||||
encry: 0,
|
||||
},
|
||||
{ configKey: 'noticeInfo', configVal: noticeInfo, public: 1, encry: 0 },
|
||||
{ configKey: 'registerSendStatus', configVal: '1', public: 1, encry: 0 },
|
||||
{ configKey: 'registerSendModel3Count', configVal: '30', public: 1, encry: 0 },
|
||||
{ configKey: 'registerSendModel4Count', configVal: '3', public: 1, encry: 0 },
|
||||
{ configKey: 'registerSendDrawMjCount', configVal: '3', public: 1, encry: 0 },
|
||||
{ configKey: 'firstRegisterSendStatus', configVal: '1', public: 1, encry: 0 },
|
||||
{ configKey: 'firstRegisterSendRank', configVal: '500', public: 1, encry: 0 },
|
||||
{ configKey: 'firstRregisterSendModel3Count', configVal: '10', public: 1, encry: 0 },
|
||||
{ configKey: 'firstRregisterSendModel4Count', configVal: '10', public: 1, encry: 0 },
|
||||
{ configKey: 'firstRregisterSendDrawMjCount', configVal: '10', public: 1, encry: 0 },
|
||||
{
|
||||
configKey: 'registerSendStatus',
|
||||
configVal: '1',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{
|
||||
configKey: 'registerSendModel3Count',
|
||||
configVal: '30',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{
|
||||
configKey: 'registerSendModel4Count',
|
||||
configVal: '3',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{
|
||||
configKey: 'registerSendDrawMjCount',
|
||||
configVal: '3',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{
|
||||
configKey: 'firstRegisterSendStatus',
|
||||
configVal: '1',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{
|
||||
configKey: 'firstRegisterSendRank',
|
||||
configVal: '500',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{
|
||||
configKey: 'firstRregisterSendModel3Count',
|
||||
configVal: '10',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{
|
||||
configKey: 'firstRregisterSendModel4Count',
|
||||
configVal: '10',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{
|
||||
configKey: 'firstRregisterSendDrawMjCount',
|
||||
configVal: '10',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{ configKey: 'inviteSendStatus', configVal: '1', public: 1, encry: 0 },
|
||||
{ configKey: 'inviteGiveSendModel3Count', configVal: '0', public: 1, encry: 0 },
|
||||
{ configKey: 'inviteGiveSendModel4Count', configVal: '0', public: 1, encry: 0 },
|
||||
{ configKey: 'inviteGiveSendDrawMjCount', configVal: '0', public: 1, encry: 0 },
|
||||
{ configKey: 'invitedGuestSendModel3Count', configVal: '10', public: 1, encry: 0 },
|
||||
{ configKey: 'invitedGuestSendModel4Count', configVal: '10', public: 1, encry: 0 },
|
||||
{ configKey: 'invitedGuestSendDrawMjCount', configVal: '10', public: 1, encry: 0 },
|
||||
{
|
||||
configKey: 'inviteGiveSendModel3Count',
|
||||
configVal: '0',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{
|
||||
configKey: 'inviteGiveSendModel4Count',
|
||||
configVal: '0',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{
|
||||
configKey: 'inviteGiveSendDrawMjCount',
|
||||
configVal: '0',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{
|
||||
configKey: 'invitedGuestSendModel3Count',
|
||||
configVal: '10',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{
|
||||
configKey: 'invitedGuestSendModel4Count',
|
||||
configVal: '10',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{
|
||||
configKey: 'invitedGuestSendDrawMjCount',
|
||||
configVal: '10',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{ configKey: 'isVerifyEmail', configVal: '1', public: 1, encry: 0 },
|
||||
];
|
||||
const res = await this.connection.query(`INSERT INTO config (configKey, configVal, public, encry) VALUES ${defaultConfig
|
||||
@@ -122,15 +229,39 @@ let DatabaseService = class DatabaseService {
|
||||
}
|
||||
async createSystemReservedApps() {
|
||||
const systemApps = [
|
||||
{ name: "提示词优化PromptOptimization", catId: 9900, des: "PromptOptimization", preset: "Translate any given phrase from any language into English. For instance, when I input '{可爱的熊猫}', you should output '{cute panda}', with no period at the end.", appModel: "gpt-3.5-turbo", isFixedModel: 1, isSystemReserved: 1 },
|
||||
{ name: "思维导图MindMap", catId: 9900, des: "MindMap", preset: "我希望你使用markdown格式回答我得问题、我的需求是得到一份markdown格式的大纲、尽量做的精细、层级多一点、不管我问你什么、都需要您回复我一个大纲出来、我想使用大纲做思维导图、除了大纲之外、不要无关内容和总结。", appModel: "gpt-3.5-turbo", isFixedModel: 1, isSystemReserved: 1 },
|
||||
{
|
||||
name: '提示词优化PromptOptimization',
|
||||
catId: 9900,
|
||||
des: 'PromptOptimization',
|
||||
preset: "Translate any given phrase from any language into English. For instance, when I input '{可爱的熊猫}', you should output '{cute panda}', with no period at the end.",
|
||||
appModel: 'gpt-3.5-turbo',
|
||||
isFixedModel: 1,
|
||||
isSystemReserved: 1,
|
||||
},
|
||||
{
|
||||
name: '思维导图MindMap',
|
||||
catId: 9900,
|
||||
des: 'MindMap',
|
||||
preset: '我希望你使用markdown格式回答我得问题、我的需求是得到一份markdown格式的大纲、尽量做的精细、层级多一点、不管我问你什么、都需要您回复我一个大纲出来、我想使用大纲做思维导图、除了大纲之外、不要无关内容和总结。',
|
||||
appModel: 'gpt-3.5-turbo',
|
||||
isFixedModel: 1,
|
||||
isSystemReserved: 1,
|
||||
},
|
||||
];
|
||||
try {
|
||||
for (const app of systemApps) {
|
||||
const result = await this.connection.query(`SELECT COUNT(*) AS count FROM app WHERE name = ? AND des = ? AND isSystemReserved = ?`, [app.name, app.des, app.isSystemReserved]);
|
||||
const count = parseInt(result[0].count, 10);
|
||||
if (count === 0) {
|
||||
await this.connection.query(`INSERT INTO app (name, catId, des, preset, appModel, isFixedModel, isSystemReserved) VALUES (?, ?, ?, ?, ?, ?, ?)`, [app.name, app.catId, app.des, app.preset, app.appModel, app.isFixedModel, app.isSystemReserved]);
|
||||
await this.connection.query(`INSERT INTO app (name, catId, des, preset, appModel, isFixedModel, isSystemReserved) VALUES (?, ?, ?, ?, ?, ?, ?)`, [
|
||||
app.name,
|
||||
app.catId,
|
||||
app.des,
|
||||
app.preset,
|
||||
app.appModel,
|
||||
app.isFixedModel,
|
||||
app.isSystemReserved,
|
||||
]);
|
||||
common_1.Logger.log(`系统预留应用${app.name}创建成功`, 'DatabaseService');
|
||||
}
|
||||
}
|
||||
|
||||
90
dist/modules/database/initDatabase.js
vendored
90
dist/modules/database/initDatabase.js
vendored
@@ -1,23 +1,95 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.initDatabase = void 0;
|
||||
const mysql = require("mysql2/promise");
|
||||
const common_1 = require("@nestjs/common");
|
||||
function initDatabase() {
|
||||
mysql
|
||||
.createConnection({
|
||||
const dotenv_1 = require("dotenv");
|
||||
const mysql = require("mysql2/promise");
|
||||
const typeorm_1 = require("typeorm");
|
||||
(0, dotenv_1.config)();
|
||||
const dataSourceOptions = {
|
||||
type: 'mysql',
|
||||
port: parseInt(process.env.DB_PORT, 10),
|
||||
host: process.env.DB_HOST,
|
||||
username: process.env.DB_USER,
|
||||
password: process.env.DB_PASS,
|
||||
database: process.env.DB_DATABASE,
|
||||
entities: [__dirname + '/../**/*.entity{.ts,.js}'],
|
||||
logging: false,
|
||||
synchronize: true,
|
||||
charset: 'utf8mb4',
|
||||
timezone: '+08:00',
|
||||
};
|
||||
async function validateDatabase() {
|
||||
const conn = await mysql.createConnection({
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASS,
|
||||
port: parseInt(process.env.DB_PORT),
|
||||
})
|
||||
.then(async (conn) => {
|
||||
const [rows] = await conn.execute(`SHOW DATABASES LIKE '${process.env.DB_DATABASE}'`);
|
||||
port: parseInt(process.env.DB_PORT, 10),
|
||||
});
|
||||
try {
|
||||
const [rows] = (await conn.execute(`SHOW DATABASES LIKE '${process.env.DB_DATABASE}'`));
|
||||
if (Array.isArray(rows) && rows.length === 0) {
|
||||
await conn.execute(`CREATE DATABASE ${process.env.DB_DATABASE}`);
|
||||
common_1.Logger.log(`数据库创建成功[${process.env.DB_DATABASE}]`);
|
||||
common_1.Logger.log(`数据库创建成功[${process.env.DB_DATABASE}]`, 'Database');
|
||||
}
|
||||
else {
|
||||
common_1.Logger.log(`数据库已存在[${process.env.DB_DATABASE}]`, 'Database');
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
common_1.Logger.error('Error during database validation:', error, 'Database');
|
||||
}
|
||||
finally {
|
||||
await conn.end();
|
||||
}
|
||||
}
|
||||
async function updateColumnType() {
|
||||
const conn = await mysql.createConnection({
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASS,
|
||||
port: parseInt(process.env.DB_PORT, 10),
|
||||
database: process.env.DB_DATABASE,
|
||||
});
|
||||
async function checkAndUpdateColumnType(tableName, columnName) {
|
||||
try {
|
||||
const [rows] = (await conn.execute(`SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND COLUMN_NAME = ?`, [tableName, columnName]));
|
||||
if (rows.length > 0 && rows[0].DATA_TYPE !== 'text') {
|
||||
await conn.execute(`ALTER TABLE ?? MODIFY COLUMN ?? TEXT`, [
|
||||
tableName,
|
||||
columnName,
|
||||
]);
|
||||
common_1.Logger.log(`Column ${columnName} type updated to TEXT in table ${tableName}`, 'Database');
|
||||
}
|
||||
else {
|
||||
common_1.Logger.log(`Column ${columnName} is already of type TEXT in table ${tableName}`, 'Database');
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
common_1.Logger.error(`Error updating column type in table ${tableName}:`, error);
|
||||
}
|
||||
}
|
||||
try {
|
||||
await checkAndUpdateColumnType('config', 'configVal');
|
||||
await checkAndUpdateColumnType('app', 'coverImg');
|
||||
}
|
||||
finally {
|
||||
await conn.end();
|
||||
}
|
||||
}
|
||||
async function initDatabase() {
|
||||
await validateDatabase();
|
||||
await updateColumnType();
|
||||
const dataSource = new typeorm_1.DataSource(dataSourceOptions);
|
||||
try {
|
||||
await dataSource.initialize();
|
||||
common_1.Logger.log('Database connected and synchronized successfully', 'Database');
|
||||
}
|
||||
catch (error) {
|
||||
common_1.Logger.error('Error during TypeORM initialization:', error);
|
||||
}
|
||||
finally {
|
||||
await dataSource.destroy();
|
||||
}
|
||||
}
|
||||
exports.initDatabase = initDatabase;
|
||||
|
||||
Reference in New Issue
Block a user