feat: add models init
This commit is contained in:
4
src/core-models.ts
Normal file
4
src/core-models.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { UserServices, User, UserInit } from './models/user.ts';
|
||||
import { Org, OrgInit } from './models/org.ts';
|
||||
|
||||
export { User, Org, UserServices, UserInit, OrgInit };
|
||||
@@ -1,13 +1,16 @@
|
||||
import { DataTypes, Model, Sequelize } from 'sequelize';
|
||||
import { useContextKey } from '@kevisual/use-config/context';
|
||||
import { SyncOpts } from './user.ts';
|
||||
export class Org extends Model {
|
||||
declare id: string;
|
||||
declare username: string;
|
||||
declare description: string;
|
||||
declare users: { role: string; uid: string }[];
|
||||
}
|
||||
|
||||
export const OrgInit = (newSequelize?: any) => {
|
||||
/**
|
||||
* 组织模型,在sequelize之后初始化
|
||||
*/
|
||||
export const OrgInit = (newSequelize?: any, tableName?: string, sync?: SyncOpts) => {
|
||||
const sequelize = useContextKey<Sequelize>('sequelize');
|
||||
Org.init(
|
||||
{
|
||||
@@ -33,14 +36,16 @@ export const OrgInit = (newSequelize?: any) => {
|
||||
},
|
||||
{
|
||||
sequelize: newSequelize || sequelize,
|
||||
modelName: 'cf_org',
|
||||
modelName: tableName || 'cf_org',
|
||||
paranoid: true,
|
||||
},
|
||||
);
|
||||
|
||||
Org.sync({ alter: true, logging: false }).catch((e) => {
|
||||
console.error('Org sync', e);
|
||||
});
|
||||
if (sync) {
|
||||
Org.sync({ alter: true, logging: false, ...sync }).catch((e) => {
|
||||
console.error('Org sync', e);
|
||||
});
|
||||
return Org;
|
||||
}
|
||||
return Org;
|
||||
};
|
||||
useContextKey('OrgModel', () => Org);
|
||||
|
||||
@@ -9,17 +9,19 @@ import { Org } from './org.ts';
|
||||
import { useContextKey } from '@kevisual/use-config/context';
|
||||
import { Redis } from 'ioredis';
|
||||
export const redis = useContextKey<Redis>('redis');
|
||||
const sequelize = useContextKey<Sequelize>('sequelize');
|
||||
const config = useConfig<{ tokenSecret: string }>();
|
||||
|
||||
type UserData = {
|
||||
orgs?: string[];
|
||||
};
|
||||
/**
|
||||
* 用户模型,在sequelize和Org之后初始化
|
||||
*/
|
||||
export class User extends Model {
|
||||
declare id: string;
|
||||
declare username: string;
|
||||
declare nickname: string; // 昵称
|
||||
declare alias: string; // 别名
|
||||
// declare alias: string; // 别名
|
||||
declare password: string;
|
||||
declare salt: string;
|
||||
declare needChangePassword: boolean;
|
||||
@@ -154,7 +156,12 @@ export class User extends Model {
|
||||
await redis.del(`user:${this.id}:orgs`);
|
||||
}
|
||||
}
|
||||
export const UserInit = (newSequelize?: any) => {
|
||||
export type SyncOpts = {
|
||||
alter?: boolean;
|
||||
logging?: boolean;
|
||||
force?: boolean;
|
||||
};
|
||||
export const UserInit = async (newSequelize?: any, tableName?: string, sync?: SyncOpts) => {
|
||||
const sequelize = useContextKey<Sequelize>('sequelize');
|
||||
User.init(
|
||||
{
|
||||
@@ -174,10 +181,10 @@ export const UserInit = (newSequelize?: any) => {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: true,
|
||||
},
|
||||
alias: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: true, // 别名,网络请求的别名,需要唯一,不能和username重复
|
||||
},
|
||||
// alias: {
|
||||
// type: DataTypes.TEXT,
|
||||
// allowNull: true, // 别名,网络请求的别名,需要唯一,不能和username重复
|
||||
// },
|
||||
password: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true,
|
||||
@@ -218,17 +225,20 @@ export const UserInit = (newSequelize?: any) => {
|
||||
},
|
||||
{
|
||||
sequelize: newSequelize || sequelize,
|
||||
tableName: 'cf_user', // codeflow user
|
||||
tableName: tableName || 'cf_user', // codeflow user
|
||||
paranoid: true,
|
||||
},
|
||||
);
|
||||
User.sync({ alter: true, logging: false })
|
||||
.then((res) => {
|
||||
initializeUser();
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('Sync User error', err);
|
||||
});
|
||||
if (sync) {
|
||||
await User.sync({ alter: true, logging: true, ...sync })
|
||||
.then((res) => {
|
||||
initializeUser();
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('Sync User error', err);
|
||||
});
|
||||
return User;
|
||||
}
|
||||
return User;
|
||||
};
|
||||
const letter = 'abcdefghijklmnopqrstuvwxyz';
|
||||
|
||||
4
src/modules/init.ts
Normal file
4
src/modules/init.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { useContextKey, useContext } from '@kevisual/use-config/context';
|
||||
import { sequelize } from './sequelize.ts';
|
||||
|
||||
export { sequelize };
|
||||
@@ -1,5 +1,6 @@
|
||||
import { useConfig } from '@kevisual/use-config';
|
||||
import { Sequelize } from 'sequelize';
|
||||
import { useContextKey, useContext } from '@kevisual/use-config/context';
|
||||
|
||||
type PostgresConfig = {
|
||||
postgres: {
|
||||
@@ -19,8 +20,11 @@ if (!postgresConfig) {
|
||||
process.exit(1);
|
||||
}
|
||||
// connect to db
|
||||
export const sequelize = new Sequelize({
|
||||
dialect: 'postgres',
|
||||
...postgresConfig,
|
||||
// logging: false,
|
||||
});
|
||||
export const init = () => {
|
||||
return new Sequelize({
|
||||
dialect: 'postgres',
|
||||
...postgresConfig,
|
||||
// logging: false,
|
||||
});
|
||||
};
|
||||
export const sequelize = useContextKey('sequelize', init);
|
||||
|
||||
24
src/scripts/migrate.ts
Normal file
24
src/scripts/migrate.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
// 迁移英文是 migrate,所以这个文件的作用是迁移数据
|
||||
import '../modules/init.ts';
|
||||
import { User, UserInit } from '../models/user.ts';
|
||||
import { where } from 'sequelize';
|
||||
import { useContextKey } from '@kevisual/use-config/context';
|
||||
const sequelize = useContextKey('sequelize');
|
||||
export const main = async () => {
|
||||
await UserInit();
|
||||
// User.sync({ force: true });
|
||||
// const users = await User.findAll({
|
||||
// paranoid: true,
|
||||
// });
|
||||
// const UserInit2 = await UserInit(sequelize, 'kv_user');
|
||||
// console.log('done', users.length);
|
||||
const user = await User.findOne({
|
||||
where: {
|
||||
username: 'system',
|
||||
},
|
||||
});
|
||||
console.log('user', user);
|
||||
// process.exit(0);
|
||||
};
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user