|
| 1 | +import { Injectable } from '@nestjs/common'; |
| 2 | +import { CouchbaseService } from '../couchbase/couchbase.service'; |
| 3 | +import { Configuration } from './configuration.interface'; |
| 4 | +import { ViewQuery } from 'couchbase'; |
| 5 | + |
| 6 | +@Injectable() |
| 7 | +export class ConfigurationService { |
| 8 | + constructor(private readonly couchbaseService: CouchbaseService) { } |
| 9 | + |
| 10 | + add(type: PlatformTypes, name: string, configuration: Configuration[]) { |
| 11 | + return new Promise((resolve, reject) => { |
| 12 | + this.couchbaseService.getBucket().insert(`configuration_${type}_${name}`, {configuration, type: `configuration_${type}`}, (err, data) => { |
| 13 | + if (err) return reject(null); |
| 14 | + resolve(data); |
| 15 | + }); |
| 16 | + }); |
| 17 | + } |
| 18 | + |
| 19 | + delete(type: PlatformTypes, name: string) { |
| 20 | + return new Promise((resolve, reject) => { |
| 21 | + this.couchbaseService.getBucket().remove(`configuration_${type}_${name}`, (err, data) => { |
| 22 | + if (err) return reject(null); |
| 23 | + resolve(data); |
| 24 | + }); |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + update(type: PlatformTypes, name: string, configuration: Configuration) { |
| 29 | + return new Promise((resolve, reject) => { |
| 30 | + this.couchbaseService.getBucket().upsert(`configuration_${type}_${name}`, {configuration, type: `configuration_${type}`}, (err, data) => { |
| 31 | + if (err) return reject(null); |
| 32 | + resolve(data); |
| 33 | + }); |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + get(type: PlatformTypes, name: string) { |
| 38 | + return new Promise((resolve, reject) => { |
| 39 | + this.couchbaseService.getBucket().get(`configuration_${type}_${name}`, (err, data) => { |
| 40 | + if (err) return reject(null); |
| 41 | + console.log("get data: ", data); |
| 42 | + resolve(data.map((g) => g.value)); |
| 43 | + }); |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + getAll(type: PlatformTypes) { |
| 48 | + return new Promise((resolve, reject) => { |
| 49 | + const query = ViewQuery |
| 50 | + .from(`configuration_${type}`, 'getAll') |
| 51 | + .stale(ViewQuery.Update.BEFORE); |
| 52 | + this.couchbaseService.getBucket().query(query, (err, data) => { |
| 53 | + if (err) return reject(null); |
| 54 | + resolve(data.map((g) => g.value)); |
| 55 | + }); |
| 56 | + }); |
| 57 | + } |
| 58 | +} |
0 commit comments