Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
c6cbff1308 | |||
3eeac7b936 | |||
761b742e21 | |||
e56def621c | |||
ab1799f4f2 | |||
9985b849d1 | |||
1800273b25 |
36
README.md
36
README.md
@ -1,25 +1,19 @@
|
|||||||
# @pushrocks/smartdata
|
# @pushrocks/smartdata
|
||||||
|
do more with data
|
||||||
|
|
||||||
do more with data and RethinkDB
|
## Availabililty and Links
|
||||||
|
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartdata)
|
||||||
## Availabililty
|
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartdata)
|
||||||
|
* [github.com (source mirror)](https://github.com/pushrocks/smartdata)
|
||||||
[](https://www.npmjs.com/package/smartdata)
|
* [docs (typedoc)](https://pushrocks.gitlab.io/smartdata/)
|
||||||
[](https://GitLab.com/pushrocks/smartdata)
|
|
||||||
[](https://github.com/pushrocks/smartdata)
|
|
||||||
[](https://pushrocks.gitlab.io/smartdata/)
|
|
||||||
|
|
||||||
## Status for master
|
## Status for master
|
||||||
|
[](https://gitlab.com/pushrocks/smartdata/commits/master)
|
||||||
[](https://GitLab.com/pushrocks/smartdata/commits/master)
|
[](https://gitlab.com/pushrocks/smartdata/commits/master)
|
||||||
[](https://GitLab.com/pushrocks/smartdata/commits/master)
|
[](https://www.npmjs.com/package/@pushrocks/smartdata)
|
||||||
[](https://www.npmjs.com/package/smartdata)
|
[](https://snyk.io/test/npm/@pushrocks/smartdata)
|
||||||
[](https://david-dm.org/pushrocks/smartdata)
|
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
[](https://www.bithound.io/github/pushrocks/smartdata/master/dependencies/npm)
|
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
[](https://www.bithound.io/github/pushrocks/smartdata)
|
|
||||||
[](https://snyk.io/test/npm/smartdata)
|
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
|
||||||
[](http://standardjs.com/)
|
[](http://standardjs.com/)
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
@ -121,9 +115,9 @@ you should get all the Intellisense and type checking you love when using smartd
|
|||||||
smartdata itself also bundles typings.
|
smartdata itself also bundles typings.
|
||||||
So you don't need to install any additional types when importing smartdata.
|
So you don't need to install any additional types when importing smartdata.
|
||||||
|
|
||||||
For further information read the linked docs at the top of this README.
|
For further information read the linked docs at the top of this readme.
|
||||||
|
|
||||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||||
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
||||||
|
|
||||||
[](https://push.rocks)
|
[](https://maintainedby.lossless.com)
|
||||||
|
@ -8,5 +8,15 @@
|
|||||||
"npmGlobalTools": [],
|
"npmGlobalTools": [],
|
||||||
"npmAccessLevel": "public",
|
"npmAccessLevel": "public",
|
||||||
"npmRegistryUrl": "registry.npmjs.org"
|
"npmRegistryUrl": "registry.npmjs.org"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"module": {
|
||||||
|
"githost": "gitlab.com",
|
||||||
|
"gitscope": "pushrocks",
|
||||||
|
"gitrepo": "smartdata",
|
||||||
|
"shortDescription": "do more with data",
|
||||||
|
"npmPackagename": "@pushrocks/smartdata",
|
||||||
|
"license": "MIT"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartdata",
|
"name": "@pushrocks/smartdata",
|
||||||
"version": "3.1.10",
|
"version": "3.1.14",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartdata",
|
"name": "@pushrocks/smartdata",
|
||||||
"version": "3.1.10",
|
"version": "3.1.14",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "do more with data",
|
"description": "do more with data",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
@ -20,7 +20,7 @@ let testDb = new smartdata.SmartdataDb({
|
|||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should establish a connection to the rethink Db cluster', async () => {
|
tap.test('should establish a connection to the rethink Db cluster', async () => {
|
||||||
await testDb.connect();
|
await testDb.init();
|
||||||
});
|
});
|
||||||
|
|
||||||
// =======================================
|
// =======================================
|
||||||
|
@ -2,4 +2,4 @@ export * from './smartdata.classes.db';
|
|||||||
export * from './smartdata.classes.collection';
|
export * from './smartdata.classes.collection';
|
||||||
export * from './smartdata.classes.doc';
|
export * from './smartdata.classes.doc';
|
||||||
|
|
||||||
export { IMongoDescriptor } from './interfaces';
|
export { IMongoDescriptor } from './interfaces';
|
||||||
|
@ -1 +1 @@
|
|||||||
export * from './mongodescriptor';
|
export * from './mongodescriptor';
|
||||||
|
@ -26,10 +26,9 @@ export function Collection(dbArg: SmartdataDb | TDelayedDbCreation) {
|
|||||||
constructor['smartdataCollection'] = new SmartdataCollection(constructor, dbArg);
|
constructor['smartdataCollection'] = new SmartdataCollection(constructor, dbArg);
|
||||||
} else {
|
} else {
|
||||||
constructor['smartdataDelayedCollection'] = () => {
|
constructor['smartdataDelayedCollection'] = () => {
|
||||||
return new SmartdataCollection(constructor, dbArg());
|
return new SmartdataCollection(constructor, dbArg());
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,7 +108,6 @@ export class SmartdataCollection<T> {
|
|||||||
await this.checkDoc(dbDocArg);
|
await this.checkDoc(dbDocArg);
|
||||||
this.markUniqueIndexes(dbDocArg.uniqueIndexes);
|
this.markUniqueIndexes(dbDocArg.uniqueIndexes);
|
||||||
const saveableObject = await dbDocArg.createSavableObject();
|
const saveableObject = await dbDocArg.createSavableObject();
|
||||||
console.log(saveableObject);
|
|
||||||
const result = await this.mongoDbCollection.insertOne(saveableObject);
|
const result = await this.mongoDbCollection.insertOne(saveableObject);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -117,11 +115,19 @@ export class SmartdataCollection<T> {
|
|||||||
/**
|
/**
|
||||||
* inserts object into the DbCollection
|
* inserts object into the DbCollection
|
||||||
*/
|
*/
|
||||||
async update(dbDocArg: T & SmartDataDbDoc<T>): Promise<any> {
|
public async update(dbDocArg: T & SmartDataDbDoc<T>): Promise<any> {
|
||||||
await this.init();
|
await this.init();
|
||||||
await this.checkDoc(dbDocArg);
|
await this.checkDoc(dbDocArg);
|
||||||
|
const identifiableObject = await dbDocArg.createIdentifiableObject();
|
||||||
const saveableObject = await dbDocArg.createSavableObject();
|
const saveableObject = await dbDocArg.createSavableObject();
|
||||||
this.mongoDbCollection.updateOne(saveableObject.dbDocUniqueId, saveableObject);
|
this.mongoDbCollection.updateOne(identifiableObject, saveableObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async delete(dbDocArg: T & SmartDataDbDoc<T>): Promise<any> {
|
||||||
|
await this.init();
|
||||||
|
await this.checkDoc(dbDocArg);
|
||||||
|
const identifiableObject = await dbDocArg.createIdentifiableObject();
|
||||||
|
this.mongoDbCollection.deleteOne(identifiableObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -129,7 +135,7 @@ export class SmartdataCollection<T> {
|
|||||||
* if this.objectValidation is not set it passes.
|
* if this.objectValidation is not set it passes.
|
||||||
*/
|
*/
|
||||||
private checkDoc(docArg: T): Promise<void> {
|
private checkDoc(docArg: T): Promise<void> {
|
||||||
let done = plugins.smartq.defer<void>();
|
const done = plugins.smartq.defer<void>();
|
||||||
let validationResult = true;
|
let validationResult = true;
|
||||||
if (this.objectValidation) {
|
if (this.objectValidation) {
|
||||||
validationResult = this.objectValidation(docArg);
|
validationResult = this.objectValidation(docArg);
|
||||||
|
@ -44,7 +44,7 @@ export class SmartdataDb {
|
|||||||
/**
|
/**
|
||||||
* connects to the database that was specified during instance creation
|
* connects to the database that was specified during instance creation
|
||||||
*/
|
*/
|
||||||
async connect(): Promise<any> {
|
async init(): Promise<any> {
|
||||||
let finalConnectionUrl = this.smartdataOptions.mongoDbUrl;
|
let finalConnectionUrl = this.smartdataOptions.mongoDbUrl;
|
||||||
if (this.smartdataOptions.mongoDbPass) {
|
if (this.smartdataOptions.mongoDbPass) {
|
||||||
finalConnectionUrl = mongoHelpers.addPassword(
|
finalConnectionUrl = mongoHelpers.addPassword(
|
||||||
@ -53,12 +53,9 @@ export class SmartdataDb {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
console.log(finalConnectionUrl);
|
console.log(finalConnectionUrl);
|
||||||
this.mongoDbClient = await plugins.mongodb.MongoClient.connect(
|
this.mongoDbClient = await plugins.mongodb.MongoClient.connect(finalConnectionUrl, {
|
||||||
finalConnectionUrl,
|
useNewUrlParser: true
|
||||||
{
|
});
|
||||||
useNewUrlParser: true
|
|
||||||
}
|
|
||||||
);
|
|
||||||
this.mongoDb = this.mongoDbClient.db(this.smartdataOptions.mongoDbName);
|
this.mongoDb = this.mongoDbClient.db(this.smartdataOptions.mongoDbName);
|
||||||
this.status = 'connected';
|
this.status = 'connected';
|
||||||
console.log(`Connected to database ${this.smartdataOptions.mongoDbName}`);
|
console.log(`Connected to database ${this.smartdataOptions.mongoDbName}`);
|
||||||
|
@ -77,7 +77,7 @@ export class SmartDataDbDoc<T> {
|
|||||||
*/
|
*/
|
||||||
constructor() {
|
constructor() {
|
||||||
this.name = this.constructor['name'];
|
this.name = this.constructor['name'];
|
||||||
if(this.constructor['smartdataCollection']) {
|
if (this.constructor['smartdataCollection']) {
|
||||||
// tslint:disable-next-line: no-string-literal
|
// tslint:disable-next-line: no-string-literal
|
||||||
this.collection = this.constructor['smartdataCollection'];
|
this.collection = this.constructor['smartdataCollection'];
|
||||||
// tslint:disable-next-line: no-string-literal
|
// tslint:disable-next-line: no-string-literal
|
||||||
@ -92,12 +92,12 @@ export class SmartDataDbDoc<T> {
|
|||||||
static async getInstances<T>(filterArg): Promise<T[]> {
|
static async getInstances<T>(filterArg): Promise<T[]> {
|
||||||
let self: any = this; // fool typesystem
|
let self: any = this; // fool typesystem
|
||||||
let referenceMongoDBCollection: SmartdataCollection<T>;
|
let referenceMongoDBCollection: SmartdataCollection<T>;
|
||||||
|
|
||||||
if (self.smartdataCollection) {
|
if (self.smartdataCollection) {
|
||||||
referenceMongoDBCollection = self.smartdataCollection;
|
referenceMongoDBCollection = self.smartdataCollection;
|
||||||
} else if (self.smartdataDelayedCollection) {
|
} else if (self.smartdataDelayedCollection) {
|
||||||
referenceMongoDBCollection = self.smartdataDelayedCollection();
|
referenceMongoDBCollection = self.smartdataDelayedCollection();
|
||||||
};
|
}
|
||||||
const foundDocs = await referenceMongoDBCollection.find(filterArg);
|
const foundDocs = await referenceMongoDBCollection.find(filterArg);
|
||||||
const returnArray = [];
|
const returnArray = [];
|
||||||
for (let item of foundDocs) {
|
for (let item of foundDocs) {
|
||||||
@ -139,6 +139,11 @@ export class SmartDataDbDoc<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* deletes a document from the database
|
||||||
|
*/
|
||||||
|
async delete() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* also store any referenced objects to DB
|
* also store any referenced objects to DB
|
||||||
* better for data consistency
|
* better for data consistency
|
||||||
@ -157,11 +162,25 @@ export class SmartDataDbDoc<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async createSavableObject() {
|
/**
|
||||||
let saveableObject: any = {}; // is not exposed to outside, so any is ok here
|
* creates a saveable object so the instance can be persisted as json in the database
|
||||||
for (let propertyNameString of this.saveableProperties) {
|
*/
|
||||||
|
public async createSavableObject() {
|
||||||
|
const saveableObject: any = {}; // is not exposed to outside, so any is ok here
|
||||||
|
for (const propertyNameString of this.saveableProperties) {
|
||||||
saveableObject[propertyNameString] = this[propertyNameString];
|
saveableObject[propertyNameString] = this[propertyNameString];
|
||||||
}
|
}
|
||||||
return saveableObject;
|
return saveableObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* creates an identifiable object for operations that require filtering
|
||||||
|
*/
|
||||||
|
public async createIdentifiableObject() {
|
||||||
|
const identifiableObject: any = {}; // is not exposed to outside, so any is ok here
|
||||||
|
for (const propertyNameString of this.uniqueIndexes) {
|
||||||
|
identifiableObject[propertyNameString] = this[propertyNameString];
|
||||||
|
}
|
||||||
|
return identifiableObject;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user