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-10-14 08:59:34 +00:00
const uri = 'https://docs.meilisearch.com/reference/api/document';
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-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-10-15 12:13:34 +00:00
get documentSetting() {
return {
uri,
2022-10-15 12:13:34 +00:00
fileMatch: [uri],
schema: this.schema,
};
}
get schema() {
return {
$id: uri,
type: 'object',
required: [this.primaryKey],
2022-10-15 12:13:34 +00:00
};
}
2022-08-30 11:59:24 +00:00
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-11-17 11:32:38 +00:00
saveDocuments(body) {
return query(`${this.path}/documents`, { method: 'POST', body });
}
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-10-22 12:48:19 +00:00
search(options) {
2022-08-30 11:59:24 +00:00
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
}