Skip to content

Updated to use ES6 transpiled extends #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -84,4 +84,4 @@ module.exports = extendSchema;

Author
----
@doasync
@thxmike
38 changes: 31 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
{
"name": "mongoose-extend-schema",
"description": "Extends mongoose schema",
"version": "1.0.0",
"version": "1.0.1",
"author": {
"name": "doasync",
"email": "[email protected]"
"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"
Expand Down