From e96a246c141f26c9787fa11fa20b11df002878de Mon Sep 17 00:00:00 2001 From: xion Date: Fri, 28 Feb 2025 09:37:22 +0800 Subject: [PATCH] feat: add models init --- README.md | 8 +++++++ package.json | 16 ++++++++++--- rollup.config.mjs | 51 ++++++++++++++++++++++++++++++++++++++-- src/core-models.ts | 4 ++++ src/models/org.ts | 19 +++++++++------ src/models/user.ts | 40 +++++++++++++++++++------------ src/modules/init.ts | 4 ++++ src/modules/sequelize.ts | 14 +++++++---- src/scripts/migrate.ts | 24 +++++++++++++++++++ 9 files changed, 148 insertions(+), 32 deletions(-) create mode 100644 README.md create mode 100644 src/core-models.ts create mode 100644 src/modules/init.ts create mode 100644 src/scripts/migrate.ts diff --git a/README.md b/README.md new file mode 100644 index 0000000..63a73be --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# code-center-module + +## models + +for models user and org + + +## sequelize \ No newline at end of file diff --git a/package.json b/package.json index 1f45d3c..9be21c1 100644 --- a/package.json +++ b/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" } } } \ No newline at end of file diff --git a/rollup.config.mjs b/rollup.config.mjs index 03cedb7..379594f 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -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]; diff --git a/src/core-models.ts b/src/core-models.ts new file mode 100644 index 0000000..7519cbe --- /dev/null +++ b/src/core-models.ts @@ -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 }; diff --git a/src/models/org.ts b/src/models/org.ts index ec6f0df..82b685e 100644 --- a/src/models/org.ts +++ b/src/models/org.ts @@ -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'); 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); diff --git a/src/models/user.ts b/src/models/user.ts index b15ce37..805b46a 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -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'); -const sequelize = useContextKey('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'); 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'; diff --git a/src/modules/init.ts b/src/modules/init.ts new file mode 100644 index 0000000..88dbee2 --- /dev/null +++ b/src/modules/init.ts @@ -0,0 +1,4 @@ +import { useContextKey, useContext } from '@kevisual/use-config/context'; +import { sequelize } from './sequelize.ts'; + +export { sequelize }; diff --git a/src/modules/sequelize.ts b/src/modules/sequelize.ts index 61c8d9d..97c3a99 100644 --- a/src/modules/sequelize.ts +++ b/src/modules/sequelize.ts @@ -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); diff --git a/src/scripts/migrate.ts b/src/scripts/migrate.ts new file mode 100644 index 0000000..559c62a --- /dev/null +++ b/src/scripts/migrate.ts @@ -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();