diff --git a/README.md b/README.md index 9acaaba..f2f1551 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # Mongoose schema inheritance -This npm module extends mongoose schema (returning new one) +This npm module extends mongoose schema (returning new one) modified to use ES6 transpiled extends NOT ~~mongoose-schema-extend~~ but **mongoose-extend-schema** ### Installation ```sh -$ npm i --save mongoose-extend-schema +$ npm install thxmike/mongoose-extend-schema --save ``` ### Usage @@ -84,4 +84,4 @@ module.exports = extendSchema; Author ---- -@doasync +@thxmike diff --git a/index.js b/index.js index 16f84d4..908c0d6 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,35 @@ +const mongoose = require("mongoose"); -const mongoose = require('mongoose'); - -function extendSchema (Schema, definition, options) { - return new mongoose.Schema( - Object.assign({}, Schema.obj, definition), - options - ); +function extendSchema(parent, child, options, injected_mongoose) { + let updated_schema = Object.assign({}, parent.obj, child); + if (injected_mongoose){ + this._mongoose = injected_mongoose; + } else { + this._mongoose = mongoose; + } + let updated_child_object = new this._mongoose.Schema(updated_schema, options); + __extends(updated_child_object, parent); + return updated_child_object; } +var __extends = + (this && this.__extends) || + function(d, b) { + for (var p in b) + if (b.hasOwnProperty(p)) { + //array + if (Array.isArray(b[p]) && Array.isArray(d[p])) { + d[p] = Array.from(new Set(d[p].concat(b[p]))); + } else { + //actual object + d[p] = Object.assign({}, b[p], d[p]); + } + } + function __() { + this.constructor = d; + } + d.prototype = + b === null ? Object.create(b) : ((__.prototype = b.prototype), new __()); + }; + module.exports = extendSchema; diff --git a/package.json b/package.json index ddc5a4a..e0407c7 100644 --- a/package.json +++ b/package.json @@ -1,21 +1,20 @@ { "name": "mongoose-extend-schema", "description": "Extends mongoose schema", - "version": "1.0.0", + "version": "1.0.1", "author": { - "name": "doasync", - "email": "doasync@gmail.com" + "name": "Michael Rivera" }, "repository": { "type": "git", - "url": "git://github.com/doasync/mongoose-extend-schema.git" + "url": "https://github.com/thxmike/mongoose-extend-schema.git" }, "engines": { "node": ">= 8.2.1", "npm": ">= 5.3.0" }, "dependencies": { - "mongoose": "*" + "mongoose": "5.6.6" }, "main": "index.js", "license": "MIT"