meilisearch-admin/app/models/index.js

129 lines
2.4 KiB
JavaScript
Raw Normal View History

2022-05-03 09:49:10 +00:00
import query from '../utils/query';
2022-08-30 11:59:24 +00:00
import Document from './document';
2022-09-02 08:53:33 +00:00
import { dasherize } from '@ember/string';
2022-05-03 09:49:10 +00:00
2022-05-02 11:29:01 +00:00
export default class Index {
2022-08-30 10:15:36 +00:00
constructor(data = {}) {
2022-05-02 11:29:01 +00:00
for (const key in data) {
this[key] = data[key];
}
}
2022-05-03 09:49:10 +00:00
2022-09-02 09:00:15 +00:00
get settingNames() {
return [
'displayedAttributes',
'distinctAttribute',
'faceting',
'filterableAttributes',
'pagination',
'rankingRules',
'searchableAttributes',
'sortableAttributes',
'stopWords',
'synonyms',
'typoTolerance',
];
}
2022-08-30 11:59:24 +00:00
get path() {
2022-05-03 09:49:10 +00:00
return `indexes/${this.uid}`;
}
2022-08-30 10:15:36 +00:00
get isNew() {
return Boolean(!this.uid);
}
2022-08-30 11:59:24 +00:00
getSortableAttributes() {
return query(`${this.path}/settings/sortable-attributes`);
}
getStats() {
return query(`${this.path}/stats`);
}
getDocument(uid) {
return query(`${this.path}/documents/${uid}`).then(
2022-09-01 06:40:44 +00:00
(result) => new Document(result)
2022-08-30 11:59:24 +00:00
);
}
2022-09-01 08:29:15 +00:00
saveDocument(document) {
2022-09-05 15:58:34 +00:00
if (typeof document === 'string') {
document = JSON.parse(document);
}
2022-09-01 08:29:15 +00:00
return query(`${this.path}/documents`, {
2022-09-01 09:14:56 +00:00
method: 'POST',
2022-09-05 15:58:34 +00:00
body: JSON.stringify([document]),
2022-09-01 08:29:15 +00:00
});
}
2022-09-01 10:28:54 +00:00
deleteDocument(document) {
const document_id = document[this.primaryKey];
return query(`${this.path}/documents/${document_id}`, {
method: 'DELETE',
});
}
2022-08-30 11:59:24 +00:00
search(q, options = {}) {
options.q = q;
return query(`${this.path}/search`, {
method: 'POST',
body: JSON.stringify(options),
});
}
save(data) {
const path = this.isNew ? 'indexes' : this.path;
const method = this.isNew ? 'POST' : 'PATCH';
return query(path, {
method: method,
body: JSON.stringify(data),
});
}
2022-09-02 08:53:33 +00:00
getSetting(name) {
2022-10-12 13:34:08 +00:00
let url = `${this.path}/settings`;
if (name) {
url = `${url}/${dasherize(name)}`;
}
return query(url);
2022-09-02 08:53:33 +00:00
}
2022-10-12 13:34:08 +00:00
updateSetting(value, name) {
const method =
name === undefined ||
['faceting', 'pagination', 'typo-tolerance'].includes(name)
? 'PATCH'
: 'PUT';
2022-10-12 13:34:08 +00:00
let url = `${this.path}/settings`;
if (name) {
url = `${url}/${dasherize(name)}`;
}
return query(url, {
method: method,
2022-09-02 08:53:33 +00:00
body: JSON.stringify(value),
});
}
resetSetting(name) {
2022-10-12 13:34:08 +00:00
let url = `${this.path}/settings`;
if (name) {
url = `${url}/${dasherize(name)}`;
}
return query(url, {
2022-09-02 08:53:33 +00:00
method: 'DELETE',
});
}
2021-01-27 14:12:19 +00:00
}