node-simple-collectionmodel
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -124,2 +124,56 @@ 'use strict'; | ||
getApiFields() { | ||
const keys = Object.keys(this.Model.schema.paths); | ||
const apiFields = []; | ||
for (let i = 0, l = keys.length; i < l; i += 1) { | ||
const key = keys[i]; | ||
const field = this.Model.schema.paths[key].options; | ||
// console.log('key', key, this.Model.schema.paths[key].options); | ||
if (field.api) { | ||
apiFields.push(key); | ||
} | ||
} | ||
return apiFields; | ||
} | ||
getApiFieldsDoc() { | ||
const keys = Object.keys(this.Model.schema.paths); | ||
const apiFields = {}; | ||
for (let i = 0, l = keys.length; i < l; i += 1) { | ||
const key = keys[i]; | ||
const field = this.Model.schema.paths[key]; | ||
// console.log('key', key, this.Model.schema.paths[key]); | ||
if (field.options.api) { | ||
apiFields[key] = `${field.instance}${field.caster ? ` of ${field.caster.instance}` : ''}` | ||
+ `${field.options.apiDoc ? `. ${field.options.apiDoc}` : ''}`; | ||
} | ||
} | ||
return apiFields; | ||
} | ||
asApiObject($dataObject) { | ||
const dataObject = $dataObject || this.dataObject; | ||
const keys = Object.keys(this.Model.schema.paths); | ||
const apiResult = {}; | ||
for (let i = 0, l = keys.length; i < l; i += 1) { | ||
const key = keys[i]; | ||
const field = this.Model.schema.paths[key].options; | ||
// console.log('key', key, this.Model.schema.paths[key].options); | ||
if (field.api) { | ||
apiResult[key] = dataObject[key]; | ||
} | ||
} | ||
return apiResult; | ||
} | ||
asApiObjects($dataObjects) { | ||
const dataObjects = $dataObjects || this.dataObjectList; | ||
const apiResult = []; | ||
for (let i = 0, l = dataObjects.length; i < l; i += 1) { | ||
const object = this.asApiObject(dataObjects[i]); | ||
apiResult.push(object); | ||
} | ||
return apiResult; | ||
} | ||
async count(query) { | ||
@@ -129,3 +183,3 @@ try { | ||
return total; | ||
} catch(error) { | ||
} catch (error) { | ||
console.error(`${this.modelName}.find: ${JSON.stringify(query, null, 4)}: ${error}`); | ||
@@ -148,2 +202,3 @@ } | ||
const documents = await this.Model.find(finalQuery, fields, options) | ||
.lean() | ||
.sort(options.sort || this.defaultSort) | ||
@@ -156,3 +211,4 @@ .limit(options.limit || 1000); | ||
if (document !== null) { | ||
const dataObject = document.toObject(); | ||
// const dataObject = document.toObject(); | ||
const dataObject = document; | ||
delete dataObject._id; | ||
@@ -163,3 +219,3 @@ this.dataObjectList.push(dataObject); | ||
return this.dataObjectList; | ||
} catch(error) { | ||
} catch (error) { | ||
console.error(`${this.modelName}.find: ${JSON.stringify(query, null, 4)}: ${error}`); | ||
@@ -181,3 +237,3 @@ } | ||
return this.dataObject; | ||
} catch(error) { | ||
} catch (error) { | ||
console.error(`${this.modelName}.findOne(${JSON.stringify(query, null, 4)}).error: ${error}`); | ||
@@ -200,3 +256,3 @@ } | ||
return documents; | ||
} catch(error) { | ||
} catch (error) { | ||
console.error(`${this.modelName}.find: ${JSON.stringify(query, null, 4)}: ${error}`); | ||
@@ -240,6 +296,18 @@ } | ||
} catch (error) { | ||
console.error(`${this.modelName}.findOneAndUpdate(${JSON.stringify(query, null, 4)}).error: ${error}`); | ||
const errorMessage = `${this.modelName}.findOneAndUpdate(${JSON.stringify(query, null, 4)}).error: ${error}`; | ||
console.error(errorMessage); | ||
return new Error(errorMessage); | ||
} | ||
} | ||
async update(obj, query) { | ||
const doc = await this.Model.findOne(query || { id: obj.id }); | ||
if (!doc) { | ||
throw new Error(`Updated failed; document ${JSON.stringify(query || obj.id, null, 4)} not found`); | ||
} | ||
doc.set(obj); | ||
return doc.save(); | ||
} | ||
async rawUpdate(query, data, options = { upsert: true }) { | ||
@@ -246,0 +314,0 @@ return new Promise((resolve, reject) => { |
{ | ||
"name": "node-simple-collectionmodel", | ||
"description": "A simpel and nice collection model for mongoose stuff.", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"homepage": "https://github.com/5orenso/node-simple-collectionmodel", | ||
@@ -6,0 +6,0 @@ "repository": { |
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
26306
408