# @keystone-alpha/fields-mongoid

> KeystoneJS MongoId Field Type

Latest version **1.1.6** (published 2019-10-23) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @keystone-alpha/fields-mongoid
pnpm add @keystone-alpha/fields-mongoid
yarn add @keystone-alpha/fields-mongoid
bun add @keystone-alpha/fields-mongoid
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.1.6 |
| Published | 2019-10-23 |
| First published | 2019-07-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=8.4.0 |
| Dependencies | 7 |
| Unpacked size | 31.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 67 |
| Author | The KeystoneJS Development Team |
| Maintainers | dominikwilkowski, jedwatson, jesstelford, mitchellhamilton, molomby, timl |

## Links

- npm: https://www.npmjs.com/package/@keystone-alpha/fields-mongoid
- Repository: https://github.com/keystonejs/keystone-5
- Homepage: https://github.com/keystonejs/keystone-5#readme
- Issues: https://github.com/keystonejs/keystone-5/issues
- npm.io page: https://npm.io/package/@keystone-alpha/fields-mongoid

## Dependencies (7)

- [react](https://npm.io/package/react.md) ^16.8.6
- [@arch-ui/input](https://npm.io/package/@arch-ui/input.md) ^0.1.0
- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.4.3
- [@keystone-alpha/fields](https://npm.io/package/@keystone-alpha/fields.md) ^15.0.0
- [@keystone-alpha/adapter-knex](https://npm.io/package/@keystone-alpha/adapter-knex.md) ^6.0.2
- [@keystone-alpha/adapter-mongoose](https://npm.io/package/@keystone-alpha/adapter-mongoose.md) ^6.0.1
- [@keystone-alpha/build-field-types](https://npm.io/package/@keystone-alpha/build-field-types.md) ^1.0.6

## Recent versions

- 1.1.6 (latest) — 2019-10-23
- 1.1.5 — 2019-10-15
- 1.1.4 — 2019-10-09
- 1.1.3 — 2019-10-02
- 1.1.2 — 2019-09-17
- 1.1.1 — 2019-09-12
- 1.1.0 — 2019-08-22
- 1.0.2 — 2019-08-13
- 1.0.1 — 2019-07-19

## README

<!--[meta]
section: api
subSection: field-types
title: MongoId
[meta]-->

# MongoId

This field allows arbitrary MongoID fields to be added to your lists.

It supports the core Mongoose and Knex adapters:

- On Mongoose the [native Mongo `ObjectId` schema type](https://mongoosejs.com/docs/schematypes.html#objectids) is used.
- On Knex a 24 character [string field](https://knexjs.org/#Schema-string) 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](#casing) for details.

## Usage

```js
const { Keystone } = require('@keystone-alpha/keystone');
const { MongoId } = require('@keystone-alpha/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 it's 24 char hex representation |

### Output Fields

| Field name | Type | Description                               |
| :--------- | :--- | :---------------------------------------- |
| `${path}`  | `ID` | The ID in it's 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](#casing).

| 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 Adaptor

The Mongoose Adaptor uses the [native Mongo `ObjectId` schema type](https://mongoosejs.com/docs/schematypes.html#objectids).
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 Adaptor

The Knex adaptor adds 24 character [string field](https://knexjs.org/#Schema-string) 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](#casing) for details.

The field adapter supplied for Knex supports
[the `isNotNullable` and `defaultTo` options](https://github.com/keystonejs/keystone-5/pull/1383#issuecomment-509889242).

## 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](https://github.com/keystonejs/keystone-5/tree/master/packages/fields/src/types/Uuid#casing).
It is also often represented using hexadecimal within a string.

---
_Source: https://npm.io/package/@keystone-alpha/fields-mongoid · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
