Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Nocobase
Commits
55f6564e
Commit
55f6564e
authored
3 years ago
by
chenos
Browse files
Options
Download
Email Patches
Plain Diff
feat: improve code
parent
31201475
Changes
14
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
packages/api/src/index.ts
+1
-1
packages/api/src/index.ts
packages/app/package.json
+1
-1
packages/app/package.json
packages/app/src/apis/index.ts
+2
-2
packages/app/src/apis/index.ts
packages/app/src/pages/index.tsx
+6
-7
packages/app/src/pages/index.tsx
packages/collections/src/database.ts
+9
-1
packages/collections/src/database.ts
packages/database/src/database.ts
+27
-0
packages/database/src/database.ts
packages/database/src/model.ts
+0
-2
packages/database/src/model.ts
packages/database/src/table.ts
+4
-0
packages/database/src/table.ts
packages/plugin-collections/src/server.ts
+20
-11
packages/plugin-collections/src/server.ts
packages/plugin-multi-apps/.gitignore
+0
-0
packages/plugin-multi-apps/.gitignore
packages/plugin-multi-apps/.npmignore
+0
-0
packages/plugin-multi-apps/.npmignore
packages/plugin-multi-apps/package.json
+1
-1
packages/plugin-multi-apps/package.json
packages/plugin-multi-apps/src/index.ts
+0
-0
packages/plugin-multi-apps/src/index.ts
packages/plugin-multi-apps/src/server.ts
+32
-5
packages/plugin-multi-apps/src/server.ts
with
103 additions
and
31 deletions
+103
-31
packages/api/src/index.ts
+
1
-
1
View file @
55f6564e
...
...
@@ -35,9 +35,9 @@ const api = new Server({
});
const
plugins
=
[
'
@nocobase/plugin-collections
'
,
'
@nocobase/plugin-ui-router
'
,
'
@nocobase/plugin-ui-schema
'
,
'
@nocobase/plugin-collections
'
,
'
@nocobase/plugin-users
'
,
'
@nocobase/plugin-action-logs
'
,
'
@nocobase/plugin-file-manager
'
,
...
...
This diff is collapsed.
Click to expand it.
packages/app/package.json
+
1
-
1
View file @
55f6564e
...
...
@@ -36,7 +36,7 @@
"@nocobase/plugin-ui-router"
:
"^0.4.0-alpha.7"
,
"@nocobase/plugin-ui-schema"
:
"^0.4.0-alpha.7"
,
"@nocobase/plugin-users"
:
"^0.4.0-alpha.7"
,
"@nocobase/plugin-
saa
s"
:
"^0.4.0-alpha.7"
,
"@nocobase/plugin-
multi-app
s"
:
"^0.4.0-alpha.7"
,
"@nocobase/server"
:
"^0.4.0-alpha.7"
,
"@umijs/preset-react"
:
"1.x"
,
"koa-mount"
:
"^4.0.0"
,
...
...
This diff is collapsed.
Click to expand it.
packages/app/src/apis/index.ts
+
2
-
2
View file @
55f6564e
...
...
@@ -41,9 +41,9 @@ const api = new Server({
});
const
plugins
=
[
'
@nocobase/plugin-saas
'
,
'
@nocobase/plugin-ui-schema
'
,
'
@nocobase/plugin-multi-apps
'
,
'
@nocobase/plugin-ui-router
'
,
'
@nocobase/plugin-ui-schema
'
,
'
@nocobase/plugin-collections
'
,
'
@nocobase/plugin-users
'
,
'
@nocobase/plugin-action-logs
'
,
...
...
This diff is collapsed.
Click to expand it.
packages/app/src/pages/index.tsx
+
6
-
7
View file @
55f6564e
...
...
@@ -8,8 +8,9 @@ import {
AdminLayout
,
AuthLayout
,
RouteSchemaRenderer
,
ConfigProvider
,
ClientSDK
,
}
from
'
@nocobase/client
'
;
import
{
UseRequestProvider
}
from
'
ahooks
'
;
import
{
extend
}
from
'
umi-request
'
;
const
request
=
extend
({
...
...
@@ -26,6 +27,8 @@ request.use(async (ctx, next) => {
await
next
();
});
const
client
=
new
ClientSDK
({
request
});
const
RouteSwitch
=
createRouteSwitch
({
components
:
{
AdminLayout
,
...
...
@@ -52,12 +55,8 @@ const App = () => {
export
default
function
IndexPage
()
{
return
(
<
UseRequestProvider
value
=
{
{
requestMethod
:
(
service
)
=>
request
(
service
),
}
}
>
<
ConfigProvider
client
=
{
client
}
>
<
App
/>
</
UseRequest
Provider
>
</
Config
Provider
>
);
}
This diff is collapsed.
Click to expand it.
packages/collections/src/database.ts
+
9
-
1
View file @
55f6564e
...
...
@@ -133,7 +133,15 @@ export class Database extends EventEmitter {
}
async
sync
(
options
?:
SyncOptions
)
{
return
this
.
sequelize
.
sync
(
options
);
const
isMySQL
=
this
.
sequelize
.
getDialect
()
===
'
mysql
'
;
if
(
isMySQL
)
{
await
this
.
sequelize
.
query
(
'
SET FOREIGN_KEY_CHECKS = 0
'
,
null
);
}
const
result
=
await
this
.
sequelize
.
sync
(
options
);
if
(
isMySQL
)
{
await
this
.
sequelize
.
query
(
'
SET FOREIGN_KEY_CHECKS = 1
'
,
null
);
}
return
result
;
}
async
close
()
{
...
...
This diff is collapsed.
Click to expand it.
packages/database/src/database.ts
+
27
-
0
View file @
55f6564e
...
...
@@ -465,6 +465,33 @@ type HookType =
return
this
.
sequelize
.
close
();
}
/**
* 添加 hook
*
* @param hookType
* @param fn
*/
public
addHook
(
hookType
:
HookType
|
string
,
fn
:
Function
)
{
const
hooks
=
this
.
hooks
[
hookType
]
||
[];
hooks
.
push
(
fn
);
this
.
hooks
[
hookType
]
=
hooks
;
}
/**
* 运行 hook
*
* @param hookType
* @param args
*/
public
async
runHooks
(
hookType
:
HookType
|
string
,
...
args
)
{
const
hooks
=
this
.
hooks
[
hookType
]
||
[];
for
(
const
hook
of
hooks
)
{
if
(
typeof
hook
===
'
function
'
)
{
await
hook
(...
args
);
}
}
}
public
getFieldByPath
(
fieldPath
:
string
)
{
const
[
tableName
,
fieldName
]
=
fieldPath
.
split
(
'
.
'
);
return
this
.
getTable
(
tableName
).
getField
(
fieldName
);
...
...
This diff is collapsed.
Click to expand it.
packages/database/src/model.ts
+
0
-
2
View file @
55f6564e
...
...
@@ -534,8 +534,6 @@ export abstract class Model extends SequelizeModel {
...
options
,
transaction
,
});
// @ts-ignore
// await this.sequelize.runHooks('afterUpdateAssociations', this, options);
if
(
!
options
.
transaction
)
{
await
transaction
.
commit
();
...
...
This diff is collapsed.
Click to expand it.
packages/database/src/table.ts
+
4
-
0
View file @
55f6564e
...
...
@@ -139,6 +139,7 @@ export class Table {
constructor
(
options
:
TableOptions
,
context
:
TabelContext
)
{
const
{
database
}
=
context
;
database
.
runHooks
(
'
beforeTableInit
'
,
options
);
database
.
emit
(
'
beforeTableInit
'
,
options
);
const
{
model
,
...
...
@@ -158,6 +159,7 @@ export class Table {
this
.
setFields
(
fields
);
this
.
initSortable
();
database
.
emit
(
'
afterTableInit
'
,
this
);
database
.
runHooks
(
'
afterTableInit
'
,
this
);
}
public
initSortable
()
{
...
...
@@ -313,6 +315,7 @@ export class Table {
* @param reinitialize
*/
public
addField
(
options
:
FieldOptions
,
reinitialize
:
Reinitialize
=
true
)
{
this
.
database
.
runHooks
(
'
beforeAddField
'
,
options
,
this
);
this
.
database
.
emit
(
'
beforeAddField
'
,
options
,
this
);
const
{
name
,
index
}
=
options
;
const
field
=
buildField
(
options
,
{
...
...
@@ -350,6 +353,7 @@ export class Table {
}
this
.
modelInit
(
reinitialize
);
this
.
database
.
emit
(
'
afterAddField
'
,
field
,
this
);
this
.
database
.
runHooks
(
'
afterAddField
'
,
field
,
this
);
return
field
;
}
...
...
This diff is collapsed.
Click to expand it.
packages/plugin-collections/src/server.ts
+
20
-
11
View file @
55f6564e
...
...
@@ -5,31 +5,40 @@ import * as models from './models';
import
{
createOrUpdate
,
findAll
}
from
'
./actions
'
;
import
{
create
}
from
'
./actions/fields
'
;
registerModels
(
models
);
export
default
{
name
:
'
collections
'
,
async
load
(
this
:
Plugin
)
{
const
database
=
this
.
app
.
db
;
registerModels
(
models
);
database
.
import
({
directory
:
path
.
resolve
(
__dirname
,
'
collections
'
),
});
this
.
app
.
on
(
'
beforeStart
'
,
async
()
=>
{
await
database
.
getModel
(
'
collections
'
).
load
();
await
database
.
getModel
(
'
collections
'
).
load
({
skipExisting
:
true
,
});
});
this
.
app
.
on
(
'
db.init
'
,
async
()
=>
{
const
userTable
=
database
.
getTable
(
'
users
'
);
const
config
=
userTable
.
getOptions
();
const
tableNames
=
[
'
users
'
,
'
applications
'
];
const
Collection
=
database
.
getModel
(
'
collections
'
);
const
collection
=
await
Collection
.
create
(
config
);
await
collection
.
updateAssociations
({
generalFields
:
config
.
fields
.
filter
((
field
)
=>
field
.
state
!==
0
),
systemFields
:
config
.
fields
.
filter
((
field
)
=>
field
.
state
===
0
),
});
await
collection
.
migrate
();
for
(
const
tableName
of
tableNames
)
{
const
table
=
database
.
getTable
(
tableName
);
console
.
log
(
tableName
,
table
);
if
(
!
table
)
{
continue
;
}
const
config
=
table
.
getOptions
();
const
collection
=
await
Collection
.
create
(
config
);
await
collection
.
updateAssociations
({
generalFields
:
config
.
fields
.
filter
((
field
)
=>
field
.
state
!==
0
),
systemFields
:
config
.
fields
.
filter
((
field
)
=>
field
.
state
===
0
),
});
// await collection.migrate();
}
});
const
[
Collection
,
Field
]
=
database
.
getModels
([
'
collections
'
,
'
fields
'
]);
...
...
This diff is collapsed.
Click to expand it.
packages/plugin-
saa
s/.gitignore
→
packages/plugin-
multi-app
s/.gitignore
+
0
-
0
View file @
55f6564e
File moved
This diff is collapsed.
Click to expand it.
packages/plugin-
saa
s/.npmignore
→
packages/plugin-
multi-app
s/.npmignore
+
0
-
0
View file @
55f6564e
File moved
This diff is collapsed.
Click to expand it.
packages/plugin-
saa
s/package.json
→
packages/plugin-
multi-app
s/package.json
+
1
-
1
View file @
55f6564e
{
"name"
:
"@nocobase/plugin-
saa
s"
,
"name"
:
"@nocobase/plugin-
multi-app
s"
,
"version"
:
"0.4.0-alpha.7"
,
"main"
:
"lib/index.js"
,
"license"
:
"MIT"
,
...
...
This diff is collapsed.
Click to expand it.
packages/plugin-
saa
s/src/index.ts
→
packages/plugin-
multi-app
s/src/index.ts
+
0
-
0
View file @
55f6564e
File moved
This diff is collapsed.
Click to expand it.
packages/plugin-
saa
s/src/server.ts
→
packages/plugin-
multi-app
s/src/server.ts
+
32
-
5
View file @
55f6564e
...
...
@@ -31,6 +31,7 @@ function createApp(opts) {
},
},
},
// 不能再 bodyParser,会卡死
bodyParser
:
false
,
// dataWrapping: false,
resourcer
:
{
...
...
@@ -40,9 +41,8 @@ function createApp(opts) {
const
app
=
new
Application
(
options
);
app
.
db
.
sequelize
.
beforeDefine
((
model
,
options
)
=>
{
options
.
tableName
=
`saas_
${
name
}
_
${
options
.
tableName
||
options
.
name
.
plural
}
`
;
options
.
tableName
=
`saas_
${
name
}
_
${
options
.
tableName
||
options
.
name
.
plural
}
`
;
});
app
.
resource
({
...
...
@@ -126,9 +126,19 @@ export default {
this
.
app
[
'
apps
'
]
=
new
Map
<
string
,
Application
>
();
this
.
app
.
collection
({
name
:
'
applications
'
,
title
:
'
应用
'
,
fields
:
[
{
type
:
'
string
'
,
name
:
'
name
'
,
unique
:
true
},
{
type
:
'
belongsTo
'
,
name
:
'
user
'
},
{
type
:
'
string
'
,
name
:
'
name
'
,
interface
:
'
string
'
,
unique
:
true
,
uiSchema
:
{
type
:
'
string
'
,
title
:
'
名称
'
,
'
x-component
'
:
'
Input
'
,
},
},
],
});
this
.
app
.
use
(
...
...
@@ -138,6 +148,23 @@ export default {
},
}),
);
this
.
app
.
db
.
on
(
'
applications.afterCreate
'
,
async
(
model
)
=>
{
const
name
=
model
.
get
(
'
name
'
);
const
app
=
createApp
({
name
,
});
(
async
()
=>
{
await
app
.
load
();
await
app
.
db
.
sync
({
force
:
true
,
alter
:
{
drop
:
true
,
},
});
await
app
.
emitAsync
(
'
db.init
'
);
await
app
.
destroy
();
})()
});
this
.
app
.
command
(
'
app:create
'
)
.
argument
(
'
<appName>
'
)
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment