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

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.20",
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.20"
28
28
  },
29
- "gitHead": "ee18af4bbf6b44e2f1d76258ad6922a535a9f0d8"
29
+ "gitHead": "6e50e3cefe9c4158500f3cb2bf14300684c05623"
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,13 +57,124 @@ 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
+ await deleteEntity(id, domain, tx)
131
+
132
+ return true
133
+ }
62
134
 
63
- await tx.getRepository(Entity).delete({ domain, name })
135
+ @Directive('@transaction')
136
+ @Mutation(returns => Boolean, { description: 'To delete multiple Entities' })
137
+ async deleteEntities(@Arg('ids', type => [String]) ids: string[], @Ctx() context: ResolverContext): Promise<boolean> {
138
+ const { domain, tx } = context.state
139
+
140
+ for (let i = 0; i < ids.length; i++) {
141
+ await deleteEntity(ids[i], domain, tx)
142
+ }
64
143
 
65
144
  return true
66
145
  }
67
146
  }
147
+
148
+ async function deleteEntityAndEntityColumns(id, domain, tx) {
149
+ const entity: Entity = tx.getRepository(Entity).findOne({ domain: { id: domain.id }, id })
150
+ const columns: EntityColumn[] = tx
151
+ .getRepository(EntityColumn)
152
+ .find({ where: { entity: { id: entity.id }, domain: { id: domain.id } }, relations: ['entity'] })
153
+
154
+ if (columns && columns.length) await tx.getRepository(EntityColumn).delete(columns)
155
+ await tx.getRepository(Entity).delete(entity)
156
+ }
157
+
158
+ async function deleteEntity(id, domain, tx) {
159
+ const entity: Entity = await tx.getRepository(Entity).findOne({
160
+ where: {
161
+ domain: { id: domain.id },
162
+ id
163
+ },
164
+ relations: ['children']
165
+ })
166
+
167
+ if ((entity.children && entity.children.length) || entity.delStrategy === DEL_STRATEGY.NULLIFY) {
168
+ if (entity.delStrategy === DEL_STRATEGY.DELETE || entity.delStrategy === DEL_STRATEGY.DESTROY) {
169
+ const children = entity.children
170
+ for (let i = 0; i < children.length; i++) {
171
+ await deleteEntityAndEntityColumns(children[i].id, domain, tx)
172
+ }
173
+ await deleteEntityAndEntityColumns(entity.id, domain, tx)
174
+ } else if (entity.delStrategy === DEL_STRATEGY.RESTRICT_WITH_ERROR) {
175
+ throw new Error('entity cannot delete')
176
+ }
177
+ } else {
178
+ await deleteEntityAndEntityColumns(entity.id, domain, tx)
179
+ }
180
+ }
@@ -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'
@@ -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,7 +38,7 @@ 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) {
@@ -51,12 +52,76 @@ 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 })) 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 })) 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 deleteEntities(@Arg('ids', type => [String]) ids: string[], @Ctx() context: ResolverContext): Promise<boolean> {
57
121
  const { domain, tx } = context.state
58
122
 
59
- await tx.getRepository(EntityColumn).delete({ domain, name })
123
+ await tx.getRepository(EntityColumn).delete({ domain: { id: domain.id }, id: In(ids) })
124
+
60
125
  return true
61
126
  }
62
127
  }
@@ -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,4 +1,4 @@
1
- import { Field, InputType, Int, ObjectType } from 'type-graphql'
1
+ import { Field, ID, InputType, Int, ObjectType } from 'type-graphql'
2
2
 
3
3
  import { EntityColumn } from './entity-column'
4
4
 
@@ -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
 
@@ -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()