Comparing version 1.1.0 to 1.2.0
@@ -59,3 +59,4 @@ 'use strict'; | ||
result[key] === new Date(); | ||
} if (typeof _document[key] === 'string' && (validType === String || validType === ObjectId)) { | ||
} | ||
if (typeof _document[key] === 'string' && (validType === String || validType === ObjectId)) { | ||
result[key] = _document[key].toString(); | ||
@@ -76,4 +77,3 @@ } else if (validType === Boolean) { | ||
} | ||
} | ||
else result[key] = this.flattenedScheme[`${ key }.default`]; | ||
} else result[key] = this.flattenedScheme[`${ key }.default`]; | ||
} else if (typeof original_document[key] === 'object' && typeof this.flattenedScheme[key] === 'function' && this.flattenedScheme[key] === Object) { //schema type mixed | ||
@@ -108,30 +108,30 @@ result[key] = original_document[key]; | ||
insert(options = {}) { | ||
let lokiCollectionInsert = options.target; | ||
let lowkieInstance = options.thisArg; | ||
let lowkieDocument = options.argumentsList; | ||
let lokiCollectionInsert = options.target; | ||
let lowkieInstance = options.thisArg; | ||
let lowkieDocument = options.argumentsList; | ||
// let { target, thisArg, argumentsList, } = options; | ||
return new Promise((resolve, reject) => { | ||
try { | ||
let newDoc = (Array.isArray(lowkieDocument)) ? | ||
lowkieDocument.map(lowkiedoc => this.createDoc(lowkiedoc)) : | ||
this.createDoc(lowkieDocument); | ||
lokiCollectionInsert.call(lowkieInstance, newDoc); | ||
this.lowkie.dbs[this.dbconnection].saveDatabase((err) => { | ||
if (err) reject(err); | ||
else return resolve(newDoc); | ||
}); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
} | ||
/** | ||
* overwrites the default remove method | ||
* | ||
* @param {any} options | ||
* @returns Promise | ||
* | ||
* @memberOf lowkieSchema | ||
*/ | ||
// let { target, thisArg, argumentsList, } = options; | ||
return new Promise((resolve, reject) => { | ||
try { | ||
let newDoc = (Array.isArray(lowkieDocument)) ? | ||
lowkieDocument.map(lowkiedoc => this.createDoc(lowkiedoc)) : | ||
this.createDoc(lowkieDocument); | ||
lokiCollectionInsert.call(lowkieInstance, newDoc); | ||
this.lowkie.dbs[this.dbconnection].saveDatabase((err) => { | ||
if (err) reject(err); | ||
else return resolve(newDoc); | ||
}); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
} | ||
/** | ||
* overwrites the default remove method | ||
* | ||
* @param {any} options | ||
* @returns Promise | ||
* | ||
* @memberOf lowkieSchema | ||
*/ | ||
remove(options = {}) { | ||
@@ -149,3 +149,3 @@ let lokiCollectionRemove = options.target; | ||
}); | ||
} else { | ||
} else { | ||
lokiCollectionRemove.call(lowkieInstance, lowkieDocument); | ||
@@ -163,23 +163,35 @@ } | ||
populate(model, refs = '', query = {}) { | ||
let _refs = (Array.isArray(refs)) ? refs : refs.split(' '); | ||
_refs = _refs.map(ref => { | ||
if (ref && typeof ref === 'object') return ref; | ||
return { path: ref }; | ||
return new Promise((resolve, reject) => { | ||
try { | ||
let _refs = (Array.isArray(refs)) ? refs : refs.split(' '); | ||
_refs = _refs.map(ref => { | ||
if (ref && typeof ref === 'object') return ref; | ||
return { path: ref }; | ||
}); | ||
let collections = this.lowkie.dbs[this.dbconnection].collections; | ||
let flattened = this.flattenedScheme; | ||
let _documents = model.chain().find(query); | ||
let populatedDocuments = _documents.data().reduce((result, doc) => { | ||
let flattenedDoc = flatten(doc); | ||
_refs.forEach(ref => { | ||
if (flattened[`${ref.path}.0.ref`]) { | ||
//ARRAY OF DOCS | ||
} else if (flattened[`${ref.path}.ref`]) { | ||
let name = pluralize(flattened[`${ref.path}.ref`]); | ||
let toPopulate = collections.filter(collection => collection.name === name)[0]; | ||
let _id = flattenedDoc[ref.path]; | ||
if (_id) { | ||
let childDocument = toPopulate.chain().find({ _id }).data()[0]; | ||
flattenedDoc[ref.path] = childDocument || null; | ||
} | ||
} | ||
}); | ||
return result.concat(flatten.unflatten(flattenedDoc)); | ||
}, []); | ||
resolve(populatedDocuments); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
let collections = this.lowkie.dbs[this.dbconnection].collections; | ||
let flattened = this.flattenedScheme; | ||
let _documents = model.chain().find(query); | ||
return _documents.data().reduce((result, doc) => { | ||
let flattenedDoc = flatten(doc); | ||
_refs.forEach(ref => { | ||
let name = pluralize(flattened[`${ ref.path }.ref`]); | ||
let toPopulate = collections.filter(collection => collection.name === name)[0]; | ||
let _id = flattenedDoc[ref.path]; | ||
if (_id) { | ||
let childDocument = toPopulate.chain().find({ _id }).data()[0]; | ||
flattenedDoc[ref.path] = childDocument || null; | ||
} | ||
}); | ||
return result.concat(flatten.unflatten(flattenedDoc)); | ||
}, []); | ||
} | ||
@@ -186,0 +198,0 @@ } |
{ | ||
"name": "lowkie", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -37,3 +37,3 @@ 'use strict'; | ||
describe('Schema', function () { | ||
describe('Schema', function() { | ||
this.timeout(10000); | ||
@@ -43,3 +43,3 @@ before('intialize lowkie instances', (done) => { | ||
lowkie.connect(testSchemaDBPath) | ||
.then((/*db*/) => { | ||
.then(( /*db*/ ) => { | ||
// console.log('connected schematestdb'); | ||
@@ -55,4 +55,4 @@ testUserSchema = lowkie.Schema(testUserSchemaScheme); | ||
}); | ||
describe('Lowkie Schema', function () { | ||
it('should be an instance of a lowkieSchema', function () { | ||
describe('Lowkie Schema', function() { | ||
it('should be an instance of a lowkieSchema', function() { | ||
expect(testUserSchema).to.be.an.instanceof(lowkieSchema) | ||
@@ -70,3 +70,3 @@ .and.to.be.an('object'); | ||
it('should include _id in valid schema properties', () => { | ||
expect(Object.keys(testUserSchemaScheme).concat([ '_id', ])).to.eql(testUserSchema.validNames); | ||
expect(Object.keys(testUserSchemaScheme).concat(['_id', ])).to.eql(testUserSchema.validNames); | ||
}); | ||
@@ -81,3 +81,3 @@ }); | ||
let customId = '1234'; | ||
expect(testUserSchema.createDoc({ _id:customId, })._id).to.eql(customId); | ||
expect(testUserSchema.createDoc({ _id: customId, })._id).to.eql(customId); | ||
}); | ||
@@ -132,4 +132,4 @@ it('should ignore invalid schema props', () => { | ||
return testAccountModel.insert({ | ||
name: 'Some Random Name' | ||
}) | ||
name: 'Some Random Name' | ||
}) | ||
.then(account => { | ||
@@ -146,3 +146,5 @@ expect(account[0]._id).to.be.ok; | ||
.then(newUser => { | ||
let result = testUserModel.populate('account', { _id: newUser[0]._id })[0]; | ||
return testUserModel.populate('account', { _id: newUser[0]._id })[0]; | ||
}) | ||
.then(result => { | ||
expect(result.account).to.have.property('name'); | ||
@@ -160,17 +162,17 @@ expect(result.account.name).to.equal('Some Random Name'); | ||
testUserSchema.insert({ | ||
target: testUserModel.insert, | ||
thisArg: lowkie, | ||
argumentsList: { | ||
name: 'testuser', | ||
email: 'user@domain.tld', | ||
profile: 'mocha test', | ||
location: { | ||
lat: 30, | ||
lng:-15, | ||
target: testUserModel.insert, | ||
thisArg: lowkie, | ||
argumentsList: { | ||
name: 'testuser', | ||
email: 'user@domain.tld', | ||
profile: 'mocha test', | ||
location: { | ||
lat: 30, | ||
lng: -15, | ||
}, | ||
active: false, | ||
age: 18, | ||
invalidprop: 'whatever', | ||
}, | ||
active: false, | ||
age: 18, | ||
invalidprop: 'whatever', | ||
}, | ||
}) | ||
}) | ||
.then(newdoc => { | ||
@@ -184,21 +186,22 @@ expect(newdoc).to.be.an('object'); | ||
testUserSchema.insert({ | ||
target: testUserModel.insert, | ||
thisArg: lowkie, | ||
argumentsList: [{ | ||
name: 'testuser', | ||
email: 'user@domain.tld', | ||
profile: 'mocha test', | ||
active: true, | ||
age: 18, | ||
invalidprop: 'whatever', | ||
}, | ||
{ | ||
name: 'testuser2', | ||
email: 'user2domain.tld', | ||
profile: 'mocha test2', | ||
active: false, | ||
age: 19, | ||
invalidprop: 'whatever', | ||
}], | ||
}) | ||
target: testUserModel.insert, | ||
thisArg: lowkie, | ||
argumentsList: [{ | ||
name: 'testuser', | ||
email: 'user@domain.tld', | ||
profile: 'mocha test', | ||
active: true, | ||
age: 18, | ||
invalidprop: 'whatever', | ||
}, | ||
{ | ||
name: 'testuser2', | ||
email: 'user2domain.tld', | ||
profile: 'mocha test2', | ||
active: false, | ||
age: 19, | ||
invalidprop: 'whatever', | ||
} | ||
], | ||
}) | ||
.then(newdocs => { | ||
@@ -205,0 +208,0 @@ // console.log({newdocs}) |
6196520
37977