-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
49 changed files
with
1,166 additions
and
314 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: check-emitted-types | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
- development | ||
- release | ||
pull_request: | ||
|
||
jobs: | ||
check-emitted-types: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Use Node.js 14.6.x | ||
uses: actions/setup-node@v1 | ||
with: | ||
node-version: '14.6.x' | ||
- name: Cache node modules | ||
uses: actions/cache@v2 | ||
with: | ||
path: ./node_modules | ||
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} | ||
- name: install dependencies | ||
run: yarn install | ||
- run: yarn emit-types --check |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
app/api/migrations/migrations/38-denormalize-inherited/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* eslint-disable no-await-in-loop */ | ||
export default { | ||
delta: 38, | ||
|
||
name: 'denormalize-inherited', | ||
|
||
description: 'Denormalize inherited metadata', | ||
|
||
async up(db) { | ||
const cursor = db.collection('entities').find({}); | ||
const templates = await db | ||
.collection('templates') | ||
.find({}) | ||
.toArray(); | ||
|
||
let index = 1; | ||
|
||
while (await cursor.hasNext()) { | ||
const entity = await cursor.next(); | ||
if (entity.template && entity.metadata) { | ||
const template = templates.find(t => t._id.toString() === entity.template.toString()); | ||
|
||
await Promise.all( | ||
template.properties.map(async prop => { | ||
if (prop.type === 'relationship') { | ||
const value = entity.metadata[prop.name] || []; | ||
const denormalizedValue = await Promise.all( | ||
value.map(async _elem => { | ||
const elem = { ..._elem }; | ||
const [partner] = await db | ||
.collection('entities') | ||
.find({ | ||
sharedId: elem.value, | ||
language: entity.language, | ||
}) | ||
.toArray(); | ||
|
||
if (prop.inherit && partner) { | ||
const partnerTemplate = templates.find( | ||
t => t._id.toString() === partner.template.toString() | ||
); | ||
|
||
const inheritedProperty = partnerTemplate.properties.find( | ||
p => p._id && p._id.toString() === prop.inheritProperty.toString() | ||
); | ||
|
||
elem.inheritedValue = partner.metadata[inheritedProperty.name] || []; | ||
elem.inheritedType = inheritedProperty.type; | ||
} | ||
return elem; | ||
}) | ||
); | ||
|
||
entity.metadata[prop.name] = denormalizedValue.filter(v => v); | ||
} | ||
}) | ||
); | ||
|
||
await db | ||
.collection('entities') | ||
.updateOne({ _id: entity._id }, { $set: { metadata: entity.metadata } }); | ||
} | ||
process.stdout.write(`-> processed: ${index} \r`); | ||
index += 1; | ||
} | ||
|
||
process.stdout.write('\r\n'); | ||
}, | ||
}; |
55 changes: 55 additions & 0 deletions
55
...api/migrations/migrations/38-denormalize-inherited/specs/38-denormalize-inherited.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import testingDB from 'api/utils/testing_db'; | ||
import migration from '../index.js'; | ||
import fixtures from './fixtures.js'; | ||
|
||
describe('migration denormalize-inherited', () => { | ||
beforeEach(async () => { | ||
spyOn(process.stdout, 'write'); | ||
await testingDB.clearAllAndLoad(fixtures); | ||
}); | ||
|
||
afterAll(async () => { | ||
await testingDB.disconnect(); | ||
}); | ||
|
||
it('should have a delta number', () => { | ||
expect(migration.delta).toBe(38); | ||
}); | ||
|
||
it('should denormalize inherited data', async () => { | ||
await migration.up(testingDB.mongodb); | ||
const [denormalizedEntity] = await testingDB.mongodb | ||
.collection('entities') | ||
.find({ title: 'test_doc' }) | ||
.toArray(); | ||
|
||
expect(denormalizedEntity.metadata.friend).toEqual([ | ||
{ | ||
inheritedType: 'text', | ||
inheritedValue: [{ value: 'Bocata Tun' }], | ||
label: 'test_doc 2', | ||
value: '456DEF', | ||
}, | ||
{ | ||
inheritedType: 'text', | ||
inheritedValue: [], | ||
label: 'test_doc 3', | ||
value: '789ZXY', | ||
}, | ||
]); | ||
|
||
const [denormalizedEntityWithoutValues] = await testingDB.mongodb | ||
.collection('entities') | ||
.find({ title: 'test_doc 4' }) | ||
.toArray(); | ||
|
||
expect(denormalizedEntityWithoutValues.metadata.friend).toEqual([ | ||
{ | ||
inheritedType: 'text', | ||
inheritedValue: [], | ||
label: 'test_doc 5', | ||
value: '789ABC', | ||
}, | ||
]); | ||
}); | ||
}); |
81 changes: 81 additions & 0 deletions
81
app/api/migrations/migrations/38-denormalize-inherited/specs/fixtures.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import db from 'api/utils/testing_db'; | ||
const templateId = db.id(); | ||
const templateId2 = db.id(); | ||
const inheritPropertyId = db.id(); | ||
export default { | ||
entities: [ | ||
{ | ||
template: templateId, | ||
title: 'test_doc', | ||
sharedId: '123ABC', | ||
metadata: { | ||
friend: [ | ||
{ value: '456DEF', label: 'test_doc 2' }, | ||
{ value: '789ZXY', label: 'test_doc 3' }, | ||
], | ||
}, | ||
language: 'en', | ||
}, | ||
|
||
{ | ||
template: templateId2, | ||
title: 'test_doc 2', | ||
sharedId: '456DEF', | ||
metadata: { name: [{ value: 'Bocata Tun' }] }, | ||
language: 'en', | ||
}, | ||
{ | ||
template: templateId2, | ||
title: 'test_doc 3', | ||
sharedId: '789ZXY', | ||
metadata: { name: [] }, | ||
language: 'en', | ||
}, | ||
{ | ||
template: templateId, | ||
title: 'test_doc 4', | ||
sharedId: '498ABC', | ||
metadata: { | ||
friend: [{ value: '789ABC', label: 'test_doc 5' }], | ||
}, | ||
language: 'en', | ||
}, | ||
{ | ||
template: templateId2, | ||
title: 'test_doc 5', | ||
sharedId: '789ABC', | ||
metadata: {}, | ||
language: 'en', | ||
}, | ||
{ | ||
title: 'Im gona break everything', | ||
}, | ||
{ | ||
title: 'Try to break it harder', | ||
template: 'whats a template?', | ||
}, | ||
{ | ||
title: 'There is only zull', | ||
template: templateId, | ||
}, | ||
], | ||
templates: [ | ||
{ | ||
_id: templateId, | ||
properties: [ | ||
{ | ||
type: 'relationship', | ||
relationType: 'something', | ||
inherit: true, | ||
content: templateId2, | ||
inheritProperty: inheritPropertyId, | ||
name: 'friend', | ||
}, | ||
], | ||
}, | ||
{ | ||
_id: templateId2, | ||
properties: [{ _id: inheritPropertyId, type: 'text', name: 'name' }], | ||
}, | ||
], | ||
}; |
Oops, something went wrong.