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

generic-repo

v2.1.0

Published

Generic repository for objects Edit

Downloads

13

Readme

generic-repo

Generic Repository for objects, based on knex.

Build Status

Install

$ npm i --save generic-repo

Usage

A persistable Class

A Class can be persisted if it satisfies the following criteria:

  • 1st argument of it's constructor accepts an Object that contains at least all the persistable properties.
  • It has a props property that contains all the props that should be persisted.

Property serialisation

Complex property values

If a prop is of type Object it will be converted into a JSON before inserting into the database. You should handle this case appropriately in your Class constructor (see children prop of User below).

Non-complex prop values

Non-complex values such as Number, String, Boolean are send to the database as-is.

class User {
  constructor(data) {
    this.props = {  
      id_user: data.id_user,
      name: data.name,
      // Some DB's might not support a JSON datatype.
      // Therefore we might also need to *parse* back the result.
      children: data.children
        ? typeof data.children === 'string'
          ? JSON.parse(data.children)
          : data.children
        : []
    }

    // Anything outside `props` will *not* be persisted
    // - e.g `age` property will not be persisted
    this.age = 15
  }

  getName() {
    return this.name
  }
}

A usage example using the above User Class:

const GenericRepo = require('generic-repo')
const knex = require('knex')({
  client: 'sqlite3',
  connection: { filename: './test_db.sqlite' }
})

const genericRepo = new GenericRepo({
  tableName: 'user',
  primaryKey: 'id_user',
  constructAs: data => new User(data)
})

genericRepo.upsert(knex, new User({
    id_user: 'lxkkfv',
    name: 'John Doe'
  }))
  .then(() => {
    return genericRepo.get(knex, { id_user: 'lxkkfv' })
  })
  .then(user => {
    console.log(user.getName()) // 'John Doe'
  })

Ensure that your DB already has the table/columns you declared when you instantiated the repo.

API

upsert(knex, instance)

  • Inserts the instance if it does not exist by primary key.
  • Updates the instance if it already exists by primary key.
genericRepo.upsert(knex, user).then(() => {
  console.log(result) // [1]
})

getAll(knex, filter)

  • If no filter is provided it returns all saved instances.
  • If a filter is provided it returns just the matched instances.
genericRepo.getAll(knex, { name: 'John Doe' }).then(users => {
  console.log(users[0].getName()) // logs 'John Doe'
})

get(knex, filter)

  • Returns an instance that matches the filter.
  • Returns undefined if no instance is found.
genericRepo.getAll(knex, { name: 'John Doe' }).then(user => {
  console.log(result) // logs the 'John Doe' instance
})

exists(knex, filter)

  • Returns true if a result is found, false otherwise.
genericRepo.exists(knex, { name: 'John Doe' }).then(result => {
  console.log(result) // logs `true`
})

del(knex, filter)

  • Removes/Deletes the instance that matches the filter.
  • Does nothing if no instance is found (no-op).
genericRepo.del(knex, { name: 'John Doe' }).then(() => {
  // Instance with name 'John Doe' was removed from the DB
})

Extend this repository

This module exports an ES6 Class which you can simply extend.

const GenericRepo = require('generic-repo')

class MySuperWowRepo extends GenericRepo {
  constructor() {
    super({
      tableName: 'employees',
      primaryKey: 'id_employee',
      constructAs: data => new Employee(data)
    })
  }

  getAllJohnDoes(knex) {
    return knex(this.tableName)
      .where({ name: 'John Doe' })
  }
}

Test

$ npm test

Authors

License

MIT