Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Client from 'some-database-api';
- import { connectionInfo } from './config';
- class AsyncDatabaseClient {
- constructor() {
- this._connection = null;
- this._initialized = Client.connect(connectionInfo).then(c => this.connection = c);
- return new Proxy(this, {
- get(target, prop, receiver) {
- const value = Reflect.get(target, prop, receiver);
- if (typeof value === 'function') {
- return async (...args) => {
- await target._initialized;
- return value.apply(receiver, args);
- };
- }
- return value;
- }
- });
- }
- static _single = new AsyncDatabaseClient();
- static get Single() {
- return this._single;
- }
- async fetch(query) {
- return this._connection.makeRequest(query);
- }
- }
- export default AsyncDatabaseClient.Single;
Add Comment
Please, Sign In to add comment