Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/yidinghan/mongoose-string-collection

A mongoose plugin that can help you quickly develop string collection related requirements
https://github.com/yidinghan/mongoose-string-collection

mongo mongodb mongoose mongoose-plugin nodejs schema

Last synced: 26 days ago
JSON representation

A mongoose plugin that can help you quickly develop string collection related requirements

Awesome Lists containing this project

README

        

# mongoose-string-collection

[![Greenkeeper badge](https://badges.greenkeeper.io/yidinghan/mongoose-string-collection.svg)](https://greenkeeper.io/)

[![Travis](https://img.shields.io/travis/yidinghan/mongoose-string-collection.svg?style=flat-square)](<>)
[![npm](https://img.shields.io/npm/l/mongoose-string-collection.svg?style=flat-square)](<>)
[![npm](https://img.shields.io/npm/v/mongoose-string-collection.svg?style=flat-square)](<>)
[![npm](https://img.shields.io/npm/dm/mongoose-string-collection.svg?style=flat-square)](<>)
[![David](https://img.shields.io/david/yidinghan/mongoose-string-collection.svg?style=flat-square)](<>)
[![David](https://img.shields.io/david/dev/yidinghan/mongoose-string-collection.svg?style=flat-square)](<>)

A mongoose plugin that can help you quickly develop string collection related requirements

- [mongoose-string-collection](#mongoose-string-collection)
- [Getting Start](#getting-start)
- [NPM](#npm)
- [Usage](#usage)
- [Configuration](#configuration)
- [Different Field Name](#different-field-name)
- [Index Elements/Collection](#index-elementscollection)
- [Unique In Collection](#unique-in-collection)
- [JSDoc](#jsdoc)
- [plugin](#plugin)
- [model](#model)
- [get](#get)
- [remove](#remove)
- [batchRemove](#batchremove)
- [add](#add)
- [batchAdd](#batchadd)
- [replace](#replace)
- [batchReplace](#batchreplace)

# Getting Start

## NPM

Installation

```shell
npm i -S mongoose-string-collection
```

## Usage

Quick code snippet

```javascript
const stringCollection = require('mongoose-string-collection');

schema.plugin(stringCollection);

// init model, etc.

model.addTags({ id: 'thisisid' }, ['thisistag']);
model.getTags({ id: 'thisisid' });
.then(console.log) // ['thisistag']
model.addTags({ id: 'thisisid' }, ['thisistagbro']);
model.getTags({ id: 'thisisid' });
.then(console.log) // ['thisistag', 'thisistagbro']
```

# Configuration

### Different Field Name

The default field [mongoose-string-collection](https://github.com/yidinghan/mongoose-string-collection) would add to schema is `tags`

If you want to change the field name, you can configuration by change [default options](#pluginschema-options)

```javascript
schema.plugin(stringCollection, {
fieldName: 'dingding'
});

// init model, etc.

model.addDingding({ id: 'thisisid' }, ['thisistag']);
model.getDingding({ id: 'thisisid' });
.then(console.log) // ['thisistag']
```

### Index Elements/Collection

If want to indexs the field created by [mongoose-string-collection](https://github.com/yidinghan/mongoose-string-collection), you can set `options.isIndex` to `true`

```javascript
schema.plugin(stringCollection, {
isIndex: true
});

// init model, etc.

const elementIndex = model.path('tags').caster.options.index;
// true
```

### Unique In Collection

Sometimes the collection may not be a unique set of elements, but an array.

If you want an array, you can set `options.isUnique` to `false`.

```javascript
schema.plugin(stringCollection, {
isUnique: true // default also is true
});

// init model, etc.

model.addDingding({ id: 'thisisid' }, ['t', 't1']);
model.getDingding({ id: 'thisisid' });
.then(console.log) // ['t', 't1']
model.addDingding({ id: 'thisisid' }, ['t', 't2']);
model.getDingding({ id: 'thisisid' });
.then(console.log) // ['t', 't1', 't2]

// set isUnique to false
schema.plugin(stringCollection, {
isUnique: false
});

// init model, etc.

model.addDingding({ id: 'thisisid' }, ['t', 't1']);
model.getDingding({ id: 'thisisid' });
.then(console.log) // ['t', 't1']
model.addDingding({ id: 'thisisid' }, ['t', 't2']);
model.getDingding({ id: 'thisisid' });
.then(console.log) // ['t', 't1', 't', 't2]
```

# JSDoc

## plugin

a plugin that help schema to build string collection field
which is an array containt batch string

**Parameters**

- `schema` **MongooseSchema** mongoose schema that use this plugin
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** plugin configuration (optional, default `{}`)
- `options.fieldName` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** the name place in schema (optional, default `tags`)
- `options.isIndex` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** whether index in target field (optional, default `false`)
- `options.isUnique` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** whether unique the content in the collection (optional, default `true`)
- `options.maxLength` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** The maximum size limit for the collection,
if the input is greater than 0, will be treated as a valid input (optional, default `-1`)
- `options.elementOptions` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** collection element options
- `options.updateOptions` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** collection default update options
for add, replace and get methods.
you can also override when using the specified method

## model

### get

sugar method that get target filed as single result

**Parameters**

- `query` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** mongoose query that place in this.findOne (optional, default `{}`)

**Examples**

```javascript
model.getTags({ _id: 'targetnotexists' }).then(console.log);
// undefined

model.insert({ _id: 'test', tags: ['test'] });
model.getTags({ _id: 'test' }).then(console.log);
// ['test]
```

Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)>** target field

### remove

remove element array from target field

**Parameters**

- `query` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** mongoose query to find out one update target
- `collection` **[array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)** string collection will remove from target document
- `updateOptions`

**Examples**

```javascript
// { _id: 'test', tags: ['t1', 't2'] }
model.removeTags({ _id: 'test' }, ['t1']).then(console.log);
// { _id: 'test', tags: ['t2'] }
model.removeTags({ _id: 'test' }, ['t2']).then(console.log);
// { _id: 'test', tags: [] }
```

Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>** updated target document

### batchRemove

batch remove element array from target field

**Parameters**

- `query` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** mongoose query to find out batch update target
- `collection` **[array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)** string collection will remove from batch target document
- `updateOptions`

**Examples**

```javascript
// { _id: 'test0', foo: 'bar', tags: ['t2'] }
// { _id: 'test1', foo: 'bar', tags: ['t1', 't2'] }
model.removeTags({ foo: 'bar' }, ['t1']).then(console.log);
// { "nMatched" : 2, "nUpserted" : 0, "nModified" : 1 }
model.removeTags({ foo: 'bar' }, ['t2']).then(console.log);
// { "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 }
```

Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>** mongoose udpate result

### add

add string array to target field

**Parameters**

- `query` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** mongoose query to find out update target
- `collection` **[array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)** string collection will add to target document
- `updateOptions`

**Examples**

```javascript
model.addTags({ _id: 'test' }, ['t1']).then(console.log);
// { _id: 'test', tags: ['t1'] }
model.addTags({ _id: 'test' }, ['t2']).then(console.log);
// { _id: 'test', tags: ['t1', 't2'] }
```

Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>** updated target document

### batchAdd

batch add element to collection

**Parameters**

- `query` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** mongoose query to find out update target
- `collection` **[array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)** string collection will add to target document
- `updateOptions`

**Examples**

```javascript
model.batchAddTags({ _id: { $in: ['id1', 'id2] } }, ['t1', 't2']).then(console.log);
// { "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 }
model.getTags({ _id: 'id1' }).then(console.log);
// ['t1', 't2']
model.batchAddTags({ _id: { $in: ['id1', 'id2] } }, ['t2', 't3']).then(console.log);
// { "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }
model.getTags({ _id: 'id2' }).then(console.log);
// ['t1', 't2', 't3']
```

Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>** mongoose udpate result

### replace

update document's collection filed,
which is first document find out by given query.
replace collection field with given collection

**Parameters**

- `query` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** mongoose query to find out update target
- `collection` **[array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)** string collection will add to target document
- `updateOptions`

**Examples**

```javascript
model.replaceTags({ _id: 'test' }, ['t1']).then(console.log);
// { _id: 'test', tags: ['t1'] }
model.replaceTags({ _id: 'test' }, ['t2', 't3']).then(console.log);
// { _id: 'test', tags: ['t2', 't3'] }
```

Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>** mongoose udpate result

### batchReplace

batch update documents' collection filed
by replace it with given collection

**Parameters**

- `query` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** mongoose query to find out update target
- `collection` **[array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)** string collection will add to target document
- `updateOptions`

**Examples**

```javascript
model.batchReplaceTags({ _id: 'test' }, ['t1']).then(console.log);
// { "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }
model.getTags({ _id: 'test' }).then(console.log);
// ['t1']
model.batchReplaceTags({ _id: 'test' }, ['t2', 't3']).then(console.log);
// { "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }
model.getTags({ _id: 'test' }).then(console.log);
// ['t2', 't3']
```

Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>** mongoose udpate result