<commit_before>'use strict';

const constants = require('../constants');
const debug = require('./debug')('nano');

exports.authenticate = function (nano, user, password) {
    return new Promise((resolve, reject) => {
        debug.trace('auth ' + user);
        nano.auth(user, password, function (err, body, headers) {
            if (err) {
                debug.warn('auth failed');
                return reject(err);
            }
            if (headers && headers['set-cookie']) {
                debug.trace('auth success');
                return resolve(headers['set-cookie']);
            }
            reject(new Error('cookie auth not supported'));
        });
    });
};

exports.getDatabase = function (nano, database) {
    return new Promise((resolve, reject) => {
        debug.trace('getDatabase ' + database);
        nano.db.get(database, function (err) {
            if (err) {
                if (err.reason === 'no_db_file') {
                    debug.trace('database not found');
                    return resolve(false);
                }
                debug.warn('getDatabase failed');
                return reject(err);
            }
            debug.trace('database exists');
            resolve(true);
        });
    });
};

exports.createDatabase = function (nano, database) {
    return new Promise((resolve, reject) => {
        debug.trace('createDatabase ' + database);
        nano.db.create(database, function (err) {
            if (err) {
                debug.warn('create failed');
                return reject(err);
            }
            debug('database created');
            resolve();
        });
    });
};

exports.getDocument = function (db, docID, options) {
    return new Promise((resolve, reject) => {
        debug.trace(`getDocument ${docID}`);
        db.get(docID, options, function (err, result) {
            if (err) {
                if (err.statusCode === 404 && (err.reason === 'missing' || err.reason === 'deleted')) {
                    debug.trace('document missing');
                    return resolve(null);
                }
                debug.warn('getDocument failed');
                return reject(err);
            }
            debug.trace('found document');
            resolve(result);
        });
    });
};

exports.insertDocument = function (db, doc) {
    return new Promise((resolve, reject) => {
        debug.trace(`insertDocument with _id ${doc._id}`);
        db.insert(doc, function (err, body) {
            if (err) return reject(err);
            debug.trace(`document inserted (${body.id})`);
            resolve(body);
        });
    });
};

exports.queryView = function (db, view, params, options) {
    options = options || {};
    if (!options.hasOwnProperty('reduce')) {
        options.reduce = false;
    }
    return new Promise((resolve, reject) => {
        debug.trace(`queryView ${view}`);
        db.view(constants.DESIGN_DOC_NAME, view, params, function (err, body) {
            if (err) return reject(err);
            if (options.onlyValue) {
                resolve(body.rows.map(row => row.value));
            } else if (options.onlyDoc) {
                resolve(body.rows.map(row => row.doc));
            } else {
                resolve(body.rows);
            }
        });
    });
};

exports.destroyDatabase = function (nano, dbName) {
    return new Promise((resolve, reject) => {
        debug('destroy database ' + dbName);
        nano.db.destroy(dbName, function (err, body) {
            if (err) return reject(err);
            resolve(body);
        });
    });
};

exports.destroyDocument = function (db, docId, revId) {
    debug.trace('destroy document');
    if (!revId) {<commit_msg>fix(nanoPromise): reduce param for views should be false by default<commit_after>'use strict';

const constants = require('../constants');
const debug = require('./debug')('nano');

exports.authenticate = function (nano, user, password) {
    return new Promise((resolve, reject) => {
        debug.trace('auth ' + user);
        nano.auth(user, password, function (err, body, headers) {
            if (err) {
                debug.warn('auth failed');
                return reject(err);
            }
            if (headers && headers['set-cookie']) {
                debug.trace('auth success');
                return resolve(headers['set-cookie']);
            }
            reject(new Error('cookie auth not supported'));
        });
    });
};

exports.getDatabase = function (nano, database) {
    return new Promise((resolve, reject) => {
        debug.trace('getDatabase ' + database);
        nano.db.get(database, function (err) {
            if (err) {
                if (err.reason === 'no_db_file') {
                    debug.trace('database not found');
                    return resolve(false);
                }
                debug.warn('getDatabase failed');
                return reject(err);
            }
            debug.trace('database exists');
            resolve(true);
        });
    });
};

exports.createDatabase = function (nano, database) {
    return new Promise((resolve, reject) => {
        debug.trace('createDatabase ' + database);
        nano.db.create(database, function (err) {
            if (err) {
                debug.warn('create failed');
                return reject(err);
            }
            debug('database created');
            resolve();
        });
    });
};

exports.getDocument = function (db, docID, options) {
    return new Promise((resolve, reject) => {
        debug.trace(`getDocument ${docID}`);
        db.get(docID, options, function (err, result) {
            if (err) {
                if (err.statusCode === 404 && (err.reason === 'missing' || err.reason === 'deleted')) {
                    debug.trace('document missing');
                    return resolve(null);
                }
                debug.warn('getDocument failed');
                return reject(err);
            }
            debug.trace('found document');
            resolve(result);
        });
    });
};

exports.insertDocument = function (db, doc) {
    return new Promise((resolve, reject) => {
        debug.trace(`insertDocument with _id ${doc._id}`);
        db.insert(doc, function (err, body) {
            if (err) return reject(err);
            debug.trace(`document inserted (${body.id})`);
            resolve(body);
        });
    });
};

exports.queryView = function (db, view, params, options) {
    params = params || {};
    options = options || {};
    if (!params.hasOwnProperty('reduce')) {
        params.reduce = false;
    }
    return new Promise((resolve, reject) => {
        debug.trace(`queryView ${view}`);
        db.view(constants.DESIGN_DOC_NAME, view, params, function (err, body) {
            if (err) return reject(err);
            if (options.onlyValue) {
                resolve(body.rows.map(row => row.value));
            } else if (options.onlyDoc) {
                resolve(body.rows.map(row => row.doc));
            } else {
                resolve(body.rows);
            }
        });
    });
};

exports.destroyDatabase = function (nano, dbName) {
    return new Promise((resolve, reject) => {
        debug('destroy database ' + dbName);
        nano.db.destroy(dbName, function (err, body) {
            if (err) return reject(err);
            resolve(body);
        });
    });
};

exports.destroyDocument = function (db, docId, revId) {
    debug.trace('destroy document');
    if (!revId) {
{'fraction_non_alphanumeric': 0.14910536779324055, 'fraction_numerical': 0.0007952286282306163, 'mean_word_length': 1.8315196998123828, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1968314', 'n_tokens_mistral': 2026, 'n_tokens_neox': 1989, 'n_words': 457}