Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/module-file/src/server/actions/attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ function getFileData(ctx: Context) {
const filename = path.basename(name);
const extname = path.extname(filename);
const urlPath = storage.path ? storage.path.replace(/^([^/])/, '/$1') : '';
let create_user_id = '';
if (storage.type === 'local' && storage.create_user_id) {
storage.create_user_id = storage.create_user_id.replace(/^([^/])/, '/$1');
create_user_id = `/${storage.create_user_id}`;
}

return {
title: Buffer.from(file.originalname, 'latin1').toString('utf8').replace(extname, ''),
Expand All @@ -40,7 +45,7 @@ function getFileData(ctx: Context) {
path: storage.path,
size: file.size,
// 直接缓存起来
url: `${storage.baseUrl}${urlPath}/${filename}`,
url: `${storage.baseUrl}${create_user_id}${urlPath}/${filename}`,
mimetype: file.mimetype,
// @ts-ignore
meta: ctx.request.body,
Expand Down
6 changes: 6 additions & 0 deletions packages/module-file/src/server/collections/storages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,11 @@ export default defineCollection({
name: 'paranoid',
defaultValue: false,
},
{
// 主应用创建者的用户id
type: 'bigInt',
name: 'create_user_id',
defaultValue: false,
},
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Migration } from '@tachybase/server';

export default class extends Migration {
on = 'afterLoad'; // 'beforeLoad' or 'afterLoad'
appVersion = '<1.0.4';

async up() {
// coding
if (this.app.name === 'main') {
return;
}
}
}
21 changes: 21 additions & 0 deletions packages/module-multi-app/src/server/collections/originalInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { defineCollection } from '@tachybase/database';

export default defineCollection({
dumpRules: {
group: 'third-party',
},
name: 'originalInfo',
autoGenId: false,
createdBy: true,
updatedBy: true,
fields: [
{
type: 'bigInt',
name: 'create_user_id', // 主应用的用户id,即使是爷孙关系,也应该是爷爷的用户id
},
{
type: 'string',
name: 'parent_id',
},
],
});
26 changes: 25 additions & 1 deletion packages/module-multi-app/src/server/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
import { Context } from '@tachybase/actions';
import { Database, IDatabaseOptions, Transactionable } from '@tachybase/database';
import Application, { AppSupervisor, Gateway, Plugin } from '@tachybase/server';

Expand Down Expand Up @@ -59,7 +60,7 @@ const defaultSubAppUpgradeHandle: SubAppUpgradeHandler = async (mainApp: Applica
}
};

const defaultDbCreator = async (app: Application) => {
const defaultDbCreator = async (app: Application, { context }: { context: Context }) => {
const databaseOptions = app.options.database as any;
const { host, port, username, password, dialect, database } = databaseOptions;
const tmpl = app.options?.tmpl;
Expand Down Expand Up @@ -121,6 +122,29 @@ const defaultDbCreator = async (app: Application) => {
} else {
await client.query(`CREATE DATABASE "${database}"`);
}

// 新建一个client, 修改数据库里的原始用户id和parent_id
const newClient = new Client({
host,
port,
user: username,
password,
database,
});
await newClient.connect();
// originalInfo插入一条数据create_user_id是当前用户id, parent_id是当前应用的name
let create_user_id;
if (app.name === 'main') {
create_user_id = context.state.currentUser.id;
} else {
const originalInfo = await context.db.getRepository('originalInfo').findOne();
create_user_id = originalInfo.create_user_id;
}
// TODO: 非模板创建的应用怎么办
await newClient.query(
`INSERT INTO originalInfo (create_user_id, parent_id) VALUES (${create_user_id}, ${app.name})`,
);
await newClient.end();
} catch (e) {
app.logger.error(JSON.stringify(e));
AppSupervisor.getInstance().setAppError(database, e);
Expand Down
64 changes: 0 additions & 64 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.