Compare commits
28 Commits
Author | SHA1 | Date | |
---|---|---|---|
7e9fc62ce7 | |||
670a0c63fb | |||
64e76b3ed1 | |||
25803330de | |||
c6cbff1308 | |||
3eeac7b936 | |||
761b742e21 | |||
e56def621c | |||
ab1799f4f2 | |||
9985b849d1 | |||
1800273b25 | |||
e715cc6d37 | |||
f7abc175aa | |||
bbcb3a614e | |||
941d2f0902 | |||
5c78f83a28 | |||
124f117352 | |||
f67da898ae | |||
3c8f70f77e | |||
3674d7d9a5 | |||
fe2a622488 | |||
f092eefbf3 | |||
8d0aa361f9 | |||
692a1223a8 | |||
ae7d101ab9 | |||
3825b15a65 | |||
3fc41678d2 | |||
39f1b7ada1 |
@ -26,6 +26,7 @@ mirror:
|
|||||||
snyk:
|
snyk:
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
- npmci command npm install -g snyk
|
- npmci command npm install -g snyk
|
||||||
- npmci command npm install --ignore-scripts
|
- npmci command npm install --ignore-scripts
|
||||||
- npmci command snyk test
|
- npmci command snyk test
|
||||||
@ -33,24 +34,39 @@ snyk:
|
|||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
|
sast:
|
||||||
|
stage: security
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-dbase:npmci
|
||||||
|
variables:
|
||||||
|
DOCKER_DRIVER: overlay2
|
||||||
|
allow_failure: true
|
||||||
|
services:
|
||||||
|
- docker:stable-dind
|
||||||
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci npm install
|
||||||
|
- npmci command npm run build
|
||||||
|
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
||||||
|
- docker run
|
||||||
|
--env SAST_CONFIDENCE_LEVEL="${SAST_CONFIDENCE_LEVEL:-3}"
|
||||||
|
--volume "$PWD:/code"
|
||||||
|
--volume /var/run/docker.sock:/var/run/docker.sock
|
||||||
|
"registry.gitlab.com/gitlab-org/security-products/sast:$SP_VERSION" /app/bin/run /code
|
||||||
|
artifacts:
|
||||||
|
reports:
|
||||||
|
sast: gl-sast-report.json
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
- priv
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# test stage
|
# test stage
|
||||||
# ====================
|
# ====================
|
||||||
testLEGACY:
|
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci node install legacy
|
|
||||||
- npmci npm install
|
|
||||||
- npmci npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
allow_failure: true
|
|
||||||
|
|
||||||
testLTS:
|
testLTS:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
- npmci node install lts
|
- npmci node install lts
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci npm test
|
- npmci npm test
|
||||||
@ -62,6 +78,7 @@ testLTS:
|
|||||||
testSTABLE:
|
testSTABLE:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
- npmci node install stable
|
- npmci node install stable
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci npm test
|
- npmci npm test
|
||||||
@ -117,8 +134,10 @@ pages:
|
|||||||
image: hosttoday/ht-docker-node:npmci
|
image: hosttoday/ht-docker-node:npmci
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g npmpage
|
- npmci command npm install -g typedoc typescript
|
||||||
- npmci command npmpage
|
- npmci npm prepare
|
||||||
|
- npmci npm install
|
||||||
|
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
@ -129,13 +148,3 @@ pages:
|
|||||||
paths:
|
paths:
|
||||||
- public
|
- public
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
|
|
||||||
windowsCompatibility:
|
|
||||||
image: stefanscherer/node-windows:10-build-tools
|
|
||||||
stage: metadata
|
|
||||||
script:
|
|
||||||
- npm install & npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- windows
|
|
||||||
allow_failure: true
|
|
||||||
|
46
README.md
46
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
|
||||||
@ -37,11 +31,11 @@ This is why we started smartdata.
|
|||||||
|
|
||||||
How RethinkDB's terms map to the ones of smartdata:
|
How RethinkDB's terms map to the ones of smartdata:
|
||||||
|
|
||||||
| MongoDb term | smartdata class |
|
| MongoDb term | smartdata class |
|
||||||
| -------------- | ------------------------------|
|
| ------------ | ----------------------------- |
|
||||||
| Database | smartdata.SmartdataDb |
|
| Database | smartdata.SmartdataDb |
|
||||||
| Collection | smartdata.SmartdataCollection |
|
| Collection | smartdata.SmartdataCollection |
|
||||||
| Document | smartdata.SmartadataDoc |
|
| Document | smartdata.SmartadataDoc |
|
||||||
|
|
||||||
### class Db
|
### class Db
|
||||||
|
|
||||||
@ -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)
|
||||||
|
@ -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)
|
|
@ -5,14 +5,18 @@
|
|||||||
"dockerSock": false
|
"dockerSock": false
|
||||||
},
|
},
|
||||||
"npmci": {
|
"npmci": {
|
||||||
"npmGlobalTools": [
|
"npmGlobalTools": [],
|
||||||
"@gitzone/npmts"
|
"npmAccessLevel": "public",
|
||||||
],
|
"npmRegistryUrl": "registry.npmjs.org"
|
||||||
"npmAccessLevel": "public"
|
|
||||||
},
|
},
|
||||||
"npmts": {
|
"gitzone": {
|
||||||
"testConfig": {
|
"module": {
|
||||||
"parallel": false
|
"githost": "gitlab.com",
|
||||||
|
"gitscope": "pushrocks",
|
||||||
|
"gitrepo": "smartdata",
|
||||||
|
"shortDescription": "do more with data",
|
||||||
|
"npmPackagename": "@pushrocks/smartdata",
|
||||||
|
"license": "MIT"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
2235
package-lock.json
generated
2235
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
40
package.json
40
package.json
@ -1,14 +1,14 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartdata",
|
"name": "@pushrocks/smartdata",
|
||||||
"version": "3.1.1",
|
"version": "3.1.16",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "do more with data",
|
"description": "do more with data",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "tsrun test/test.ts",
|
"test": "(tstest test/)",
|
||||||
"testLocal": "(npmdocker)",
|
"testLocal": "(npmdocker)",
|
||||||
"build": "(npmts)"
|
"build": "(tsbuild)"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -21,23 +21,27 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartdata#README",
|
"homepage": "https://gitlab.com/pushrocks/smartdata#README",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartlog": "^1.0.6",
|
"@pushrocks/lik": "^3.0.11",
|
||||||
"@pushrocks/smartpromise": "^2.0.5",
|
"@pushrocks/smartlog": "^2.0.19",
|
||||||
"@types/lodash": "^4.14.110",
|
"@pushrocks/smartpromise": "^3.0.2",
|
||||||
"@types/mongodb": "^3.1.1",
|
"@pushrocks/smartstring": "^3.0.10",
|
||||||
"lik": "^2.0.5",
|
"@pushrocks/smartunique": "^3.0.1",
|
||||||
"lodash": "^4.17.10",
|
"@types/lodash": "^4.14.138",
|
||||||
"mongodb": "^3.1.1",
|
"@types/mongodb": "^3.3.1",
|
||||||
"runtime-type-checks": "0.0.4",
|
"lodash": "^4.17.15",
|
||||||
"smartstring": "^2.0.28"
|
"mongodb": "^3.3.2",
|
||||||
|
"runtime-type-checks": "0.0.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsrun": "^1.1.2",
|
"@gitzone/tsbuild": "^2.1.17",
|
||||||
"@types/node": "^10.5.2",
|
"@gitzone/tstest": "^1.0.24",
|
||||||
|
"@pushrocks/qenv": "^4.0.4",
|
||||||
|
"@pushrocks/tapbundle": "^3.0.13",
|
||||||
|
"@types/mongodb-memory-server": "^1.8.0",
|
||||||
|
"@types/node": "^12.7.3",
|
||||||
"@types/shortid": "0.0.29",
|
"@types/shortid": "0.0.29",
|
||||||
"mongodb-memory-server": "^1.9.0",
|
"mongodb-memory-server": "^5.2.0",
|
||||||
"qenv": "^1.1.7",
|
"tslint": "^5.19.0",
|
||||||
"shortid": "^2.2.11",
|
"tslint-config-prettier": "^1.18.0"
|
||||||
"tapbundle": "^2.0.2"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
2
qenv.yml
2
qenv.yml
@ -1,4 +1,4 @@
|
|||||||
vars:
|
required:
|
||||||
- MONGO_URL
|
- MONGO_URL
|
||||||
- MONGO_DBNAME
|
- MONGO_DBNAME
|
||||||
- MONGO_PASS
|
- MONGO_PASS
|
||||||
|
78
test/test.ts
78
test/test.ts
@ -1,26 +1,44 @@
|
|||||||
import { tap, expect } from 'tapbundle';
|
import { tap, expect } from '@pushrocks/tapbundle';
|
||||||
import * as smartpromise from '@pushrocks/smartpromise';
|
import { Qenv } from '@pushrocks/qenv';
|
||||||
import { Qenv } from 'qenv';
|
|
||||||
|
|
||||||
let testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit/');
|
const testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit/');
|
||||||
|
|
||||||
// the tested module
|
// the tested module
|
||||||
import * as smartdata from '../ts';
|
import * as smartdata from '../ts/index';
|
||||||
import { smartstring } from '../ts/smartdata.plugins';
|
import { smartstring } from '../ts/smartdata.plugins';
|
||||||
import * as shortid from 'shortid';
|
import * as smartunique from '@pushrocks/smartunique';
|
||||||
|
|
||||||
|
import * as mongoPlugin from 'mongodb-memory-server';
|
||||||
|
|
||||||
// =======================================
|
// =======================================
|
||||||
// Connecting to the database server
|
// Connecting to the database server
|
||||||
// =======================================
|
// =======================================
|
||||||
|
|
||||||
let testDb = new smartdata.SmartdataDb({
|
let testDb: smartdata.SmartdataDb;
|
||||||
mongoDbName: process.env.MONGO_DBNAME,
|
let smartdataOptions: smartdata.ISmartdataOptions;
|
||||||
mongoDbUrl: process.env.MONGO_URL,
|
let mongod: mongoPlugin.MongoMemoryServer;
|
||||||
mongoDbPass: process.env.MONGO_PASS
|
|
||||||
|
tap.test('should create a testinstance as database', async () => {
|
||||||
|
mongod = new mongoPlugin.MongoMemoryServer();
|
||||||
|
smartdataOptions = {
|
||||||
|
mongoDbName: await mongod.getDbName(),
|
||||||
|
mongoDbPass: '',
|
||||||
|
mongoDbUrl: await mongod.getConnectionString()
|
||||||
|
};
|
||||||
|
console.log(smartdataOptions);
|
||||||
|
testDb = new smartdata.SmartdataDb(smartdataOptions);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.skip.test('should create a smartdb', async () => {
|
||||||
|
testDb = new smartdata.SmartdataDb({
|
||||||
|
mongoDbName: testQenv.getEnvVarOnDemand('MONGO_DBNAME'),
|
||||||
|
mongoDbUrl: testQenv.getEnvVarOnDemand('MONGO_URL'),
|
||||||
|
mongoDbPass: testQenv.getEnvVarOnDemand('MONGO_PASS')
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
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();
|
||||||
});
|
});
|
||||||
|
|
||||||
// =======================================
|
// =======================================
|
||||||
@ -31,11 +49,19 @@ tap.test('should establish a connection to the rethink Db cluster', async () =>
|
|||||||
// Collections
|
// Collections
|
||||||
// ------
|
// ------
|
||||||
|
|
||||||
@smartdata.Collection(testDb)
|
@smartdata.Collection(() => {
|
||||||
|
return testDb;
|
||||||
|
})
|
||||||
class Car extends smartdata.SmartDataDbDoc<Car> {
|
class Car extends smartdata.SmartDataDbDoc<Car> {
|
||||||
@smartdata.unI() index: string = shortid();
|
@smartdata.unI()
|
||||||
@smartdata.svDb() color: string;
|
public index: string = smartunique.shortId();
|
||||||
@smartdata.svDb() brand: string;
|
|
||||||
|
@smartdata.svDb()
|
||||||
|
public color: string;
|
||||||
|
|
||||||
|
@smartdata.svDb()
|
||||||
|
public brand: string;
|
||||||
|
|
||||||
constructor(colorArg: string, brandArg: string) {
|
constructor(colorArg: string, brandArg: string) {
|
||||||
super();
|
super();
|
||||||
this.color = colorArg;
|
this.color = colorArg;
|
||||||
@ -46,20 +72,38 @@ class Car extends smartdata.SmartDataDbDoc<Car> {
|
|||||||
tap.test('should save the car to the db', async () => {
|
tap.test('should save the car to the db', async () => {
|
||||||
const myCar = new Car('red', 'Volvo');
|
const myCar = new Car('red', 'Volvo');
|
||||||
await myCar.save();
|
await myCar.save();
|
||||||
|
|
||||||
|
const myCar2 = new Car('red', 'Renault');
|
||||||
|
await myCar2.save();
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('expect to get instance of Car', async () => {
|
tap.test('expect to get instance of Car', async () => {
|
||||||
let myCars = await Car.getInstances<Car>({
|
const myCars = await Car.getInstances<Car>({
|
||||||
brand: 'Volvo'
|
brand: 'Volvo'
|
||||||
});
|
});
|
||||||
expect(myCars[0].color).to.equal('red');
|
expect(myCars[0].color).to.equal('red');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('expect to get instance of Car and update it', async () => {
|
||||||
|
const myCar = await Car.getInstance<Car>({
|
||||||
|
brand: 'Volvo'
|
||||||
|
});
|
||||||
|
expect(myCar.color).to.equal('red');
|
||||||
|
myCar.color = 'blue';
|
||||||
|
await myCar.save();
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should be able to update an instance of car', async () => {});
|
||||||
|
|
||||||
|
tap.test('should be able to delete an instance of car', async () => {});
|
||||||
|
|
||||||
// =======================================
|
// =======================================
|
||||||
// close the database connection
|
// close the database connection
|
||||||
// =======================================
|
// =======================================
|
||||||
tap.test('should close the database connection', async tools => {
|
tap.test('should close the database connection', async tools => {
|
||||||
await testDb.close();
|
await testDb.close();
|
||||||
|
await mongod.stop();
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start({throwOnError: true});
|
tap.start({ throwOnError: true });
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
export * from './smartdata.classes.db';
|
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';
|
||||||
|
1
ts/interfaces/index.ts
Normal file
1
ts/interfaces/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './mongodescriptor';
|
5
ts/interfaces/mongodescriptor.ts
Normal file
5
ts/interfaces/mongodescriptor.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
export interface IMongoDescriptor {
|
||||||
|
mongoDbName: string;
|
||||||
|
mongoDbUrl: string;
|
||||||
|
mongoDbPass: string;
|
||||||
|
}
|
@ -13,13 +13,22 @@ export interface IDocValidationFunc<T> {
|
|||||||
(doc: T): boolean;
|
(doc: T): boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type TDelayedDbCreation = () => SmartdataDb;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is a decorator that will tell the decorated class what dbTable to use
|
* This is a decorator that will tell the decorated class what dbTable to use
|
||||||
* @param db
|
* @param dbArg
|
||||||
*/
|
*/
|
||||||
export function Collection(db: SmartdataDb) {
|
export function Collection(dbArg: SmartdataDb | TDelayedDbCreation) {
|
||||||
return function(constructor) {
|
return function(constructor) {
|
||||||
constructor['smartdataCollection'] = new SmartdataCollection(constructor, db);
|
if (dbArg instanceof SmartdataDb) {
|
||||||
|
// tslint:disable-next-line: no-string-literal
|
||||||
|
constructor['smartdataCollection'] = new SmartdataCollection(constructor, dbArg);
|
||||||
|
} else {
|
||||||
|
constructor['smartdataDelayedCollection'] = () => {
|
||||||
|
return new SmartdataCollection(constructor, dbArg());
|
||||||
|
};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,11 +36,11 @@ export class SmartdataCollection<T> {
|
|||||||
/**
|
/**
|
||||||
* the collection that is used
|
* the collection that is used
|
||||||
*/
|
*/
|
||||||
mongoDbCollection: plugins.mongodb.Collection;
|
public mongoDbCollection: plugins.mongodb.Collection;
|
||||||
objectValidation: IDocValidationFunc<T> = null;
|
public objectValidation: IDocValidationFunc<T> = null;
|
||||||
collectionName: string;
|
public collectionName: string;
|
||||||
smartdataDb: SmartdataDb;
|
public smartdataDb: SmartdataDb;
|
||||||
uniqueIndexes: string[] = [];
|
public uniqueIndexes: string[] = [];
|
||||||
|
|
||||||
constructor(collectedClassArg: T & SmartDataDbDoc<T>, smartDataDbArg: SmartdataDb) {
|
constructor(collectedClassArg: T & SmartDataDbDoc<T>, smartDataDbArg: SmartdataDb) {
|
||||||
// tell the collection where it belongs
|
// tell the collection where it belongs
|
||||||
@ -45,7 +54,7 @@ export class SmartdataCollection<T> {
|
|||||||
/**
|
/**
|
||||||
* makes sure a collection exists within MongoDb that maps to the SmartdataCollection
|
* makes sure a collection exists within MongoDb that maps to the SmartdataCollection
|
||||||
*/
|
*/
|
||||||
async init() {
|
public async init() {
|
||||||
if (!this.mongoDbCollection) {
|
if (!this.mongoDbCollection) {
|
||||||
// connect this instance to a MongoDB collection
|
// connect this instance to a MongoDB collection
|
||||||
const availableMongoDbCollections = await this.smartdataDb.mongoDb.collections();
|
const availableMongoDbCollections = await this.smartdataDb.mongoDb.collections();
|
||||||
@ -56,16 +65,16 @@ export class SmartdataCollection<T> {
|
|||||||
await this.smartdataDb.mongoDb.createCollection(this.collectionName);
|
await this.smartdataDb.mongoDb.createCollection(this.collectionName);
|
||||||
}
|
}
|
||||||
this.mongoDbCollection = await this.smartdataDb.mongoDb.collection(this.collectionName);
|
this.mongoDbCollection = await this.smartdataDb.mongoDb.collection(this.collectionName);
|
||||||
console.log(`Successfully initiated Collection ${this.collectionName}`)
|
// console.log(`Successfully initiated Collection ${this.collectionName}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* mark unique index
|
* mark unique index
|
||||||
*/
|
*/
|
||||||
markUniqueIndexes(keyArrayArg: string[] = []) {
|
public markUniqueIndexes(keyArrayArg: string[] = []) {
|
||||||
for(let key of keyArrayArg) {
|
for (const key of keyArrayArg) {
|
||||||
if(!this.uniqueIndexes.includes(key)) {
|
if (!this.uniqueIndexes.includes(key)) {
|
||||||
this.mongoDbCollection.createIndex(key, {
|
this.mongoDbCollection.createIndex(key, {
|
||||||
unique: true
|
unique: true
|
||||||
});
|
});
|
||||||
@ -78,14 +87,14 @@ export class SmartdataCollection<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
|
||||||
*/
|
*/
|
||||||
addDocValidation(funcArg: IDocValidationFunc<T>) {
|
public addDocValidation(funcArg: IDocValidationFunc<T>) {
|
||||||
this.objectValidation = funcArg;
|
this.objectValidation = funcArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* finds an object in the DbCollection
|
* finds an object in the DbCollection
|
||||||
*/
|
*/
|
||||||
async find(filterObject: any): Promise<any> {
|
public async find(filterObject: any): Promise<any> {
|
||||||
await this.init();
|
await this.init();
|
||||||
const result = await this.mongoDbCollection.find(filterObject).toArray();
|
const result = await this.mongoDbCollection.find(filterObject).toArray();
|
||||||
return result;
|
return result;
|
||||||
@ -94,12 +103,11 @@ export class SmartdataCollection<T> {
|
|||||||
/**
|
/**
|
||||||
* create an object in the database
|
* create an object in the database
|
||||||
*/
|
*/
|
||||||
async insert(dbDocArg: T & SmartDataDbDoc<T>): Promise<any> {
|
public async insert(dbDocArg: T & SmartDataDbDoc<T>): Promise<any> {
|
||||||
await this.init();
|
await this.init();
|
||||||
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;
|
||||||
}
|
}
|
||||||
@ -107,11 +115,29 @@ 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);
|
console.log(identifiableObject);
|
||||||
|
console.log(saveableObject);
|
||||||
|
const updateableObject: any = {};
|
||||||
|
for (const key of Object.keys(saveableObject)) {
|
||||||
|
if (identifiableObject[key]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
updateableObject[key] = saveableObject[key];
|
||||||
|
}
|
||||||
|
console.log(updateableObject);
|
||||||
|
this.mongoDbCollection.updateOne(identifiableObject, { $set: updateableObject }, {upsert: true});
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -119,7 +145,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);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import * as plugins from './smartdata.plugins';
|
import * as plugins from './smartdata.plugins';
|
||||||
import { Objectmap } from 'lik';
|
import { Objectmap } from '@pushrocks/lik';
|
||||||
|
|
||||||
import { SmartdataCollection } from './smartdata.classes.collection';
|
import { SmartdataCollection } from './smartdata.classes.collection';
|
||||||
|
|
||||||
@ -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> {
|
public 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(
|
||||||
@ -52,13 +52,10 @@ export class SmartdataDb {
|
|||||||
this.smartdataOptions.mongoDbPass
|
this.smartdataOptions.mongoDbPass
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
console.log(finalConnectionUrl);
|
console.log(`connection Url: ${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}`);
|
||||||
@ -67,17 +64,18 @@ export class SmartdataDb {
|
|||||||
/**
|
/**
|
||||||
* closes the connection to the databse
|
* closes the connection to the databse
|
||||||
*/
|
*/
|
||||||
async close(): Promise<any> {
|
public async close(): Promise<any> {
|
||||||
await this.mongoDbClient.close();
|
await this.mongoDbClient.close();
|
||||||
this.status = 'disconnected';
|
this.status = 'disconnected';
|
||||||
plugins.smartlog
|
plugins.smartlog.defaultLogger.log(
|
||||||
.getDefaultLogger()
|
'info',
|
||||||
.info(`disconnected from database ${this.smartdataOptions.mongoDbName}`);
|
`disconnected from database ${this.smartdataOptions.mongoDbName}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle table to class distribution
|
// handle table to class distribution
|
||||||
|
|
||||||
addTable(SmartdataCollectionArg: SmartdataCollection<any>) {
|
public addTable(SmartdataCollectionArg: SmartdataCollection<any>) {
|
||||||
this.smartdataCollectionMap.add(SmartdataCollectionArg);
|
this.smartdataCollectionMap.add(SmartdataCollectionArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,8 +84,8 @@ export class SmartdataDb {
|
|||||||
* @param nameArg
|
* @param nameArg
|
||||||
* @returns DbTable
|
* @returns DbTable
|
||||||
*/
|
*/
|
||||||
async getSmartdataCollectionByName<T>(nameArg: string): Promise<SmartdataCollection<T>> {
|
public async getSmartdataCollectionByName<T>(nameArg: string): Promise<SmartdataCollection<T>> {
|
||||||
let resultCollection = this.smartdataCollectionMap.find(dbTableArg => {
|
const resultCollection = this.smartdataCollectionMap.find(dbTableArg => {
|
||||||
return dbTableArg.collectionName === nameArg;
|
return dbTableArg.collectionName === nameArg;
|
||||||
});
|
});
|
||||||
return resultCollection;
|
return resultCollection;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import * as plugins from './smartdata.plugins';
|
import * as plugins from './smartdata.plugins';
|
||||||
|
|
||||||
import { Objectmap } from 'lik';
|
import { Objectmap } from '@pushrocks/lik';
|
||||||
|
|
||||||
import { SmartdataDb } from './smartdata.classes.db';
|
import { SmartdataDb } from './smartdata.classes.db';
|
||||||
import { SmartdataCollection } from './smartdata.classes.collection';
|
import { SmartdataCollection } from './smartdata.classes.collection';
|
||||||
@ -45,49 +45,65 @@ export class SmartDataDbDoc<T> {
|
|||||||
/**
|
/**
|
||||||
* the collection object an Doc belongs to
|
* the collection object an Doc belongs to
|
||||||
*/
|
*/
|
||||||
collection: SmartdataCollection<T>;
|
public collection: SmartdataCollection<T>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* how the Doc in memory was created, may prove useful later.
|
* how the Doc in memory was created, may prove useful later.
|
||||||
*/
|
*/
|
||||||
creationStatus: TDocCreation = 'new';
|
public creationStatus: TDocCreation = 'new';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* unique indexes
|
* unique indexes
|
||||||
*/
|
*/
|
||||||
uniqueIndexes: string[];
|
public uniqueIndexes: string[];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* an array of saveable properties of a doc
|
* an array of saveable properties of a doc
|
||||||
*/
|
*/
|
||||||
saveableProperties: string[];
|
public saveableProperties: string[];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* name
|
* name
|
||||||
*/
|
*/
|
||||||
name: string;
|
public name: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* primary id in the database
|
* primary id in the database
|
||||||
*/
|
*/
|
||||||
dbDocUniqueId: string;
|
public dbDocUniqueId: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* class constructor
|
* class constructor
|
||||||
*/
|
*/
|
||||||
constructor() {
|
constructor() {
|
||||||
this.name = this.constructor['name'];
|
this.name = this.constructor['name'];
|
||||||
this.collection = this.constructor['smartdataCollection'];
|
if (this.constructor['smartdataCollection']) {
|
||||||
|
// tslint:disable-next-line: no-string-literal
|
||||||
|
this.collection = this.constructor['smartdataCollection'];
|
||||||
|
// tslint:disable-next-line: no-string-literal
|
||||||
|
} else if (typeof this.constructor['smartdataDelayedCollection'] === 'function') {
|
||||||
|
// tslint:disable-next-line: no-string-literal
|
||||||
|
this.collection = this.constructor['smartdataDelayedCollection']();
|
||||||
|
} else {
|
||||||
|
console.error('Could not determine collection for DbDoc');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async getInstances<T>(filterArg): Promise<T[]> {
|
public static async getInstances<T>(filterArg): Promise<T[]> {
|
||||||
let self: any = this; // fool typesystem
|
const self: any = this; // fool typesystem
|
||||||
let referenceMongoDBCollection: SmartdataCollection<T> = self.smartdataCollection;
|
let referenceMongoDBCollection: SmartdataCollection<T>;
|
||||||
|
|
||||||
|
if (self.smartdataCollection) {
|
||||||
|
referenceMongoDBCollection = self.smartdataCollection;
|
||||||
|
} else if (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 (const item of foundDocs) {
|
||||||
let newInstance = new this();
|
const newInstance = new this();
|
||||||
for (let key in item) {
|
newInstance.creationStatus = 'db';
|
||||||
|
for (const key in item) {
|
||||||
if (key !== 'id') {
|
if (key !== 'id') {
|
||||||
newInstance[key] = item[key];
|
newInstance[key] = item[key];
|
||||||
}
|
}
|
||||||
@ -98,7 +114,7 @@ export class SmartDataDbDoc<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async getInstance<T>(filterArg): Promise<T> {
|
static async getInstance<T>(filterArg): Promise<T> {
|
||||||
let result = await this.getInstances<T>(filterArg);
|
const result = await this.getInstances<T>(filterArg);
|
||||||
if (result && result.length > 0) {
|
if (result && result.length > 0) {
|
||||||
return result[0];
|
return result[0];
|
||||||
}
|
}
|
||||||
@ -108,14 +124,15 @@ export class SmartDataDbDoc<T> {
|
|||||||
* saves this instance but not any connected items
|
* saves this instance but not any connected items
|
||||||
* may lead to data inconsistencies, but is faster
|
* may lead to data inconsistencies, but is faster
|
||||||
*/
|
*/
|
||||||
async save() {
|
public async save() {
|
||||||
let self: any = this;
|
// tslint:disable-next-line: no-this-assignment
|
||||||
|
const self: any = this;
|
||||||
switch (this.creationStatus) {
|
switch (this.creationStatus) {
|
||||||
case 'db':
|
case 'db':
|
||||||
await this.collection.update(self);
|
await this.collection.update(self);
|
||||||
break;
|
break;
|
||||||
case 'new':
|
case 'new':
|
||||||
let writeResult = await this.collection.insert(self);
|
const writeResult = await this.collection.insert(self);
|
||||||
this.creationStatus = 'db';
|
this.creationStatus = 'db';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -123,29 +140,48 @@ export class SmartDataDbDoc<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* deletes a document from the database
|
||||||
|
*/
|
||||||
|
public async delete() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* also store any referenced objects to DB
|
* also store any referenced objects to DB
|
||||||
* better for data consistency
|
* better for data consistency
|
||||||
*/
|
*/
|
||||||
saveDeep(savedMapArg: Objectmap<SmartDataDbDoc<any>> = null) {
|
public saveDeep(savedMapArg: Objectmap<SmartDataDbDoc<any>> = null) {
|
||||||
if (!savedMapArg) {
|
if (!savedMapArg) {
|
||||||
savedMapArg = new Objectmap<SmartDataDbDoc<any>>();
|
savedMapArg = new Objectmap<SmartDataDbDoc<any>>();
|
||||||
}
|
}
|
||||||
savedMapArg.add(this);
|
savedMapArg.add(this);
|
||||||
this.save();
|
this.save();
|
||||||
for (let propertyKey in this) {
|
for (const propertyKey of Object.keys(this)) {
|
||||||
let property: any = this[propertyKey];
|
const property: any = this[propertyKey];
|
||||||
if (property instanceof SmartDataDbDoc && !savedMapArg.checkForObject(property)) {
|
if (property instanceof SmartDataDbDoc && !savedMapArg.checkForObject(property)) {
|
||||||
property.saveDeep(savedMapArg);
|
property.saveDeep(savedMapArg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,8 @@ import * as assert from 'assert';
|
|||||||
import * as smartlog from '@pushrocks/smartlog';
|
import * as smartlog from '@pushrocks/smartlog';
|
||||||
import * as lodash from 'lodash';
|
import * as lodash from 'lodash';
|
||||||
import * as mongodb from 'mongodb';
|
import * as mongodb from 'mongodb';
|
||||||
import * as smartq from 'smartq';
|
import * as smartq from '@pushrocks/smartpromise';
|
||||||
import * as smartstring from 'smartstring';
|
import * as smartstring from '@pushrocks/smartstring';
|
||||||
|
import * as smartunique from '@pushrocks/smartunique';
|
||||||
|
|
||||||
export { assert, smartlog, lodash, smartq, mongodb, smartstring };
|
export { assert, smartlog, lodash, smartq, mongodb, smartstring, smartunique };
|
||||||
|
17
tslint.json
Normal file
17
tslint.json
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||||
|
"rules": {
|
||||||
|
"semicolon": [true, "always"],
|
||||||
|
"no-console": false,
|
||||||
|
"ordered-imports": false,
|
||||||
|
"object-literal-sort-keys": false,
|
||||||
|
"member-ordering": {
|
||||||
|
"options":{
|
||||||
|
"order": [
|
||||||
|
"static-method"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"defaultSeverity": "warning"
|
||||||
|
}
|
Reference in New Issue
Block a user