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

database-wrapper

v1.0.2

Published

Abstract class that provides API to wrap any database instance/entity. It includes development methods that save a json file and the user is responsible for handling the production methods when the mode is production.

Downloads

6

Readme

database-wrapper

Abstract class that provides API to wrap any database instance/entity. It includes development methods that save a json file and the user is responsible for handling the production methods when the mode is production.

Install

npm i database-wrapper

or

yarn add database-wrapper

Example Use

Extending the class

//userdb.ts
import { DatabaseWrapper, Connection, WhereType } from "database-wrapper";
import { v4 } from "uuid";
import path from "path";

export interface UserInput {
  firstname: string;
  lastname: string;
  email: string;
  password: string;
}

//This could be the Regular Database object. for example a mongoosee Model or a micro-orm entity.
class User implements UserInput {
  id: string;
  name: string;
  email: string;
  firstname: string;
  lastname: string;
  password: string;
  constructor({ firstname, email, lastname, password }: UserInput) {
    this.id = v4();
    this.name = `${firstname} ${lastname}`;
    this.email = email;
    this.firstname = firstname;
    this.lastname = lastname;
    this.password = password;
  }
}

//Extend the abstract Class and add production Methods.
class UserDbWrapper extends DatabaseWrapper<User> {
  protected _connection: Connection<User>;
  protected _isProd: boolean;
  constructor() {
    super();
    const userPath = path.join(__dirname, "users.json");
    this._connection = new Connection<User>(userPath);
    this._isProd = process.env.NODE_ENV === "development";
  }

  create(userInput: UserInput) {
    return new User(userInput);
  }

  async find(
    where: WhereType<Omit<User, "surveys">>,
    _populate?: string[]
  ): Promise<User[]> {
    if (!this._isProd) {
      return this._findDev(where);
    } else {
      //...PROD CODE
    }
    throw new Error("Method not implemented.");
  }

  findById(id: string): Promise<User | null> {
    if (!this._isProd) {
      return this._findByIdDev(id);
    }
    throw new Error("Method not implemented.");
  }

  findOne(where: WhereType<Omit<User, "surveys">>): Promise<User | null> {
    if (!this._isProd) {
      return this._findOneDev(where);
    }
    throw new Error("Method not implemented.");
  }
  remove(where: WhereType<User>): Promise<this> {
    if (!this._isProd) {
      return this._removeDev(where);
    }
    throw new Error("Method not implemented.");
  }
  deleteOne(where: WhereType<User>): Promise<this> {
    if (!this._isProd) {
      return this._deleteOneDev(where);
    }
    throw new Error("Method not implemented.");
  }
  update(where: WhereType<User>, values: Partial<User>): Promise<User[]> {
    if (!this._isProd) {
      return this._updateDev(where, values);
    }
    throw new Error("Method not implemented.");
  }
  async updateOne(
    where: WhereType<User>,
    values: Partial<User>
  ): Promise<User | null> {
    if (!this._isProd) {
      return this._updateOneDev(where, values);
    }

    throw new Error("Method Not Implemented");
  }

  async save(user: User): Promise<this> {
    if (!this._isProd) {
      return this._saveDev(user);
    }
    throw new Error("Method Not Implemented");
  }
}

//Export an instance of the wrapper
export const UserDB = new UserDbWrapper();

Usage after creation.

// user.controller.ts
import { Router } from "express";
import { UserDB, UserInput } from "../userdb";

const Controller = Router();

//Find All
Controller.get("/", async (_req, res) => {
  const users = await UserDB.find({});

  res.json(users);
});

//Find by id
Controller.get("/:id", async (req, res) => {
  const id = req.params.id as string;
  const user = await UserDB.findById(id);
  res.json(user);
});

//Login
Controller.post("/login", async (req, res) => {
  const { email, password } = req.body;
  console.log(email);
  const user = await UserDB.findOne({
    email: email,
  });

  if (!user) {
    return res.json({
      message: "User Not Found.",
    });
  }
  if (user.password !== password) {
    return res.json({
      message: "Unauthorized",
    });
  }
  return res.json(user);
});

//Register
Controller.post("/", async (req, res) => {
  const data = req.body as UserInput;
  if (!data.email || !data.firstname || !data.password || !data.lastname) {
    res.status(400);
    return res.json({
      message: "Missing one of email, firstname, lastname, password.",
    });
  }
  const user = UserDB.create(data);
  await UserDB.save(user);
  return res.json(user);
});

//Update
Controller.put("/:id", async (req, res) => {
  const data = req.body;
  const updated = await UserDB.updateOne({ id: req.params.id }, data);
  res.json(updated);
});

export const UserController = Controller;