introduce checkDoc method
This commit is contained in:
parent
411b0b1ae9
commit
316030be7c
8
dist/smartdata.classes.dbcollection.d.ts
vendored
8
dist/smartdata.classes.dbcollection.d.ts
vendored
@ -1,3 +1,4 @@
|
|||||||
|
/// <reference types="q" />
|
||||||
import * as plugins from './smartdata.plugins';
|
import * as plugins from './smartdata.plugins';
|
||||||
import { DbConnection } from './smartdata.classes.dbconnection';
|
import { DbConnection } from './smartdata.classes.dbconnection';
|
||||||
export declare class DbCollection<T> {
|
export declare class DbCollection<T> {
|
||||||
@ -10,13 +11,14 @@ export declare class DbCollection<T> {
|
|||||||
/**
|
/**
|
||||||
* finds an object in the DbCollection
|
* finds an object in the DbCollection
|
||||||
*/
|
*/
|
||||||
find(docMatchArg: T): T[];
|
find(docMatchArg: T): plugins.q.Promise<T[]>;
|
||||||
/**
|
/**
|
||||||
* inserts object into the DbCollection
|
* inserts object into the DbCollection
|
||||||
*/
|
*/
|
||||||
insertOne(docArg: T): PromiseLike<void>;
|
insertOne(docArg: T): plugins.q.Promise<void>;
|
||||||
/**
|
/**
|
||||||
* inserts many objects at once into the DbCollection
|
* inserts many objects at once into the DbCollection
|
||||||
*/
|
*/
|
||||||
insertMany(docArrayArg: T[]): void;
|
insertMany(docArrayArg: T[]): plugins.q.Promise<void>;
|
||||||
|
private checkDoc(doc);
|
||||||
}
|
}
|
||||||
|
33
dist/smartdata.classes.dbcollection.js
vendored
33
dist/smartdata.classes.dbcollection.js
vendored
@ -1,4 +1,5 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
const plugins = require('./smartdata.plugins');
|
||||||
class DbCollection {
|
class DbCollection {
|
||||||
constructor(nameArg, dbConnectionArg) {
|
constructor(nameArg, dbConnectionArg) {
|
||||||
this.collection = dbConnectionArg.db.collection(nameArg);
|
this.collection = dbConnectionArg.db.collection(nameArg);
|
||||||
@ -11,19 +12,45 @@ class DbCollection {
|
|||||||
* finds an object in the DbCollection
|
* finds an object in the DbCollection
|
||||||
*/
|
*/
|
||||||
find(docMatchArg) {
|
find(docMatchArg) {
|
||||||
return this.collection.find().toArray();
|
let done = plugins.q.defer();
|
||||||
|
this.collection.find().toArray((err, docs) => {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
done.resolve(docs);
|
||||||
|
});
|
||||||
|
return done.promise;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* inserts object into the DbCollection
|
* inserts object into the DbCollection
|
||||||
*/
|
*/
|
||||||
insertOne(docArg) {
|
insertOne(docArg) {
|
||||||
return this.collection.insertOne(docArg);
|
let done = plugins.q.defer();
|
||||||
|
this.checkDoc(docArg).then(() => {
|
||||||
|
this.collection.insertOne(docArg)
|
||||||
|
.then(() => { done.resolve(); });
|
||||||
|
});
|
||||||
|
return done.promise;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* inserts many objects at once into the DbCollection
|
* inserts many objects at once into the DbCollection
|
||||||
*/
|
*/
|
||||||
insertMany(docArrayArg) {
|
insertMany(docArrayArg) {
|
||||||
|
let done = plugins.q.defer();
|
||||||
|
let checkDocPromiseArray = [];
|
||||||
|
for (let docArg of docArrayArg) {
|
||||||
|
checkDocPromiseArray.push(this.checkDoc(docArg));
|
||||||
|
}
|
||||||
|
plugins.q.all(checkDocPromiseArray).then(() => {
|
||||||
|
this.collection.insertMany(docArrayArg);
|
||||||
|
});
|
||||||
|
return done.promise;
|
||||||
|
}
|
||||||
|
checkDoc(doc) {
|
||||||
|
let done = plugins.q.defer();
|
||||||
|
done.resolve();
|
||||||
|
return done.promise;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exports.DbCollection = DbCollection;
|
exports.DbCollection = DbCollection;
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGJjb2xsZWN0aW9uLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRkYXRhLmNsYXNzZ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BQUMsQ0FBQyxLQUFLLEVBQVEsQ0FBQTtRQUNsQyxJQUFJLG9CQUFvQixHQUE4QixFQUFFLENBQUE7UUFDeEQsR0FBRyxDQUFDLENBQUMsSUFBSSxNQUFNLElBQUksV0FBVyxDQUFDLENBQUEsQ0FBQztZQUM1QixvQkFBb0IsQ0FBQyxJQUFJLENBQUMsSUFBSSxDQUFDLFFBQVEsQ0FBQyxNQUFNLENBQUMsQ0FBQyxDQUFBO1FBQ3BELENBQUM7UUFDRCxPQUFPLENBQUMsQ0FBQyxDQUFDLEdBQUcsQ0FBQyxvQkFBb0IsQ0FBQyxDQUFDLElBQUksQ0FBQztZQUNyQyxJQUFJLENBQUMsVUFBVSxDQUFDLFVBQVUsQ0FBQyxXQUFXLENBQUMsQ0FBQTtRQUMzQyxDQUFDLENBQUMsQ0FBQTtRQUNGLE1BQU0sQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFBO0lBQ3ZCLENBQUM7SUFFTyxRQUFRLENBQUMsR0FBTTtRQUNuQixJQUFJLElBQUksR0FBRyxPQUFPLENBQUMsQ0FBQyxDQUFDLEtBQUssRUFBUSxDQUFBO1FBQ2xDLElBQUksQ0FBQyxPQUFPLEVBQUUsQ0FBQTtRQUNkLE1BQU0sQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFBO0lBQ3ZCLENBQUM7QUFDTCxDQUFDO0FBdkRZLG9CQUFZLGVBdUR4QixDQUFBIn0=
|
25
test/test.js
25
test/test.js
@ -1,6 +1,7 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
require('typings-test');
|
require('typings-test');
|
||||||
const shelljs = require('shelljs');
|
const shelljs = require('shelljs');
|
||||||
|
const should = require('should');
|
||||||
const smartstring = require('smartstring');
|
const smartstring = require('smartstring');
|
||||||
// the tested module
|
// the tested module
|
||||||
const smartdata = require('../dist/index');
|
const smartdata = require('../dist/index');
|
||||||
@ -9,7 +10,7 @@ let testDbConnection;
|
|||||||
let testDbCollection;
|
let testDbCollection;
|
||||||
describe('mongodb', function () {
|
describe('mongodb', function () {
|
||||||
it('should start mongodb', function (done) {
|
it('should start mongodb', function (done) {
|
||||||
this.timeout(10000);
|
this.timeout(30000);
|
||||||
mongoChildProcess = shelljs.exec('mongod --dbpath=./test/data --port 27017', { async: true, silent: true });
|
mongoChildProcess = shelljs.exec('mongod --dbpath=./test/data --port 27017', { async: true, silent: true });
|
||||||
let doneCalled = false;
|
let doneCalled = false;
|
||||||
mongoChildProcess.stdout.on('data', function (data) {
|
mongoChildProcess.stdout.on('data', function (data) {
|
||||||
@ -31,18 +32,30 @@ describe('smartdata', function () {
|
|||||||
it('should create a collection', function () {
|
it('should create a collection', function () {
|
||||||
testDbCollection = new smartdata.DbCollection('something', testDbConnection);
|
testDbCollection = new smartdata.DbCollection('something', testDbConnection);
|
||||||
});
|
});
|
||||||
it('should insert something into the collection', function () {
|
it('should insert something into the collection', function (done) {
|
||||||
testDbCollection.insertOne({ hello: 'test' });
|
testDbCollection.insertOne({ hello: 'test' }).then(() => { done(); });
|
||||||
|
});
|
||||||
|
it('should find all instances of test', function (done) {
|
||||||
|
testDbCollection.find({}).then((resultArray) => {
|
||||||
|
console.log(resultArray);
|
||||||
|
should(resultArray[0].hello).equal('test');
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
it('should close the db Connection', function () {
|
it('should close the db Connection', function () {
|
||||||
testDbConnection.close();
|
testDbConnection.close();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('mongodb', function () {
|
describe('mongodb', function () {
|
||||||
it('should kill mongodb', function () {
|
it('should kill mongodb', function (done) {
|
||||||
this.timeout(10000);
|
this.timeout(30000);
|
||||||
|
mongoChildProcess.stdout.on('data', function (data) {
|
||||||
|
if (/dbexit: rc: 0/.test(data)) {
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
});
|
||||||
shelljs.exec('mongod --dbpath=./test/data --shutdown');
|
shelljs.exec('mongod --dbpath=./test/data --shutdown');
|
||||||
mongoChildProcess.k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UFDLGdCQUFnQixDQUFDLElBQUksQ0FBQyxJQUFJLENBQUMsQ0FBQyxDQUFDLENBQUM7Z0JBQzlCLElBQUksRUFBRSxDQUFBO1lBQ1YsQ0FBQztRQUNMLENBQUMsQ0FBQyxDQUFBO1FBQ0YsT0FBTyxDQUFDLElBQUksQ0FBQyx3Q0FBd0MsQ0FBQyxDQUFBO1FBQ3RELGlCQUFpQixDQUFDLElBQUksQ0FBQyxTQUFTLENBQUMsQ0FBQTtJQUNyQyxDQUFDLENBQUMsQ0FBQTtBQUNOLENBQUMsQ0FBQyxDQUFBIn0=
|
29
test/test.ts
29
test/test.ts
@ -13,12 +13,12 @@ let testDbCollection: smartdata.DbCollection<any>
|
|||||||
|
|
||||||
describe('mongodb', function () {
|
describe('mongodb', function () {
|
||||||
it('should start mongodb', function (done) {
|
it('should start mongodb', function (done) {
|
||||||
this.timeout(10000)
|
this.timeout(30000)
|
||||||
mongoChildProcess = shelljs.exec('mongod --dbpath=./test/data --port 27017', { async: true, silent: true })
|
mongoChildProcess = shelljs.exec('mongod --dbpath=./test/data --port 27017', { async: true, silent: true })
|
||||||
let doneCalled = false
|
let doneCalled = false
|
||||||
mongoChildProcess.stdout.on('data', function(data) {
|
mongoChildProcess.stdout.on('data', function (data) {
|
||||||
console.log(smartstring.indent.indentWithPrefix(data,"*** MongoDB Process *** : "))
|
console.log(smartstring.indent.indentWithPrefix(data, "*** MongoDB Process *** : "))
|
||||||
if (!doneCalled){
|
if (!doneCalled) {
|
||||||
if (/waiting for connections on port 27017/.test(data)) {
|
if (/waiting for connections on port 27017/.test(data)) {
|
||||||
doneCalled = true
|
doneCalled = true
|
||||||
done()
|
done()
|
||||||
@ -35,18 +35,29 @@ describe('smartdata', function () {
|
|||||||
it('should create a collection', function () {
|
it('should create a collection', function () {
|
||||||
testDbCollection = new smartdata.DbCollection('something', testDbConnection)
|
testDbCollection = new smartdata.DbCollection('something', testDbConnection)
|
||||||
})
|
})
|
||||||
it('should insert something into the collection',function(done){
|
it('should insert something into the collection', function (done) {
|
||||||
testDbCollection.insertOne({hello: 'test'}).then(() => { done() })
|
testDbCollection.insertOne({ hello: 'test' }).then(() => { done() })
|
||||||
|
})
|
||||||
|
it('should find all instances of test', function (done) {
|
||||||
|
testDbCollection.find({}).then((resultArray) => {
|
||||||
|
console.log(resultArray)
|
||||||
|
should(resultArray[0].hello).equal('test')
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
it('should find all instances of test')
|
|
||||||
it('should close the db Connection', function () {
|
it('should close the db Connection', function () {
|
||||||
testDbConnection.close()
|
testDbConnection.close()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('mongodb', function () {
|
describe('mongodb', function () {
|
||||||
it('should kill mongodb', function () {
|
it('should kill mongodb', function (done) {
|
||||||
this.timeout(10000)
|
this.timeout(30000)
|
||||||
|
mongoChildProcess.stdout.on('data', function (data) {
|
||||||
|
if (/dbexit: rc: 0/.test(data)) {
|
||||||
|
done()
|
||||||
|
}
|
||||||
|
})
|
||||||
shelljs.exec('mongod --dbpath=./test/data --shutdown')
|
shelljs.exec('mongod --dbpath=./test/data --shutdown')
|
||||||
mongoChildProcess.kill('SIGTERM')
|
mongoChildProcess.kill('SIGTERM')
|
||||||
})
|
})
|
||||||
|
@ -10,26 +10,50 @@ export class DbCollection<T> {
|
|||||||
/**
|
/**
|
||||||
* adds a validation function that all newly inserted and updated objects have to pass
|
* adds a validation function that all newly inserted and updated objects have to pass
|
||||||
*/
|
*/
|
||||||
addObjectValidation(funcArg){}
|
addObjectValidation(funcArg) { }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* finds an object in the DbCollection
|
* finds an object in the DbCollection
|
||||||
*/
|
*/
|
||||||
find(docMatchArg: T): T[] {
|
find(docMatchArg: T): plugins.q.Promise<T[]> {
|
||||||
return this.collection.find().toArray()
|
let done = plugins.q.defer<T[]>()
|
||||||
|
this.collection.find().toArray((err, docs) => {
|
||||||
|
if (err) { throw err }
|
||||||
|
done.resolve(docs)
|
||||||
|
})
|
||||||
|
return done.promise
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* inserts object into the DbCollection
|
* inserts object into the DbCollection
|
||||||
*/
|
*/
|
||||||
insertOne(docArg: T): PromiseLike<void> {
|
insertOne(docArg: T): plugins.q.Promise<void> {
|
||||||
return this.collection.insertOne(docArg)
|
let done = plugins.q.defer<void>()
|
||||||
|
this.checkDoc(docArg).then(() => {
|
||||||
|
this.collection.insertOne(docArg)
|
||||||
|
.then(() => { done.resolve() })
|
||||||
|
})
|
||||||
|
return done.promise
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* inserts many objects at once into the DbCollection
|
* inserts many objects at once into the DbCollection
|
||||||
*/
|
*/
|
||||||
insertMany(docArrayArg: T[]): void {
|
insertMany(docArrayArg: T[]): plugins.q.Promise<void> {
|
||||||
|
let done = plugins.q.defer<void>()
|
||||||
|
let checkDocPromiseArray: plugins.q.Promise<void>[] = []
|
||||||
|
for (let docArg of docArrayArg){
|
||||||
|
checkDocPromiseArray.push(this.checkDoc(docArg))
|
||||||
|
}
|
||||||
|
plugins.q.all(checkDocPromiseArray).then(() => {
|
||||||
|
this.collection.insertMany(docArrayArg)
|
||||||
|
})
|
||||||
|
return done.promise
|
||||||
|
}
|
||||||
|
|
||||||
|
private checkDoc(doc: T): plugins.q.Promise<void> {
|
||||||
|
let done = plugins.q.defer<void>()
|
||||||
|
done.resolve()
|
||||||
|
return done.promise
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user