Comparing version 0.1.4 to 0.1.5
@@ -247,11 +247,28 @@ 'use strict'; | ||
// If is a a document that belong to inherit model set | ||
// the discriminator key | ||
if (doc._options.inheritOptions && doc._options.inheritOptions.discriminatorKey) { | ||
doc._data[doc._options.inheritOptions.discriminatorKey] = doc._discriminator ? doc._discriminator : doc._modelName; | ||
// If we are inserting a resources in a discriminator model | ||
// we have to set the proper filter and addres to the parent collection | ||
let model = {}, | ||
collection = doc._discriminator ? doc._discriminator : doc._modelName; | ||
if (_this.models[collection]) { | ||
const _discriminator = _this.models[collection]._discriminator; | ||
const discriminatorKey = doc._options.inheritOptions ? doc._options.inheritOptions.discriminatorKey : null; | ||
if (_discriminator || discriminatorKey) { | ||
const discriminatorKey = _this.models[collection]._schemaOptions.inheritOptions.discriminatorKey; | ||
if (doc._data[discriminatorKey]) throw new Error('You can not include a specific value for the "discriminatorKey" on the doc.'); | ||
doc._data[discriminatorKey] = collection; | ||
} | ||
model = _this.models[collection]; | ||
collection = _this.models[collection]._modelName; | ||
} else { | ||
throw new Error('The collection ' + collection + 'does not exist and is not registered.'); | ||
} | ||
// Ensure index are created | ||
if (_this._indexes[doc._modelName] && _this._indexes[doc._modelName].length > 0) { | ||
yield _this.createIndexes(tenant, doc._modelName, _this._indexes[doc._modelName]); | ||
if (_this._indexes[collection] && _this._indexes[collection].length > 0) { | ||
yield _this.createIndexes(tenant, collection, _this._indexes[collection]); | ||
} | ||
@@ -275,3 +292,3 @@ | ||
var _ref2 = yield to(conn.db(tenant).collection(doc._modelName).insert(doc._data, { forceServerObjectId: true })), | ||
var _ref2 = yield to(conn.db(tenant).collection(collection).insert(doc._data, { forceServerObjectId: true })), | ||
_ref3 = _slicedToArray(_ref2, 2); | ||
@@ -289,3 +306,12 @@ | ||
return resolve(result); | ||
if (result) { | ||
// Binding model properties to the document | ||
const Document = require('../document'); | ||
const docInserted = new Document(result.ops[0], model._preHooks, model._postHooks, model._methods, model._schemaOptions, model._modelName, model._discriminator); | ||
return resolve(docInserted); | ||
} else { | ||
return resolve(result); | ||
} | ||
} | ||
@@ -292,0 +318,0 @@ } catch (error) { |
{ | ||
"name": "moltyjs", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "A tiny ODM for MongoDB with multy tenancy support.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -88,19 +88,19 @@ # What is moltyjs? | ||
_type_: Mandatory [String, Number, Boolean, Buffer, Date, Array, Object] | ||
_required_: Optional {Boolean} | ||
_unique_: Optional {Boolean} | ||
_default_: Optional | ||
_match_: Optional | ||
_enum_: Optional | ||
_min_: Optional | ||
_max_: Optional | ||
_maxlength_: Optional | ||
_validate_: Optional | ||
* _type_: Mandatory [String, Number, Boolean, Buffer, Date, Array, Object] | ||
* _required_: Optional {Boolean} | ||
* _unique_: Optional {Boolean} | ||
* _default_: Optional | ||
* _match_: Optional | ||
* _enum_: Optional | ||
* _min_: Optional | ||
* _max_: Optional | ||
* _maxlength_: Optional | ||
* _validate_: Optional | ||
And the schema options allowed are: | ||
_timestamps_: Optional | ||
_inheritOptions_: Optional | ||
--_discriminatorKey_: Required once "_inheritOptions_" is set | ||
--_merge_: Optional ['methods', 'preHooks', 'postHooks'] | ||
* _timestamps_: Optional | ||
* _inheritOptions_: Optional | ||
** --_discriminatorKey_: Required once "_inheritOptions_" is set | ||
** --_merge_: Optional ['methods', 'preHooks', 'postHooks'] | ||
@@ -151,5 +151,5 @@ ## Create a new Model | ||
methods: which corresponds to the static methods. | ||
preHooks: which corresponds to the hooks that are executed **before** performing actions on the DB | ||
postHooks: which corresponds to the hooks that are executed **after** performing actions on the DB | ||
* methods: which corresponds to the static methods. | ||
* preHooks: which corresponds to the hooks that are executed **before** performing actions on the DB | ||
* postHooks: which corresponds to the hooks that are executed **after** performing actions on the DB | ||
@@ -174,10 +174,14 @@ ## Create a new document | ||
Document middleware is supported for the following document functions. In document middleware functions, **this** refers to the document. | ||
Document middleware is supported for the following document functions. | ||
save | ||
* save | ||
Query middleware is supported for the following Model and Query functions. In query middleware functions, this refers to the query. | ||
In document middleware functions, **this** refers to the document. | ||
update | ||
Query middleware is supported for the following Model and Query functions. | ||
* update | ||
In query middleware functions, this refers to the query. | ||
## Referencing Documents | ||
@@ -236,5 +240,8 @@ | ||
* insertOne | ||
### `insertOne(tenant, doc)` | ||
### insertOne() | ||
```javascript | ||
const res = await connection.insertOne('tenant_test', newDoc); | ||
// Document || Error | ||
``` | ||
@@ -245,4 +252,33 @@ ## Recovering a document | ||
* findOne | ||
### `findOne(tenant, collection, query = {}, options = {})` | ||
```javascript | ||
const resFind = await connection.findOne('tenant_test', 'TestModel', { | ||
name: 'Mario Lemes', | ||
}); | ||
// Document || Error | ||
``` | ||
## Updating a document | ||
There are several operations to recover a document from the database: | ||
### `updateOne(tenant, collection, filter, payload, options = {})` | ||
```javascript | ||
const resUpdate = await connection.updateOne( | ||
'tenant_test', | ||
'TestModel', | ||
{ name: 'Mario Lemes' }, | ||
{ | ||
$set: { | ||
name: 'Some other name', | ||
}, | ||
}, | ||
); | ||
// Document || Error | ||
``` | ||
Updating a document support all the [update operators](https://docs.mongodb.com/v3.4/reference/operator/update/) from MongoDB | ||
# TODO | ||
@@ -249,0 +285,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
57845
1237
286