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

@keystonejs/fields-mongoid

v9.2.2

Published

KeystoneJS MongoId Field Type

Downloads

3,355

Readme

MongoId

This is the last active development release of this package as Keystone 5 is now in a 6 to 12 month active maintenance phase. For more information please read our Keystone 5 and beyond post.

This field allows arbitrary Mongo ObjectId fields to be added to your lists.

It supports the core Mongoose and Knex adapters:

  • On Mongoose the native Mongo ObjectId schema type is used.
  • On Knex a 24 character string field is added to the schema. This resolves down to varchar(24), character varying(24) or similar, depending on the underlying DB platform. Values stored are forced to lowercase on read and write to avoid issues with case-sensitive string comparisons. See the casing section for details.

Usage

const { Keystone } = require('@keystonejs/keystone');
const { MongoId } = require('@keystonejs/fields-mongoid');

const keystone = new Keystone(/* ... */);

keystone.createList('Product', {
  fields: {
    name: { type: Text },
    oldId: { type: MongoId },
    // ...
  },
});

Config

| Option | Type | Default | Description | | :----------- | :-------- | :------ | :-------------------------------------------------------------- | | isRequired | Boolean | false | Does this field require a value? | | isUnique | Boolean | false | Adds a unique index that allows only unique values to be stored |

Admin UI

We reuse the interface implementation from the native Text field.

GraphQL

MongoId fields use the ID type in GraphQL.

Input fields

| Field name | Type | Description | | :--------- | :--- | :--------------------------------------- | | ${path} | ID | The ID in its 24 char hex representation |

Output fields

| Field name | Type | Description | | :--------- | :--- | :--------------------------------------- | | ${path} | ID | The ID in its 24 char hex representation |

Filters

Since MongoId fields encode identifiers, "text" filters (eg. contains, starts_with, etc) are not supported. Note also that hexadecimal encoding, as used here, is case agnostic. As such, despite the GraphQL ID type being encoded as Strings, all MongoId filters are effectively case insensitive. See the the Casing section.

| Field name | Type | Description | | :--------------- | :----- | :------------------------------------ | | ${path} | ID | Exact match to the ID provided | | ${path}_not | ID | Not an exact match to the ID provided | | ${path}_in | [ID] | In the array of IDs provided | | ${path}_not_in | [ID] | Not in the array of IDs provided |

Storage

Mongoose adapter

The Mongoose adapter uses the native Mongo ObjectId schema type. Internally, the 12-byte value are stored in a binary format. Mongoose automatically and transparently converts to and from the 24 char hexadecimal representation when reading and writing.

Knex Adapter

The Knex adapter adds 24 character string field to the schema. This resolves down to varchar(24), character varying(24) or similar, depending on the underlying DB platform.

Values stored are forced to lowercase on read and write to avoid issues with case-sensitive string comparisons. See the casing section for details.

The field adapter supplied for Knex supports the isNotNullable and defaultTo options.

Casing

Mongo ObjectIDs are usually passed around in a hexadecimal string representation. Hexadecimal itself is case agnostic; the hex value AF3D is identical to the hex value af3d (they encode the same value as decimal 44861 or binary 1010111100111101). However, JavaScript and (depending on your configuration) some DB platforms are case-sensitive; in these contexts, the string 'AF3D' does not equal the string 'af3d'.

For the MongoId type, we mitigate this problem by forcing values to lowercase when using the Knex adapter.

Similar issues are faced by the core Uuid field type. It is also often represented using hexadecimal within a string.