Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
ae7d101ab9 | |||
3825b15a65 | |||
3fc41678d2 | |||
39f1b7ada1 | |||
f763de2403 | |||
cba1d031b6 | |||
d31e6a5931 | |||
46514a2743 | |||
1cf02157c4 | |||
72efa14da8 | |||
a73c2084c0 | |||
5ed557a21a | |||
2b6c30ff55 |
@ -1,29 +0,0 @@
|
||||
# smartdata
|
||||
|
||||
do more with data
|
||||
|
||||
## Availabililty
|
||||
|
||||
[](https://www.npmjs.com/package/smartdata)
|
||||
[](https://GitLab.com/pushrocks/smartdata)
|
||||
[](https://github.com/pushrocks/smartdata)
|
||||
[](https://pushrocks.gitlab.io/smartdata/)
|
||||
|
||||
## Status for master
|
||||
|
||||
[](https://GitLab.com/pushrocks/smartdata/commits/master)
|
||||
[](https://GitLab.com/pushrocks/smartdata/commits/master)
|
||||
[](https://www.npmjs.com/package/smartdata)
|
||||
[](https://david-dm.org/pushrocks/smartdata)
|
||||
[](https://www.bithound.io/github/pushrocks/smartdata/master/dependencies/npm)
|
||||
[](https://www.bithound.io/github/pushrocks/smartdata)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](http://standardjs.com/)
|
||||
|
||||
For further information read the linked docs at the top of this README.
|
||||
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
||||
|
||||
[](https://push.rocks)
|
@ -6,7 +6,7 @@
|
||||
},
|
||||
"npmci": {
|
||||
"npmGlobalTools": [
|
||||
"npmts"
|
||||
"@gitzone/npmts"
|
||||
],
|
||||
"npmAccessLevel": "public"
|
||||
},
|
||||
|
1693
package-lock.json
generated
1693
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
29
package.json
29
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartdata",
|
||||
"version": "3.0.2",
|
||||
"version": "3.1.3",
|
||||
"private": false,
|
||||
"description": "do more with data",
|
||||
"main": "dist/index.js",
|
||||
@ -21,20 +21,23 @@
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/smartdata#README",
|
||||
"dependencies": {
|
||||
"@pushrocks/smartlog": "^1.0.6",
|
||||
"@pushrocks/lik": "^3.0.4",
|
||||
"@pushrocks/smartlog": "^2.0.9",
|
||||
"@pushrocks/smartpromise": "^2.0.5",
|
||||
"@types/lodash": "^4.14.110",
|
||||
"@types/mongodb": "^3.1.1",
|
||||
"lik": "^2.0.5",
|
||||
"lodash": "^4.17.10",
|
||||
"mongodb": "^3.1.1",
|
||||
"runtime-type-checks": "0.0.4",
|
||||
"smartstring": "^2.0.28"
|
||||
"@pushrocks/smartstring": "^3.0.5",
|
||||
"@types/lodash": "^4.14.119",
|
||||
"@types/mongodb": "^3.1.18",
|
||||
"lodash": "^4.17.11",
|
||||
"mongodb": "^3.1.10",
|
||||
"runtime-type-checks": "0.0.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsrun": "^1.1.2",
|
||||
"@types/node": "^10.5.2",
|
||||
"qenv": "^1.1.7",
|
||||
"tapbundle": "^2.0.2"
|
||||
"@gitzone/tsrun": "^1.1.17",
|
||||
"@pushrocks/qenv": "^3.0.2",
|
||||
"@pushrocks/tapbundle": "^3.0.7",
|
||||
"@types/node": "^10.12.18",
|
||||
"@types/shortid": "0.0.29",
|
||||
"mongodb-memory-server": "^2.9.1",
|
||||
"shortid": "^2.2.14"
|
||||
}
|
||||
}
|
||||
|
10
test/test.ts
10
test/test.ts
@ -1,12 +1,13 @@
|
||||
import { tap, expect } from 'tapbundle';
|
||||
import { tap, expect } from '@pushrocks/tapbundle';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import { Qenv } from 'qenv';
|
||||
import { Qenv } from '@pushrocks/qenv';
|
||||
|
||||
let testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit/');
|
||||
|
||||
// the tested module
|
||||
import * as smartdata from '../ts';
|
||||
import * as smartdata from '../ts/index';
|
||||
import { smartstring } from '../ts/smartdata.plugins';
|
||||
import * as shortid from 'shortid';
|
||||
|
||||
// =======================================
|
||||
// Connecting to the database server
|
||||
@ -32,6 +33,7 @@ tap.test('should establish a connection to the rethink Db cluster', async () =>
|
||||
|
||||
@smartdata.Collection(testDb)
|
||||
class Car extends smartdata.SmartDataDbDoc<Car> {
|
||||
@smartdata.unI() index: string = shortid();
|
||||
@smartdata.svDb() color: string;
|
||||
@smartdata.svDb() brand: string;
|
||||
constructor(colorArg: string, brandArg: string) {
|
||||
@ -60,4 +62,4 @@ tap.test('should close the database connection', async tools => {
|
||||
await testDb.close();
|
||||
});
|
||||
|
||||
tap.start();
|
||||
tap.start({throwOnError: true});
|
||||
|
@ -31,6 +31,7 @@ export class SmartdataCollection<T> {
|
||||
objectValidation: IDocValidationFunc<T> = null;
|
||||
collectionName: string;
|
||||
smartdataDb: SmartdataDb;
|
||||
uniqueIndexes: string[] = [];
|
||||
|
||||
constructor(collectedClassArg: T & SmartDataDbDoc<T>, smartDataDbArg: SmartdataDb) {
|
||||
// tell the collection where it belongs
|
||||
@ -59,6 +60,21 @@ export class SmartdataCollection<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* mark unique index
|
||||
*/
|
||||
markUniqueIndexes(keyArrayArg: string[] = []) {
|
||||
for(let key of keyArrayArg) {
|
||||
if(!this.uniqueIndexes.includes(key)) {
|
||||
this.mongoDbCollection.createIndex(key, {
|
||||
unique: true
|
||||
});
|
||||
// make sure we only call this once and not for every doc we create
|
||||
this.uniqueIndexes.push(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* adds a validation function that all newly inserted and updated objects have to pass
|
||||
*/
|
||||
@ -81,6 +97,7 @@ export class SmartdataCollection<T> {
|
||||
async insert(dbDocArg: T & SmartDataDbDoc<T>): Promise<any> {
|
||||
await this.init();
|
||||
await this.checkDoc(dbDocArg);
|
||||
this.markUniqueIndexes(dbDocArg.uniqueIndexes);
|
||||
const saveableObject = await dbDocArg.createSavableObject();
|
||||
console.log(saveableObject);
|
||||
const result = await this.mongoDbCollection.insertOne(saveableObject);
|
||||
|
@ -1,5 +1,5 @@
|
||||
import * as plugins from './smartdata.plugins';
|
||||
import { Objectmap } from 'lik';
|
||||
import { Objectmap } from '@pushrocks/lik';
|
||||
|
||||
import { SmartdataCollection } from './smartdata.classes.collection';
|
||||
|
||||
@ -71,8 +71,8 @@ export class SmartdataDb {
|
||||
await this.mongoDbClient.close();
|
||||
this.status = 'disconnected';
|
||||
plugins.smartlog
|
||||
.getDefaultLogger()
|
||||
.info(`disconnected from database ${this.smartdataOptions.mongoDbName}`);
|
||||
.defaultLogger
|
||||
.log('info', `disconnected from database ${this.smartdataOptions.mongoDbName}`);
|
||||
}
|
||||
|
||||
// handle table to class distribution
|
||||
|
@ -12,7 +12,7 @@ export type TDocCreation = 'db' | 'new' | 'mixed';
|
||||
*/
|
||||
export function svDb() {
|
||||
return (target: SmartDataDbDoc<any>, key: string) => {
|
||||
console.log('called sva');
|
||||
console.log(`called svDb() on ${key}`);
|
||||
if (!target.saveableProperties) {
|
||||
target.saveableProperties = [];
|
||||
}
|
||||
@ -20,6 +20,27 @@ export function svDb() {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* unique index - decorator to mark a unique index
|
||||
*/
|
||||
export function unI() {
|
||||
return (target: SmartDataDbDoc<any>, key: string) => {
|
||||
console.log('called unI');
|
||||
|
||||
// mark the index as unique
|
||||
if (!target.uniqueIndexes) {
|
||||
target.uniqueIndexes = [];
|
||||
}
|
||||
target.uniqueIndexes.push(key);
|
||||
|
||||
// and also save it
|
||||
if (!target.saveableProperties) {
|
||||
target.saveableProperties = [];
|
||||
}
|
||||
target.saveableProperties.push(key);
|
||||
};
|
||||
}
|
||||
|
||||
export class SmartDataDbDoc<T> {
|
||||
/**
|
||||
* the collection object an Doc belongs to
|
||||
@ -31,6 +52,11 @@ export class SmartDataDbDoc<T> {
|
||||
*/
|
||||
creationStatus: TDocCreation = 'new';
|
||||
|
||||
/**
|
||||
* unique indexes
|
||||
*/
|
||||
uniqueIndexes: string[];
|
||||
|
||||
/**
|
||||
* an array of saveable properties of a doc
|
||||
*/
|
||||
|
@ -2,7 +2,7 @@ import * as assert from 'assert';
|
||||
import * as smartlog from '@pushrocks/smartlog';
|
||||
import * as lodash from 'lodash';
|
||||
import * as mongodb from 'mongodb';
|
||||
import * as smartq from 'smartq';
|
||||
import * as smartstring from 'smartstring';
|
||||
import * as smartq from '@pushrocks/smartpromise';
|
||||
import * as smartstring from '@pushrocks/smartstring';
|
||||
|
||||
export { assert, smartlog, lodash, smartq, mongodb, smartstring };
|
||||
|
Reference in New Issue
Block a user