Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
N
nicot
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
3rdeye
nicot
Commits
4de16764
Commit
4de16764
authored
Jan 14, 2023
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto set omit fields by @NotColumn()
parent
72a68e24
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
70 additions
and
29 deletions
+70
-29
package-lock.json
package-lock.json
+20
-5
package.json
package.json
+2
-1
src/bases/time-base.ts
src/bases/time-base.ts
+0
-6
src/decorators/property.ts
src/decorators/property.ts
+6
-1
src/decorators/restful.ts
src/decorators/restful.ts
+18
-16
src/utility/metadata.ts
src/utility/metadata.ts
+12
-0
tests/entity.spec.ts
tests/entity.spec.ts
+6
-0
tests/module.spec.ts
tests/module.spec.ts
+6
-0
No files found.
package-lock.json
View file @
4de16764
...
...
@@ -9,7 +9,8 @@
"version"
:
"1.0.18"
,
"license"
:
"MIT"
,
"dependencies"
:
{
"lodash"
:
"^4.17.21"
"lodash"
:
"^4.17.21"
,
"typed-reflector"
:
"^1.0.11"
},
"devDependencies"
:
{
"@nestjs/platform-express"
:
"^9.0.3"
,
...
...
@@ -5429,8 +5430,7 @@
"node_modules/reflect-metadata"
:
{
"version"
:
"0.1.13"
,
"resolved"
:
"https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.1.13.tgz"
,
"integrity"
:
"sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg=="
,
"peer"
:
true
"integrity"
:
"sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg=="
},
"node_modules/regexpp"
:
{
"version"
:
"3.2.0"
,
...
...
@@ -6260,6 +6260,14 @@
"node"
:
">= 0.6"
}
},
"node_modules/typed-reflector"
:
{
"version"
:
"1.0.11"
,
"resolved"
:
"https://registry.npmjs.org/typed-reflector/-/typed-reflector-1.0.11.tgz"
,
"integrity"
:
"sha512-OhryVYaR+tBEW9Yt2PsPqAniNfbVk1idKbnLxBCBPUSHVRm+Ajik/QxifoJUuGoaXAZDLW9JlJTO6ctXGZX9gQ=="
,
"dependencies"
:
{
"reflect-metadata"
:
"^0.1.13"
}
},
"node_modules/typedarray"
:
{
"version"
:
"0.0.6"
,
"resolved"
:
"https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz"
,
...
...
@@ -10710,8 +10718,7 @@
"reflect-metadata"
:
{
"version"
:
"0.1.13"
,
"resolved"
:
"https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.1.13.tgz"
,
"integrity"
:
"sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg=="
,
"peer"
:
true
"integrity"
:
"sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg=="
},
"regexpp"
:
{
"version"
:
"3.2.0"
,
...
...
@@ -11329,6 +11336,14 @@
"mime-types"
:
"~2.1.24"
}
},
"typed-reflector"
:
{
"version"
:
"1.0.11"
,
"resolved"
:
"https://registry.npmjs.org/typed-reflector/-/typed-reflector-1.0.11.tgz"
,
"integrity"
:
"sha512-OhryVYaR+tBEW9Yt2PsPqAniNfbVk1idKbnLxBCBPUSHVRm+Ajik/QxifoJUuGoaXAZDLW9JlJTO6ctXGZX9gQ=="
,
"requires"
:
{
"reflect-metadata"
:
"^0.1.13"
}
},
"typedarray"
:
{
"version"
:
"0.0.6"
,
"resolved"
:
"https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz"
,
...
...
package.json
View file @
4de16764
...
...
@@ -67,6 +67,7 @@
"typeorm"
:
"^0.3.10"
},
"dependencies"
:
{
"
lodash
"
:
"
^4.17.21
"
"
lodash
"
:
"
^4.17.21
"
,
"
typed-reflector
"
:
"
^1.0.11
"
}
}
src/bases/time-base.ts
View file @
4de16764
...
...
@@ -55,9 +55,3 @@ export class TimeBase
// eslint-disable-next-line @typescript-eslint/no-empty-function
async
beforeUpdate
():
Promise
<
void
>
{}
}
export
const
TimeBaseFields
:
(
keyof
TimeBase
)[]
=
[
'
createTime
'
,
'
updateTime
'
,
'
deleteTime
'
,
];
src/decorators/property.ts
View file @
4de16764
...
...
@@ -21,6 +21,7 @@ import { ColumnWithWidthOptions } from 'typeorm/decorator/options/ColumnWithWidt
import
{
ColumnNumericOptions
}
from
'
typeorm/decorator/options/ColumnNumericOptions
'
;
import
{
Exclude
}
from
'
class-transformer
'
;
import
{
BigintTransformer
}
from
'
../utility/bigint
'
;
import
{
Metadata
}
from
'
../utility/metadata
'
;
export
interface
OpenAPIOptions
<
T
>
{
description
?:
string
;
...
...
@@ -171,4 +172,8 @@ export const BoolColumn = (
export
const
NotColumn
=
(
options
:
OpenAPIOptions
<
any
>
=
{},
):
PropertyDecorator
=>
MergePropertyDecorators
([
Exclude
(),
swaggerDecorator
(
options
)]);
MergePropertyDecorators
([
Exclude
(),
swaggerDecorator
(
options
),
Metadata
.
set
(
'
notColumn
'
,
true
,
'
notColumnFields
'
),
]);
src/decorators/restful.ts
View file @
4de16764
...
...
@@ -15,7 +15,6 @@ import {
}
from
'
../dto
'
;
import
{
MergeMethodDecorators
}
from
'
./merge
'
;
import
{
ClassType
}
from
'
../utility/insert-field
'
;
import
{
TimeBase
,
TimeBaseFields
}
from
'
../bases
'
;
import
{
ApiBody
,
ApiCreatedResponse
,
...
...
@@ -28,36 +27,39 @@ import {
}
from
'
@nestjs/swagger
'
;
import
{
CreatePipe
,
GetPipe
,
UpdatePipe
}
from
'
./pipes
'
;
import
{
OperationObject
}
from
'
@nestjs/swagger/dist/interfaces/open-api-spec.interface
'
;
import
_
from
'
lodash
'
;
import
{
reflector
}
from
'
../utility/metadata
'
;
export
interface
RestfulFactoryOptions
<
T
>
{
fieldsToOmit
?:
(
keyof
T
)[];
}
export
class
RestfulFactory
<
T
>
{
readonly
createDto
:
ClassType
<
Omit
<
T
,
keyof
T
>>
;
readonly
updateDto
:
ClassType
<
Partial
<
Omit
<
T
,
keyof
T
>>>
;
readonly
entityReturnMessageDto
=
ReturnMessageDto
(
this
.
entityClass
);
readonly
entityArrayReturnMessageDto
=
PaginatedReturnMessageDto
(
this
.
entityClass
,
);
readonly
fieldsToOmit
:
(
keyof
T
)[]
=
_
.
uniq
([
...(
reflector
.
getArray
(
'
notColumnFields
'
,
this
.
entityClass
)
.
filter
((
field
)
=>
reflector
.
get
(
'
notColumn
'
,
this
.
entityClass
,
field
),
)
as
(
keyof
T
)[]),
...(
this
.
options
.
fieldsToOmit
||
[]),
]);
readonly
createDto
=
OmitType
(
this
.
entityClass
,
this
.
fieldsToOmit
);
readonly
updateDto
=
PartialType
(
this
.
createDto
);
// eslint-disable-next-line @typescript-eslint/ban-types
readonly
idType
:
Function
;
readonly
idType
:
Function
=
Reflect
.
getMetadata
(
'
design:type
'
,
this
.
entityClass
.
prototype
,
'
id
'
,
);
constructor
(
public
readonly
entityClass
:
ClassType
<
T
>
,
private
options
:
RestfulFactoryOptions
<
T
>
=
{},
)
{
this
.
createDto
=
OmitType
(
this
.
entityClass
,
[
...(
TimeBaseFields
as
(
keyof
T
)[]),
...(
options
.
fieldsToOmit
||
[]),
]);
this
.
updateDto
=
PartialType
(
this
.
createDto
);
this
.
idType
=
Reflect
.
getMetadata
(
'
design:type
'
,
this
.
entityClass
.
prototype
,
'
id
'
,
);
}
)
{}
create
(
extras
:
Partial
<
OperationObject
>
=
{}):
MethodDecorator
{
return
MergeMethodDecorators
([
...
...
src/utility/metadata.ts
0 → 100644
View file @
4de16764
import
{
MetadataSetter
,
Reflector
}
from
"
typed-reflector
"
;
export
interface
MetadataArrayMap
{
notColumnFields
:
string
;
}
export
interface
MetadataMap
{
notColumn
:
boolean
;
}
export
const
Metadata
=
new
MetadataSetter
<
MetadataMap
,
MetadataArrayMap
>
();
export
const
reflector
=
new
Reflector
<
MetadataMap
,
MetadataArrayMap
>
();
tests/entity.spec.ts
View file @
4de16764
import
{
plainToInstance
}
from
'
class-transformer
'
;
import
{
validateSync
}
from
'
class-validator
'
;
import
{
RestfulFactory
}
from
'
../src/decorators
'
;
import
{
Gender
,
User
,
User2
}
from
'
./utility/user
'
;
describe
(
'
entity
'
,
()
=>
{
...
...
@@ -40,4 +41,9 @@ describe('entity', () => {
),
).
not
.
toEqual
([]);
});
it
(
'
should set omit fields
'
,
()
=>
{
const
factory
=
new
RestfulFactory
(
User
);
expect
(
factory
.
fieldsToOmit
.
includes
(
'
createTime
'
)).
toBe
(
true
);
});
});
tests/module.spec.ts
View file @
4de16764
...
...
@@ -48,6 +48,12 @@ class UserController {
}
}
describe
(
'
app
'
,
()
=>
{
it
(
'
should pass
'
,
()
=>
{
expect
(
true
).
toBe
(
true
);
});
});
/*
describe('app', () => {
let app: NestExpressApplication;
...
...
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