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

mongo-base-crud

v0.3.0

Published

Class to handler access and handler database

Downloads

306

Readme

mongo-base-crud

Lib to simple handler singleton classes em typescript

Install

npm i mongo-base-crud

Usage

Create .env file with

MONGO_URL="mongodb://admin:admin@localhost:27017"
MONGO_DB=test
MONGO_PREFIX_NAME=test_

MONGO_URL full url with a mongo connection MONGO_DB database name MONGO_PREFIX_NAME prefix name to any database that will be created. To keep simple to deal with enviroment or whitelabel projects

Using to create repository class

import { Singleton } from "typescript-singleton";
import BaseCrud from "mongo-base-crud";

export class MyClassExample extends BaseCrud<{id:string, name:string}> {
  public static instance(): MyClassExample {
    return Singleton.getInstance<MyClassExample>(`MyClassExample`, MyClassExample);
  }
  constructor() {
    super("my_collection", "my_database", { clinicAlias: true, unitAlias: true });
  }
}

Using to create repository function to get generic BaseCrud to deal with a collection

function getCrud(){
  const genericCrud:BaseCrud<MyDTO> = BaseCrud<MyDTO>.getInstance("collectionName", "dbName", { type: 1 });
  return genericCrud;
}
getCrud().find().then(console.log)

Another examples

// Example to add an object

const newObj = { id: "1", name: "John Doe" };
MyClassExample.instance().add(newObj).then(result => {
  console.log("Object added:", result);
}).catch(error => {
  console.error("Error adding object:", error);
});

//if you do not pass id, it will auto created

const newObj = { name: "John Doe" };
MyClassExample.instance().save()

// Example to find objects
MyClassExample.instance().find().then(result => {
  console.log("Objects found:", result);
}).catch(error => {
  console.error("Error finding objects:", error);
});

// Example to delete an object
const objectId = "1";
MyClassExample.instance().delete(objectId).then(result => {
  console.log("Object deleted:", result);
}).catch(error => {
  console.error("Error deleting object:", error);
});

MyClassExample.instance().find().then(result=>{console.log(result)});

Find

Find params

filter

Optional, object to find exactly filter. If you do not pass, no filter will be used and will be get all data

select

Data to return just some params of full object data fields

ex: {id:true, name:true},

skip

ex: 0

for pagination, start in first, optional, default 0

limit

ex: 10

for pagination, get just 10 or less, optional, default 10, get all

orderBy

ex: "name"

order by name, optional, default undefined

direction

ex: "asc" or "desc"

optional, default asc if is setted orderBy

searchValue

ex: "ab" to find abajour or ában or foolabicon

search by part of string, optional, default undefined, do not filter

searchFields

ex: ["name"]

the fields that a search will be use to search, optional, no default, if you do not pass, nothing will be search

Find example

export interface MyUserDto{
  id: string,
  name: string,
  cateogry: string,
  birthdate: Date,
  lastname: string,
  active: boolean
}
export async function serviceTofind(active:boolean, category:string, freeQuery?:string):Promise<List<MyUserDto>>{
  const filter: Partial<MyUserDto> = {
    active,
    category
  }
  const resultData:List = MyClassExample.instance().find(
    filter, //filter active and category identical data
    {id:true, name:true}, // return just name and id
    skip = 0, // for pagination, start in first, optional, default 0
    limit = 20, // for pagination, get just 10 or less, optional, default 10, get alll
    orderBy = "name", // order by name, optional, default undefined
    direction = "asc", //optional, default asc if is setted orderBy
    searchValue = freeQuery, //search by part of string, optional, default undefined, do not filter
    searchFields = freeQuery?["name", "lastname"]: undefined // the fields that a search will be use to search, optional, no default, if you do not pass nothing, nothing will be search
  )
  const total:number  = resultData.total;
  const listData:any[] = resultData.data;
  return resultData;
}