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",
|
"name": "@kevisual/code-center-module",
|
||||||
"version": "0.0.7-alpha.2",
|
"version": "0.0.7",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "dist/system.mjs",
|
"main": "dist/system.mjs",
|
||||||
"module": "dist/system.mjs",
|
"module": "dist/system.mjs",
|
||||||
@ -23,6 +23,14 @@
|
|||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
"access": "public"
|
"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": {
|
"dependencies": {
|
||||||
"@kevisual/auth": "1.0.5",
|
"@kevisual/auth": "1.0.5",
|
||||||
"@kevisual/router": "^0.0.7",
|
"@kevisual/router": "^0.0.7",
|
||||||
@ -65,13 +73,15 @@
|
|||||||
"exports": {
|
"exports": {
|
||||||
".": {
|
".": {
|
||||||
"import": "./dist/system.mjs",
|
"import": "./dist/system.mjs",
|
||||||
"require": "./dist/system.cjs",
|
|
||||||
"types": "./dist/system.d.ts"
|
"types": "./dist/system.d.ts"
|
||||||
},
|
},
|
||||||
"./lib": {
|
"./lib": {
|
||||||
"import": "./dist/lib.mjs",
|
"import": "./dist/lib.mjs",
|
||||||
"require": "./dist/lib.cjs",
|
|
||||||
"types": "./dist/lib.d.ts"
|
"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 = [
|
const external = [
|
||||||
/@kevisual\/router(\/.*)?/, //, // 路由
|
/@kevisual\/router(\/.*)?/, //, // 路由
|
||||||
/@kevisual\/use-config(\/.*)?/, //
|
/@kevisual\/use-config(\/.*)?/, //
|
||||||
|
/@kevisual\/auth(\/.*)?/, //
|
||||||
|
|
||||||
'sequelize', // 数据库 orm
|
'sequelize', // 数据库 orm
|
||||||
'ioredis', // redis
|
'ioredis', // redis
|
||||||
@ -151,5 +152,51 @@ const systemConfig = [
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
export const modelConfig = [
|
||||||
export default [config, dtsConfig, ...systemConfig];
|
{
|
||||||
|
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 { DataTypes, Model, Sequelize } from 'sequelize';
|
||||||
import { useContextKey } from '@kevisual/use-config/context';
|
import { useContextKey } from '@kevisual/use-config/context';
|
||||||
|
import { SyncOpts } from './user.ts';
|
||||||
export class Org extends Model {
|
export class Org extends Model {
|
||||||
declare id: string;
|
declare id: string;
|
||||||
declare username: string;
|
declare username: string;
|
||||||
declare description: string;
|
declare description: string;
|
||||||
declare users: { role: string; uid: 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');
|
const sequelize = useContextKey<Sequelize>('sequelize');
|
||||||
Org.init(
|
Org.init(
|
||||||
{
|
{
|
||||||
@ -33,14 +36,16 @@ export const OrgInit = (newSequelize?: any) => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
sequelize: newSequelize || sequelize,
|
sequelize: newSequelize || sequelize,
|
||||||
modelName: 'cf_org',
|
modelName: tableName || 'cf_org',
|
||||||
paranoid: true,
|
paranoid: true,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
if (sync) {
|
||||||
Org.sync({ alter: true, logging: false }).catch((e) => {
|
Org.sync({ alter: true, logging: false, ...sync }).catch((e) => {
|
||||||
console.error('Org sync', e);
|
console.error('Org sync', e);
|
||||||
});
|
});
|
||||||
|
return Org;
|
||||||
|
}
|
||||||
return Org;
|
return Org;
|
||||||
};
|
};
|
||||||
useContextKey('OrgModel', () => Org);
|
useContextKey('OrgModel', () => Org);
|
||||||
|
@ -9,17 +9,19 @@ import { Org } from './org.ts';
|
|||||||
import { useContextKey } from '@kevisual/use-config/context';
|
import { useContextKey } from '@kevisual/use-config/context';
|
||||||
import { Redis } from 'ioredis';
|
import { Redis } from 'ioredis';
|
||||||
export const redis = useContextKey<Redis>('redis');
|
export const redis = useContextKey<Redis>('redis');
|
||||||
const sequelize = useContextKey<Sequelize>('sequelize');
|
|
||||||
const config = useConfig<{ tokenSecret: string }>();
|
const config = useConfig<{ tokenSecret: string }>();
|
||||||
|
|
||||||
type UserData = {
|
type UserData = {
|
||||||
orgs?: string[];
|
orgs?: string[];
|
||||||
};
|
};
|
||||||
|
/**
|
||||||
|
* 用户模型,在sequelize和Org之后初始化
|
||||||
|
*/
|
||||||
export class User extends Model {
|
export class User extends Model {
|
||||||
declare id: string;
|
declare id: string;
|
||||||
declare username: string;
|
declare username: string;
|
||||||
declare nickname: string; // 昵称
|
declare nickname: string; // 昵称
|
||||||
declare alias: string; // 别名
|
// declare alias: string; // 别名
|
||||||
declare password: string;
|
declare password: string;
|
||||||
declare salt: string;
|
declare salt: string;
|
||||||
declare needChangePassword: boolean;
|
declare needChangePassword: boolean;
|
||||||
@ -154,7 +156,12 @@ export class User extends Model {
|
|||||||
await redis.del(`user:${this.id}:orgs`);
|
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');
|
const sequelize = useContextKey<Sequelize>('sequelize');
|
||||||
User.init(
|
User.init(
|
||||||
{
|
{
|
||||||
@ -174,10 +181,10 @@ export const UserInit = (newSequelize?: any) => {
|
|||||||
type: DataTypes.TEXT,
|
type: DataTypes.TEXT,
|
||||||
allowNull: true,
|
allowNull: true,
|
||||||
},
|
},
|
||||||
alias: {
|
// alias: {
|
||||||
type: DataTypes.TEXT,
|
// type: DataTypes.TEXT,
|
||||||
allowNull: true, // 别名,网络请求的别名,需要唯一,不能和username重复
|
// allowNull: true, // 别名,网络请求的别名,需要唯一,不能和username重复
|
||||||
},
|
// },
|
||||||
password: {
|
password: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: true,
|
allowNull: true,
|
||||||
@ -218,17 +225,20 @@ export const UserInit = (newSequelize?: any) => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
sequelize: newSequelize || sequelize,
|
sequelize: newSequelize || sequelize,
|
||||||
tableName: 'cf_user', // codeflow user
|
tableName: tableName || 'cf_user', // codeflow user
|
||||||
paranoid: true,
|
paranoid: true,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
User.sync({ alter: true, logging: false })
|
if (sync) {
|
||||||
.then((res) => {
|
await User.sync({ alter: true, logging: true, ...sync })
|
||||||
initializeUser();
|
.then((res) => {
|
||||||
})
|
initializeUser();
|
||||||
.catch((err) => {
|
})
|
||||||
console.error('Sync User error', err);
|
.catch((err) => {
|
||||||
});
|
console.error('Sync User error', err);
|
||||||
|
});
|
||||||
|
return User;
|
||||||
|
}
|
||||||
return User;
|
return User;
|
||||||
};
|
};
|
||||||
const letter = 'abcdefghijklmnopqrstuvwxyz';
|
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 { useConfig } from '@kevisual/use-config';
|
||||||
import { Sequelize } from 'sequelize';
|
import { Sequelize } from 'sequelize';
|
||||||
|
import { useContextKey, useContext } from '@kevisual/use-config/context';
|
||||||
|
|
||||||
type PostgresConfig = {
|
type PostgresConfig = {
|
||||||
postgres: {
|
postgres: {
|
||||||
@ -19,8 +20,11 @@ if (!postgresConfig) {
|
|||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
// connect to db
|
// connect to db
|
||||||
export const sequelize = new Sequelize({
|
export const init = () => {
|
||||||
dialect: 'postgres',
|
return new Sequelize({
|
||||||
...postgresConfig,
|
dialect: 'postgres',
|
||||||
// logging: false,
|
...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