Guest User

Untitled

a guest
Dec 13th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. import Client from 'some-database-api';
  2. import { connectionInfo } from './config';
  3.  
  4. class AsyncDatabaseClient {
  5. constructor() {
  6. this._connection = null;
  7. this._initialized = Client.connect(connectionInfo).then(c => this.connection = c);
  8.  
  9. return new Proxy(this, {
  10. get(target, prop, receiver) {
  11. const value = Reflect.get(target, prop, receiver);
  12. if (typeof value === 'function') {
  13. return async (...args) => {
  14. await target._initialized;
  15.  
  16. return value.apply(receiver, args);
  17. };
  18. }
  19.  
  20. return value;
  21. }
  22. });
  23. }
  24.  
  25. static _single = new AsyncDatabaseClient();
  26.  
  27. static get Single() {
  28. return this._single;
  29. }
  30.  
  31. async fetch(query) {
  32. return this._connection.makeRequest(query);
  33. }
  34. }
  35.  
  36. export default AsyncDatabaseClient.Single;
Add Comment
Please, Sign In to add comment