npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@thingsup/greenlock-sql-manager

v1.0.17

Published

Greenlock Manager and Store for SQL Databases

Downloads

29

Readme

@thingsup/greenlock-sql-manager

A database-driven Greenlock storage plugin and manager.

This library is created and maintained by Thingsup

This library is compatible with Express Framework

Lot of codebase is taken from greenlock-store-sequelize

Features

  • Many Supported SQL Databases
    • [x] PostgreSQL (best)
    • [x] SQLite3 (easiest)
    • [x] Microsoft SQL Server (mssql)
    • [x] MySQL, MariaDB
  • Works on all platforms
    • [x] Mac, Linux, VPS
    • [x] AWS, Heroku, Akkeris, Docker
    • [x] Windows

Installation

npm i @thingsup/greenlock-sql-manager

You also have to install the database ORM Library which you will be using.

# One of the following:
npm install  pg pg-hstore # Postgres
npm install  mysql2
npm install  mariadb
npm install  sqlite3
npm install  tedious # Microsoft SQL Server

Usage

Running Express App

To use, with express.

const express = require("express");
const app = express();
const glx = require("@thingsup/greenlock-sql-manager");
glx
  .init({
    greenlockDefaults: {
      maintainerEmail: "[email protected]",
      cluster: false,
      packageRoot: __dirname,

      // Options passed to greenlock-express library init function
      // Most of the options are already pre-configured
    },
    managerDefaults: {
      subscriberEmail: "[email protected]",

      // Options passed to greenlock-manager or the options which are passed in config.json of greenlock-express library
    },
    storeDefaults: {
      prefix: "<CUSTOM_PREFIX>",
      storeDatabaseUrl: "<DB_URL>",
    }, // Options passed to greenlock-sequelize with one additional argument prefix
  })
  .serve(app);

Manage Sites

We have created a handlers function to easily manage your sites stored in database.

const storeOptions = {
  // Pass the same objects that you have passed to storeDefaults
};
const glx = require("@thingsup/greenlock-sql-manager");
const { add, getCertificates, getDB, remove, removeAll, getAll } =
  glx.handles(storeOptions);
// List of the functions that we currently support.

Get list of Domains Added

To get all sites,

try {
  console.log(await getAll());
  // List of all domains which is currently added ie ['abc.com','abc2.com','abc3.com']
} catch (err) {
  console.log("Unable to get sites");
}

Adding Sites

To add a site,

try {
  await add({
    subject: "example.com",
    altnames: ["www.example.com", "example.com"],
  });
  console.log("Site added");
} catch (err) {
  console.log("Unable to add sites");
}

Remove a site

try {
  await remove("abc.com");
  // Remove this site from db.
} catch (err) {
  console.log("Unable to get sites");
}

Remove all sites

try {
  await removeAll();
  // Remove all the sites.
} catch (err) {
  console.log("Unable to get sites");
}

Getting Certificates and keys of a site

Get keys and certificates necessary to run https server

try {
  const certOpts = await getCertificates("example.com");
  /* returns {
     ca: '....',
     key: '...', // Private Key
     cert: '...'
   }  (certificate exist) || null (certicate not exist)
  */
} catch (err) {
  console.log("Error Occured");
}

Get Sequelize DB Instance

try {
  const db = await getDB();
} catch (err) {
  console.log("Error Occured");
}

Opening TCP/IP socket using TLS

const express = require("express");
const app = express();
const glx = require("@thingsup/greenlock-sql-manager");
let server = null;
const storeOptions = {
  prefix: "<CUSTOM_PREFIX>",
  storeDatabaseUrl: "<DB_URL>",
};

const PORT = 8888;
const startServer = (certificates) => {
  // Incase of renewal, we can either restart the whole process. or close the existing server and then start it with new certificate.
  if (server) {
    server.close(() => {
      server = require("tls").createServer(certificates, () => {});
      server.listen(PORT);
    });
  } else {
    server = require("tls").createServer(certificates, () => {});
    server.listen(PORT);
  }
};

const siteHandles = glx.handles(storeOptions);

const getCertificateAndRunServer = () => {
  siteHandles.getCertificate("yourdomain.com").then((certs) => {
    if (certs) {
      // Certificate exist
      startServer(certs);
    } else {
    } // certificate not exist. wait for the certificate issue.
  });
};

getCertificateAndRunServer();

glx
  .init({
    greenlockDefaults: {
      maintainerEmail: "[email protected]",
      cluster: false,
      packageRoot: __dirname,
      notify: (ev) => {
        if (ev.trim() === "cert_issue" || ev.trim() === "cert_renewal") {
          // Certificate is either issued or renewed now you have to change your socket's certificate
          getCertificateAndRunServer();
        }
      },
    },
    managerDefaults: {
      subscriberEmail: "[email protected]",
    },
    storeDefaults: storeOptions,
  })
  .serve(app);

Default Table Structure

This is the default table structure (Unless a prefix option is given) that's created.

CREATE TABLE `Keypairs` (
  `id` INTEGER PRIMARY KEY AUTOINCREMENT,
  `xid` VARCHAR(255) UNIQUE,
  `content` TEXT,
  `createdAt` DATETIME NOT NULL,
  `updatedAt` DATETIME NOT NULL);

CREATE TABLE `Domains` (
  `id` INTEGER PRIMARY KEY AUTOINCREMENT,
  `subject` VARCHAR(255) UNIQUE,
  `altnames` TEXT,
  `renewAt` DATE,
  `createdAt` DATETIME NOT NULL,
  `updatedAt` DATETIME NOT NULL);

CREATE TABLE `Certificates` (
  `id` INTEGER PRIMARY KEY AUTOINCREMENT,
  `subject` VARCHAR(255) UNIQUE,
  `cert` TEXT,
  `issuedAt` DATETIME,
  `expiresAt` DATETIME,
  `altnames` TEXT,
  `chain` TEXT,
  `createdAt` DATETIME NOT NULL,
  `updatedAt` DATETIME NOT NULL);

CREATE TABLE `Chains` (
  `id` INTEGER PRIMARY KEY AUTOINCREMENT,
  `xid` VARCHAR(255),
  `content` TEXT,
  `createdAt` DATETIME NOT NULL,
  `updatedAt` DATETIME NOT NULL,
  `CertificateId` INTEGER REFERENCES
  `Certificates` (`id`) ON DELETE SET NULL ON UPDATE CASCADE);