<commit_before>import cuid from './cuid.js';

import {CHANGES_DB_STORE_NAME} from './constants.js';
import * as IndexedDB from './indexeddb_connector.js';
import {createUpdateChangeObject, createRemoveChangeObject} from './change_object_helpers.js';

export default class Collection {
  constructor(collectionName, dbName, dbCollections) {
    this.collectionName = collectionName;
    this.dbName = dbName;
    this.dbCollections = dbCollections;
  }

  save(data) {
    return new Promise((resolve, reject) => {
      IndexedDB.open(this.dbName, this.dbCollections).then((openDB) => {
        const requestErrors = [];

        function onTransactionError(e) {
          requestErrors.push(e);
          openDB.close();
          reject(requestErrors);
        }

        function onTransactionComplete() {
          openDB.close();
          resolve(data._id);
        }

        const transaction = IndexedDB.createReadWriteTransaction(openDB, [CHANGES_DB_STORE_NAME, this.collectionName], onTransactionComplete, onTransactionError);

        const changeObjectsStore = transaction.objectStore(CHANGES_DB_STORE_NAME);
        const objectStore = transaction.objectStore(this.collectionName);

        if (!data._id) {
          data._id = cuid();
        }
        const changeObject = createUpdateChangeObject(this.collectionName, data);

        const changeObjectPromise = IndexedDB.save(changeObjectsStore, changeObject);
        const savePromise = IndexedDB.save(objectStore, data);

        Promise.all([changeObjectPromise, savePromise]).catch((e) => {
          requestErrors.push(e);
        });
      }).catch(function(e) {
        reject(e);
      });
    });
  }

  remove(id) {
    return new Promise((resolve, reject) => {
      IndexedDB.open(this.dbName, this.dbCollections).then((openDB) => {
        const requestErrors = [];

        function onTransactionError(e) {
          requestErrors.push(e);
          openDB.close();
          reject(requestErrors);
        }

        function onTransactionComplete() {
          openDB.close();
          resolve();
        }

        const transaction = IndexedDB.createReadWriteTransaction(openDB, [CHANGES_DB_STORE_NAME, this.collectionName], onTransactionComplete, onTransactionError);

        const changeObjectsStore = transaction.objectStore(CHANGES_DB_STORE_NAME);
        const objectStore = transaction.objectStore(this.collectionName);

        const changeObject = createRemoveChangeObject(this.collectionName, id);

        const changeObjectPromise = IndexedDB.save(changeObjectsStore, changeObject);
        const removePromise = IndexedDB.remove(objectStore, id);
        Promise.all([changeObjectPromise, removePromise]).catch((e) => {
          requestErrors.push(e);
        });
      });
    });
  }

  getAll() {
    return IndexedDB.open(this.dbName, this.dbCollections).then((openDB) => {
      const transaction = IndexedDB.createReadTransaction(openDB, [this.collectionName]);
      const objectStore = transaction.objectStore(this.collectionName);
      return IndexedDB.getAll(objectStore).then((data) => {
        openDB.clos();
        return data;
      }).catch((err) => {
        openDB.clos();
        return err;
      });
    });
  }

  getOne(id) {
    return IndexedDB.open(this.dbName, this.dbCollections).then((openDB) => {
      const transaction = IndexedDB.createReadTransaction(openDB, [this.collectionName]);
      const objectStore = transaction.objectStore(this.collectionName);
      return IndexedDB.getOne(objectStore, id).then((data) => {
        openDB.close();
        return data;
      }).catch((err) => {
        openDB.close();
        return err;
      });
    });
  }
}<commit_msg>Refactor<commit_after>import cuid from './cuid.js';

import {CHANGES_DB_STORE_NAME} from './constants.js';
import * as IndexedDB from './indexeddb_connector.js';
import {createUpdateChangeObject, createRemoveChangeObject} from './change_object_helpers.js';

export default class Collection {
  constructor(collectionName, dbName, dbCollectionNames) {
    const containsCollectionName = dbCollectionNames.indexOf(collectionName);
    const containsChangeCollectionName = dbCollectionNames.indexOf(CHANGES_DB_STORE_NAME);

    if (containsCollectionName === -1) {
      throw Error('Collection name is not in the collections list');
    } else if (containsChangeCollectionName === -1) {
      throw Error('Change collection is not in the collections list');
    } else {
      this.collectionName = collectionName;
      this.dbName = dbName;
      this.dbCollectionNames = dbCollectionNames;
    }
  }

  save(data) {
    return new Promise((resolve, reject) => {
      IndexedDB.open(this.dbName, this.dbCollectionNames).then((openDB) => {
        const requestErrors = [];

        function onTransactionError(e) {
          requestErrors.push(e);
          openDB.close();
          reject(requestErrors);
        }

        function onTransactionComplete() {
          openDB.close();
          resolve(data._id);
        }

        const transaction = IndexedDB.createReadWriteTransaction(openDB, [CHANGES_DB_STORE_NAME, this.collectionName], onTransactionComplete, onTransactionError);

        const changeObjectsStore = transaction.objectStore(CHANGES_DB_STORE_NAME);
        const objectStore = transaction.objectStore(this.collectionName);

        if (!data._id) {
          data._id = cuid();
        }
        const changeObject = createUpdateChangeObject(this.collectionName, data);

        const changeObjectPromise = IndexedDB.save(changeObjectsStore, changeObject);
        const savePromise = IndexedDB.save(objectStore, data);

        Promise.all([changeObjectPromise, savePromise]).catch((e) => {
          requestErrors.push(e);
        });
      }).catch(function(e) {
        reject(e);
      });
    });
  }

  remove(id) {
    return new Promise((resolve, reject) => {
      IndexedDB.open(this.dbName, this.dbCollectionNames).then((openDB) => {
        const requestErrors = [];

        function onTransactionError(e) {
          requestErrors.push(e);
          openDB.close();
          reject(requestErrors);
        }

        function onTransactionComplete() {
          openDB.close();
          resolve();
        }

        const transaction = IndexedDB.createReadWriteTransaction(openDB, [CHANGES_DB_STORE_NAME, this.collectionName], onTransactionComplete, onTransactionError);

        const changeObjectsStore = transaction.objectStore(CHANGES_DB_STORE_NAME);
        const objectStore = transaction.objectStore(this.collectionName);

        const changeObject = createRemoveChangeObject(this.collectionName, id);

        const changeObjectPromise = IndexedDB.save(changeObjectsStore, changeObject);
        const removePromise = IndexedDB.remove(objectStore, id);
        Promise.all([changeObjectPromise, removePromise]).catch((e) => {
          requestErrors.push(e);
        });
      });
    });
  }

  getAll() {
    return IndexedDB.open(this.dbName, this.dbCollectionNames).then((openDB) => {
      const transaction = IndexedDB.createReadTransaction(openDB, [this.collectionName]);
      const objectStore = transaction.objectStore(this.collectionName);
      return IndexedDB.getAll(objectStore).then((data) => {
        openDB.close();
        return data;
      }).catch((err) => {
        openDB.close();
        return Promise.reject(err);
      });
    });
  }

  getOne(id) {
    return IndexedDB.open(this.dbName, this.dbCollectionNames).then((openDB) => {
      const transaction = IndexedDB.createReadTransaction(openDB, [this.collectionName]);
      const objectStore = transaction.objectStore(this.collectionName);
      return IndexedDB.getOne(objectStore, id).then((data) => {
        openDB.close();
        return data;
      }).catch((err) => {
        openDB.close();
        return Promise.reject(err);
      });
    });
  }
}
{'fraction_non_alphanumeric': 0.11919524231304568, 'fraction_numerical': 0.00025306845501708213, 'mean_word_length': 3.04503582395087, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '5677572', 'n_tokens_mistral': 2180, 'n_tokens_neox': 2147, 'n_words': 372}