Socket
Book a DemoInstallSign in
Socket

singx

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

singx

> A tiny utility library with ts + batteries included to easily create singletons in JavaScript

latest
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

singl 🗝

A tiny utility library with ts + batteries included to easily create singletons in JavaScript

Usage

They say an example is worth more than 100 words 😉

Async

import { singletonAsync } from 'singl'

import mongoose from 'mongoose'

export const connection = singletonAsync<typeof mongoose>(
   // This function initializes the singleton
   async () => {
      const instance = await mongoose.connect(
         'mongodb://some-arbitary-connection-uri:27017/test',
         {
            useNewUrlParser: true,
            useUnifiedTopology: true
         }
      )
      return instance
   },
   // This optional function is responsible for specifing
   // wether the instance should re-init or not
   async instance => !instance.connection
)

Sync

import { singleton } from 'singl'

import Redis from 'ioredis'

export const redis = singleton<Redis.Redis>(
   // This function initializes the singleton
   () => {
      return new Redis()
   },
   // This optional function is responsible for specifing
   // wether the instance should re-init or not
   instance => instance.status === 'connected'
)

FAQs

Package last updated on 28 Apr 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts