import path from 'path';
import DataStore from '@seald-io/nedb';
import { getResponseData } from './common.js';
export default class TableNameService {
    db;
    constructor(dbpath) {
        this.db = new DataStore({
            filename: path.resolve(dbpath, 'database/tablename.db'),
            autoload: true,
        });
        this.db.ensureIndex({
            fieldName: 'entityName',
            unique: true,
        });
    }
    async create(params) {
        const pageData = { ...params };
        const result = await this.db.insertAsync(pageData);
        const { _id } = result;
        await this.db.updateAsync({ _id }, { $set: { id: _id } });
        result.id = result._id;
        return getResponseData(result);
    }
    async update(id, params) {
        await this.db.updateAsync({ _id: id }, { $set: params });
        const result = await this.db.findOneAsync({ _id: id });
        return getResponseData(result);
    }
    async list() {
        const result = await this.db.findAsync({});
        return getResponseData(result);
    }
    async detail(id) {
        const result = await this.db.findOneAsync({ _id: id });
        return getResponseData(result);
    }
    async findByEntityName(entityName) {
        const result = await this.db.findOneAsync({ entityName });
        return getResponseData(result);
    }
    async delete(id) {
        const result = await this.db.findOneAsync({ _id: id });
        await this.db.removeAsync({ _id: id });
        return getResponseData(result);
    }
}
