Skip to content

Commit fcf1da3

Browse files
committed
optimize organizations updates
1 parent 9f639b4 commit fcf1da3

File tree

2 files changed

+67
-44
lines changed

2 files changed

+67
-44
lines changed

services/apps/data_sink_worker/src/repo/organization.repo.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,16 +266,24 @@ export class OrganizationRepository extends RepositoryBase<OrganizationRepositor
266266
return id
267267
}
268268

269-
public async update(id: string, data: IDbUpdateOrganizationData): Promise<void> {
269+
public async update(id: string, data: Partial<IDbUpdateOrganizationData>): Promise<void> {
270+
const keys = Object.keys(data)
271+
keys.push('updatedAt')
272+
// construct dynamic column set
273+
const dynamicColumnSet = new this.dbInstance.helpers.ColumnSet(keys, {
274+
table: {
275+
table: 'organizations',
276+
},
277+
})
270278
const prepared = RepositoryBase.prepare(
271279
{
272280
...data,
273281
updatedAt: new Date(),
274282
},
275-
this.updateOrganizationColumnSet,
283+
dynamicColumnSet,
276284
)
277285

278-
const query = this.dbInstance.helpers.update(prepared, this.updateOrganizationColumnSet)
286+
const query = this.dbInstance.helpers.update(prepared, dynamicColumnSet)
279287
const condition = this.format('where id = $(id)', { id })
280288

281289
const result = await this.db().result(`${query} ${condition}`)

services/apps/data_sink_worker/src/service/organization.service.ts

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,33 @@ export class OrganizationService extends LoggerBase {
3636
// if exists in cache update it
3737
const updateData: Partial<IOrganization> = {}
3838
// no need to update name since it's aka primary key
39-
if (data.url) updateData.url = data.url
40-
if (data.description) updateData.description = data.description
41-
if (data.emails) updateData.emails = data.emails
42-
if (data.logo) updateData.logo = data.logo
43-
if (data.tags) updateData.tags = data.tags
44-
if (data.github) updateData.github = data.github as IOrganizationSocial
45-
if (data.twitter) updateData.twitter = data.twitter as IOrganizationSocial
46-
if (data.linkedin) updateData.linkedin = data.linkedin as IOrganizationSocial
47-
if (data.crunchbase) updateData.crunchbase = data.crunchbase as IOrganizationSocial
48-
if (data.employees) updateData.employees = data.employees
49-
if (data.location) updateData.location = data.location
50-
if (data.website) updateData.website = data.website
51-
if (data.type) updateData.type = data.type
52-
if (data.size) updateData.size = data.size
53-
if (data.headline) updateData.headline = data.headline
54-
if (data.industry) updateData.industry = data.industry
55-
if (data.founded) updateData.founded = data.founded
39+
const fields = [
40+
'url',
41+
'description',
42+
'emails',
43+
'logo',
44+
'tags',
45+
'github',
46+
'twitter',
47+
'linkedin',
48+
'crunchbase',
49+
'employees',
50+
'location',
51+
'website',
52+
'type',
53+
'size',
54+
'headline',
55+
'industry',
56+
'founded',
57+
]
58+
fields.forEach((field) => {
59+
if (data[field] && !isEqual(data[field], cached[field])) {
60+
updateData[field] = data[field]
61+
}
62+
})
5663
if (Object.keys(updateData).length > 0) {
5764
await this.repo.updateCache(cached.id, updateData)
65+
cached = { ...cached, ...updateData } // Update the cached data with the new data
5866
}
5967
} else {
6068
// if it doesn't exists in cache create it
@@ -91,11 +99,9 @@ export class OrganizationService extends LoggerBase {
9199
// now check if exists in this tenant
92100
const existing = await this.repo.findByName(tenantId, segmentId, data.name)
93101

94-
const displayName = existing?.displayName ? existing?.displayName : data?.name
95-
96102
let attributes = existing?.attributes
97103

98-
if (data.attributes) {
104+
if (data?.attributes) {
99105
const temp = mergeWith({}, existing?.attributes, data?.attributes)
100106
if (!isEqual(temp, existing?.attributes)) {
101107
attributes = temp
@@ -104,28 +110,37 @@ export class OrganizationService extends LoggerBase {
104110

105111
if (existing) {
106112
// if it does exists update it
107-
await this.repo.update(existing.id, {
108-
name: cached.name,
109-
displayName,
110-
url: cached.url,
111-
description: cached.description,
112-
emails: cached.emails,
113-
logo: cached.logo,
114-
tags: cached.tags,
115-
github: cached.github,
116-
twitter: cached.twitter,
117-
linkedin: cached.linkedin,
118-
crunchbase: cached.crunchbase,
119-
employees: cached.employees,
120-
location: cached.location,
121-
website: cached.website,
122-
type: cached.type,
123-
size: cached.size,
124-
headline: cached.headline,
125-
industry: cached.industry,
126-
founded: cached.founded,
127-
attributes,
113+
const updateData: Partial<IOrganization> = {}
114+
const fields = [
115+
'name',
116+
'displayName',
117+
'url',
118+
'description',
119+
'emails',
120+
'logo',
121+
'tags',
122+
'github',
123+
'twitter',
124+
'linkedin',
125+
'crunchbase',
126+
'employees',
127+
'location',
128+
'website',
129+
'type',
130+
'size',
131+
'headline',
132+
'industry',
133+
'founded',
134+
'attributes',
135+
]
136+
fields.forEach((field) => {
137+
if (cached[field] && !isEqual(cached[field], existing[field])) {
138+
updateData[field] = cached[field]
139+
}
128140
})
141+
if (Object.keys(updateData).length > 0) {
142+
await this.repo.update(existing.id, updateData)
143+
}
129144

130145
return existing.id
131146
} else {

0 commit comments

Comments
 (0)