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-serdes-js

v0.0.3

Published

Types serializer and deserializer for MongoDB. It can be used in API project in order to deserialize data from requests and serialize it to response.

Downloads

16

Readme

Mongo Serdes JS

Types serializer and deserializer for MongoDB.

It can be used in API project in order to deserialize data from requests and serialize it to response.

First usage is in express-openapi-validator project usage when serDes is used.

Install

npm install mongo-serdes-js

Usage

####Import library

const {mongoSerDes} = require('mongo-serdes-js');

Serialize or deserialize data in Javascript

// Example with ObjectID
mongoSerDes.objectid.deserialize('5fec2665792afbcd812ace9b'); // return ObjectID object
const anObjectID = new ObjectID();
mongoSerDes.objectid.serialize(anObjectID);  // return the ObjectID as String

// Example with UUID (stored as Binary in MongoDB)
mongoSerDes.uuid.deserialize('7c239196-53b2-428c-8f1d-275e52c661f3'); // return a Binary BSON object
// Retrieve data with an UUID Binary field 
const myData = await myCollection.findOne({name : 'Pierre'});
mongoSerDes.uuid.serialize(myData.uuidField);  // return the ObjectID as String

express-openapi-validator usage

If you use serDes setting in middleware, you may want to :

  • deserialize requests string to Mongodb objects
  • serialize responses Mongodb objects to string

This serialization or deserialization is done according to each data format configuration in your OpenAPI description.

Example :

  • YAML description
paths:
  /users/{id}:
    get:
      parameters:
        - name: id
          in: path
          required: true
          schema:
            $ref: "#/components/schemas/ObjectId"
        - name: uuid
          in: path
          required: true
          schema:
            $ref: "#/components/schemas/UUID"
...

components:
  schemas:
    ObjectId:
      type: string
      format: objectid
      pattern: '^[0-9a-fA-F]{24}$'
    UUID:
      type: string
      format: uuid
      pattern: '^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$'
...
  • Your Express OpenAPI configuration with serDes setting including mongoSerDes formats. Formats must be added to unknownFormats
const {MongoSerDes, MongoSerDesFormats} = require('mongo-serdes-js');
const OpenApiValidator = require('express-openapi-validator');

app.use(OpenApiValidator.middleware({
    apiSpec: ...,
    validateRequests: {
      coerceTypes: true
    },
    validateResponses: {
      coerceTypes: true
    },
    serDes: [
      OpenApiValidator.baseSerDes.date,
      OpenApiValidator.baseSerDes.dateTime,
      MongoSerDes.objectid, // this configuration if we want to deserialize objectid in request and serialize it in response
      MongoSerDes.uuid.serializer, // this configuration if we only want to serialize on response
    ],
    unknownFormats: MongoSerDesFormats,
    // Or declare only used formats ===> unknownFormats: ['objectid', 'uuid'],
    // Or ===> unknownFormats: [ MongoSerDes.objectid.format, MongoSerDes.uuid.format],
  }));

License

MIT