@things-factory/resource-base 6.0.0-alpha.13 → 6.0.0-alpha.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@things-factory/resource-base",
3
- "version": "6.0.0-alpha.13",
3
+ "version": "6.0.0-alpha.21",
4
4
  "main": "dist-server/index.js",
5
5
  "browser": "client/index.js",
6
6
  "things-factory": true,
@@ -24,7 +24,7 @@
24
24
  "migration:create": "node ../../node_modules/typeorm/cli.js migration:create -d ./server/migrations"
25
25
  },
26
26
  "dependencies": {
27
- "@things-factory/auth-base": "^6.0.0-alpha.13"
27
+ "@things-factory/auth-base": "^6.0.0-alpha.21"
28
28
  },
29
- "gitHead": "ee18af4bbf6b44e2f1d76258ad6922a535a9f0d8"
29
+ "gitHead": "484fbcb93bff65e7ffe0104b79eb7360ac9a8134"
30
30
  }
@@ -0,0 +1,6 @@
1
+ export const DEL_STRATEGY = {
2
+ DELETE: 'delete',
3
+ DESTROY: 'destroy',
4
+ NULLIFY: 'nullify',
5
+ RESTRICT_WITH_ERROR: 'restrict with error'
6
+ }
@@ -1,5 +1,7 @@
1
+ import { DEL_STRATEGY } from '../../constants/del-strategy-type'
1
2
  import { Arg, Ctx, Directive, Mutation, Resolver } from 'type-graphql'
2
3
  import { In } from 'typeorm'
4
+ import { EntityColumn } from '../entity-column/entity-column'
3
5
 
4
6
  import { Entity } from './entity'
5
7
  import { EntityPatch, NewEntity } from './entity-type'
@@ -28,7 +30,7 @@ export class EntityMutation {
28
30
  @Directive('@transaction')
29
31
  @Mutation(returns => Entity, { description: "To modify Entity' information" })
30
32
  async updateEntity(
31
- @Arg('name', type => String) name: string,
33
+ @Arg('id', type => String) id: string,
32
34
  @Arg('patch', type => EntityPatch) patch: EntityPatch,
33
35
  @Ctx() context: ResolverContext
34
36
  ): Promise<Entity> {
@@ -36,7 +38,7 @@ export class EntityMutation {
36
38
 
37
39
  const repository = tx.getRepository(Entity)
38
40
  const entity = repository.findOne({
39
- where: { domain: { id: domain.id }, name },
41
+ where: { domain: { id: domain.id }, id },
40
42
  relations: ['master', 'children']
41
43
  })
42
44
 
@@ -55,12 +57,104 @@ export class EntityMutation {
55
57
  })
56
58
  }
57
59
 
60
+ @Directive('@transaction')
61
+ @Mutation(returns => [Entity], { description: "To modify multiple Entitys' information" })
62
+ async updateMultipleEntity(
63
+ @Arg('patches', type => [EntityPatch]) patches: EntityPatch[],
64
+ @Ctx() context: ResolverContext
65
+ ): Promise<Entity[]> {
66
+ const { domain, user, tx } = context.state
67
+
68
+ let results = []
69
+ const _createRecords = patches.filter((patch: any) => patch.cuFlag.toUpperCase() === '+')
70
+ const _updateRecords = patches.filter((patch: any) => patch.cuFlag.toUpperCase() === 'M')
71
+ const entityRepo = tx.getRepository(Entity)
72
+
73
+ if (_createRecords.length > 0) {
74
+ for (let i = 0; i < _createRecords.length; i++) {
75
+ const newRecord = _createRecords[i]
76
+
77
+ if (newRecord.master) {
78
+ newRecord.master = (await tx
79
+ .getRepository(Entity)
80
+ .findOne({ where: { domain: { id: domain.id }, id: newRecord.master } })) as any
81
+ }
82
+
83
+ if (newRecord.children && newRecord.children.length) {
84
+ newRecord.children = (await tx.getRepository(Entity).findBy({ id: In(newRecord.children) })) as any
85
+ }
86
+
87
+ const result = await entityRepo.save({
88
+ ...(newRecord as any),
89
+ domain,
90
+ creator: user,
91
+ updater: user
92
+ })
93
+
94
+ results.push({ ...result, cuFlag: '+' })
95
+ }
96
+ }
97
+
98
+ if (_updateRecords.length > 0) {
99
+ for (let i = 0; i < _updateRecords.length; i++) {
100
+ const updateRecord = _updateRecords[i]
101
+ const entity = await entityRepo.findOneBy({ id: updateRecord.id })
102
+
103
+ if (updateRecord.master) {
104
+ updateRecord.master = (await tx
105
+ .getRepository(Entity)
106
+ .findOne({ where: { domain: { id: domain.id }, id: updateRecord.master } })) as any
107
+ }
108
+
109
+ if (updateRecord.children && updateRecord.children.length) {
110
+ updateRecord.children = (await tx.getRepository(Entity).findBy({ id: In(updateRecord.children) })) as any
111
+ }
112
+
113
+ const result = await entityRepo.save({
114
+ ...entity,
115
+ ...(updateRecord as any),
116
+ updater: user
117
+ })
118
+
119
+ results.push({ ...result, cuFlag: 'M' })
120
+ }
121
+ }
122
+
123
+ return results
124
+ }
125
+
58
126
  @Directive('@transaction')
59
127
  @Mutation(returns => Boolean, { description: 'To delete Entity' })
60
- async deleteEntity(@Arg('name') name: string, @Ctx() context: ResolverContext): Promise<boolean> {
128
+ async deleteEntity(@Arg('id') id: string, @Ctx() context: ResolverContext): Promise<boolean> {
61
129
  const { domain, tx } = context.state
130
+ const entityRepo = tx.getRepository(Entity)
62
131
 
63
- await tx.getRepository(Entity).delete({ domain, name })
132
+ const entity: Entity = await entityRepo.findOne({
133
+ where: { domain: { id: domain.id }, id },
134
+ relations: ['columns']
135
+ })
136
+
137
+ if (entity.columns.length) throw new Error('cannot delete Entity because columns of entity exists')
138
+ else await entityRepo.delete(entity.id)
139
+
140
+ return true
141
+ }
142
+
143
+ @Directive('@transaction')
144
+ @Mutation(returns => Boolean, { description: 'To delete multiple Entities' })
145
+ async deleteEntities(@Arg('ids', type => [String]) ids: string[], @Ctx() context: ResolverContext): Promise<boolean> {
146
+ const { domain, tx } = context.state
147
+ const entityRepo = tx.getRepository(Entity)
148
+
149
+ for (const id of ids) {
150
+ const entity: Entity = await entityRepo.findOne({
151
+ where: { domain: { id: domain.id }, id },
152
+ relations: ['columns']
153
+ })
154
+
155
+ if (entity.columns.length) throw new Error('cannot delete Entity because columns of entity exists')
156
+ else await entityRepo.delete(entity.id)
157
+ }
64
158
 
65
159
  return true
66
160
  }
@@ -10,11 +10,11 @@ import { EntityList } from './entity-type'
10
10
  @Resolver(Entity)
11
11
  export class EntityQuery {
12
12
  @Query(returns => Entity, { description: 'To fetch a Entity' })
13
- async entity(@Arg('name') name: string, @Ctx() context: ResolverContext): Promise<Entity> {
13
+ async entity(@Arg('id') id: string, @Ctx() context: ResolverContext): Promise<Entity> {
14
14
  const { domain } = context.state
15
15
 
16
16
  return await getRepository(Entity).findOne({
17
- where: { domain: { id: domain.id }, name }
17
+ where: { domain: { id: domain.id }, id }
18
18
  })
19
19
  }
20
20
 
@@ -120,6 +120,9 @@ export class EntityPatch {
120
120
 
121
121
  @Field(type => [String], { nullable: true })
122
122
  children: string[]
123
+
124
+ @Field({ nullable: true })
125
+ cuFlag?: string
123
126
  }
124
127
 
125
128
  @ObjectType()
@@ -1,4 +1,5 @@
1
1
  import { Arg, Ctx, Directive, Mutation, Resolver } from 'type-graphql'
2
+ import { In } from 'typeorm'
2
3
 
3
4
  import { Entity } from '../entity/entity'
4
5
  import { EntityColumn } from './entity-column'
@@ -15,7 +16,7 @@ export class EntityColumnMutation {
15
16
  const { domain, user, tx } = context.state
16
17
 
17
18
  if (entityColumn.entity) {
18
- entityColumn.entity = (await tx.getRepository(Entity).findOne({ where: { id: entityColumn.entity } })) as any
19
+ entityColumn.entity = (await tx.getRepository(Entity).findOne({ where: { id: entityColumn.entity.id } })) as any
19
20
  }
20
21
 
21
22
  return await tx.getRepository(EntityColumn).save({
@@ -29,7 +30,7 @@ export class EntityColumnMutation {
29
30
  @Directive('@transaction')
30
31
  @Mutation(returns => EntityColumn, { description: 'To modify EntityColumn information' })
31
32
  async updateEntityColumn(
32
- @Arg('name') name: string,
33
+ @Arg('id') id: string,
33
34
  @Arg('patch') patch: EntityColumnPatch,
34
35
  @Ctx() context: ResolverContext
35
36
  ): Promise<EntityColumn> {
@@ -37,11 +38,11 @@ export class EntityColumnMutation {
37
38
 
38
39
  const repository = tx.getRepository(EntityColumn)
39
40
  const entityColumn = await repository.findOne({
40
- where: { domain: { id: domain.id }, name }
41
+ where: { domain: { id: domain.id }, id }
41
42
  })
42
43
 
43
44
  if (patch.entity) {
44
- patch.entity = (await tx.getRepository(Entity).findOneBy({ id: patch.entity })) as any
45
+ patch.entity = (await tx.getRepository(Entity).findOneBy({ id: patch.entity.id })) as any
45
46
  }
46
47
 
47
48
  return await repository.save({
@@ -51,12 +52,79 @@ export class EntityColumnMutation {
51
52
  })
52
53
  }
53
54
 
55
+ @Directive('@transaction')
56
+ @Mutation(returns => [EntityColumn], { description: "To modify multiple Entitys' information" })
57
+ async updateMultipleEntityColumn(
58
+ @Arg('patches', type => [EntityColumnPatch]) patches: EntityColumnPatch[],
59
+ @Ctx() context: ResolverContext
60
+ ): Promise<EntityColumn[]> {
61
+ const { domain, user, tx } = context.state
62
+
63
+ let results = []
64
+ const _createRecords = patches.filter((patch: any) => patch.cuFlag.toUpperCase() === '+')
65
+ const _updateRecords = patches.filter((patch: any) => patch.cuFlag.toUpperCase() === 'M')
66
+ const entityColumnRepo = tx.getRepository(EntityColumn)
67
+
68
+ if (_createRecords.length > 0) {
69
+ for (let i = 0; i < _createRecords.length; i++) {
70
+ const newRecord = _createRecords[i]
71
+
72
+ if (newRecord.entity) {
73
+ newRecord.entity = (await tx.getRepository(Entity).findOneBy({ id: newRecord.entity.id })) as any
74
+ }
75
+
76
+ const result = await entityColumnRepo.save({
77
+ ...(newRecord as any),
78
+ domain,
79
+ creator: user,
80
+ updater: user
81
+ })
82
+
83
+ results.push({ ...result, cuFlag: '+' })
84
+ }
85
+ }
86
+
87
+ if (_updateRecords.length > 0) {
88
+ for (let i = 0; i < _updateRecords.length; i++) {
89
+ const updateRecord = _updateRecords[i]
90
+ const entity = await entityColumnRepo.findOneBy({ id: updateRecord.id })
91
+
92
+ if (updateRecord.entity) {
93
+ updateRecord.entity = (await tx.getRepository(Entity).findOneBy({ id: updateRecord.entity.id })) as any
94
+ }
95
+
96
+ const result = await entityColumnRepo.save({
97
+ ...entity,
98
+ ...(updateRecord as any),
99
+ updater: user
100
+ })
101
+
102
+ results.push({ ...result, cuFlag: 'M' })
103
+ }
104
+ }
105
+
106
+ return results
107
+ }
108
+
54
109
  @Directive('@transaction')
55
110
  @Mutation(returns => Boolean, { description: 'To delete EntityColumn' })
56
- async deleteEntityColumn(@Arg('name') name: string, @Ctx() context: ResolverContext): Promise<boolean> {
111
+ async deleteEntityColumn(@Arg('id') id: string, @Ctx() context: ResolverContext): Promise<boolean> {
112
+ const { domain, tx } = context.state
113
+
114
+ await tx.getRepository(EntityColumn).delete({ domain: { id: domain.id }, id })
115
+ return true
116
+ }
117
+
118
+ @Directive('@transaction')
119
+ @Mutation(returns => Boolean, { description: 'To delete multiple EntityColumns' })
120
+ async deleteEntityColumns(
121
+ @Arg('ids', type => [String]) ids: string[],
122
+ @Ctx() context: ResolverContext
123
+ ): Promise<boolean> {
57
124
  const { domain, tx } = context.state
58
125
 
59
- await tx.getRepository(EntityColumn).delete({ domain, name })
126
+ await tx.getRepository(EntityColumn).delete({ domain: { id: domain.id }, id: In(ids) })
127
+
60
128
  return true
61
129
  }
62
130
  }
@@ -10,11 +10,11 @@ import { EntityColumnList } from './entity-column-type'
10
10
  @Resolver(EntityColumn)
11
11
  export class EntityColumnQuery {
12
12
  @Query(returns => EntityColumn, { description: 'To fetch a EntityColumn' })
13
- async entityColumn(@Arg('name') name: string, @Ctx() context: ResolverContext): Promise<EntityColumn> {
13
+ async entityColumn(@Arg('id') id: string, @Ctx() context: ResolverContext): Promise<EntityColumn> {
14
14
  const { domain } = context.state
15
15
 
16
16
  return await getRepository(EntityColumn).findOne({
17
- where: { domain: { id: domain.id }, name }
17
+ where: { domain: { id: domain.id }, id }
18
18
  })
19
19
  }
20
20
 
@@ -1,5 +1,5 @@
1
- import { Field, InputType, Int, ObjectType } from 'type-graphql'
2
-
1
+ import { Field, ID, InputType, Int, ObjectType } from 'type-graphql'
2
+ import { ObjectRef } from '@things-factory/shell'
3
3
  import { EntityColumn } from './entity-column'
4
4
 
5
5
  @InputType()
@@ -11,7 +11,7 @@ export class NewEntityColumn {
11
11
  description?: string
12
12
 
13
13
  @Field()
14
- entity: string
14
+ entity: ObjectRef
15
15
 
16
16
  @Field({ nullable: true })
17
17
  rank?: number
@@ -109,6 +109,9 @@ export class NewEntityColumn {
109
109
 
110
110
  @InputType()
111
111
  export class EntityColumnPatch {
112
+ @Field(type => ID, { nullable: true })
113
+ id?: string
114
+
112
115
  @Field({ nullable: true })
113
116
  name?: string
114
117
 
@@ -116,7 +119,7 @@ export class EntityColumnPatch {
116
119
  description?: string
117
120
 
118
121
  @Field({ nullable: true })
119
- entity?: string
122
+ entity?: ObjectRef
120
123
 
121
124
  @Field(type => Int, { nullable: true })
122
125
  rank?: number
@@ -210,6 +213,9 @@ export class EntityColumnPatch {
210
213
 
211
214
  @Field({ nullable: true })
212
215
  ignoreOnSav?: boolean
216
+
217
+ @Field({ nullable: true })
218
+ cuFlag?: string
213
219
  }
214
220
 
215
221
  @ObjectType()