Comparing version 0.1.1 to 0.1.2
@@ -1,1 +0,1 @@ | ||
{"filename":"/Users/yawetse/Developer/github/typesettin/lowkie/example/sampledb.json","collections":[{"name":"kittens","data":[],"idIndex":[201,202,208,209,210,211],"binaryIndices":{},"constraints":null,"uniqueNames":[],"transforms":{},"objType":"kittens","dirty":true,"cachedIndex":null,"cachedBinaryIndex":null,"cachedData":null,"adaptiveBinaryIndices":true,"transactional":false,"cloneObjects":false,"cloneMethod":"parse-stringify","asyncListeners":false,"disableChangesApi":true,"autoupdate":false,"ttl":null,"maxId":211,"DynamicViews":[],"events":{"insert":[null],"update":[null],"pre-insert":[],"pre-update":[],"close":[],"flushbuffer":[],"error":[],"delete":[null],"warning":[null]},"changes":[]}],"databaseVersion":1.1,"engineVersion":1.1,"autosave":false,"autosaveInterval":5000,"autosaveHandle":null,"throttledSaves":true,"options":{"serializationMethod":"normal","destructureDelimiter":"$<\n"},"persistenceMethod":"fs","persistenceAdapter":null,"verbose":false,"events":{"init":[null],"loaded":[],"flushChanges":[],"close":[],"changes":[],"warning":[]},"ENV":"NODEJS"} | ||
{"filename":"/Users/yawetse/Developer/github/typesettin/lowkie/example/sampledb.json","collections":[{"name":"kittens","data":[],"idIndex":[201,217,218],"binaryIndices":{},"constraints":null,"uniqueNames":[],"transforms":{},"objType":"kittens","dirty":true,"cachedIndex":null,"cachedBinaryIndex":null,"cachedData":null,"adaptiveBinaryIndices":true,"transactional":false,"cloneObjects":false,"cloneMethod":"parse-stringify","asyncListeners":false,"disableChangesApi":true,"autoupdate":false,"ttl":null,"maxId":218,"DynamicViews":[],"events":{"insert":[null],"update":[null],"pre-insert":[],"pre-update":[],"close":[],"flushbuffer":[],"error":[],"delete":[null],"warning":[null]},"changes":[]}],"databaseVersion":1.1,"engineVersion":1.1,"autosave":false,"autosaveInterval":5000,"autosaveHandle":null,"throttledSaves":true,"options":{"serializationMethod":"normal","destructureDelimiter":"$<\n"},"persistenceMethod":"fs","persistenceAdapter":null,"verbose":false,"events":{"init":[null],"loaded":[],"flushChanges":[],"close":[],"changes":[],"warning":[]},"ENV":"NODEJS"} |
@@ -10,5 +10,2 @@ const path = require('path'); | ||
}; | ||
// console.log('exposed: lowkie.Schema.Types', lowkie.Schema.Types); | ||
// console.log( 'lowkie.connection', lowkie.connection ); | ||
// console.log(lowkie === lowkie2); | ||
lowkie.connect(path.join(__dirname, './sampledb.json')) | ||
@@ -15,0 +12,0 @@ .then((db) => { |
@@ -16,10 +16,13 @@ 'use strict'; | ||
*/ | ||
function connect(dbpath = defaultDBPath, options = {}) { | ||
function connect(dbpath = defaultDBPath, options = {}, lowkieConfig = {}) { | ||
this.config = Object.assign(this.config, lowkieConfig); | ||
const dbname = path.resolve(dbpath); | ||
const adapter = new lokiFSAdapter(dbname); | ||
const adapter = (this.config.adapterType === 'file') | ||
? { adapter: new lokiFSAdapter(dbname), } | ||
: {}; | ||
const lokiDBOptions = Object.assign({ | ||
autosave: true, | ||
autosaveInterval: 5000, // 5 seconds | ||
adapter, | ||
}, options); | ||
}, | ||
adapter, options); | ||
const t = setImmediate(() => { | ||
@@ -31,10 +34,14 @@ this.connection.emit('connecting', { dbname, options, }); | ||
const ensureAdapterFilePromise = () => { | ||
return (this.config.adapterType === 'file') | ||
? new Promise((resolve, reject) => { | ||
fs.ensureFile(dbname, (err) => { | ||
if (err) reject(err); | ||
else resolve(true); | ||
}); | ||
}) | ||
: Promise.resolve(); | ||
if (!this.config.adapterType) { | ||
return Promise.reject(new Error('Invalid Adapter Type')); | ||
} else { | ||
return (this.config.adapterType === 'file') | ||
? new Promise((resolve, reject) => { | ||
fs.ensureFile(dbname, (err) => { | ||
if (err) reject(err); | ||
else resolve(true); | ||
}); | ||
}) | ||
: Promise.resolve(); | ||
} | ||
}; | ||
@@ -50,3 +57,3 @@ this.db = db; | ||
clearImmediate(t); | ||
if (err || (dbdata && !dbdata.collections.length) ) { | ||
if (this.config.overwriteInvalidJSON && err || ( dbdata && (!dbdata.collections || !dbdata.collections.length)) ) { | ||
db.saveDatabase((err) => { | ||
@@ -60,2 +67,4 @@ if (err) { | ||
}); | ||
} else if (err) { | ||
reject (err); | ||
} else{ | ||
@@ -72,5 +81,7 @@ db.loadDatabase({}, (err) => { | ||
}); | ||
}) | ||
.catch(reject); | ||
.catch((e) => { | ||
this.connection.emit('connectionError', e, { dbname, options, }); | ||
reject(e); | ||
}); | ||
}); | ||
@@ -77,0 +88,0 @@ } catch (e) { |
@@ -24,2 +24,3 @@ 'use strict'; | ||
strictSchemas: true, | ||
overwriteInvalidJSON:true, | ||
}, options); | ||
@@ -26,0 +27,0 @@ this.connections = new Map(); |
{ | ||
"name": "lowkie", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
'use strict'; | ||
/*jshint expr: true*/ | ||
const path = require('path'); | ||
const fs = require('fs-extra'); | ||
const events = require('events'); | ||
const sinon = require('sinon'); | ||
const chai = require('chai'); | ||
const fs = require('fs-extra'); | ||
const expect = require('chai').expect; | ||
const testConnectDBPath = path.join(__dirname, '../mock/connecttestdb.json'); | ||
const removeTestDB = require('../util/removeTestDB'); | ||
const invalidJSONPATH = path.join(__dirname, '../mock/invalid.db.json'); | ||
const invalidFILEPATH = path.join(__dirname, '../mock/invalid.db.jsonfile'); | ||
let lowkie = require('../../index'); | ||
@@ -58,2 +61,19 @@ let lowkieConnectTest = require('../../lib/connect'); | ||
}); | ||
it('should connect and load an existing db filepath', (done) => { | ||
let newLOWKIE = new lowkieClass({}); | ||
let lowkieConnect = lowkieConnectTest.bind(newLOWKIE); | ||
let existingDB = path.join(__dirname, '../mock/sampledb.json'); | ||
lowkieConnect(existingDB) | ||
.then((db) => { | ||
expect(db).to.be.an('object'); | ||
expect(db).to.eql(newLOWKIE.db); | ||
// done(); | ||
}) | ||
.catch(done); | ||
newLOWKIE.connection.once('connected', (status) => { | ||
expect(status).to.be.an('object'); | ||
done(); | ||
}); | ||
}); | ||
it('should emit connected event once connected to a new db filepath', (done) => { | ||
@@ -70,6 +90,114 @@ let newLOWKIE = new lowkieClass({}); | ||
}); | ||
it('should allow for custom lowkie configurations', (done) => { | ||
let newLOWKIE = new lowkieClass({}); | ||
let lowkieConnect = lowkieConnectTest.bind(newLOWKIE); | ||
let throwawayfilepath = path.join(__dirname, '../mock/connecttestthrowaway2.json'); | ||
lowkieConnect(throwawayfilepath, {}, { adapterType: 'default', }) | ||
.then(() => { | ||
// console.log('newLOWKIE.config', newLOWKIE.config); | ||
expect(newLOWKIE.config.adapterType).to.eql('default'); | ||
removeTestDB(throwawayfilepath); | ||
done(); | ||
}) | ||
.catch(done); | ||
}); | ||
it('should allow handle invalid database json', (done) => { | ||
let newLOWKIE = new lowkieClass({}); | ||
let lowkieConnect = lowkieConnectTest.bind(newLOWKIE); | ||
let throwawayfilepath = invalidJSONPATH; | ||
lowkieConnect(throwawayfilepath, {}, { adapterType: 'default', }, { | ||
overwriteInvalidJSON:false, | ||
}) | ||
.then(() => { | ||
// console.log('newLOWKIE.config', newLOWKIE.config); | ||
expect(newLOWKIE.config.adapterType).to.eql('default'); | ||
done(); | ||
}) | ||
.catch(e => { | ||
console.log(e); | ||
done(); | ||
}); | ||
}); | ||
it('should throw error handling invalid database file', (done) => { | ||
let newLOWKIE = new lowkieClass({}); | ||
let lowkieConnect = lowkieConnectTest.bind(newLOWKIE); | ||
let throwawayfilepath = invalidFILEPATH; | ||
lowkieConnect(throwawayfilepath, {}, { | ||
overwriteInvalidJSON:false, | ||
}) | ||
.then(() => { | ||
console.log('resoved here') | ||
// console.log('newLOWKIE.config', newLOWKIE.config); | ||
done(); | ||
}) | ||
.catch(e => { | ||
expect(e).to.be.instanceof(Error); | ||
done(); | ||
}); | ||
}); | ||
it('should throw error on invalid adapter types and emit error', (done) => { | ||
let newLOWKIE = new lowkieClass({}); | ||
let lowkieConnect = lowkieConnectTest.bind(newLOWKIE); | ||
let throwawayfilepath = invalidFILEPATH; | ||
// expect(lowkieConnect.bind(lowkieConnect, throwawayfilepath, {}, { | ||
// overwriteInvalidJSON: false, | ||
// adapterType: false, | ||
// })).to.throw(Error); | ||
lowkieConnect(throwawayfilepath, {}, { | ||
overwriteInvalidJSON: false, | ||
adapterType:false, | ||
}) | ||
.then(() => { | ||
// console.log('resoved here') | ||
// console.log('newLOWKIE.config', newLOWKIE.config); | ||
// done(); | ||
}) | ||
.catch(e => { | ||
expect(e).to.be.instanceof(Error); | ||
// done(); | ||
}); | ||
newLOWKIE.connection.once('connectionError', (e) => { | ||
expect(e).to.be.instanceOf(Error); | ||
done(); | ||
}); | ||
}); | ||
// it('should emit connection error', (done) => { | ||
// let newLOWKIE = new lowkieClass({}); | ||
// let lowkieConnect = lowkieConnectTest.bind(newLOWKIE); | ||
// let throwawayfilepath = invalidFILEPATH; | ||
// let nameCounter = 0; | ||
// let configProxy = new Proxy({ | ||
// overwriteInvalidJSON: false, | ||
// adapterType: 'default', | ||
// }, { | ||
// get: function (target, name) { | ||
// if (name === 'adapterType') { | ||
// nameCounter++; | ||
// } | ||
// if (nameCounter > 0) { | ||
// // throw new Error('testing error handling'); | ||
// console.log({ target, name, nameCounter }); | ||
// return new Error('testing error handling'); | ||
// } else { | ||
// console.log({ target, name, nameCounter }); | ||
// return target[ name ]; | ||
// } | ||
// }, | ||
// }); | ||
// lowkieConnect('/private', {}, configProxy) | ||
// .catch((e) => { | ||
// console.log(e);//do nothing, wait for event | ||
// // done(); | ||
// }); | ||
// newLOWKIE.connection.once('connectionError', (e) => { | ||
// expect(e).to.be.instanceOf(Error); | ||
// done(); | ||
// }); | ||
// }); | ||
}); | ||
after('remove test schema db', () => { | ||
removeTestDB(testConnectDBPath, true); | ||
fs.outputJsonSync(invalidJSONPATH, {}); | ||
}); | ||
}); |
@@ -6,2 +6,3 @@ 'use strict'; | ||
const chai = require('chai'); | ||
const sinon = require('sinon'); | ||
const fs = require('fs-extra'); | ||
@@ -24,3 +25,4 @@ const expect = require('chai').expect; | ||
let testUserModel; | ||
// let testUserModel; | ||
let goodModelSchema; | ||
let goodModel; | ||
@@ -43,4 +45,9 @@ describe('Model', function () { | ||
it('should throw an error if creating a model before databse is loaded', function () { | ||
let spy = sinon.spy(); | ||
// let originalConsoleError = console.error; | ||
let newLOWKIE = new lowkieClass({}); | ||
newLOWKIE.debug = true; | ||
console.error = spy; | ||
expect(newLOWKIE.model.bind(newLOWKIE)).to.throw('There has to be an active lowkie connection before creating models, lowkie.connect is asynchronous'); | ||
expect(spy).to.be.been.called; | ||
}); | ||
@@ -71,7 +78,14 @@ it('should throw an error if model name is not a valid string', function (done) { | ||
it('should register models globally', () => { | ||
let goodModelSchema = lowkie.Schema(testUserModelScheme); | ||
lowkie.model('goodModel', goodModelSchema); | ||
console.log(lowkie.models); | ||
goodModelSchema = lowkie.Schema(testUserModelScheme); | ||
goodModel = lowkie.model('goodModel', goodModelSchema); | ||
// console.log(lowkie.models); | ||
expect(Object.keys(lowkie.models)).to.have.length.above(0); | ||
}); | ||
it('should use existing schema if already exists', () => { | ||
let newGoodModelSchema = lowkie.Schema(testUserModelScheme); | ||
let newGoodModel = lowkie.model('goodModel', newGoodModelSchema); | ||
// console.log(lowkie.models); | ||
expect(Object.keys(lowkie.models)).to.have.lengthOf(1); | ||
expect(goodModel).to.eql(newGoodModel); | ||
}); | ||
}); | ||
@@ -78,0 +92,0 @@ after('remove test schema db', () => { |
@@ -17,2 +17,3 @@ 'use strict'; | ||
age: Number, | ||
location: Object, | ||
profile: { | ||
@@ -105,3 +106,7 @@ type: String, | ||
profile: 'mocha test', | ||
active: true, | ||
location: { | ||
lat: 30, | ||
lng:-15, | ||
}, | ||
active: false, | ||
age: 18, | ||
@@ -108,0 +113,0 @@ invalidprop: 'whatever', |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
2418636
79
37809