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

dao2express

v0.0.2

Published

dao for mongodb

Downloads

2

Readme

dao2express

Automatic generated APIs are great. I am a big fan of projects like JSON-server and sails.js. The intend for this project is to set the express development first.

On the other hand, I strongly believe in dao modules or some kind of data source definition with common interface. Modules that access each a single collection on a database and provides standard methods to retrieve data from that resource.

In first place, this project is designed to use daos prepared by tmysqlpromisedao and tmongodao, a library to provide dao-modules to access the mysql and mongo database. But it is of course easy to implement a dao by hand, with custom logic.

  • getBy[fieldName] functions to load entries by given values
  • save insert remove methods to manipulate the entries in that table.
  • fetch methods to load data from related collections and extending the given entries.

Utilizing these conventions dao2express can provide a RESTful API not just for mysql but also for any other data resource that can be accessed through a dao.

Usage

  1. Install the dao2express module to your express application.
  2. Prepare your dao, following the conventions.
  3. generate a router using the only function exposed by this module and mount it to your app
    var app = new Express();
    app.use('/user', dao2express(daos.userDao));
  4. start the app and access the endpoint.

exposed API

The generated API provides the following endpoints

  • GET / - list all using the dao's getAll methods

    • queryParams:
    • q=[word] for fulltext search (only using tmysqlpromisedao)
    • _order=[fieldName] sort the result by the given field name
    • _direction=[asc|desc] to order forward and backward
    • _fetch=[fieldnames] commaseparated names, naming related data to extend the loaded Items using the dao's fetch methods
    • _page=[number] the page of the resultset you want to receive
    • _pagesize=[number] how many items to receive
    • [propname]=[modifier][value]
      • propname the name of the field
      • value the value to compare with
      • modifier:
        • no modifier: the value need to be equal to the given
        • < the value need to be lower
        • the value need to be higher (alphabetic, numbers, dates)

  • GET /:id - retrieve a single item by Id using the daos getById method.

  • POST / to create a new entry in the collection. uses the daos insert method to add a new item to the table. the items properties can be send using post or the URLs querystring

  • PUT /:id/ - to update an item uses the daos save method to update an entry of the table. the properties can be send using post or querystring

  • DELETE /:id to remove an item from the collection.

Options

Providing an API which is as powerful as very risky, but this API generator is meant to be used in production. So there are several ways to implement authentication validation and trigger further methods like logging clearing related data, adding statistics and/or handle de-normalization.

Using dap2express, you have two ways to change the generated API. One way is the direct way, as you would do it with any express middleware. Handle the route before the API is utilized. Add logging and general authentication, in fact anything you could imagine. Or implement yourself.

The second is for convenience. By passing an options object as second parameter to the daoToRestRouter - method. The options are:

  • inputFilter a function that takes (express request, newObjectprops, oldObject).
    • The context is always there.
    • newObjectprops is there if the entry should get updated or is new.
    • oldObject is the object before update or befor delete. if this function return false, the optiation will not happen.
  • outputFilter is a function to manipulate readed object before delivering it to the client.
    • context the exoress req
    • object the object the user has requested. return what ever the user should get.
  • maxPageSize the maximum result size that the client can request. (int) default 100
  • defaultPageSize if the client is not providing a pagesize, in the query, he will get this one.
  • allowChangesOnGet allow to use the changes using just get requests. defaut true. set this to false, to disable /create, /delete/:id, /update/:id for get request
  • searchableFields array of fields that can be used for filters
  • fulltextFields array of fields that are used for fulltext search using the q parameter
  • fetchableFields array of related data that can be queried by the client. by the _fetch option.
  • defaultOrder the default order in search

Call To Action

Now it is time to implement some cool application using - tasyncexpress to use modern Javascript with async await. - tmysqlpromisedao to implement a standard way for data access in your application. - tmongodao to implement a standard way for data access in your application. - and now dao2express to get your API up and running as fast as you can and focus.

Developer

Tobias Nickel, German Software Engineer and Tech Lead located in Shanghai.

alt text