feat: add admin router manager

This commit is contained in:
xion
2024-06-25 00:52:43 +08:00
parent f8777bd4ea
commit 3454e39ea4
18 changed files with 831 additions and 368 deletions

50
test/db/code.test.ts Normal file
View File

@@ -0,0 +1,50 @@
import { QueryTypes } from 'sequelize';
import { RouterCodeModel } from '../../src/models/code.ts';
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
// yarn test --testNamePattern='RouterCodeModel'
describe('RouterCodeModel', () => {
// 编写一个测试用例
// yarn test --testNamePattern='RouterCodeModel:init'
test('RouterCodeModel:init', async () => {
try {
RouterCodeModel.sync({ force: true });
console.log('connect success');
await sleep(2000);
} catch (error) {
console.error('connect error', error);
}
});
// yarn test --testNamePattern='RouterCodeModel:create'
test('RouterCodeModel:create', async () => {
try {
const file = await RouterCodeModel.create({
path: 'demo',
key: 'returnDemo',
active: true,
project: 'default',
code: `async function run(ctx) {
ctx.body = 'test js';
return ctx;
}`,
});
console.log('create success', file);
await sleep(2000);
} catch (error) {
console.error('create error', error);
}
});
// yarn test --testNamePattern='RouterCodeModel:findAll'
test('RouterCodeModel:findAll', async () => {
try {
const files = await RouterCodeModel.findAll();
console.log(
'findAll success',
files.map((file) => file.toJSON()),
);
await sleep(2000);
} catch (error) {
console.error('findAll error', error);
}
});
});

36
test/db/connect.test.ts Normal file
View File

@@ -0,0 +1,36 @@
import { QueryTypes } from 'sequelize';
import { sequelize } from '../../src/modules/index.ts';
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
// yarn test --testNamePattern='Sequelize'
describe('Hello Sequelize', () => {
// 编写一个测试用例
// yarn test --testNamePattern='Sequelize:connect success'
test('Sequelize:connect success', async () => {
try {
const auth = await sequelize.authenticate();
console.log('connect success');
await sleep(2000);
} catch (error) {
console.error('connect error', error);
}
});
// yarn test --testNamePattern='Sequelize:queryTables'
test('Sequelize:queryTables', async () => {
try {
const tablesCount: any = await sequelize.query(
"SELECT COUNT(*) AS table_count FROM information_schema.tables WHERE table_schema = 'public';",
{ type: QueryTypes.SELECT },
);
if (!tablesCount[0]) {
console.error('未查询到表数量');
return;
}
console.log('数据库中的表数量:', tablesCount[0].table_count);
await sleep(2000);
} catch (error) {
console.error('查询表数量时出错:', error);
}
});
});