fix: init for model

This commit is contained in:
2025-02-28 03:06:35 +08:00
parent 4feba785d1
commit b5242d0734
9 changed files with 359 additions and 156 deletions

View File

@@ -1,14 +1,21 @@
/**
* redis和sequelizeuseContextKey当中
*/
import { app } from './app.ts';
import { UserServices } from './models/user.ts';
import { Org } from './models/org.ts';
import { UserServices, UserInit } from './models/user.ts';
import { Org, OrgInit } from './models/org.ts';
import { useContextKey } from '@kevisual/use-config/context';
import { Sequelize } from 'sequelize';
import { Redis } from 'ioredis';
export const User = UserServices;
export { Org };
export { Org, OrgInit, UserInit };
export const redis = useContextKey<Redis>('redis');
export const sequelize = useContextKey<Sequelize>('sequelize');
export const UserModel = useContextKey<typeof UserServices>('UserModel');
export const OrgModel = useContextKey<typeof Org>('OrgModel');
export const UserModel = useContextKey<typeof UserServices>('UserModel', () => UserServices);
export const OrgModel = useContextKey<typeof Org>('OrgModel', () => Org);
export { app };
export const init = () => {
OrgInit();
UserInit();
};

View File

@@ -1,6 +1,5 @@
import { DataTypes, Model, Sequelize } from 'sequelize';
import { useContextKey } from '@kevisual/use-config/context';
const sequelize = useContextKey<Sequelize>('sequelize');
export class Org extends Model {
declare id: string;
declare username: string;
@@ -8,37 +7,40 @@ export class Org extends Model {
declare users: { role: string; uid: string }[];
}
Org.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: DataTypes.UUIDV4,
export const OrgInit = (newSequelize?: any) => {
const sequelize = useContextKey<Sequelize>('sequelize');
Org.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: DataTypes.UUIDV4,
},
username: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
description: {
type: DataTypes.STRING,
allowNull: true,
},
users: {
type: DataTypes.JSONB,
allowNull: true,
defaultValue: [],
},
},
username: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
{
sequelize: newSequelize || sequelize,
modelName: 'cf_org',
paranoid: true,
},
description: {
type: DataTypes.STRING,
allowNull: true,
},
users: {
type: DataTypes.JSONB,
allowNull: true,
defaultValue: [],
},
},
{
sequelize,
modelName: 'cf_org',
paranoid: true,
},
);
Org.sync({ alter: true, logging: false }).catch((e) => {
console.error('Org sync', e);
});
);
Org.sync({ alter: true, logging: false }).catch((e) => {
console.error('Org sync', e);
});
return Org;
};
useContextKey('OrgModel', () => Org);

View File

@@ -154,80 +154,83 @@ export class User extends Model {
await redis.del(`user:${this.id}:orgs`);
}
}
User.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: DataTypes.UUIDV4,
export const UserInit = (newSequelize?: any) => {
const sequelize = useContextKey<Sequelize>('sequelize');
User.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: DataTypes.UUIDV4,
},
username: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
// 用户名或者手机号
// 创建后避免修改的字段,当注册用户后,用户名注册则默认不能用手机号
},
nickname: {
type: DataTypes.TEXT,
allowNull: true,
},
alias: {
type: DataTypes.TEXT,
allowNull: true, // 别名网络请求的别名需要唯一不能和username重复
},
password: {
type: DataTypes.STRING,
allowNull: true,
},
email: {
type: DataTypes.STRING,
allowNull: true,
},
avatar: {
type: DataTypes.TEXT,
allowNull: true,
},
salt: {
type: DataTypes.STRING,
allowNull: true,
},
description: {
type: DataTypes.TEXT,
},
type: {
type: DataTypes.STRING,
defaultValue: 'user',
},
owner: {
type: DataTypes.UUID,
},
orgId: {
type: DataTypes.UUID,
},
needChangePassword: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
data: {
type: DataTypes.JSONB,
defaultValue: {},
},
},
username: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
// 用户名或者手机号
// 创建后避免修改的字段,当注册用户后,用户名注册则默认不能用手机号
{
sequelize: newSequelize || sequelize,
tableName: 'cf_user', // codeflow user
paranoid: true,
},
nickname: {
type: DataTypes.TEXT,
allowNull: true,
},
alias: {
type: DataTypes.TEXT,
allowNull: true, // 别名网络请求的别名需要唯一不能和username重复
},
password: {
type: DataTypes.STRING,
allowNull: true,
},
email: {
type: DataTypes.STRING,
allowNull: true,
},
avatar: {
type: DataTypes.TEXT,
allowNull: true,
},
salt: {
type: DataTypes.STRING,
allowNull: true,
},
description: {
type: DataTypes.TEXT,
},
type: {
type: DataTypes.STRING,
defaultValue: 'user',
},
owner: {
type: DataTypes.UUID,
},
orgId: {
type: DataTypes.UUID,
},
needChangePassword: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
data: {
type: DataTypes.JSON,
defaultValue: {},
},
},
{
sequelize,
tableName: 'cf_user', // codeflow user
paranoid: true,
},
);
User.sync({ alter: true, logging: false })
.then((res) => {
initializeUser();
})
.catch((err) => {
console.error('Sync User error', err);
});
);
User.sync({ alter: true, logging: false })
.then((res) => {
initializeUser();
})
.catch((err) => {
console.error('Sync User error', err);
});
return User;
};
const letter = 'abcdefghijklmnopqrstuvwxyz';
const custom = customAlphabet(letter, 6);
export const initializeUser = async (pwd = custom()) => {

33
src/modules/redis.ts Normal file
View File

@@ -0,0 +1,33 @@
import { Redis } from 'ioredis';
import { useConfig } from '@kevisual/use-config';
const config = useConfig<{
redis: ConstructorParameters<typeof Redis>;
}>();
// 配置 Redis 连接
export const redis = new Redis({
host: 'localhost', // Redis 服务器的主机名或 IP 地址
port: 6379, // Redis 服务器的端口号
// password: 'your_password', // Redis 的密码 (如果有)
db: 0, // 要使用的 Redis 数据库索引 (0-15)
keyPrefix: '', // key 前缀
retryStrategy(times) {
// 连接重试策略
return Math.min(times * 50, 2000); // 每次重试时延迟增加
},
maxRetriesPerRequest: null, // 允许请求重试的次数 (如果需要无限次重试)
...config.redis,
});
// 监听连接事件
redis.on('connect', () => {
console.log('Redis 连接成功');
});
redis.on('error', (err) => {
console.error('Redis 连接错误', err);
});
// 初始化 Redis 客户端
export const redisPublisher = new Redis(); // 用于发布消息
export const redisSubscriber = new Redis(); // 用于订阅消息

26
src/modules/sequelize.ts Normal file
View File

@@ -0,0 +1,26 @@
import { useConfig } from '@kevisual/use-config';
import { Sequelize } from 'sequelize';
type PostgresConfig = {
postgres: {
username: string;
password: string;
host: string;
port: number;
database: string;
};
};
const config = useConfig<PostgresConfig>();
const postgresConfig = config.postgres;
if (!postgresConfig) {
console.error('postgres config is required');
process.exit(1);
}
// connect to db
export const sequelize = new Sequelize({
dialect: 'postgres',
...postgresConfig,
// logging: false,
});

29
src/system.ts Normal file
View File

@@ -0,0 +1,29 @@
/**
* 直接开发业务代码把redis和sequelize的初始化放到库当中。
*/
import { useConfig } from '@kevisual/use-config';
import { app } from './app.ts';
import * as sequelizeLib from './modules/sequelize.ts';
export const sequelize = useContextKey('sequelize', () => sequelizeLib.sequelize);
import { UserServices, UserInit } from './models/user.ts';
import { Org, OrgInit } from './models/org.ts';
import * as redisLib from './modules/redis.ts';
import { useContextKey } from '@kevisual/use-config/context';
useConfig();
export const redis = useContextKey('redis', () => redisLib.redis);
export const redisPublisher = useContextKey('redisPublisher', () => redisLib.redisPublisher);
export const redisSubscriber = useContextKey('redisSubscriber', () => redisLib.redisSubscriber);
export const UserModel = useContextKey<typeof UserServices>('UserModel', () => UserServices);
export const OrgModel = useContextKey<typeof Org>('OrgModel', () => Org);
export { app };
export const User = UserServices;
export { Org, OrgInit, UserInit };
export const init = () => {
OrgInit();
UserInit();
};