75 lines
1.9 KiB
TypeScript
75 lines
1.9 KiB
TypeScript
import { Body, Controller, Get, Injectable, Post } from '@nestjs/common';
|
|
|
|
import { ApiTags } from '@nestjs/swagger';
|
|
import { Public } from 'src/core/guards';
|
|
import * as Nano from 'nano';
|
|
import { CreateUserPrivilegeDto } from 'src/modules/user-related/user-privilege/infrastructure/dto/create-user-privilege.dto';
|
|
import { ConfigService } from '@nestjs/config';
|
|
|
|
@ApiTags(`couch`)
|
|
@Controller('v1/couch')
|
|
@Public()
|
|
@Injectable()
|
|
export class CouchDataController {
|
|
constructor(private configService: ConfigService) {}
|
|
|
|
get nanoInstance() {
|
|
const couchConfiguration = this.configService.get<string>('COUCHDB_CONFIG');
|
|
return Nano(couchConfiguration);
|
|
}
|
|
|
|
@Post()
|
|
async createDoc(@Body() entity: CreateUserPrivilegeDto) {
|
|
try {
|
|
const n = this.nanoInstance;
|
|
await n.db.create(entity.name);
|
|
} catch (error) {
|
|
console.log(error, 'dsa');
|
|
}
|
|
}
|
|
|
|
@Post('doc')
|
|
async createDocs(@Body() entity: CreateUserPrivilegeDto) {
|
|
try {
|
|
const nano = this.nanoInstance;
|
|
const people = nano.db.use('string');
|
|
console.log(await people.info(), entity);
|
|
// const data = {
|
|
// id: '1212',
|
|
// name: 'dsadas',
|
|
// };
|
|
// await people.insert(data)
|
|
|
|
people.changesReader
|
|
.start({})
|
|
.on('change', (change) => {
|
|
console.log(change);
|
|
})
|
|
.on('batch', (b) => {
|
|
console.log('a batch of', b.length, 'changes has arrived');
|
|
})
|
|
.on('seq', (s) => {
|
|
console.log('sequence token', s);
|
|
})
|
|
.on('error', (e) => {
|
|
console.error('error', e);
|
|
});
|
|
} catch (error) {
|
|
console.log(error, 'dsa');
|
|
}
|
|
}
|
|
|
|
@Get()
|
|
async getDoc() {
|
|
try {
|
|
const n = this.nanoInstance;
|
|
const people = n.db.get('user');
|
|
|
|
return people;
|
|
// return people.get();
|
|
} catch (error) {
|
|
console.log(error, 'dsa');
|
|
}
|
|
}
|
|
}
|