Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: mapeoProject.importConfig #405

Merged
merged 36 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
2feae41
initial implementation of mapeoProject.importConfig
Dec 5, 2023
86c9b91
parse icons (still wip)
Dec 5, 2023
7dd8abd
Can already import fields, icons and presets
Dec 13, 2023
0fd026e
delete presets fields and icons when importing config
Dec 13, 2023
b79198b
fix type errors
Dec 13, 2023
17e34e0
readConfig abstraction & bug fixes
gmaclennan Dec 14, 2023
ee183cd
Merge branch 'main' of github.com:digidem/mapeo-core-next into feat/i…
Dec 14, 2023
5a7ff4f
Merge commit '17e34e00046a66b4d4c286f0bf13268dc391ea41' of github.com…
Dec 14, 2023
8246a18
Write defaultPresets to projectSettings
Dec 18, 2023
e384251
fix wrong merge
Dec 18, 2023
9395c85
sort preset ids to add to default config
Dec 18, 2023
131697b
sort presets on generator, add `deleteAll` helper
Dec 19, 2023
9c186c3
Merge branch 'main' of github.com:digidem/mapeo-core-next into feat/i…
Jan 31, 2024
3d52628
initial tests of src/config-import.js
Jan 31, 2024
5836fbd
added tests for invalid presets.json file
Jan 31, 2024
1f5d870
fix wrong matching of icons directory, add icons tests
Jan 31, 2024
cdf2f6f
add missing validConfig.zip
Jan 31, 2024
e3aa86a
add tests for fields
Feb 1, 2024
529e303
add tests for presets, move preset error check on a filter before
Feb 1, 2024
30cc84d
Apply suggestions from code review from evan
tomasciccola Feb 1, 2024
3ab89e7
various changes
Feb 1, 2024
d8f7d75
Clean up parseIcon fn
Feb 1, 2024
b1a7a58
add `isRecord` and 'hasOwn' to avoid duplicating checks
Feb 1, 2024
8e31ab0
address review changes:
Feb 5, 2024
c5dab4f
Use Infinity as default for preset.sort, logic bug in :106
Feb 5, 2024
8e8da77
delete `sort` field in preset after sorting
Feb 5, 2024
5f59e82
fix tests
Feb 5, 2024
4067976
call config.close() as soon as we know we won't need the file handle
Feb 5, 2024
526a9a1
Merge branch 'main' of github.com:digidem/mapeo-core-next into feat/i…
Feb 5, 2024
f3313b4
add valid cases for icons, fields and presets tests
Feb 6, 2024
f72c244
replace bigZip with smaller file, add warning with MAX_ENTIES for mac,
Feb 6, 2024
a0c04be
Apply suggestions from code review
tomasciccola Feb 7, 2024
7acf998
Merge branch 'main' of github.com:digidem/mapeo-core-next into feat/i…
Feb 7, 2024
118bc55
add limit for icon size, raise a warning if going over it
Feb 7, 2024
49ca3df
add default config zip
Feb 7, 2024
00d9bba
fix silly bug, update deps to solve type errors
Feb 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/config-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export async function readConfig(configPath) {
},

/**
* @returns {Iterable<{ fieldNames: string[], iconName: string | undefined, value: import('@mapeo/schema').PresetValue }>}
* @returns {Iterable<{ fieldNames: string[], iconName: string | undefined, value: import('@mapeo/schema').PresetValue, sortValue: number }>}
*/
*presets() {
const { presets } = presetsFile
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -159,6 +159,8 @@ export async function readConfig(configPath) {
? preset.icon
: undefined,
value: presetValue,
//@ts-ignore TODO: remove this
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
sortValue: preset['sort'],
}
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
}
},
Expand Down
65 changes: 34 additions & 31 deletions src/mapeo-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,34 +554,24 @@ export class MapeoProject extends TypedEmitter {
* @param {string} opts.configPath
*/
async importConfig({ configPath }) {
// Commenting until #417 and #418 get merged
// // check for already present fields, icons and presets and delete them if exist
// const alreadyPresets = await this.preset.getMany()
// if (alreadyPresets.length !== 0) {
// for (const preset of alreadyPresets) {
// const { fieldIds, iconId, versionId: presetVersionId } = preset
// // delete fields
// for (const fieldId of fieldIds) {
// const field = await this.field.getByDocId(fieldId)
// const { deleted } = await this.field.delete(field.versionId, field)
// if (!deleted) throw new Error('error deleting field from db')
// }
// // delete icon
// const icon = await this.#iconApi.dataType.getByDocId(iconId)
// const { deleted: deletedIcon } = await this.#iconApi.dataType.delete(
// icon.versionId,
// icon
// )
// if (!deletedIcon) throw new Error('error deleting icon from db')

// // delete preset
// const { deleted: deletedPreset } = await this.preset.delete(
// presetVersionId,
// preset
// )
// if (!deletedPreset) throw new Error('error deleting preset from db')
// }
// }
// check for already present fields and presets and delete them if exist
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it make sense to do this here instead of moving it outside to a function in src/config-imprort.js? I left it here since we are using a couple of methods of the class and it felt weird passing them to the function as params...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with it here. Could also add a deleteAll() method to DataType or we could do a quick helper:

async function deleteAll(dataType) {
  const deletions = []
  for (const doc of await dataType.getMany()) {
    deletions.push(dataType.delete(doc.versionId))
  }
  return Promise.all(deletions)
}

const presets = await this.preset.getMany()
if (presets.length !== 0) {
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
for (const preset of presets) {
const { fieldIds, versionId: presetVersionId } = preset
// delete fields
for (const fieldId of fieldIds) {
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
const field = await this.field.getByDocId(fieldId)
const { deleted } = await this.field.delete(field.versionId)
if (!deleted) throw new Error('error deleting field from db')
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
}
// delete preset
const { deleted: deletedPreset } = await this.preset.delete(
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
presetVersionId
)
if (!deletedPreset) throw new Error('error deleting preset from db')
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
}
}

const config = await readConfig(configPath)
/** @type {Map<string, string>} */
EvanHahn marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -607,7 +597,9 @@ export class MapeoProject extends TypedEmitter {
await Promise.all(fieldPromises)

const presetsWithRefs = []
for (const { fieldNames, iconName, value } of config.presets()) {
/** @type {Map<string,number>} */
const presetNameToSortValue = new Map()
for (const { fieldNames, iconName, value, sortValue } of config.presets()) {
const fieldIds = fieldNames.map((fieldName) => {
const id = fieldNameToId.get(fieldName)
if (!id) {
Expand All @@ -617,6 +609,7 @@ export class MapeoProject extends TypedEmitter {
}
return id
})
presetNameToSortValue.set(value.name, sortValue)
presetsWithRefs.push({
...value,
iconId: iconName && iconNameToId.get(iconName),
Expand All @@ -627,10 +620,20 @@ export class MapeoProject extends TypedEmitter {
this.preset.create(preset)
)
const createdPresets = await Promise.all(presetPromises)
const presetIds = createdPresets.map(({ docId }) => docId)
const sortedPresetIds = createdPresets
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
.sort((preset, nextPreset) => {
const sortValue = presetNameToSortValue.get(preset.name)
const nextSortValue = presetNameToSortValue.get(nextPreset.name)
if (!sortValue || !nextSortValue) {
throw new Error(`invalid sort value`)
}
return sortValue - nextSortValue
})
.map(({ docId }) => docId)

await this.$setProjectSettings({
defaultPresets: {
point: presetIds,
point: sortedPresetIds,
line: [],
area: [],
vertex: [],
Expand Down