SHARE
TWEET

Untitled

a guest Jan 21st, 2020 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   connect: async (url, schema, modelName, key, dbOptions, schemaOptions, algorithm) => {
  2.     if (algorithm) setAlgorithm(algorithm);
  3.     if (!key) throw new PersonalDataStorageError("Key must not be undefined!");
  4.     setKey(key);
  5.     await generateModel(schema, modelName, schemaOptions);
  6.     await connect(url, dbOptions);
  7.     return setIndexSettings(schema, dbOptions, key);
  8.   },
  9.  
  10.   close: () => {
  11.     disconnect();
  12.   },
  13.  
  14.   isConnected: () => {
  15.     return getConnect();
  16.   },
  17.  
  18.   upsert: async (data, opts) => {
  19.     if (!getConnect()) throw new PersonalDataStorageError("No connection to MongoDb!");
  20.     if (opts && typeof opts !== "object"){
  21.       console.log("\x1b[33m%s\x1b[0m","\n[personal-data-storage] - WARNING - options must be as object. Applied options by default\n");
  22.       opts = undefined;
  23.     }
  24.     deleteUndefinedKey(data);
  25.     if (!data) throw new PersonalDataStorageError("No data for upsert");
  26.     await validateData(data);
  27.     return data.id ? update(data.id, data, opts) : insert(data, opts); // promise
  28.   },
  29.  
  30.   get: async (id) => {
  31.     if (!id) throw new PersonalDataStorageError("No id to find");
  32.     if (!getConnect()) throw new PersonalDataStorageError("No connection to MongoDb!");
  33.     if (!ObjectId.isValid(id)) throw new PersonalDataStorageError("id is not ObjectId!", 400);
  34.     return getDataByID(id); //promise
  35.   },
  36.  
  37.   find: async (key, value) => {
  38.     if (!key) throw new PersonalDataStorageError("No key to find");
  39.     if (!value) throw new PersonalDataStorageError("No value to find");
  40.     return find(key, value); //promise
  41.   },
  42.  
  43.   delete: async (id) => {
  44.     if (!id) throw new PersonalDataStorageError("No id to delete");
  45.     if (!getConnect()) throw new PersonalDataStorageError("No connection to MongoDb!");
  46.     if (!ObjectId.isValid(id)) throw new PersonalDataStorageError("id is not ObjectId!", 400);
  47.     return deleteById(id); //promise
  48.   },
  49.  
  50.   startTransaction: () => {
  51.     return new Promise(async (resolve, reject) => {
  52.       const session = await getModel().startSession();
  53.       session.startTransaction();
  54.       resolve(session);
  55.     });
  56.   },
  57.  
  58.   commitTransaction: (session) => {
  59.     return new Promise(async (resolve, reject) => {
  60.       await session.commitTransaction();
  61.       session.endSession();
  62.       resolve();
  63.     });
  64.   },
  65.  
  66.   abortTransaction: (session) => {
  67.     return new Promise(async (resolve, reject) => {
  68.       await session.abortTransaction();
  69.       session.endSession();
  70.       resolve();
  71.     });
  72.   }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top