Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
S
srvpro2
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nanahira
srvpro2
Commits
35e61c0a
Commit
35e61c0a
authored
Feb 20, 2026
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add dynamic load typeorm entity
parent
ccb8c97c
Pipeline
#43337
passed with stages
in 3 minutes and 22 seconds
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
138 additions
and
8 deletions
+138
-8
src/services/plugin-typeorm-entity-loader.ts
src/services/plugin-typeorm-entity-loader.ts
+116
-0
src/services/typeorm.ts
src/services/typeorm.ts
+22
-8
No files found.
src/services/plugin-typeorm-entity-loader.ts
0 → 100644
View file @
35e61c0a
import
fs
from
'
node:fs
'
;
import
path
from
'
node:path
'
;
import
{
getMetadataArgsStorage
}
from
'
typeorm
'
;
type
PluginEntityLogger
=
{
warn
:
(...
args
:
unknown
[])
=>
void
;
};
export
function
collectPluginTypeormEntities
(
logger
?:
PluginEntityLogger
):
Function
[]
{
const
pluginDir
=
path
.
resolve
(
__dirname
,
'
..
'
,
'
..
'
,
'
plugins
'
);
const
pluginEntityFiles
=
collectPluginEntityFiles
(
pluginDir
);
const
entities
=
new
Set
<
Function
>
();
for
(
const
pluginEntityFile
of
pluginEntityFiles
)
{
const
requirePath
=
resolveRequirePath
(
pluginEntityFile
);
let
loadedModule
:
unknown
;
try
{
loadedModule
=
require
(
requirePath
);
}
catch
(
error
)
{
logger
?.
warn
(
{
pluginEntityFile
:
requirePath
,
error
:
(
error
as
Error
)?.
stack
||
String
(
error
),
},
'
Failed requiring plugin entity file
'
,
);
continue
;
}
const
exportedItems
=
resolveExportedItems
(
loadedModule
);
const
entityTargets
=
collectTypeormEntityTargets
();
for
(
const
item
of
exportedItems
)
{
if
(
typeof
item
!==
'
function
'
)
{
continue
;
}
if
(
!
entityTargets
.
has
(
item
))
{
continue
;
}
entities
.
add
(
item
);
}
}
return
[...
entities
];
}
function
collectPluginEntityFiles
(
rootDir
:
string
):
string
[]
{
if
(
!
fs
.
existsSync
(
rootDir
))
{
return
[];
}
const
files
:
string
[]
=
[];
const
walk
=
(
currentDir
:
string
)
=>
{
const
entries
=
fs
.
readdirSync
(
currentDir
,
{
withFileTypes
:
true
,
});
for
(
const
entry
of
entries
)
{
const
fullPath
=
path
.
join
(
currentDir
,
entry
.
name
);
if
(
entry
.
isDirectory
())
{
walk
(
fullPath
);
continue
;
}
if
(
!
entry
.
isFile
())
{
continue
;
}
if
(
!
fullPath
.
endsWith
(
'
.entity.js
'
))
{
continue
;
}
files
.
push
(
fullPath
);
}
};
walk
(
rootDir
);
return
files
;
}
function
resolveRequirePath
(
filePath
:
string
)
{
const
cwdRelative
=
path
.
relative
(
process
.
cwd
(),
filePath
);
return
path
.
resolve
(
process
.
cwd
(),
cwdRelative
);
}
function
collectTypeormEntityTargets
()
{
const
targets
=
new
Set
<
Function
>
();
for
(
const
table
of
getMetadataArgsStorage
().
tables
)
{
if
(
table
.
type
===
'
view
'
)
{
continue
;
}
if
(
typeof
table
.
target
!==
'
function
'
)
{
continue
;
}
targets
.
add
(
table
.
target
);
}
return
targets
;
}
function
resolveExportedItems
(
loadedModule
:
unknown
)
{
const
items
:
unknown
[]
=
[];
if
(
!
loadedModule
)
{
return
items
;
}
if
(
typeof
loadedModule
===
'
function
'
)
{
items
.
push
(
loadedModule
);
return
items
;
}
if
(
typeof
loadedModule
!==
'
object
'
)
{
return
items
;
}
const
record
=
loadedModule
as
Record
<
string
,
unknown
>
;
if
(
record
.
default
)
{
items
.
push
(
record
.
default
);
}
for
(
const
value
of
Object
.
values
(
record
))
{
items
.
push
(
value
);
}
return
items
;
}
src/services/typeorm.ts
View file @
35e61c0a
...
@@ -7,6 +7,7 @@ import { DuelRecordEntity, DuelRecordPlayer } from '../feats/cloud-replay';
...
@@ -7,6 +7,7 @@ import { DuelRecordEntity, DuelRecordPlayer } from '../feats/cloud-replay';
import
{
LegacyApiRecordEntity
}
from
'
../legacy-api/legacy-api-record.entity
'
;
import
{
LegacyApiRecordEntity
}
from
'
../legacy-api/legacy-api-record.entity
'
;
import
{
LegacyBanEntity
}
from
'
../legacy-api/legacy-ban.entity
'
;
import
{
LegacyBanEntity
}
from
'
../legacy-api/legacy-ban.entity
'
;
import
{
LegacyDeckEntity
}
from
'
../legacy-api/legacy-deck.entity
'
;
import
{
LegacyDeckEntity
}
from
'
../legacy-api/legacy-deck.entity
'
;
import
{
collectPluginTypeormEntities
}
from
'
./plugin-typeorm-entity-loader
'
;
export
class
TypeormLoader
{
export
class
TypeormLoader
{
constructor
(
private
ctx
:
AppContext
)
{}
constructor
(
private
ctx
:
AppContext
)
{}
...
@@ -35,6 +36,26 @@ export const TypeormFactory = async (ctx: AppContext) => {
...
@@ -35,6 +36,26 @@ export const TypeormFactory = async (ctx: AppContext) => {
const
password
=
config
.
getString
(
'
DB_PASS
'
);
const
password
=
config
.
getString
(
'
DB_PASS
'
);
const
database
=
config
.
getString
(
'
DB_NAME
'
);
const
database
=
config
.
getString
(
'
DB_NAME
'
);
const
synchronize
=
!
config
.
getBoolean
(
'
DB_NO_INIT
'
);
const
synchronize
=
!
config
.
getBoolean
(
'
DB_NO_INIT
'
);
const
staticEntities
:
Function
[]
=
[
RandomDuelScore
,
DuelRecordEntity
,
DuelRecordPlayer
,
LegacyApiRecordEntity
,
LegacyBanEntity
,
LegacyDeckEntity
,
];
const
pluginEntities
=
collectPluginTypeormEntities
(
logger
);
const
entities
=
[...
new
Set
<
Function
>
([...
staticEntities
,
...
pluginEntities
])];
if
(
pluginEntities
.
length
>
0
)
{
logger
.
info
(
{
count
:
pluginEntities
.
length
,
entities
:
pluginEntities
.
map
((
entity
)
=>
entity
.
name
),
},
'
Collected plugin typeorm entities
'
,
);
}
const
dataSource
=
new
DataSource
({
const
dataSource
=
new
DataSource
({
type
:
'
postgres
'
,
type
:
'
postgres
'
,
...
@@ -44,14 +65,7 @@ export const TypeormFactory = async (ctx: AppContext) => {
...
@@ -44,14 +65,7 @@ export const TypeormFactory = async (ctx: AppContext) => {
password
,
password
,
database
,
database
,
synchronize
,
synchronize
,
entities
:
[
entities
,
RandomDuelScore
,
DuelRecordEntity
,
DuelRecordPlayer
,
LegacyApiRecordEntity
,
LegacyBanEntity
,
LegacyDeckEntity
,
],
});
});
try
{
try
{
...
...
Write
Preview
Markdown
is supported
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