tachybase_todo/packages/plugin-client/src/server.ts

69 lines
1.9 KiB
TypeScript
Raw Normal View History

2021-10-01 23:31:49 +08:00
import path from 'path';
import send from 'koa-send';
import serve from 'koa-static';
import { PluginOptions } from '@nocobase/server';
2021-10-07 16:31:19 +08:00
import { readFileSync } from 'fs';
import glob from 'glob';
function getInitSqls() {
const part1 = [];
const part2 = [];
const files1 = glob.sync(path.resolve(__dirname, './db/part1/*.sql'));
for (const file of files1) {
const sql = readFileSync(file).toString();
part1.push(sql);
}
const files2 = glob.sync(path.resolve(__dirname, './db/part2/*.sql'));
for (const file of files2) {
const sql = readFileSync(file).toString();
part2.push(sql);
}
return { part1, part2 };
}
2021-10-01 23:31:49 +08:00
export default {
name: 'client',
async load() {
let root = this.options.dist;
if (root && !root.startsWith('/')) {
root = path.resolve(process.cwd(), root);
}
this.app.middleware.unshift(async (ctx, next) => {
if (!root) {
return next();
}
await serve(root)(ctx, next);
2021-10-07 19:32:55 +08:00
// console.log('koa-send', root, ctx.status);
2021-10-01 23:31:49 +08:00
if (ctx.status == 404) {
return send(ctx, 'index.html', { root });
}
});
2021-10-07 16:31:19 +08:00
const app = this.app;
this.app.on('db.init', async () => {
const transaction = await app.db.sequelize.transaction();
const sqls = getInitSqls();
try {
for (const sql of sqls.part1) {
await app.db.sequelize.query(sql, { transaction });
}
await transaction.commit();
} catch (error) {
await transaction.rollback();
}
await app.db.getModel('collections').load({
skipExisting: true,
});
await app.db.sync();
const transaction2 = await app.db.sequelize.transaction();
try {
for (const sql of sqls.part2) {
await app.db.sequelize.query(sql, { transaction: transaction2 });
}
await transaction2.commit();
} catch (error) {
await transaction2.rollback();
}
});
2021-10-01 23:31:49 +08:00
}
} as PluginOptions;