-
Notifications
You must be signed in to change notification settings - Fork 583
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: modify elasticsearch index creation (#3466)
- Loading branch information
1 parent
b53a4ad
commit ed3ef6a
Showing
15 changed files
with
284 additions
and
198 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
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,122 @@ | ||
import { Client } from "@elastic/elasticsearch"; | ||
import { PluginsContainer } from "@webiny/plugins"; | ||
import { getLastAddedIndexPlugin } from "~/indices"; | ||
import { ElasticsearchIndexPlugin } from "~/plugins"; | ||
import WebinyError from "@webiny/error"; | ||
|
||
interface OnExists { | ||
(): void; | ||
} | ||
|
||
interface OnError { | ||
(ex: Error): Error; | ||
} | ||
|
||
interface ExistsIndexParams { | ||
client: Client; | ||
index: string; | ||
onExists?: OnExists; | ||
} | ||
|
||
const indexExists = async (params: ExistsIndexParams): Promise<boolean> => { | ||
const { client, index, onExists } = params; | ||
|
||
try { | ||
const response = await client.indices.exists({ | ||
index, | ||
ignore_unavailable: false, | ||
allow_no_indices: true, | ||
include_defaults: true, | ||
flat_settings: false, | ||
local: false | ||
}); | ||
if (!response.body) { | ||
return false; | ||
} | ||
if (onExists) { | ||
onExists(); | ||
} | ||
return true; | ||
} catch (ex) { | ||
console.error(`Could not determine if the index "${index}" exists.`); | ||
} | ||
return false; | ||
}; | ||
|
||
interface IndexCreateParams { | ||
client: Client; | ||
index: string; | ||
type: string; | ||
tenant: string; | ||
locale: string; | ||
plugin: ElasticsearchIndexPlugin; | ||
onError?: OnError; | ||
} | ||
|
||
const indexCreate = async (params: IndexCreateParams): Promise<void> => { | ||
const { client, index, plugin, tenant, locale, type, onError } = params; | ||
|
||
try { | ||
await client.indices.create({ | ||
index, | ||
body: { | ||
...plugin.body | ||
} | ||
}); | ||
} catch (ex) { | ||
let error = ex; | ||
if (onError) { | ||
error = onError(ex); | ||
} | ||
throw new WebinyError( | ||
error.message || `Could not create Elasticsearch index for the ${type}.`, | ||
error.code || "CREATE_ELASTICSEARCH_INDEX_ERROR", | ||
{ | ||
error: { | ||
...error, | ||
message: error.message, | ||
code: error.code, | ||
data: error.data | ||
}, | ||
type, | ||
locale, | ||
tenant, | ||
index, | ||
body: plugin.body | ||
} | ||
); | ||
} | ||
}; | ||
|
||
interface CreateIndexParams { | ||
client: Client; | ||
plugins: PluginsContainer; | ||
type: string; | ||
tenant: string; | ||
locale: string; | ||
index: string; | ||
onExists?: OnExists; | ||
onError?: OnError; | ||
} | ||
|
||
export const createIndex = async (params: CreateIndexParams): Promise<void> => { | ||
const { plugins, type, locale, onExists } = params; | ||
const plugin = getLastAddedIndexPlugin<ElasticsearchIndexPlugin>({ | ||
container: plugins, | ||
type, | ||
locale | ||
}); | ||
|
||
const exists = await indexExists(params); | ||
if (exists) { | ||
if (onExists) { | ||
onExists(); | ||
} | ||
return; | ||
} | ||
|
||
await indexCreate({ | ||
...params, | ||
plugin | ||
}); | ||
}; |
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 @@ | ||
export * from "./createIndex"; |
This file was deleted.
Oops, something went wrong.
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
54 changes: 21 additions & 33 deletions
54
packages/api-headless-cms-ddb-es/src/elasticsearch/createElasticsearchIndex.ts
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 |
---|---|---|
@@ -1,52 +1,40 @@ | ||
import WebinyError from "@webiny/error"; | ||
import { Client } from "@elastic/elasticsearch"; | ||
import { PluginsContainer } from "@webiny/plugins"; | ||
import { CmsEntryElasticsearchIndexPlugin } from "~/plugins/CmsEntryElasticsearchIndexPlugin"; | ||
import { getLastAddedIndexPlugin } from "@webiny/api-elasticsearch"; | ||
import { createIndex } from "@webiny/api-elasticsearch"; | ||
import { configurations } from "~/configurations"; | ||
import { CmsModel } from "@webiny/api-headless-cms/types"; | ||
|
||
export interface CreateElasticsearchIndexParams { | ||
elasticsearch: Client; | ||
client: Client; | ||
plugins: PluginsContainer; | ||
model: CmsModel; | ||
} | ||
|
||
export const createElasticsearchIndex = async (params: CreateElasticsearchIndexParams) => { | ||
const { elasticsearch, plugins: container, model } = params; | ||
|
||
const plugin = getLastAddedIndexPlugin<CmsEntryElasticsearchIndexPlugin>({ | ||
container, | ||
type: CmsEntryElasticsearchIndexPlugin.type, | ||
locale: model.locale | ||
}); | ||
const { client, plugins, model } = params; | ||
|
||
const { index } = configurations.es({ | ||
model | ||
}); | ||
|
||
try { | ||
const response = await elasticsearch.indices.exists({ | ||
index | ||
}); | ||
if (response.body) { | ||
return; | ||
await createIndex({ | ||
client, | ||
index, | ||
type: CmsEntryElasticsearchIndexPlugin.type, | ||
tenant: model.tenant, | ||
locale: model.locale, | ||
plugins, | ||
onExists: () => { | ||
console.log( | ||
`Elasticsearch index "${index}" for the CMS model "${model.name}" already exists.` | ||
); | ||
}, | ||
onError: ex => { | ||
console.error( | ||
`Could not create Elasticsearch index "${index}" for the CMS model "${model.name}".` | ||
); | ||
return ex; | ||
} | ||
await elasticsearch.indices.create({ | ||
index, | ||
body: plugin.body | ||
}); | ||
} catch (ex) { | ||
throw new WebinyError( | ||
ex.message || "Could not create Elasticsearch index for the Headless CMS model.", | ||
ex.code || "CMS_ELASTICSEARCH_INDEX_ERROR", | ||
{ | ||
error: ex, | ||
type: CmsEntryElasticsearchIndexPlugin.type, | ||
locale: model.locale, | ||
tenant: model.tenant, | ||
body: plugin.body | ||
} | ||
); | ||
} | ||
}); | ||
}; |
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
Oops, something went wrong.