Compare commits
145 Commits
Author | SHA1 | Date | |
---|---|---|---|
c1c4a29415 | |||
6448516aa0 | |||
24a7b2dbd3 | |||
afa7c2fe61 | |||
b768896fbe | |||
8d7d9adef1 | |||
ca6d3c60c0 | |||
38c1fcd53a | |||
e433b9e212 | |||
bf3656f792 | |||
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 | |||
f763de2403 | |||
cba1d031b6 | |||
d31e6a5931 | |||
46514a2743 | |||
1cf02157c4 | |||
72efa14da8 | |||
a73c2084c0 | |||
5ed557a21a | |||
2b6c30ff55 | |||
12ef599333 | |||
191ea5d3c6 | |||
bdf33ed1ca | |||
1c76905dcd | |||
4d88cea69d | |||
8b4f7169ff | |||
2a1c45608f | |||
85a6444263 | |||
766ae1d1ff | |||
7630882312 | |||
4aec47f207 | |||
c2e3a1ae6e | |||
3d8f8646b1 | |||
aca9817c56 | |||
7e341f2b50 | |||
d2ca108ef8 | |||
e2d12f8c9c | |||
cfcd9ab386 | |||
3048035374 | |||
6c70cf05c4 | |||
04b0910883 | |||
3671fe4df4 | |||
bd30da1c4a | |||
05938bf2af | |||
cddb0caf1f | |||
1af51dd989 | |||
8101e49026 | |||
2d97ab9dc5 | |||
3ba2bc8446 | |||
5735ab8577 | |||
def671cbe4 | |||
14042151ba | |||
c74873d02c | |||
800cdd655a | |||
68c0ceeb14 | |||
154dc5c1b3 | |||
d5c027caf0 | |||
30776a7da0 | |||
fedc0bd8c8 | |||
7b78b47a1b | |||
a4acfc0f6e | |||
a934f3608f | |||
a4f3f23bed | |||
2039f1c807 | |||
57d1a6dd0a | |||
d504e90e47 | |||
7079340657 | |||
63ceded1b8 | |||
b45e7ee206 | |||
715709f3ec | |||
0b8adea736 | |||
90d9a25b3f | |||
e49811eecd | |||
9cbdf317dc | |||
d55339013a | |||
9acdbd2842 | |||
dfce8cfcc0 | |||
38446fc79b | |||
af8c41ad6c | |||
3a24f829b4 | |||
67db1325c1 | |||
6683c05c22 | |||
f7f7852b7f | |||
df4346e0a2 | |||
eb54fbcd0d | |||
f13c12cd47 | |||
08d53f22f5 | |||
5b6523e6f4 | |||
138290a7b8 | |||
036cafc282 | |||
7764ba5cb8 | |||
7f77634ea1 | |||
acf3c29939 | |||
925cd639b4 | |||
af8de58dcd | |||
f39703b8d9 | |||
fa1951b4dd | |||
8c37a367a0 | |||
37f388e998 | |||
78bdb6760e | |||
ce1dceb60c | |||
16add8bff0 | |||
11149b2712 | |||
e508a942e3 | |||
5b093c68e4 | |||
d1b0a65993 | |||
316030be7c | |||
411b0b1ae9 | |||
50a6a1a690 | |||
ee3c3580d3 | |||
6dd82994a6 | |||
295096de24 | |||
50b5f84955 | |||
1557c713cd | |||
4ce457fadf | |||
56d6e0fd45 | |||
89ebf23d46 | |||
a861444c30 |
23
.gitignore
vendored
23
.gitignore
vendored
@ -1,5 +1,22 @@
|
|||||||
node_modules/
|
.nogit/
|
||||||
test/data
|
|
||||||
pages/
|
# artifacts
|
||||||
coverage/
|
coverage/
|
||||||
public/
|
public/
|
||||||
|
pages/
|
||||||
|
|
||||||
|
# installs
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# caches
|
||||||
|
.yarn/
|
||||||
|
.cache/
|
||||||
|
.rpt2_cache
|
||||||
|
|
||||||
|
# builds
|
||||||
|
dist/
|
||||||
|
dist_web/
|
||||||
|
dist_serve/
|
||||||
|
dist_ts_web/
|
||||||
|
|
||||||
|
# custom
|
112
.gitlab-ci.yml
112
.gitlab-ci.yml
@ -1,43 +1,119 @@
|
|||||||
image: hosttoday/ht-docker-node:npmts
|
# gitzone ci_default
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
|
||||||
|
cache:
|
||||||
|
paths:
|
||||||
|
- .npmci_cache/
|
||||||
|
key: "$CI_BUILD_STAGE"
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
|
- security
|
||||||
- test
|
- test
|
||||||
- release
|
- release
|
||||||
|
- metadata
|
||||||
|
|
||||||
before_script:
|
# ====================
|
||||||
- apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
|
# security stage
|
||||||
- echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-3.2.list
|
# ====================
|
||||||
- apt-get update
|
mirror:
|
||||||
- apt-get install -y mongodb-org
|
stage: security
|
||||||
|
|
||||||
testLEGACY:
|
|
||||||
stage: test
|
|
||||||
script:
|
script:
|
||||||
- npmci test legacy
|
- npmci git mirror
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
allow_failure: true
|
- notpriv
|
||||||
|
|
||||||
testLTS:
|
snyk:
|
||||||
stage: test
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci test lts
|
- npmci npm prepare
|
||||||
|
- npmci command npm install -g snyk
|
||||||
|
- npmci command npm install --ignore-scripts
|
||||||
|
- npmci command snyk test
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
testSTABLE:
|
# ====================
|
||||||
|
# test stage
|
||||||
|
# ====================
|
||||||
|
|
||||||
|
testStable:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci test stable
|
- npmci npm prepare
|
||||||
|
- npmci node install stable
|
||||||
|
- npmci npm install
|
||||||
|
- npmci npm test
|
||||||
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
- priv
|
||||||
|
|
||||||
|
testBuild:
|
||||||
|
stage: test
|
||||||
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci node install lts
|
||||||
|
- npmci npm install
|
||||||
|
- npmci command npm run build
|
||||||
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
environment: npm_registry
|
|
||||||
script:
|
script:
|
||||||
- npmci publish
|
- npmci node install lts
|
||||||
|
- npmci npm publish
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
|
# ====================
|
||||||
|
# metadata stage
|
||||||
|
# ====================
|
||||||
|
codequality:
|
||||||
|
stage: metadata
|
||||||
|
allow_failure: true
|
||||||
|
script:
|
||||||
|
- npmci command npm install -g tslint typescript
|
||||||
|
- npmci npm install
|
||||||
|
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
- priv
|
||||||
|
|
||||||
|
trigger:
|
||||||
|
stage: metadata
|
||||||
|
script:
|
||||||
|
- npmci trigger
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
|
pages:
|
||||||
|
image: hosttoday/ht-docker-dbase:npmci
|
||||||
|
services:
|
||||||
|
- docker:stable-dind
|
||||||
|
stage: metadata
|
||||||
|
script:
|
||||||
|
- npmci command npm install -g @gitzone/tsdoc
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci npm install
|
||||||
|
- npmci command tsdoc
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
- notpriv
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
|
artifacts:
|
||||||
|
expire_in: 1 week
|
||||||
|
paths:
|
||||||
|
- public
|
||||||
|
allow_failure: true
|
||||||
|
137
README.md
Normal file
137
README.md
Normal file
@ -0,0 +1,137 @@
|
|||||||
|
# @pushrocks/smartdata
|
||||||
|
do more with data
|
||||||
|
|
||||||
|
## Availabililty and Links
|
||||||
|
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartdata)
|
||||||
|
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartdata)
|
||||||
|
* [github.com (source mirror)](https://github.com/pushrocks/smartdata)
|
||||||
|
* [docs (typedoc)](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/@pushrocks/smartdata)
|
||||||
|
[](https://snyk.io/test/npm/@pushrocks/smartdata)
|
||||||
|
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
|
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
|
[](https://prettier.io/)
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Use TypeScript for best in class instellisense.
|
||||||
|
|
||||||
|
smartdata is an ODM that adheres to TypeScript practices and uses classes to organize data.
|
||||||
|
It uses RethinkDB as persistent storage.
|
||||||
|
|
||||||
|
## Intention
|
||||||
|
|
||||||
|
There are many ODMs out there, however when we searched for an ODM that uses TypeScript,
|
||||||
|
acts smart while still embracing the NoSQL idea we didn't find a matching solution.
|
||||||
|
This is why we started smartdata.
|
||||||
|
|
||||||
|
How RethinkDB's terms map to the ones of smartdata:
|
||||||
|
|
||||||
|
| MongoDb term | smartdata class |
|
||||||
|
| ------------ | ----------------------------- |
|
||||||
|
| Database | smartdata.SmartdataDb |
|
||||||
|
| Collection | smartdata.SmartdataCollection |
|
||||||
|
| Document | smartdata.SmartadataDoc |
|
||||||
|
|
||||||
|
### class Db
|
||||||
|
|
||||||
|
represents a Database. Naturally it has .connect() etc. methods on it.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import * as smartdata from 'smartdata';
|
||||||
|
|
||||||
|
const smartdataDb = new smartdata.SmartdataDb({
|
||||||
|
mongoDbUrl: '//someurl',
|
||||||
|
mongoDbName: 'myDatabase',
|
||||||
|
mongoDbPass: 'mypassword'
|
||||||
|
});
|
||||||
|
|
||||||
|
smartdataDb.connect();
|
||||||
|
```
|
||||||
|
|
||||||
|
### class DbCollection
|
||||||
|
|
||||||
|
represents a collection of objects.
|
||||||
|
A collection is defined by the object class (that is extending smartdata.dbdoc) it respresents
|
||||||
|
|
||||||
|
So to get to get access to a specific collection you document
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// continues from the block before...
|
||||||
|
|
||||||
|
@smartdata.Collection(smartdataDb)
|
||||||
|
class MyObject extends smartdata.DbDoc<MyObject> {
|
||||||
|
// read the next block about DbDoc
|
||||||
|
@smartdata.svDb()
|
||||||
|
property1: string; // @smartdata.svDb() marks the property for db save
|
||||||
|
|
||||||
|
property2: number; // this one is not marked, so it won't be save upon calling this.save()
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
super(); // the super call is important ;) But you probably know that.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// start to instantiate instances of classes from scratch or database
|
||||||
|
|
||||||
|
const localObject = new MyObject({
|
||||||
|
property1: 'hi',
|
||||||
|
property2: 2
|
||||||
|
});
|
||||||
|
localObject.save(); // saves the object to the database
|
||||||
|
|
||||||
|
// start retrieving instances
|
||||||
|
|
||||||
|
MyObject.getInstance<MyObject>({
|
||||||
|
property: 'hi'
|
||||||
|
}); // outputs a new instance of MyObject with the values from db assigned
|
||||||
|
```
|
||||||
|
|
||||||
|
### class DbDoc
|
||||||
|
|
||||||
|
represents a individual document in a collection
|
||||||
|
and thereby is ideally suited to extend the class you want to actually store.
|
||||||
|
|
||||||
|
### CRUD operations
|
||||||
|
smartdata supports full CRUD operations
|
||||||
|
|
||||||
|
**Store** or **Update** instances of classes to MongoDB:
|
||||||
|
DbDoc extends your class with the following methods:
|
||||||
|
|
||||||
|
- async `.save()` will save (or update) the object you call it on only. Any referenced non-savable objects will not get stored.
|
||||||
|
- async `.saveDeep()` does the same like `.save()`.
|
||||||
|
In addition it will look for properties that reference an object
|
||||||
|
that extends DbDoc as well and call .saveDeep() on them as well.
|
||||||
|
Loops are prevented
|
||||||
|
|
||||||
|
**Get** a new class instance from MongoDB:
|
||||||
|
DbDoc exposes a static method that allows you specify a filter to retrieve a cloned class of the one you used to that doc at some point later in time:
|
||||||
|
|
||||||
|
* static async `.getInstance({ /* filter props here */ })` gets you an instance that has the data of the first matched document as properties.
|
||||||
|
* static async `getInstances({ /* filter props here */ })` get you an array instances (one instance for every matched document).
|
||||||
|
|
||||||
|
**Delete** instances from MongoDb:
|
||||||
|
smartdata extends your class with a method to easily delete the doucment from DB:
|
||||||
|
|
||||||
|
* async `.delete()`will delete the document from DB.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## TypeScript
|
||||||
|
|
||||||
|
How does TypeScript play into this?
|
||||||
|
Since you define your classes in TypeScript and types flow through smartdata in a generic way
|
||||||
|
you should get all the Intellisense and type checking you love when using smartdata.
|
||||||
|
smartdata itself also bundles typings. You don't need to install any additional types for smartdata.
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
[](https://maintainedby.lossless.com)
|
14
dist/index.d.ts
vendored
14
dist/index.d.ts
vendored
@ -1,14 +0,0 @@
|
|||||||
/// <reference types="q" />
|
|
||||||
import * as plugins from './smartdata.plugins';
|
|
||||||
export declare class DbCollection<T> {
|
|
||||||
constructor(nameArg: string, db: plugins.mongodb.Db);
|
|
||||||
}
|
|
||||||
export declare type TDbConnectionStatus = 'disconnected' | 'connected' | 'failed';
|
|
||||||
export declare class DbConnection {
|
|
||||||
dbUrl: string;
|
|
||||||
db: plugins.mongodb.Db;
|
|
||||||
status: TDbConnectionStatus;
|
|
||||||
constructor(dbUrl: string);
|
|
||||||
connect(): plugins.q.Promise<any>;
|
|
||||||
close(): plugins.q.Promise<any>;
|
|
||||||
}
|
|
35
dist/index.js
vendored
35
dist/index.js
vendored
@ -1,35 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
const plugins = require('./smartdata.plugins');
|
|
||||||
class DbCollection {
|
|
||||||
constructor(nameArg, db) {
|
|
||||||
let collection = db.collection(nameArg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
exports.DbCollection = DbCollection;
|
|
||||||
class DbConnection {
|
|
||||||
constructor(dbUrl) {
|
|
||||||
this.dbUrl = dbUrl;
|
|
||||||
}
|
|
||||||
connect() {
|
|
||||||
let done = plugins.q.defer();
|
|
||||||
plugins.mongodb.MongoClient.connect(this.dbUrl, (err, db) => {
|
|
||||||
if (err) {
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
plugins.assert.equal(null, err);
|
|
||||||
this.db = db;
|
|
||||||
plugins.beautylog.success(`connected to database at ${this.dbUrl}`);
|
|
||||||
done.resolve(this.db);
|
|
||||||
});
|
|
||||||
return done.promise;
|
|
||||||
}
|
|
||||||
close() {
|
|
||||||
let done = plugins.q.defer();
|
|
||||||
this.db.close();
|
|
||||||
plugins.beautylog.ok(`disconnected to database at ${this.dbUrl}`);
|
|
||||||
done.resolve();
|
|
||||||
return done.promise;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
exports.DbConnection = DbConnection;
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsTUFBWSxPQUFPLFdBQU0scUJBRXpCLENBQUMsQ0FGNkM7QUFFOUM7SUFDSSxZQUFZLE9BQWUsRUFBRSxFQUFzQjtRQUMvQyxJQUFJLFVBQVUsR0FBRyxFQUFFLENBQUMsVUFBVSxDQUFDLE9BQU8sQ0FBQyxDQUFBO0lBQzNDLENBQUM7QUFDTCxDQUFDO0FBSlksb0JBQVksZUFJeEIsQ0FBQTtBQUlEO0lBS0ksWUFBWSxLQUFhO1FBQ3JCLElBQUksQ0FBQyxLQUFLLEdBQUcsS0FBSyxDQUFBO0lBQ3RCLENBQUM7SUFFRCxPQUFPO1FBQ0gsSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQTtRQUM1QixPQUFPLENBQUMsT0FBTyxDQUFDLFdBQVcsQ0FBQyxPQUFPLENBQUMsSUFBSSxDQUFDLEtBQUssRUFBRSxDQUFDLEdBQUcsRUFBRSxFQUFFO1lBQ3BELEVBQUUsQ0FBQyxDQUFDLEdBQUcsQ0FBQyxDQUFDLENBQUM7Z0JBQUMsT0FBTyxDQUFDLEdBQUcsQ0FBQyxHQUFHLENBQUMsQ0FBQTtZQUFDLENBQUM7WUFDN0IsT0FBTyxDQUFDLE1BQU0sQ0FBQyxLQUFLLENBQUMsSUFBSSxFQUFFLEdBQUcsQ0FBQyxDQUFBO1lBQy9CLElBQUksQ0FBQyxFQUFFLEdBQUcsRUFBRSxDQUFBO1lBQ1osT0FBTyxDQUFDLFNBQVMsQ0FBQyxPQUFPLENBQUMsNEJBQTRCLElBQUksQ0FBQyxLQUFLLEVBQUUsQ0FBQyxDQUFBO1lBQ25FLElBQUksQ0FBQyxPQUFPLENBQUMsSUFBSSxDQUFDLEVBQUUsQ0FBQyxDQUFBO1FBQ3pCLENBQUMsQ0FBQyxDQUFBO1FBQ0YsTUFBTSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUE7SUFDdkIsQ0FBQztJQUVELEtBQUs7UUFDRCxJQUFJLElBQUksR0FBRyxPQUFPLENBQUMsQ0FBQyxDQUFDLEtBQUssRUFBRSxDQUFBO1FBQzVCLElBQUksQ0FBQyxFQUFFLENBQUMsS0FBSyxFQUFFLENBQUE7UUFDZixPQUFPLENBQUMsU0FBUyxDQUFDLEVBQUUsQ0FBQywrQkFBK0IsSUFBSSxDQUFDLEtBQUssRUFBRSxDQUFDLENBQUE7UUFDakUsSUFBSSxDQUFDLE9BQU8sRUFBRSxDQUFBO1FBQ2QsTUFBTSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUE7SUFDdkIsQ0FBQztBQUNMLENBQUM7QUE1Qlksb0JBQVksZUE0QnhCLENBQUEifQ==
|
|
5
dist/smartdata.plugins.d.ts
vendored
5
dist/smartdata.plugins.d.ts
vendored
@ -1,5 +0,0 @@
|
|||||||
import 'typings-global';
|
|
||||||
export import assert = require('assert');
|
|
||||||
export import beautylog = require('beautylog');
|
|
||||||
export import mongodb = require('mongodb');
|
|
||||||
export import q = require('q');
|
|
7
dist/smartdata.plugins.js
vendored
7
dist/smartdata.plugins.js
vendored
@ -1,7 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
require('typings-global');
|
|
||||||
exports.assert = require('assert');
|
|
||||||
exports.beautylog = require('beautylog');
|
|
||||||
exports.mongodb = require('mongodb');
|
|
||||||
exports.q = require('q');
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLnBsdWdpbnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEucGx1Z2lucy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsUUFBTyxnQkFDUCxDQUFDLENBRHNCO0FBQ1QsY0FBTSxXQUFXLFFBQVEsQ0FBQyxDQUFBO0FBQzFCLGlCQUFTLFdBQVcsV0FBVyxDQUFDLENBQUE7QUFDaEMsZUFBTyxXQUFXLFNBQVMsQ0FBQyxDQUFBO0FBQzVCLFNBQUMsV0FBVyxHQUFHLENBQUMsQ0FBQSJ9
|
|
22
npmextra.json
Normal file
22
npmextra.json
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"npmdocker": {
|
||||||
|
"baseImage": "hosttoday/ht-docker-node:mongo",
|
||||||
|
"command": "npmci test stable",
|
||||||
|
"dockerSock": false
|
||||||
|
},
|
||||||
|
"npmci": {
|
||||||
|
"npmGlobalTools": [],
|
||||||
|
"npmAccessLevel": "public",
|
||||||
|
"npmRegistryUrl": "registry.npmjs.org"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"module": {
|
||||||
|
"githost": "gitlab.com",
|
||||||
|
"gitscope": "pushrocks",
|
||||||
|
"gitrepo": "smartdata",
|
||||||
|
"shortDescription": "do more with data",
|
||||||
|
"npmPackagename": "@pushrocks/smartdata",
|
||||||
|
"license": "MIT"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
2607
package-lock.json
generated
Normal file
2607
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
53
package.json
53
package.json
@ -1,11 +1,14 @@
|
|||||||
{
|
{
|
||||||
"name": "smartdata",
|
"name": "@pushrocks/smartdata",
|
||||||
"version": "1.0.1",
|
"version": "3.1.21",
|
||||||
|
"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": "(npmts)"
|
"test": "(tstest test/)",
|
||||||
|
"testLocal": "(npmdocker)",
|
||||||
|
"build": "(tsbuild)"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -18,18 +21,38 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartdata#README",
|
"homepage": "https://gitlab.com/pushrocks/smartdata#README",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/mongodb": "^2.1.32",
|
"@pushrocks/lik": "^3.0.11",
|
||||||
"@types/q": "0.0.30",
|
"@pushrocks/smartlog": "^2.0.19",
|
||||||
"beautylog": "^5.0.23",
|
"@pushrocks/smartpromise": "^3.0.2",
|
||||||
"mongodb": "^2.2.9",
|
"@pushrocks/smartstring": "^3.0.10",
|
||||||
"q": "^1.4.1",
|
"@pushrocks/smartunique": "^3.0.1",
|
||||||
"typings-global": "^1.0.14"
|
"@types/lodash": "^4.14.138",
|
||||||
|
"@types/mongodb": "^3.3.1",
|
||||||
|
"lodash": "^4.17.15",
|
||||||
|
"mongodb": "^3.3.2",
|
||||||
|
"runtime-type-checks": "0.0.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/shelljs": "^0.3.30",
|
"@gitzone/tsbuild": "^2.1.17",
|
||||||
"@types/should": "^8.1.29",
|
"@gitzone/tstest": "^1.0.24",
|
||||||
"shelljs": "^0.7.4",
|
"@pushrocks/qenv": "^4.0.4",
|
||||||
"should": "^11.1.0",
|
"@pushrocks/tapbundle": "^3.0.13",
|
||||||
"typings-test": "^1.0.3"
|
"@types/mongodb-memory-server": "^1.8.0",
|
||||||
}
|
"@types/node": "^12.7.3",
|
||||||
|
"@types/shortid": "0.0.29",
|
||||||
|
"mongodb-memory-server": "^5.2.0",
|
||||||
|
"tslint": "^5.19.0",
|
||||||
|
"tslint-config-prettier": "^1.18.0"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"ts/*",
|
||||||
|
"ts_web/*",
|
||||||
|
"dist/*",
|
||||||
|
"dist_web/*",
|
||||||
|
"dist_ts_web/*",
|
||||||
|
"assets/*",
|
||||||
|
"cli.js",
|
||||||
|
"npmextra.json",
|
||||||
|
"readme.md"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
1
test/test.d.ts
vendored
1
test/test.d.ts
vendored
@ -1 +0,0 @@
|
|||||||
import 'typings-test';
|
|
30
test/test.js
30
test/test.js
@ -1,30 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
require('typings-test');
|
|
||||||
const shelljs = require('shelljs');
|
|
||||||
const smartdata = require('../dist/index');
|
|
||||||
let mongoChildProcess;
|
|
||||||
let testDbConnection;
|
|
||||||
describe('mongodb', function () {
|
|
||||||
it('should start mongodb', function (done) {
|
|
||||||
mongoChildProcess = shelljs.exec('mongod --dbpath=./test/data --port 27017', { async: true });
|
|
||||||
setTimeout(() => { done(); }, 1500);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
describe('smartdata', function () {
|
|
||||||
it('should establish a connection to mongodb', function (done) {
|
|
||||||
testDbConnection = new smartdata.DbConnection('mongodb://localhost:27017/smartdata');
|
|
||||||
testDbConnection.connect().then(() => { done(); });
|
|
||||||
});
|
|
||||||
it('should create a collection', function () {
|
|
||||||
});
|
|
||||||
it('should close the db Connection', function () {
|
|
||||||
testDbConnection.close();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
describe('mongodb', function () {
|
|
||||||
it('should kill mongodb', function () {
|
|
||||||
shelljs.exec('mongod --dbpath=./test/data --shutdown');
|
|
||||||
mongoChildProcess.kill('SIGTERM');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sY0FDUCxDQUFDLENBRG9CO0FBQ3JCLE1BQVksT0FBTyxXQUFNLFNBQ3pCLENBQUMsQ0FEaUM7QUFFbEMsTUFBWSxTQUFTLFdBQU0sZUFHM0IsQ0FBQyxDQUh5QztBQUcxQyxJQUFJLGlCQUFpQixDQUFBO0FBQ3JCLElBQUksZ0JBQXdDLENBQUE7QUFHNUMsUUFBUSxDQUFDLFNBQVMsRUFBQztJQUNmLEVBQUUsQ0FBQyxzQkFBc0IsRUFBQyxVQUFTLElBQUk7UUFDbkMsaUJBQWlCLEdBQUcsT0FBTyxDQUFDLElBQUksQ0FBQywwQ0FBMEMsRUFBQyxFQUFDLEtBQUssRUFBRSxJQUFJLEVBQUMsQ0FBQyxDQUFBO1FBQzFGLFVBQVUsQ0FBQyxRQUFRLElBQUksRUFBRSxDQUFBLENBQUMsQ0FBQyxFQUFFLElBQUksQ0FBQyxDQUFBO0lBQ3RDLENBQUMsQ0FBQyxDQUFBO0FBQ04sQ0FBQyxDQUFDLENBQUE7QUFDRixRQUFRLENBQUMsV0FBVyxFQUFDO0lBQ2pCLEVBQUUsQ0FBQywwQ0FBMEMsRUFBQyxVQUFTLElBQUk7UUFDdkQsZ0JBQWdCLEdBQUcsSUFBSSxTQUFTLENBQUMsWUFBWSxDQUFDLHFDQUFxQyxDQUFDLENBQUE7UUFDcEYsZ0JBQWdCLENBQUMsT0FBTyxFQUFFLENBQUMsSUFBSSxDQUFDLFFBQVEsSUFBSSxFQUFFLENBQUEsQ0FBQyxDQUFDLENBQUMsQ0FBQTtJQUNyRCxDQUFDLENBQUMsQ0FBQTtJQUNGLEVBQUUsQ0FBQyw0QkFBNEIsRUFBQztJQUVoQyxDQUFDLENBQUMsQ0FBQTtJQUNGLEVBQUUsQ0FBQyxnQ0FBZ0MsRUFBQztRQUNoQyxnQkFBZ0IsQ0FBQyxLQUFLLEVBQUUsQ0FBQTtJQUM1QixDQUFDLENBQUMsQ0FBQTtBQUNOLENBQUMsQ0FBQyxDQUFBO0FBRUYsUUFBUSxDQUFDLFNBQVMsRUFBQztJQUNmLEVBQUUsQ0FBQyxxQkFBcUIsRUFBQztRQUNyQixPQUFPLENBQUMsSUFBSSxDQUFDLHdDQUF3QyxDQUFDLENBQUE7UUFDdEQsaUJBQWlCLENBQUMsSUFBSSxDQUFDLFNBQVMsQ0FBQyxDQUFBO0lBQ3JDLENBQUMsQ0FBQyxDQUFBO0FBQ04sQ0FBQyxDQUFDLENBQUEifQ==
|
|
171
test/test.ts
171
test/test.ts
@ -1,35 +1,152 @@
|
|||||||
import 'typings-test'
|
import { tap, expect } from '@pushrocks/tapbundle';
|
||||||
import * as shelljs from 'shelljs'
|
import { Qenv } from '@pushrocks/qenv';
|
||||||
import * as should from 'should'
|
|
||||||
import * as smartdata from '../dist/index'
|
|
||||||
|
|
||||||
|
const testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit/');
|
||||||
|
|
||||||
let mongoChildProcess
|
// the tested module
|
||||||
let testDbConnection: smartdata.DbConnection
|
import * as smartdata from '../ts/index';
|
||||||
|
import { smartstring } from '../ts/smartdata.plugins';
|
||||||
|
import * as smartunique from '@pushrocks/smartunique';
|
||||||
|
|
||||||
|
import * as mongoPlugin from 'mongodb-memory-server';
|
||||||
|
|
||||||
describe('mongodb',function(){
|
// =======================================
|
||||||
it('should start mongodb',function(done){
|
// Connecting to the database server
|
||||||
mongoChildProcess = shelljs.exec('mongod --dbpath=./test/data --port 27017',{async: true})
|
// =======================================
|
||||||
setTimeout(() => { done() }, 1500)
|
|
||||||
})
|
let testDb: smartdata.SmartdataDb;
|
||||||
|
let smartdataOptions: smartdata.ISmartdataOptions;
|
||||||
|
let mongod: mongoPlugin.MongoMemoryServer;
|
||||||
|
|
||||||
|
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 () => {
|
||||||
|
await testDb.init();
|
||||||
|
});
|
||||||
|
|
||||||
|
// =======================================
|
||||||
|
// The actual tests
|
||||||
|
// =======================================
|
||||||
|
|
||||||
|
// ------
|
||||||
|
// Collections
|
||||||
|
// ------
|
||||||
|
|
||||||
|
@smartdata.Collection(() => {
|
||||||
|
return testDb;
|
||||||
})
|
})
|
||||||
describe('smartdata',function(){
|
class Car extends smartdata.SmartDataDbDoc<Car> {
|
||||||
it('should establish a connection to mongodb',function(done){
|
@smartdata.unI()
|
||||||
testDbConnection = new smartdata.DbConnection('mongodb://localhost:27017/smartdata')
|
public index: string = smartunique.shortId();
|
||||||
testDbConnection.connect().then(() => { done() })
|
|
||||||
})
|
|
||||||
it('should create a collection',function(){
|
|
||||||
|
|
||||||
})
|
@smartdata.svDb()
|
||||||
it('should close the db Connection',function(){
|
public color: string;
|
||||||
testDbConnection.close()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('mongodb',function(){
|
@smartdata.svDb()
|
||||||
it('should kill mongodb',function(){
|
public brand: string;
|
||||||
shelljs.exec('mongod --dbpath=./test/data --shutdown')
|
|
||||||
mongoChildProcess.kill('SIGTERM')
|
constructor(colorArg: string, brandArg: string) {
|
||||||
})
|
super();
|
||||||
|
this.color = colorArg;
|
||||||
|
this.brand = brandArg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tap.test('should save the car to the db', async () => {
|
||||||
|
const myCar = new Car('red', 'Volvo');
|
||||||
|
await myCar.save();
|
||||||
|
|
||||||
|
const myCar2 = new Car('red', 'Volvo');
|
||||||
|
await myCar2.save();
|
||||||
|
|
||||||
|
const myCar3 = new Car('red', 'Renault');
|
||||||
|
await myCar3.save();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('expect to get instance of Car', async () => {
|
||||||
|
const myCars = await Car.getInstances<Car>({
|
||||||
|
brand: 'Volvo'
|
||||||
|
});
|
||||||
|
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 delete an instance of car', async () => {
|
||||||
|
const myCar = await Car.getInstance<Car>({
|
||||||
|
brand: 'Volvo'
|
||||||
|
});
|
||||||
|
expect(myCar.color).to.equal('blue');
|
||||||
|
await myCar.delete();
|
||||||
|
|
||||||
|
const myCar2 = await Car.getInstance<Car>({
|
||||||
|
brand: 'Volvo'
|
||||||
|
});
|
||||||
|
expect(myCar2.color).to.equal('red');
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// tslint:disable-next-line: max-classes-per-file
|
||||||
|
@smartdata.Collection(() => {
|
||||||
|
return testDb;
|
||||||
})
|
})
|
||||||
|
class Truck extends smartdata.SmartDataDbDoc<Car> {
|
||||||
|
@smartdata.unI()
|
||||||
|
public id: string = smartunique.shortId();
|
||||||
|
|
||||||
|
@smartdata.svDb()
|
||||||
|
public color: string;
|
||||||
|
|
||||||
|
@smartdata.svDb()
|
||||||
|
public brand: string;
|
||||||
|
|
||||||
|
constructor(colorArg: string, brandArg: string) {
|
||||||
|
super();
|
||||||
|
this.color = colorArg;
|
||||||
|
this.brand = brandArg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tap.test('should store a new Truck', async () => {
|
||||||
|
const truck = new Truck('blue', 'MAN');
|
||||||
|
await truck.save();
|
||||||
|
const myTruck = await Truck.getInstance<Truck>({color: 'blue'});
|
||||||
|
console.log(myTruck);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// =======================================
|
||||||
|
// close the database connection
|
||||||
|
// =======================================
|
||||||
|
tap.test('should close the database connection', async tools => {
|
||||||
|
await testDb.close();
|
||||||
|
await mongod.stop();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.start({ throwOnError: true });
|
||||||
|
42
ts/index.ts
42
ts/index.ts
@ -1,39 +1,5 @@
|
|||||||
import * as plugins from './smartdata.plugins'
|
export * from './smartdata.classes.db';
|
||||||
|
export * from './smartdata.classes.collection';
|
||||||
|
export * from './smartdata.classes.doc';
|
||||||
|
|
||||||
export class DbCollection<T> {
|
export { IMongoDescriptor } from './interfaces';
|
||||||
constructor(nameArg: string, db: plugins.mongodb.Db) {
|
|
||||||
let collection = db.collection(nameArg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export type TDbConnectionStatus = 'disconnected' | 'connected' | 'failed'
|
|
||||||
|
|
||||||
export class DbConnection {
|
|
||||||
dbUrl: string
|
|
||||||
db: plugins.mongodb.Db
|
|
||||||
status: TDbConnectionStatus
|
|
||||||
|
|
||||||
constructor(dbUrl: string) {
|
|
||||||
this.dbUrl = dbUrl
|
|
||||||
}
|
|
||||||
|
|
||||||
connect(): plugins.q.Promise<any> {
|
|
||||||
let done = plugins.q.defer()
|
|
||||||
plugins.mongodb.MongoClient.connect(this.dbUrl, (err, db) => {
|
|
||||||
if (err) { console.log(err) }
|
|
||||||
plugins.assert.equal(null, err)
|
|
||||||
this.db = db
|
|
||||||
plugins.beautylog.success(`connected to database at ${this.dbUrl}`)
|
|
||||||
done.resolve(this.db)
|
|
||||||
})
|
|
||||||
return done.promise
|
|
||||||
}
|
|
||||||
|
|
||||||
close(): plugins.q.Promise<any> {
|
|
||||||
let done = plugins.q.defer()
|
|
||||||
this.db.close()
|
|
||||||
plugins.beautylog.ok(`disconnected to database at ${this.dbUrl}`)
|
|
||||||
done.resolve()
|
|
||||||
return done.promise
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
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;
|
||||||
|
}
|
164
ts/smartdata.classes.collection.ts
Normal file
164
ts/smartdata.classes.collection.ts
Normal file
@ -0,0 +1,164 @@
|
|||||||
|
import * as plugins from './smartdata.plugins';
|
||||||
|
import { SmartdataDb } from './smartdata.classes.db';
|
||||||
|
import { SmartDataDbDoc } from './smartdata.classes.doc';
|
||||||
|
|
||||||
|
export interface IFindOptions {
|
||||||
|
limit?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
export interface IDocValidationFunc<T> {
|
||||||
|
(doc: T): boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type TDelayedDbCreation = () => SmartdataDb;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is a decorator that will tell the decorated class what dbTable to use
|
||||||
|
* @param dbArg
|
||||||
|
*/
|
||||||
|
export function Collection(dbArg: SmartdataDb | TDelayedDbCreation) {
|
||||||
|
return function(constructor) {
|
||||||
|
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());
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export class SmartdataCollection<T> {
|
||||||
|
/**
|
||||||
|
* the collection that is used
|
||||||
|
*/
|
||||||
|
public mongoDbCollection: plugins.mongodb.Collection;
|
||||||
|
public objectValidation: IDocValidationFunc<T> = null;
|
||||||
|
public collectionName: string;
|
||||||
|
public smartdataDb: SmartdataDb;
|
||||||
|
public uniqueIndexes: string[] = [];
|
||||||
|
|
||||||
|
constructor(collectedClassArg: T & SmartDataDbDoc<T>, smartDataDbArg: SmartdataDb) {
|
||||||
|
// tell the collection where it belongs
|
||||||
|
this.collectionName = collectedClassArg.name;
|
||||||
|
this.smartdataDb = smartDataDbArg;
|
||||||
|
|
||||||
|
// tell the db class about it (important since Db uses different systems under the hood)
|
||||||
|
this.smartdataDb.addTable(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* makes sure a collection exists within MongoDb that maps to the SmartdataCollection
|
||||||
|
*/
|
||||||
|
public async init() {
|
||||||
|
if (!this.mongoDbCollection) {
|
||||||
|
// connect this instance to a MongoDB collection
|
||||||
|
const availableMongoDbCollections = await this.smartdataDb.mongoDb.collections();
|
||||||
|
const wantedCollection = availableMongoDbCollections.find(collection => {
|
||||||
|
return collection.collectionName === this.collectionName;
|
||||||
|
});
|
||||||
|
if (!wantedCollection) {
|
||||||
|
await this.smartdataDb.mongoDb.createCollection(this.collectionName);
|
||||||
|
}
|
||||||
|
this.mongoDbCollection = await this.smartdataDb.mongoDb.collection(this.collectionName);
|
||||||
|
// console.log(`Successfully initiated Collection ${this.collectionName}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* mark unique index
|
||||||
|
*/
|
||||||
|
public markUniqueIndexes(keyArrayArg: string[] = []) {
|
||||||
|
for (const 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
|
||||||
|
*/
|
||||||
|
public addDocValidation(funcArg: IDocValidationFunc<T>) {
|
||||||
|
this.objectValidation = funcArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* finds an object in the DbCollection
|
||||||
|
*/
|
||||||
|
public async find(filterObject: any): Promise<any> {
|
||||||
|
await this.init();
|
||||||
|
const result = await this.mongoDbCollection.find(filterObject).toArray();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* create an object in the database
|
||||||
|
*/
|
||||||
|
public async insert(dbDocArg: T & SmartDataDbDoc<T>): Promise<any> {
|
||||||
|
await this.init();
|
||||||
|
await this.checkDoc(dbDocArg);
|
||||||
|
this.markUniqueIndexes(dbDocArg.uniqueIndexes);
|
||||||
|
const saveableObject = await dbDocArg.createSavableObject();
|
||||||
|
const result = await this.mongoDbCollection.insertOne(saveableObject);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* inserts object into the DbCollection
|
||||||
|
*/
|
||||||
|
public async update(dbDocArg: T & SmartDataDbDoc<T>): Promise<any> {
|
||||||
|
await this.init();
|
||||||
|
await this.checkDoc(dbDocArg);
|
||||||
|
const identifiableObject = await dbDocArg.createIdentifiableObject();
|
||||||
|
const saveableObject = await dbDocArg.createSavableObject();
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* checks a Doc for constraints
|
||||||
|
* if this.objectValidation is not set it passes.
|
||||||
|
*/
|
||||||
|
private checkDoc(docArg: T): Promise<void> {
|
||||||
|
const done = plugins.smartq.defer<void>();
|
||||||
|
let validationResult = true;
|
||||||
|
if (this.objectValidation) {
|
||||||
|
validationResult = this.objectValidation(docArg);
|
||||||
|
}
|
||||||
|
if (validationResult) {
|
||||||
|
done.resolve();
|
||||||
|
} else {
|
||||||
|
done.reject('validation of object did not pass');
|
||||||
|
}
|
||||||
|
return done.promise;
|
||||||
|
}
|
||||||
|
}
|
93
ts/smartdata.classes.db.ts
Normal file
93
ts/smartdata.classes.db.ts
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
import * as plugins from './smartdata.plugins';
|
||||||
|
import { Objectmap } from '@pushrocks/lik';
|
||||||
|
|
||||||
|
import { SmartdataCollection } from './smartdata.classes.collection';
|
||||||
|
|
||||||
|
import * as mongoHelpers from './smartdata.mongohelpers';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* interface - indicates the connection status of the db
|
||||||
|
*/
|
||||||
|
export type TConnectionStatus = 'initial' | 'disconnected' | 'connected' | 'failed';
|
||||||
|
|
||||||
|
export interface ISmartdataOptions {
|
||||||
|
/**
|
||||||
|
* the URL to connect to
|
||||||
|
*/
|
||||||
|
mongoDbUrl: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the db to use for the project
|
||||||
|
*/
|
||||||
|
mongoDbName: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* an optional password that will be replace <PASSWORD> in the connection string
|
||||||
|
*/
|
||||||
|
mongoDbPass?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class SmartdataDb {
|
||||||
|
smartdataOptions: ISmartdataOptions;
|
||||||
|
mongoDbClient: plugins.mongodb.MongoClient;
|
||||||
|
mongoDb: plugins.mongodb.Db;
|
||||||
|
status: TConnectionStatus;
|
||||||
|
smartdataCollectionMap = new Objectmap<SmartdataCollection<any>>();
|
||||||
|
|
||||||
|
constructor(smartdataOptions: ISmartdataOptions) {
|
||||||
|
this.smartdataOptions = smartdataOptions;
|
||||||
|
this.status = 'initial';
|
||||||
|
}
|
||||||
|
|
||||||
|
// basic connection stuff ----------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* connects to the database that was specified during instance creation
|
||||||
|
*/
|
||||||
|
public async init(): Promise<any> {
|
||||||
|
let finalConnectionUrl = this.smartdataOptions.mongoDbUrl;
|
||||||
|
if (this.smartdataOptions.mongoDbPass) {
|
||||||
|
finalConnectionUrl = mongoHelpers.addPassword(
|
||||||
|
this.smartdataOptions.mongoDbUrl,
|
||||||
|
this.smartdataOptions.mongoDbPass
|
||||||
|
);
|
||||||
|
}
|
||||||
|
console.log(`connection Url: ${finalConnectionUrl}`);
|
||||||
|
this.mongoDbClient = await plugins.mongodb.MongoClient.connect(finalConnectionUrl, {
|
||||||
|
useNewUrlParser: true
|
||||||
|
});
|
||||||
|
this.mongoDb = this.mongoDbClient.db(this.smartdataOptions.mongoDbName);
|
||||||
|
this.status = 'connected';
|
||||||
|
console.log(`Connected to database ${this.smartdataOptions.mongoDbName}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* closes the connection to the databse
|
||||||
|
*/
|
||||||
|
public async close(): Promise<any> {
|
||||||
|
await this.mongoDbClient.close();
|
||||||
|
this.status = 'disconnected';
|
||||||
|
plugins.smartlog.defaultLogger.log(
|
||||||
|
'info',
|
||||||
|
`disconnected from database ${this.smartdataOptions.mongoDbName}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// handle table to class distribution
|
||||||
|
|
||||||
|
public addTable(SmartdataCollectionArg: SmartdataCollection<any>) {
|
||||||
|
this.smartdataCollectionMap.add(SmartdataCollectionArg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a collection's name and returns a SmartdataCollection instance
|
||||||
|
* @param nameArg
|
||||||
|
* @returns DbTable
|
||||||
|
*/
|
||||||
|
public async getSmartdataCollectionByName<T>(nameArg: string): Promise<SmartdataCollection<T>> {
|
||||||
|
const resultCollection = this.smartdataCollectionMap.find(dbTableArg => {
|
||||||
|
return dbTableArg.collectionName === nameArg;
|
||||||
|
});
|
||||||
|
return resultCollection;
|
||||||
|
}
|
||||||
|
}
|
190
ts/smartdata.classes.doc.ts
Normal file
190
ts/smartdata.classes.doc.ts
Normal file
@ -0,0 +1,190 @@
|
|||||||
|
import * as plugins from './smartdata.plugins';
|
||||||
|
|
||||||
|
import { Objectmap } from '@pushrocks/lik';
|
||||||
|
|
||||||
|
import { SmartdataDb } from './smartdata.classes.db';
|
||||||
|
import { SmartdataCollection } from './smartdata.classes.collection';
|
||||||
|
|
||||||
|
export type TDocCreation = 'db' | 'new' | 'mixed';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* saveable - saveable decorator to be used on class properties
|
||||||
|
*/
|
||||||
|
export function svDb() {
|
||||||
|
return (target: SmartDataDbDoc<any>, key: string) => {
|
||||||
|
console.log(`called svDb() on ${key}`);
|
||||||
|
if (!target.saveableProperties) {
|
||||||
|
target.saveableProperties = [];
|
||||||
|
}
|
||||||
|
target.saveableProperties.push(key);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
public collection: SmartdataCollection<T>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* how the Doc in memory was created, may prove useful later.
|
||||||
|
*/
|
||||||
|
public creationStatus: TDocCreation = 'new';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* unique indexes
|
||||||
|
*/
|
||||||
|
public uniqueIndexes: string[];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* an array of saveable properties of a doc
|
||||||
|
*/
|
||||||
|
public saveableProperties: string[];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* name
|
||||||
|
*/
|
||||||
|
public name: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* primary id in the database
|
||||||
|
*/
|
||||||
|
public dbDocUniqueId: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* class constructor
|
||||||
|
*/
|
||||||
|
constructor() {
|
||||||
|
this.name = this.constructor['name'];
|
||||||
|
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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async getInstances<T>(filterArg): Promise<T[]> {
|
||||||
|
const self: any = this; // fool typesystem
|
||||||
|
let referenceMongoDBCollection: SmartdataCollection<T>;
|
||||||
|
|
||||||
|
if (self.smartdataCollection) {
|
||||||
|
referenceMongoDBCollection = self.smartdataCollection;
|
||||||
|
} else if (self.smartdataDelayedCollection) {
|
||||||
|
referenceMongoDBCollection = self.smartdataDelayedCollection();
|
||||||
|
}
|
||||||
|
const foundDocs = await referenceMongoDBCollection.find(filterArg);
|
||||||
|
const returnArray = [];
|
||||||
|
for (const item of foundDocs) {
|
||||||
|
const newInstance = new this();
|
||||||
|
newInstance.creationStatus = 'db';
|
||||||
|
for (const key in item) {
|
||||||
|
if (key !== 'id') {
|
||||||
|
newInstance[key] = item[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
returnArray.push(newInstance);
|
||||||
|
}
|
||||||
|
return returnArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async getInstance<T>(filterArg): Promise<T> {
|
||||||
|
const result = await this.getInstances<T>(filterArg);
|
||||||
|
if (result && result.length > 0) {
|
||||||
|
return result[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* saves this instance but not any connected items
|
||||||
|
* may lead to data inconsistencies, but is faster
|
||||||
|
*/
|
||||||
|
public async save() {
|
||||||
|
// tslint:disable-next-line: no-this-assignment
|
||||||
|
const self: any = this;
|
||||||
|
switch (this.creationStatus) {
|
||||||
|
case 'db':
|
||||||
|
await this.collection.update(self);
|
||||||
|
break;
|
||||||
|
case 'new':
|
||||||
|
const writeResult = await this.collection.insert(self);
|
||||||
|
this.creationStatus = 'db';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.error('neither new nor in db?');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* deletes a document from the database
|
||||||
|
*/
|
||||||
|
public async delete() {
|
||||||
|
const self: any = this;
|
||||||
|
await this.collection.delete(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* also store any referenced objects to DB
|
||||||
|
* better for data consistency
|
||||||
|
*/
|
||||||
|
public saveDeep(savedMapArg: Objectmap<SmartDataDbDoc<any>> = null) {
|
||||||
|
if (!savedMapArg) {
|
||||||
|
savedMapArg = new Objectmap<SmartDataDbDoc<any>>();
|
||||||
|
}
|
||||||
|
savedMapArg.add(this);
|
||||||
|
this.save();
|
||||||
|
for (const propertyKey of Object.keys(this)) {
|
||||||
|
const property: any = this[propertyKey];
|
||||||
|
if (property instanceof SmartDataDbDoc && !savedMapArg.checkForObject(property)) {
|
||||||
|
property.saveDeep(savedMapArg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* creates a saveable object so the instance can be persisted as json in the database
|
||||||
|
*/
|
||||||
|
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];
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
3
ts/smartdata.mongohelpers.ts
Normal file
3
ts/smartdata.mongohelpers.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export const addPassword = (mongoUrlArg: string, passwordArg: string): string => {
|
||||||
|
return mongoUrlArg.replace('<PASSWORD>', passwordArg);
|
||||||
|
};
|
@ -1,5 +1,9 @@
|
|||||||
import 'typings-global'
|
import * as assert from 'assert';
|
||||||
export import assert = require('assert')
|
import * as smartlog from '@pushrocks/smartlog';
|
||||||
export import beautylog = require('beautylog')
|
import * as lodash from 'lodash';
|
||||||
export import mongodb = require('mongodb')
|
import * as mongodb from 'mongodb';
|
||||||
export import q = require('q')
|
import * as smartq from '@pushrocks/smartpromise';
|
||||||
|
import * as smartstring from '@pushrocks/smartstring';
|
||||||
|
import * as smartunique from '@pushrocks/smartunique';
|
||||||
|
|
||||||
|
export { assert, smartlog, lodash, smartq, mongodb, smartstring, smartunique };
|
||||||
|
7
tsconfig.json
Normal file
7
tsconfig.json
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"target": "es2017",
|
||||||
|
"module": "commonjs"
|
||||||
|
}
|
||||||
|
}
|
16
tslint.json
16
tslint.json
@ -1,3 +1,17 @@
|
|||||||
{
|
{
|
||||||
"extends": "tslint-config-standard"
|
"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