feat: add models init
This commit is contained in:
parent
b5242d0734
commit
e96a246c14
8
README.md
Normal file
8
README.md
Normal file
@ -0,0 +1,8 @@
|
||||
# code-center-module
|
||||
|
||||
## models
|
||||
|
||||
for models user and org
|
||||
|
||||
|
||||
## sequelize
|
16
package.json
16
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@kevisual/code-center-module",
|
||||
"version": "0.0.7-alpha.2",
|
||||
"version": "0.0.7",
|
||||
"description": "",
|
||||
"main": "dist/system.mjs",
|
||||
"module": "dist/system.mjs",
|
||||
@ -23,6 +23,14 @@
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@kevisual/auth": "^1.0.5",
|
||||
"@kevisual/router": "^0.0.7",
|
||||
"@kevisual/use-config": "^1.0.8",
|
||||
"ioredis": "^5.5.0",
|
||||
"pg": "^8.13.3",
|
||||
"sequelize": "^6.37.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@kevisual/auth": "1.0.5",
|
||||
"@kevisual/router": "^0.0.7",
|
||||
@ -65,13 +73,15 @@
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./dist/system.mjs",
|
||||
"require": "./dist/system.cjs",
|
||||
"types": "./dist/system.d.ts"
|
||||
},
|
||||
"./lib": {
|
||||
"import": "./dist/lib.mjs",
|
||||
"require": "./dist/lib.cjs",
|
||||
"types": "./dist/lib.d.ts"
|
||||
},
|
||||
"./models": {
|
||||
"import": "./dist/models.mjs",
|
||||
"types": "./dist/models.d.ts"
|
||||
}
|
||||
}
|
||||
}
|
@ -13,6 +13,7 @@ const version = pkgs.version|| '1.0.0';
|
||||
const external = [
|
||||
/@kevisual\/router(\/.*)?/, //, // 路由
|
||||
/@kevisual\/use-config(\/.*)?/, //
|
||||
/@kevisual\/auth(\/.*)?/, //
|
||||
|
||||
'sequelize', // 数据库 orm
|
||||
'ioredis', // redis
|
||||
@ -151,5 +152,51 @@ const systemConfig = [
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
export default [config, dtsConfig, ...systemConfig];
|
||||
export const modelConfig = [
|
||||
{
|
||||
input: './src/core-models.ts',
|
||||
output: {
|
||||
dir: './dist',
|
||||
entryFileNames: 'models.mjs',
|
||||
chunkFileNames: '[name]-[hash].mjs',
|
||||
format: 'esm',
|
||||
},
|
||||
plugins: [
|
||||
replace({
|
||||
preventAssignment: true, // 防止意外赋值
|
||||
DEV_SERVER: JSON.stringify(isDev), // 替换 process.env.NODE_ENV
|
||||
VERSION: JSON.stringify(version), // 替换版本号
|
||||
}),
|
||||
alias({
|
||||
entries: [
|
||||
{ find: '@', replacement: path.resolve('src') }, // 配置 @ 为 src 目录
|
||||
],
|
||||
}),
|
||||
resolve({
|
||||
preferBuiltins: true, // 强制优先使用内置模块
|
||||
}),
|
||||
commonjs(),
|
||||
esbuild({
|
||||
target: 'node22', // 目标为 Node.js 14
|
||||
minify: false, // 启用代码压缩
|
||||
tsconfig: 'tsconfig.json',
|
||||
}),
|
||||
json(),
|
||||
],
|
||||
external: [
|
||||
...external,//
|
||||
],
|
||||
},
|
||||
{
|
||||
input: './src/core-models.ts',
|
||||
output: {
|
||||
dir: './dist',
|
||||
entryFileNames: 'models.d.ts',
|
||||
format: 'esm',
|
||||
},
|
||||
plugins: [
|
||||
dts(),
|
||||
],
|
||||
},
|
||||
]
|
||||
export default [config, dtsConfig, ...systemConfig, ...modelConfig];
|
||||
|
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) => {
|
||||
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,11 +225,12 @@ 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 })
|
||||
if (sync) {
|
||||
await User.sync({ alter: true, logging: true, ...sync })
|
||||
.then((res) => {
|
||||
initializeUser();
|
||||
})
|
||||
@ -230,6 +238,8 @@ export const UserInit = (newSequelize?: any) => {
|
||||
console.error('Sync User error', err);
|
||||
});
|
||||
return User;
|
||||
}
|
||||
return User;
|
||||
};
|
||||
const letter = 'abcdefghijklmnopqrstuvwxyz';
|
||||
const custom = customAlphabet(letter, 6);
|
||||
|
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({
|
||||
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();
|
Loading…
x
Reference in New Issue
Block a user