You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
potato-launcher/src/js/database/tags.js

140 lines
2.9 KiB

import DatabaseHandler from './databaseHandler.js'
import AppsStore from './apps.js'
/**
* apps
*
* @author Björn Hase
* @license hhttps://www.gnu.org/licenses/gpl-3.0.en.html GPL-3
* @link https://gitea.node001.net/HerrHase/tellme-bot.git
*
*/
class TagsDatabase extends DatabaseHandler {
constructor() {
super('tags')
// add index for apps
this.createIndex([
'name'
], 'tags')
}
/**
*
*
* @param {object} data
*
*/
async update(tags) {
const query = {
'selector': {
'name': {
'$in': tags
}
}, 'fields': [
'name'
]
}
// check for existings tags and remove
// adding tags that are not removed
this.db.find(query).then((documents) => {
return this.filterTags(documents, tags)
}).then(async (tags) => {
if (tags.length > 0) {
// adding tags to entries
for (let i = 0; i < tags.length; i++) {
await this.db.post({
'name': tags[i]
})
}
this.removeNotNeeded()
}
})
}
/**
*
*
*/
removeNotNeeded() {
this.find().then((tags) => {
const appsStore = new AppsStore()
for (let i = 0; i < tags.length; i++) {
const parameters = {
tags: []
}
parameters.tags.push(tags[i].name)
appsStore.find(parameters).then((documents) => {
if (!documents) {
this.db.remove(tags[i])
}
})
}
})
}
/**
* filter tags, if they are already exists
*
* @param {array} documents
* @param {array} tags
* @return {array}
*
*/
filterTags(documents, tags) {
if (documents.docs.length > 0) {
for (let i; i < documents.docs.length; i++) {
const index = tags.indexOf(documents.docs[i].name)
if (index >= 0) {
tags.splice(index, 1)
}
}
}
return tags
}
/**
* find apps
*
*
* @return
*
*/
find() {
const query = {
'fields': [
'name'
],
'selector': {
'name': {
'$exists': true
}
},
'sort': [
'name'
]
}
return this.db.find(query).then((documents) => {
if (documents.warning) {
console.warning(documents.warning)
}
return documents.docs
})
}
}
export default TagsDatabase