Compare commits
65 Commits
Author | SHA1 | Date | |
---|---|---|---|
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 |
5
.gitignore
vendored
5
.gitignore
vendored
@ -1,5 +1,4 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
test/data
|
|
||||||
pages/
|
|
||||||
coverage/
|
|
||||||
public/
|
public/
|
||||||
|
coverage/
|
||||||
|
.nogit/
|
||||||
|
135
.gitlab-ci.yml
135
.gitlab-ci.yml
@ -1,43 +1,150 @@
|
|||||||
image: hosttoday/ht-docker-node:npmts
|
# gitzone standard
|
||||||
|
image: 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
|
||||||
|
- notpriv
|
||||||
|
|
||||||
|
snyk:
|
||||||
|
stage: security
|
||||||
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci command npm install -g snyk
|
||||||
|
- npmci command npm install --ignore-scripts
|
||||||
|
- npmci command snyk test
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
|
sast:
|
||||||
|
stage: security
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-dbase:npmci
|
||||||
|
variables:
|
||||||
|
DOCKER_DRIVER: overlay2
|
||||||
allow_failure: true
|
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
|
||||||
|
# ====================
|
||||||
|
|
||||||
testLTS:
|
testLTS:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci test lts
|
- npmci npm prepare
|
||||||
|
- npmci node install lts
|
||||||
|
- npmci npm install
|
||||||
|
- npmci npm test
|
||||||
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
testSTABLE:
|
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
|
||||||
|
- notpriv
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
environment: npm_registry
|
|
||||||
script:
|
script:
|
||||||
- npmci publish
|
- npmci node install stable
|
||||||
|
- npmci npm publish
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
|
# ====================
|
||||||
|
# metadata stage
|
||||||
|
# ====================
|
||||||
|
codequality:
|
||||||
|
stage: metadata
|
||||||
|
image: docker:stable
|
||||||
|
allow_failure: true
|
||||||
|
services:
|
||||||
|
- docker:stable-dind
|
||||||
|
script:
|
||||||
|
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
||||||
|
- docker run
|
||||||
|
--env SOURCE_CODE="$PWD"
|
||||||
|
--volume "$PWD":/code
|
||||||
|
--volume /var/run/docker.sock:/var/run/docker.sock
|
||||||
|
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
|
||||||
|
artifacts:
|
||||||
|
paths: [codeclimate.json]
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
- priv
|
||||||
|
|
||||||
|
trigger:
|
||||||
|
stage: metadata
|
||||||
|
script:
|
||||||
|
- npmci trigger
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
|
pages:
|
||||||
|
image: hosttoday/ht-docker-node:npmci
|
||||||
|
stage: metadata
|
||||||
|
script:
|
||||||
|
- npmci command npm install -g typedoc typescript
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci npm install
|
||||||
|
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
- notpriv
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
|
artifacts:
|
||||||
|
expire_in: 1 week
|
||||||
|
paths:
|
||||||
|
- public
|
||||||
|
allow_failure: true
|
||||||
|
118
README.md
118
README.md
@ -1,105 +1,129 @@
|
|||||||
# smartdata
|
# @pushrocks/smartdata
|
||||||
|
|
||||||
> Note: Still in Beta
|
do more with data and RethinkDB
|
||||||
|
|
||||||
## Availabililty
|
## Availabililty
|
||||||
[](https://www.npmjs.com/package/smartdata)
|
|
||||||
[](https://gitlab.com/pushrocks/smartdata)
|
[](https://www.npmjs.com/package/smartdata)
|
||||||
[](https://github.com/pushrocks/smartdata)
|
[](https://GitLab.com/pushrocks/smartdata)
|
||||||
[](https://pushrocks.gitlab.io/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/smartdata)
|
||||||
[](https://david-dm.org/pushrocks/smartdata)
|
[](https://david-dm.org/pushrocks/smartdata)
|
||||||
[](https://www.bithound.io/github/pushrocks/smartdata/master/dependencies/npm)
|
[](https://www.bithound.io/github/pushrocks/smartdata/master/dependencies/npm)
|
||||||
[](https://www.bithound.io/github/pushrocks/smartdata)
|
[](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/)
|
||||||
[](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
|
||||||
|
|
||||||
|
Use TypeScript for best in class instellisense.
|
||||||
|
|
||||||
smartdata is an ODM that adheres to TypeScript practices and uses classes to organize data.
|
smartdata is an ODM that adheres to TypeScript practices and uses classes to organize data.
|
||||||
It uses MongoDB or NeDb as persistent storage.
|
It uses RethinkDB as persistent storage.
|
||||||
|
|
||||||
## Intention
|
## Intention
|
||||||
|
|
||||||
There are many ODMs out there, however when we searched for an ODM that uses TypeScript,
|
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.
|
acts smart while still embracing the NoSQL idea we didn't find a matching solution.
|
||||||
This is why we started smartdata.
|
This is why we started smartdata.
|
||||||
|
|
||||||
How MongoDB terms map to smartdata classes
|
How RethinkDB's terms map to the ones of smartdata:
|
||||||
|
|
||||||
MongoDB term | smartdata class
|
| MongoDb term | smartdata class |
|
||||||
--- | ---
|
| ------------ | ----------------------------- |
|
||||||
Database | smartdata.Db
|
| Database | smartdata.SmartdataDb |
|
||||||
Collection | smartdata.DbCollection
|
| Collection | smartdata.SmartdataCollection |
|
||||||
Document | smartdata.DbDoc
|
| Document | smartdata.SmartadataDoc |
|
||||||
|
|
||||||
### class Db
|
### class Db
|
||||||
|
|
||||||
represents a Database. Naturally it has .connect() etc. methods on it.
|
represents a Database. Naturally it has .connect() etc. methods on it.
|
||||||
Since it is a class you can have multiple DBs defined.
|
|
||||||
```javascript
|
|
||||||
import * as smartdata from 'smartdata'
|
|
||||||
|
|
||||||
// mongodb
|
```typescript
|
||||||
let myDb1 = new smartdata.Db('someConnectionUrl')
|
import * as smartdata from 'smartdata';
|
||||||
let myDb2 = new smartdata.Db('someConnectionUrl')
|
|
||||||
|
|
||||||
// nedb
|
const smartdataDb = new smartdata.SmartdataDb({
|
||||||
let myDb3 = new smartdata('/some/path/for/persistence', 'nedb') // you may set first argument to null for just in memory db
|
mongoDbUrl: '//someurl',
|
||||||
|
mongoDbName: 'myDatabase',
|
||||||
|
mongoDbPass: 'mypassword'
|
||||||
|
});
|
||||||
|
|
||||||
myDb1.connect()
|
smartdataDb.connect();
|
||||||
myDb2.connect()
|
|
||||||
|
|
||||||
// continues in next block...
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### class DbCollection
|
### class DbCollection
|
||||||
|
|
||||||
represents a collection of objects.
|
represents a collection of objects.
|
||||||
A collection is defined by the object class (that is extending smartdata.dbdoc) it respresents
|
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
|
So to get to get access to a specific collection you document
|
||||||
```javascript
|
|
||||||
|
```typescript
|
||||||
// continues from the block before...
|
// continues from the block before...
|
||||||
|
|
||||||
@Collection(myDb1)
|
@smartdata.Collection(smartdataDb)
|
||||||
class myObject extends smartdata.DbDoc<myObject> { // read the next block about DbDoc
|
class MyObject extends smartdata.DbDoc<MyObject> {
|
||||||
@smartdata.svDb() property1: string // @smartdata.svDb() marks the property for db save
|
// read the next block about DbDoc
|
||||||
property2: number // this one is not marked, so it won't be save upon calling this.save()
|
@smartdata.svDb() property1: string; // @smartdata.svDb() marks the property for db save
|
||||||
constructor(optionsArg:{
|
property2: number; // this one is not marked, so it won't be save upon calling this.save()
|
||||||
property1: string,
|
constructor(optionsArg: { property1: string; property2: number }) {
|
||||||
property2: number
|
super();
|
||||||
}) {
|
|
||||||
super()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let myCollection = myDb1.getCollectionByName<myObject>(myObject)
|
|
||||||
|
|
||||||
// start to instantiate classes from scratch or database
|
// start to instantiate instances of classes from scratch or database
|
||||||
|
|
||||||
|
let 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
|
||||||
```
|
```
|
||||||
|
|
||||||
> Alert: You NEVER instantiate a collection.
|
|
||||||
This is done for you!!!
|
|
||||||
|
|
||||||
### class DbDoc
|
### class DbDoc
|
||||||
|
|
||||||
represents a individual document in a collection
|
represents a individual document in a collection
|
||||||
and thereby is ideally suited to extend the class you want to actually store.
|
and thereby is ideally suited to extend the class you want to actually store.
|
||||||
|
|
||||||
|
**sStore** instances of classes to Db:
|
||||||
DbDoc extends your class with the following methods:
|
DbDoc extends your class with the following methods:
|
||||||
|
|
||||||
* `.save()` will save (or update) the object you call it on only. Any referenced non-savable objects will not get stored.
|
- `.save()` will save (or update) the object you call it on only. Any referenced non-savable objects will not get stored.
|
||||||
* `.saveDeep()` does the same like `.save()`.
|
- `.saveDeep()` does the same like `.save()`.
|
||||||
In addition it will look for properties that reference an object
|
In addition it will look for properties that reference an object
|
||||||
that extends DbDoc as well and call .saveDeep() on them as well.
|
that extends DbDoc as well and call .saveDeep() on them as well.
|
||||||
Loops are prevented
|
Loops are prevented
|
||||||
|
|
||||||
So now we can **store** instances of classes to Db...
|
**Get** a new class instance from a Doc in the DB:
|
||||||
How do we **get** a new class instance from a Doc in the DB?
|
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. Yes, let that sink in a minute :)
|
||||||
|
|
||||||
|
So you can just call `.getInstance({ /* filter props here */ })`.
|
||||||
|
|
||||||
## TypeScript
|
## TypeScript
|
||||||
|
|
||||||
How does TypeScript play into this?
|
How does TypeScript play into this?
|
||||||
Since you define your classes in TypeScript and types flow through smartdata in a generic way
|
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.
|
you should get all the Intellisense and type checking you love when using smartdata.
|
||||||
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.
|
||||||
|
|
||||||
[](https://push.rocks)
|
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)
|
||||||
|
3
dist/index.d.ts
vendored
3
dist/index.d.ts
vendored
@ -1,3 +0,0 @@
|
|||||||
export * from './smartdata.classes.db';
|
|
||||||
export * from './smartdata.classes.dbcollection';
|
|
||||||
export * from './smartdata.classes.dbdoc';
|
|
8
dist/index.js
vendored
8
dist/index.js
vendored
@ -1,8 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
function __export(m) {
|
|
||||||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
|
||||||
}
|
|
||||||
__export(require("./smartdata.classes.db"));
|
|
||||||
__export(require("./smartdata.classes.dbcollection"));
|
|
||||||
__export(require("./smartdata.classes.dbdoc"));
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7O0FBRUEsNENBQXNDO0FBQ3RDLHNEQUFnRDtBQUNoRCwrQ0FBeUMifQ==
|
|
33
dist/smartdata.classes.db.d.ts
vendored
33
dist/smartdata.classes.db.d.ts
vendored
@ -1,33 +0,0 @@
|
|||||||
/// <reference types="q" />
|
|
||||||
import * as plugins from './smartdata.plugins';
|
|
||||||
import { Objectmap } from 'lik';
|
|
||||||
import { DbCollection } from './smartdata.classes.dbcollection';
|
|
||||||
/**
|
|
||||||
* interface - indicates the database type
|
|
||||||
*/
|
|
||||||
export declare type TDbType = 'mongodb' | 'nedb';
|
|
||||||
/**
|
|
||||||
* interface - indicates the connection status of the db
|
|
||||||
*/
|
|
||||||
export declare type TConnectionStatus = 'disconnected' | 'connected' | 'failed';
|
|
||||||
export declare class Db {
|
|
||||||
dbType: TDbType;
|
|
||||||
dbUrl: string;
|
|
||||||
db: plugins.mongodb.Db;
|
|
||||||
status: TConnectionStatus;
|
|
||||||
collections: Objectmap<DbCollection<any>>;
|
|
||||||
constructor(dbUrlArg: string, dbTypeArg?: TDbType);
|
|
||||||
/**
|
|
||||||
* connects to the database that was specified during instance creation
|
|
||||||
*/
|
|
||||||
connect(): plugins.q.Promise<any>;
|
|
||||||
/**
|
|
||||||
* closes the connection to the databse
|
|
||||||
*/
|
|
||||||
close(): plugins.q.Promise<any>;
|
|
||||||
/**
|
|
||||||
* gets a collection by name: string
|
|
||||||
*/
|
|
||||||
getCollectionByName<T>(nameArg: string): plugins.q.Promise<DbCollection<T>>;
|
|
||||||
addCollection(dbCollectionArg: DbCollection<any>): void;
|
|
||||||
}
|
|
64
dist/smartdata.classes.db.js
vendored
64
dist/smartdata.classes.db.js
vendored
@ -1,64 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
const plugins = require("./smartdata.plugins");
|
|
||||||
const lik_1 = require("lik");
|
|
||||||
class Db {
|
|
||||||
constructor(dbUrlArg, dbTypeArg = 'mongodb') {
|
|
||||||
this.collections = new lik_1.Objectmap();
|
|
||||||
this.dbType = dbTypeArg;
|
|
||||||
this.dbUrl = dbUrlArg;
|
|
||||||
}
|
|
||||||
// basic connection stuff ----------------------------------------------
|
|
||||||
/**
|
|
||||||
* connects to the database that was specified during instance creation
|
|
||||||
*/
|
|
||||||
connect() {
|
|
||||||
let done = plugins.q.defer();
|
|
||||||
if (this.dbType === 'mongodb') {
|
|
||||||
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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else if (this.dbType === 'nedb') {
|
|
||||||
this.db = null;
|
|
||||||
}
|
|
||||||
return done.promise;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* closes the connection to the databse
|
|
||||||
*/
|
|
||||||
close() {
|
|
||||||
let done = plugins.q.defer();
|
|
||||||
if (this.dbType === 'mongodb') {
|
|
||||||
this.db.close();
|
|
||||||
}
|
|
||||||
plugins.beautylog.ok(`disconnected to database at ${this.dbUrl}`);
|
|
||||||
done.resolve();
|
|
||||||
return done.promise;
|
|
||||||
}
|
|
||||||
// advanced communication with the database --------------------------------
|
|
||||||
/**
|
|
||||||
* gets a collection by name: string
|
|
||||||
*/
|
|
||||||
getCollectionByName(nameArg) {
|
|
||||||
let done = plugins.q.defer();
|
|
||||||
let resultCollection = this.collections.find((dbCollectionArg) => {
|
|
||||||
return dbCollectionArg.name === nameArg;
|
|
||||||
});
|
|
||||||
if (resultCollection !== null) {
|
|
||||||
done.resolve(resultCollection);
|
|
||||||
}
|
|
||||||
return done.promise;
|
|
||||||
}
|
|
||||||
;
|
|
||||||
addCollection(dbCollectionArg) {
|
|
||||||
this.collections.add(dbCollectionArg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
exports.Db = Db;
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEuY2xhc3Nlcy5kYi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsK0NBQThDO0FBQzlDLDZCQUErQjtBQWMvQjtJQU9JLFlBQVksUUFBZ0IsRUFBRSxZQUFxQixTQUFTO1FBRjVELGdCQUFXLEdBQUcsSUFBSSxlQUFTLEVBQXFCLENBQUE7UUFHNUMsSUFBSSxDQUFDLE1BQU0sR0FBRyxTQUFTLENBQUE7UUFDdkIsSUFBSSxDQUFDLEtBQUssR0FBRyxRQUFRLENBQUE7SUFDekIsQ0FBQztJQUVELHdFQUF3RTtJQUV4RTs7T0FFRztJQUNILE9BQU87UUFDSCxJQUFJLElBQUksR0FBRyxPQUFPLENBQUMsQ0FBQyxDQUFDLEtBQUssRUFBRSxDQUFBO1FBQzVCLEVBQUUsQ0FBQyxDQUFDLElBQUksQ0FBQyxNQUFNLEtBQUssU0FBUyxDQUFDLENBQUMsQ0FBQztZQUM1QixPQUFPLENBQUMsT0FBTyxDQUFDLFdBQVcsQ0FBQyxPQUFPLENBQUMsSUFBSSxDQUFDLEtBQUssRUFBRSxDQUFDLEdBQUcsRUFBRSxFQUFFO2dCQUNwRCxFQUFFLENBQUMsQ0FBQyxHQUFHLENBQUMsQ0FBQyxDQUFDO29CQUFDLE9BQU8sQ0FBQyxHQUFHLENBQUMsR0FBRyxDQUFDLENBQUE7Z0JBQUMsQ0FBQztnQkFDN0IsT0FBTyxDQUFDLE1BQU0sQ0FBQyxLQUFLLENBQUMsSUFBSSxFQUFFLEdBQUcsQ0FBQyxDQUFBO2dCQUMvQixJQUFJLENBQUMsRUFBRSxHQUFHLEVBQUUsQ0FBQTtnQkFDWixPQUFPLENBQUMsU0FBUyxDQUFDLE9BQU8sQ0FBQyw0QkFBNEIsSUFBSSxDQUFDLEtBQUssRUFBRSxDQUFDLENBQUE7Z0JBQ25FLElBQUksQ0FBQyxPQUFPLENBQUMsSUFBSSxDQUFDLEVBQUUsQ0FBQyxDQUFBO1lBQ3pCLENBQUMsQ0FBQyxDQUFBO1FBQ04sQ0FBQztRQUFDLElBQUksQ0FBQyxFQUFFLENBQUMsQ0FBQyxJQUFJLENBQUMsTUFBTSxLQUFLLE1BQU0sQ0FBQyxDQUFDLENBQUM7WUFDaEMsSUFBSSxDQUFDLEVBQUUsR0FBRyxJQUFJLENBQUE7UUFDbEIsQ0FBQztRQUNELE1BQU0sQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFBO0lBQ3ZCLENBQUM7SUFFRDs7T0FFRztJQUNILEtBQUs7UUFDRCxJQUFJLElBQUksR0FBRyxPQUFPLENBQUMsQ0FBQyxDQUFDLEtBQUssRUFBRSxDQUFBO1FBQzVCLEVBQUUsQ0FBQyxDQUFDLElBQUksQ0FBQyxNQUFNLEtBQUssU0FBUyxDQUFDLENBQUMsQ0FBQztZQUM1QixJQUFJLENBQUMsRUFBRSxDQUFDLEtBQUssRUFBRSxDQUFBO1FBQ25CLENBQUM7UUFDRCxPQUFPLENBQUMsU0FBUyxDQUFDLEVBQUUsQ0FBQywrQkFBK0IsSUFBSSxDQUFDLEtBQUssRUFBRSxDQUFDLENBQUE7UUFDakUsSUFBSSxDQUFDLE9BQU8sRUFBRSxDQUFBO1FBQ2QsTUFBTSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUE7SUFDdkIsQ0FBQztJQUVELDRFQUE0RTtJQUU1RTs7T0FFRztJQUNILG1CQUFtQixDQUFJLE9BQWU7UUFDbEMsSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLENBQUMsQ0FBQyxLQUFLLEVBQXFCLENBQUE7UUFDL0MsSUFBSSxnQkFBZ0IsR0FBRyxJQUFJLENBQUMsV0FBVyxDQUFDLElBQUksQ0FBQyxDQUFDLGVBQWU7WUFDekQsTUFBTSxDQUFDLGVBQWUsQ0FBQyxJQUFJLEtBQUssT0FBTyxDQUFBO1FBQzNDLENBQUMsQ0FBQyxDQUFBO1FBQ0YsRUFBRSxDQUFDLENBQUMsZ0JBQWdCLEtBQUssSUFBSSxDQUFDLENBQUMsQ0FBQztZQUM1QixJQUFJLENBQUMsT0FBTyxDQUFDLGdCQUFnQixDQUFDLENBQUE7UUFDbEMsQ0FBQztRQUNELE1BQU0sQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFBO0lBQ3ZCLENBQUM7SUFBQSxDQUFDO0lBRUYsYUFBYSxDQUFDLGVBQWtDO1FBQzVDLElBQUksQ0FBQyxXQUFXLENBQUMsR0FBRyxDQUFDLGVBQWUsQ0FBQyxDQUFBO0lBQ3pDLENBQUM7Q0FFSjtBQWxFRCxnQkFrRUMifQ==
|
|
41
dist/smartdata.classes.dbcollection.d.ts
vendored
41
dist/smartdata.classes.dbcollection.d.ts
vendored
@ -1,41 +0,0 @@
|
|||||||
/// <reference types="q" />
|
|
||||||
import * as plugins from './smartdata.plugins';
|
|
||||||
import { Db } from './smartdata.classes.db';
|
|
||||||
export interface IFindOptions {
|
|
||||||
limit?: number;
|
|
||||||
}
|
|
||||||
export interface IDocValidation<T> {
|
|
||||||
(doc: T): boolean;
|
|
||||||
}
|
|
||||||
export declare function Collection(db: Db): (constructor: any) => void;
|
|
||||||
export declare class DbCollection<T> {
|
|
||||||
/**
|
|
||||||
* the collection that is used, defaults to mongodb collection,
|
|
||||||
* can be nedb datastore (sub api of mongodb)
|
|
||||||
*/
|
|
||||||
collection: plugins.mongodb.Collection;
|
|
||||||
name: string;
|
|
||||||
db: Db;
|
|
||||||
objectValidation: IDocValidation<T>;
|
|
||||||
constructor(nameArg: string, dbArg: Db);
|
|
||||||
/**
|
|
||||||
* adds a validation function that all newly inserted and updated objects have to pass
|
|
||||||
*/
|
|
||||||
addDocValidation(funcArg: IDocValidation<T>): void;
|
|
||||||
/**
|
|
||||||
* finds an object in the DbCollection
|
|
||||||
*/
|
|
||||||
find(docMatchArg: T | any, optionsArg?: IFindOptions): plugins.q.Promise<T[]>;
|
|
||||||
/**
|
|
||||||
* inserts object into the DbCollection
|
|
||||||
*/
|
|
||||||
insertOne(docArg: T): plugins.q.Promise<void>;
|
|
||||||
/**
|
|
||||||
* inserts many objects at once into the DbCollection
|
|
||||||
*/
|
|
||||||
insertMany(docArrayArg: T[]): plugins.q.Promise<void>;
|
|
||||||
/**
|
|
||||||
* checks a Doc for constraints
|
|
||||||
*/
|
|
||||||
private checkDoc(docArg);
|
|
||||||
}
|
|
128
dist/smartdata.classes.dbcollection.js
vendored
128
dist/smartdata.classes.dbcollection.js
vendored
File diff suppressed because one or more lines are too long
34
dist/smartdata.classes.dbdoc.d.ts
vendored
34
dist/smartdata.classes.dbdoc.d.ts
vendored
@ -1,34 +0,0 @@
|
|||||||
import { DbCollection } from './smartdata.classes.dbcollection';
|
|
||||||
export declare type TDocCreation = 'db' | 'new' | 'mixed';
|
|
||||||
/**
|
|
||||||
* saveable - saveable decorator to be used on class properties
|
|
||||||
*/
|
|
||||||
export declare function svDb(): (target: DbDoc<any>, key: string) => void;
|
|
||||||
export declare class DbDoc<T> {
|
|
||||||
/**
|
|
||||||
* the collection object an Doc belongs to
|
|
||||||
*/
|
|
||||||
collection: DbCollection<T>;
|
|
||||||
/**
|
|
||||||
* how the Doc in memory was created, may prove useful later.
|
|
||||||
*/
|
|
||||||
creationType: TDocCreation;
|
|
||||||
/**
|
|
||||||
* an array of saveable properties of a doc
|
|
||||||
*/
|
|
||||||
saveableProperties: string[];
|
|
||||||
/**
|
|
||||||
* class constructor
|
|
||||||
*/
|
|
||||||
constructor();
|
|
||||||
/**
|
|
||||||
* saves this instance but not any connected items
|
|
||||||
* may lead to data inconsistencies, but is faster
|
|
||||||
*/
|
|
||||||
save(): void;
|
|
||||||
/**
|
|
||||||
* also store any referenced objects to DB
|
|
||||||
* better for data consistency
|
|
||||||
*/
|
|
||||||
saveDeep(): void;
|
|
||||||
}
|
|
48
dist/smartdata.classes.dbdoc.js
vendored
48
dist/smartdata.classes.dbdoc.js
vendored
@ -1,48 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
/**
|
|
||||||
* saveable - saveable decorator to be used on class properties
|
|
||||||
*/
|
|
||||||
function svDb() {
|
|
||||||
return (target, key) => {
|
|
||||||
console.log('called sva');
|
|
||||||
if (!target.saveableProperties) {
|
|
||||||
target.saveableProperties = [];
|
|
||||||
}
|
|
||||||
target.saveableProperties.push(key);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
exports.svDb = svDb;
|
|
||||||
class DbDoc {
|
|
||||||
/**
|
|
||||||
* class constructor
|
|
||||||
*/
|
|
||||||
constructor() {
|
|
||||||
this.collection = this.constructor['dbCollection'];
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* saves this instance but not any connected items
|
|
||||||
* may lead to data inconsistencies, but is faster
|
|
||||||
*/
|
|
||||||
save() {
|
|
||||||
let saveableObject = {};
|
|
||||||
for (let propertyNameString of this.saveableProperties) {
|
|
||||||
saveableObject[propertyNameString] = this[propertyNameString];
|
|
||||||
}
|
|
||||||
switch (this.creationType) {
|
|
||||||
case 'db':
|
|
||||||
this.collection; // TODO implement collection.update()
|
|
||||||
break;
|
|
||||||
case 'new':
|
|
||||||
this.collection.insertOne(saveableObject);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* also store any referenced objects to DB
|
|
||||||
* better for data consistency
|
|
||||||
*/
|
|
||||||
saveDeep() {
|
|
||||||
this.save();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
exports.DbDoc = DbDoc;
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGJkb2MuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEuY2xhc3Nlcy5kYmRvYy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBT0E7O0dBRUc7QUFDSDtJQUNJLE1BQU0sQ0FBQyxDQUFDLE1BQWtCLEVBQUUsR0FBVztRQUNuQyxPQUFPLENBQUMsR0FBRyxDQUFDLFlBQVksQ0FBQyxDQUFBO1FBQ3pCLEVBQUUsQ0FBQyxDQUFDLENBQUMsTUFBTSxDQUFDLGtCQUFrQixDQUFDLENBQUMsQ0FBQztZQUFDLE1BQU0sQ0FBQyxrQkFBa0IsR0FBRyxFQUFFLENBQUE7UUFBQyxDQUFDO1FBQ2xFLE1BQU0sQ0FBQyxrQkFBa0IsQ0FBQyxJQUFJLENBQUMsR0FBRyxDQUFDLENBQUE7SUFDdkMsQ0FBQyxDQUFBO0FBQ0wsQ0FBQztBQU5ELG9CQU1DO0FBRUQ7SUFpQkk7O09BRUc7SUFDSDtRQUNJLElBQUksQ0FBQyxVQUFVLEdBQUcsSUFBSSxDQUFDLFdBQVcsQ0FBQyxjQUFjLENBQUMsQ0FBQTtJQUN0RCxDQUFDO0lBRUQ7OztPQUdHO0lBQ0gsSUFBSTtRQUNBLElBQUksY0FBYyxHQUFHLEVBQUUsQ0FBQTtRQUN2QixHQUFHLENBQUMsQ0FBQyxJQUFJLGtCQUFrQixJQUFJLElBQUksQ0FBQyxrQkFBa0IsQ0FBQyxDQUFDLENBQUM7WUFDckQsY0FBYyxDQUFDLGtCQUFrQixDQUFDLEdBQUcsSUFBSSxDQUFDLGtCQUFrQixDQUFDLENBQUE7UUFDakUsQ0FBQztRQUNELE1BQU0sQ0FBQyxDQUFDLElBQUksQ0FBQyxZQUFZLENBQUMsQ0FBQyxDQUFDO1lBQ3hCLEtBQUssSUFBSTtnQkFDTCxJQUFJLENBQUMsVUFBVSxDQUFBLENBQUMscUNBQXFDO2dCQUNyRCxLQUFLLENBQUE7WUFDVCxLQUFLLEtBQUs7Z0JBQ04sSUFBSSxDQUFDLFVBQVUsQ0FBQyxTQUFTLENBQUMsY0FBYyxDQUFDLENBQUE7UUFDakQsQ0FBQztJQUNMLENBQUM7SUFFRDs7O09BR0c7SUFDSCxRQUFRO1FBQ0osSUFBSSxDQUFDLElBQUksRUFBRSxDQUFBO0lBQ2YsQ0FBQztDQUNKO0FBakRELHNCQWlEQyJ9
|
|
8
dist/smartdata.plugins.d.ts
vendored
8
dist/smartdata.plugins.d.ts
vendored
@ -1,8 +0,0 @@
|
|||||||
import 'typings-global';
|
|
||||||
import * as assert from 'assert';
|
|
||||||
import * as beautylog from 'beautylog';
|
|
||||||
import * as lodash from 'lodash';
|
|
||||||
import * as mongodb from 'mongodb';
|
|
||||||
import * as q from 'q';
|
|
||||||
declare let nedb: any;
|
|
||||||
export { assert, beautylog, lodash, mongodb, q, nedb };
|
|
15
dist/smartdata.plugins.js
vendored
15
dist/smartdata.plugins.js
vendored
@ -1,15 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
require("typings-global");
|
|
||||||
const assert = require("assert");
|
|
||||||
exports.assert = assert;
|
|
||||||
const beautylog = require("beautylog");
|
|
||||||
exports.beautylog = beautylog;
|
|
||||||
const lodash = require("lodash");
|
|
||||||
exports.lodash = lodash;
|
|
||||||
const mongodb = require("mongodb");
|
|
||||||
exports.mongodb = mongodb;
|
|
||||||
const q = require("q");
|
|
||||||
exports.q = q;
|
|
||||||
let nedb = require('nedb');
|
|
||||||
exports.nedb = nedb;
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLnBsdWdpbnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEucGx1Z2lucy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsMEJBQXVCO0FBQ3ZCLGlDQUFnQztBQVE1Qix3QkFBTTtBQVBWLHVDQUFzQztBQVFsQyw4QkFBUztBQVBiLGlDQUFnQztBQVE1Qix3QkFBTTtBQVBWLG1DQUFrQztBQVE5QiwwQkFBTztBQVBYLHVCQUFzQjtBQVFsQixjQUFDO0FBUEwsSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLE1BQU0sQ0FBQyxDQUFBO0FBUXRCLG9CQUFJIn0=
|
|
@ -1,19 +0,0 @@
|
|||||||
# smartdata
|
|
||||||
|
|
||||||
smartdata is a ODM that adheres to TypeScript practices and uses classes to organize data.
|
|
||||||
It uses MongoDB as persistent storage.
|
|
||||||
|
|
||||||
## Intention
|
|
||||||
There are many ODMs out there, however when we searched for a ODM that uses TypeScript,
|
|
||||||
acts smart while still embracing an easy the NoSQL idea we didn't find a matching solution.
|
|
||||||
This is why we started smartdata
|
|
||||||
|
|
||||||
How MongoDB terms map to smartdata classes
|
|
||||||
|
|
||||||
MongoDB term | smartdata class
|
|
||||||
--- | ---
|
|
||||||
Database | smartdata.DbConnection
|
|
||||||
Collection | smartdata.DbCollection
|
|
||||||
Document | smartdata.DbDoc
|
|
||||||
|
|
||||||
[](https://push.rocks)
|
|
12
npmextra.json
Normal file
12
npmextra.json
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"npmdocker": {
|
||||||
|
"baseImage": "hosttoday/ht-docker-node:mongo",
|
||||||
|
"command": "npmci test stable",
|
||||||
|
"dockerSock": false
|
||||||
|
},
|
||||||
|
"npmci": {
|
||||||
|
"npmGlobalTools": [],
|
||||||
|
"npmAccessLevel": "public",
|
||||||
|
"npmRegistryUrl": "registry.npmjs.org"
|
||||||
|
}
|
||||||
|
}
|
2260
package-lock.json
generated
Normal file
2260
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
47
package.json
47
package.json
@ -1,12 +1,14 @@
|
|||||||
{
|
{
|
||||||
"name": "smartdata",
|
"name": "@pushrocks/smartdata",
|
||||||
"version": "1.0.22",
|
"version": "3.1.7",
|
||||||
|
"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": "(npm run prepareMongo && npmts)",
|
"test": "(tstest test/)",
|
||||||
"prepareMongo": "(rm -rf ./test/data && mkdir ./test/data/)"
|
"testLocal": "(npmdocker)",
|
||||||
|
"build": "(tsbuild)"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -19,25 +21,26 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartdata#README",
|
"homepage": "https://gitlab.com/pushrocks/smartdata#README",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/lodash": "^4.14.39",
|
"@pushrocks/lik": "^3.0.4",
|
||||||
"@types/mongodb": "^2.1.34",
|
"@pushrocks/smartlog": "^2.0.9",
|
||||||
"@types/nedb": "0.0.31",
|
"@pushrocks/smartpromise": "^2.0.5",
|
||||||
"@types/q": "0.0.32",
|
"@pushrocks/smartstring": "^3.0.5",
|
||||||
"beautylog": "^6.0.0",
|
"@types/lodash": "^4.14.119",
|
||||||
"lik": "^1.0.24",
|
"@types/mongodb": "^3.1.18",
|
||||||
"lodash": "^4.17.2",
|
"lodash": "^4.17.11",
|
||||||
"mongodb": "^2.2.11",
|
"mongodb": "^3.1.10",
|
||||||
"nedb": "^1.8.0",
|
"runtime-type-checks": "0.0.4"
|
||||||
"q": "^1.4.1",
|
|
||||||
"runtime-type-checks": "0.0.4",
|
|
||||||
"typings-global": "^1.0.14"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/shelljs": "^0.3.32",
|
"@gitzone/tsbuild": "^2.1.4",
|
||||||
"@types/should": "^8.1.30",
|
"@gitzone/tstest": "^1.0.18",
|
||||||
"shelljs": "^0.7.5",
|
"@pushrocks/qenv": "^3.0.2",
|
||||||
"should": "^11.1.1",
|
"@pushrocks/tapbundle": "^3.0.7",
|
||||||
"smartstring": "^2.0.22",
|
"@types/node": "^10.12.18",
|
||||||
"typings-test": "^1.0.3"
|
"@types/shortid": "0.0.29",
|
||||||
|
"mongodb-memory-server": "^2.9.1",
|
||||||
|
"shortid": "^2.2.14",
|
||||||
|
"tslint": "^5.12.0",
|
||||||
|
"tslint-config-prettier": "^1.17.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
1
test/test.d.ts
vendored
1
test/test.d.ts
vendored
@ -1 +0,0 @@
|
|||||||
import 'typings-test';
|
|
129
test/test.js
129
test/test.js
File diff suppressed because one or more lines are too long
178
test/test.ts
178
test/test.ts
@ -1,133 +1,65 @@
|
|||||||
import 'typings-test'
|
import { tap, expect } from '@pushrocks/tapbundle';
|
||||||
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
|
import { Qenv } from '@pushrocks/qenv';
|
||||||
|
|
||||||
import * as shelljs from 'shelljs'
|
let testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit/');
|
||||||
import * as should from 'should'
|
|
||||||
import * as smartstring from 'smartstring'
|
|
||||||
|
|
||||||
// the tested module
|
// the tested module
|
||||||
import * as smartdata from '../dist/index'
|
import * as smartdata from '../ts/index';
|
||||||
|
import { smartstring } from '../ts/smartdata.plugins';
|
||||||
|
import * as shortid from 'shortid';
|
||||||
|
|
||||||
let mongoChildProcess
|
// =======================================
|
||||||
let testDb: smartdata.Db
|
// Connecting to the database server
|
||||||
|
// =======================================
|
||||||
|
|
||||||
interface ITestObject1 {
|
let testDb = new smartdata.SmartdataDb({
|
||||||
value1: string
|
mongoDbName: process.env.MONGO_DBNAME,
|
||||||
value2?: number
|
mongoDbUrl: process.env.MONGO_URL,
|
||||||
value3?: string
|
mongoDbPass: process.env.MONGO_PASS
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should establish a connection to the rethink Db cluster', async () => {
|
||||||
|
await testDb.connect();
|
||||||
|
});
|
||||||
|
|
||||||
|
// =======================================
|
||||||
|
// The actual tests
|
||||||
|
// =======================================
|
||||||
|
|
||||||
|
// ------
|
||||||
|
// Collections
|
||||||
|
// ------
|
||||||
|
|
||||||
|
@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) {
|
||||||
|
super();
|
||||||
|
this.color = colorArg;
|
||||||
|
this.brand = brandArg;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let testDbCollection: smartdata.DbCollection<ITestObject1>
|
tap.test('should save the car to the db', async () => {
|
||||||
|
const myCar = new Car('red', 'Volvo');
|
||||||
|
await myCar.save();
|
||||||
|
});
|
||||||
|
|
||||||
describe('mongodb', function () {
|
tap.test('expect to get instance of Car', async () => {
|
||||||
it('should start mongodb', function (done) {
|
let myCars = await Car.getInstances<Car>({
|
||||||
this.timeout(30000)
|
brand: 'Volvo'
|
||||||
mongoChildProcess = shelljs.exec('mongod --dbpath=./test/data --port 27017', { async: true, silent: true })
|
});
|
||||||
let doneCalled = false
|
expect(myCars[0].color).to.equal('red');
|
||||||
mongoChildProcess.stdout.on('data', function (data) {
|
});
|
||||||
console.log(smartstring.indent.indentWithPrefix(data, '*** MongoDB Process *** : '))
|
|
||||||
if (!doneCalled) {
|
|
||||||
if (/waiting for connections on port 27017/.test(data)) {
|
|
||||||
doneCalled = true
|
|
||||||
done()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('smartdata', function () {
|
// =======================================
|
||||||
it('should establish a connection to mongodb', function (done) {
|
// close the database connection
|
||||||
testDb = new smartdata.Db('mongodb://localhost:27017/smartdata')
|
// =======================================
|
||||||
testDb.connect().then(() => { done() })
|
tap.test('should close the database connection', async tools => {
|
||||||
})
|
await testDb.close();
|
||||||
it('should create a collection', function () {
|
});
|
||||||
testDbCollection = new smartdata.DbCollection<ITestObject1>('something', testDb)
|
|
||||||
})
|
|
||||||
it('should insert a doc into the collection', function (done) {
|
|
||||||
testDbCollection.insertOne({ value1: 'test' }).then(() => { done() })
|
|
||||||
})
|
|
||||||
it('should find all docs of testDbCollection', function (done) {
|
|
||||||
testDbCollection.find({}).then((resultArray) => {
|
|
||||||
console.log(resultArray)
|
|
||||||
should(resultArray[0].value1).equal('test')
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
it('should insert many docs into the collection', function (done) {
|
|
||||||
testDbCollection.insertMany([
|
|
||||||
{ value1: 'test2' },
|
|
||||||
{ value1: 'test', value2: 3, value3: 'hi' }
|
|
||||||
]).then(() => { done() })
|
|
||||||
})
|
|
||||||
it('should find a specified doc', function (done) {
|
|
||||||
testDbCollection.find({ 'value3': { '$exists': true } }).then((resultArray) => {
|
|
||||||
console.log(resultArray)
|
|
||||||
should(resultArray[0].value3).equal('hi')
|
|
||||||
done()
|
|
||||||
}).catch(console.log)
|
|
||||||
})
|
|
||||||
it('should close the db Connection', function () {
|
|
||||||
testDb.close()
|
|
||||||
})
|
|
||||||
it('should create an extended class', function () {
|
|
||||||
@smartdata.Collection(testDb)
|
|
||||||
class TestCar extends smartdata.DbDoc<TestCar> {
|
|
||||||
@smartdata.svDb()
|
|
||||||
color: string
|
|
||||||
constructor(optionsArg: {
|
|
||||||
color: string,
|
|
||||||
property2: number
|
|
||||||
}) {
|
|
||||||
super()
|
|
||||||
this.color = optionsArg.color
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let testCarInstance = new TestCar({
|
|
||||||
color: 'red',
|
|
||||||
property2: 2
|
|
||||||
})
|
|
||||||
|
|
||||||
should(testCarInstance.saveableProperties[0]).equal('color')
|
tap.start({ throwOnError: true });
|
||||||
console.log(TestCar)
|
|
||||||
should(testCarInstance.collection).be.instanceof(smartdata.DbCollection)
|
|
||||||
should(testCarInstance).be.instanceof(smartdata.DbDoc)
|
|
||||||
testCarInstance.save()
|
|
||||||
it('should get a collection for testCar', function () {
|
|
||||||
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('mongodb', function () {
|
|
||||||
it('should kill mongodb', function (done) {
|
|
||||||
this.timeout(30000)
|
|
||||||
mongoChildProcess.stdout.on('data', function (data) {
|
|
||||||
if (/dbexit: rc: 0/.test(data)) {
|
|
||||||
done()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
shelljs.exec('mongod --dbpath=./test/data --shutdown')
|
|
||||||
mongoChildProcess.kill('SIGTERM')
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('smartdata with nedb', function () {
|
|
||||||
let testDb: smartdata.Db
|
|
||||||
let testCollection: smartdata.DbCollection<ITestObject1>
|
|
||||||
it('should create a DB with nedb', function () {
|
|
||||||
testDb = new smartdata.Db('any', 'nedb')
|
|
||||||
testDb.connect()
|
|
||||||
testCollection = new smartdata.DbCollection<ITestObject1>('anyName', testDb)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should insert a doc', function(done){
|
|
||||||
testCollection.insertOne({ value1: 'hi' }).then(() => {done()})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should find the inserted document', function(done){
|
|
||||||
testCollection.find({ value1: 'hi' }).then(x => {
|
|
||||||
console.log(x)
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import * as plugins from './smartdata.plugins'
|
export * from './smartdata.classes.db';
|
||||||
|
export * from './smartdata.classes.collection';
|
||||||
|
export * from './smartdata.classes.doc';
|
||||||
|
|
||||||
export * from './smartdata.classes.db'
|
export { IMongoDescriptor } from './interfaces';
|
||||||
export * from './smartdata.classes.dbcollection'
|
|
||||||
export * from './smartdata.classes.dbdoc'
|
|
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 {
|
||||||
|
connectionUrl: string;
|
||||||
|
password: string;
|
||||||
|
database: string;
|
||||||
|
}
|
134
ts/smartdata.classes.collection.ts
Normal file
134
ts/smartdata.classes.collection.ts
Normal file
@ -0,0 +1,134 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is a decorator that will tell the decorated class what dbTable to use
|
||||||
|
* @param db
|
||||||
|
*/
|
||||||
|
export function Collection(db: SmartdataDb) {
|
||||||
|
return function(constructor) {
|
||||||
|
constructor['smartdataCollection'] = new SmartdataCollection(constructor, db);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export class SmartdataCollection<T> {
|
||||||
|
/**
|
||||||
|
* the collection that is used
|
||||||
|
*/
|
||||||
|
mongoDbCollection: plugins.mongodb.Collection;
|
||||||
|
objectValidation: IDocValidationFunc<T> = null;
|
||||||
|
collectionName: string;
|
||||||
|
smartdataDb: SmartdataDb;
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
addDocValidation(funcArg: IDocValidationFunc<T>) {
|
||||||
|
this.objectValidation = funcArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* finds an object in the DbCollection
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* inserts object into the DbCollection
|
||||||
|
*/
|
||||||
|
async update(dbDocArg: T & SmartDataDbDoc<T>): Promise<any> {
|
||||||
|
await this.init();
|
||||||
|
await this.checkDoc(dbDocArg);
|
||||||
|
const saveableObject = await dbDocArg.createSavableObject();
|
||||||
|
this.mongoDbCollection.updateOne(saveableObject.dbDocUniqueId, saveableObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* checks a Doc for constraints
|
||||||
|
* if this.objectValidation is not set it passes.
|
||||||
|
*/
|
||||||
|
private checkDoc(docArg: T): Promise<void> {
|
||||||
|
let 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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,28 +1,42 @@
|
|||||||
import * as plugins from './smartdata.plugins'
|
import * as plugins from './smartdata.plugins';
|
||||||
import { Objectmap } from 'lik'
|
import { Objectmap } from '@pushrocks/lik';
|
||||||
|
|
||||||
import { DbCollection } from './smartdata.classes.dbcollection'
|
import { SmartdataCollection } from './smartdata.classes.collection';
|
||||||
|
|
||||||
/**
|
import * as mongoHelpers from './smartdata.mongohelpers';
|
||||||
* interface - indicates the database type
|
|
||||||
*/
|
|
||||||
export type TDbType = 'mongodb' | 'nedb'
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* interface - indicates the connection status of the db
|
* interface - indicates the connection status of the db
|
||||||
*/
|
*/
|
||||||
export type TConnectionStatus = 'disconnected' | 'connected' | 'failed'
|
export type TConnectionStatus = 'initial' | 'disconnected' | 'connected' | 'failed';
|
||||||
|
|
||||||
export class Db {
|
export interface ISmartdataOptions {
|
||||||
dbType: TDbType
|
/**
|
||||||
dbUrl: string
|
* the URL to connect to
|
||||||
db: plugins.mongodb.Db
|
*/
|
||||||
status: TConnectionStatus
|
mongoDbUrl: string;
|
||||||
collections = new Objectmap<DbCollection<any>>()
|
|
||||||
|
|
||||||
constructor(dbUrlArg: string, dbTypeArg: TDbType = 'mongodb') {
|
/**
|
||||||
this.dbType = dbTypeArg
|
* the db to use for the project
|
||||||
this.dbUrl = dbUrlArg
|
*/
|
||||||
|
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 ----------------------------------------------
|
// basic connection stuff ----------------------------------------------
|
||||||
@ -30,53 +44,53 @@ export class Db {
|
|||||||
/**
|
/**
|
||||||
* connects to the database that was specified during instance creation
|
* connects to the database that was specified during instance creation
|
||||||
*/
|
*/
|
||||||
connect(): plugins.q.Promise<any> {
|
async connect(): Promise<any> {
|
||||||
let done = plugins.q.defer()
|
let finalConnectionUrl = this.smartdataOptions.mongoDbUrl;
|
||||||
if (this.dbType === 'mongodb') {
|
if (this.smartdataOptions.mongoDbPass) {
|
||||||
plugins.mongodb.MongoClient.connect(this.dbUrl, (err, db) => {
|
finalConnectionUrl = mongoHelpers.addPassword(
|
||||||
if (err) { console.log(err) }
|
this.smartdataOptions.mongoDbUrl,
|
||||||
plugins.assert.equal(null, err)
|
this.smartdataOptions.mongoDbPass
|
||||||
this.db = db
|
);
|
||||||
plugins.beautylog.success(`connected to database at ${this.dbUrl}`)
|
|
||||||
done.resolve(this.db)
|
|
||||||
})
|
|
||||||
} else if (this.dbType === 'nedb') {
|
|
||||||
this.db = null
|
|
||||||
}
|
}
|
||||||
return done.promise
|
console.log(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
|
* closes the connection to the databse
|
||||||
*/
|
*/
|
||||||
close(): plugins.q.Promise<any> {
|
async close(): Promise<any> {
|
||||||
let done = plugins.q.defer()
|
await this.mongoDbClient.close();
|
||||||
if (this.dbType === 'mongodb') {
|
this.status = 'disconnected';
|
||||||
this.db.close()
|
plugins.smartlog.defaultLogger.log(
|
||||||
}
|
'info',
|
||||||
plugins.beautylog.ok(`disconnected to database at ${this.dbUrl}`)
|
`disconnected from database ${this.smartdataOptions.mongoDbName}`
|
||||||
done.resolve()
|
);
|
||||||
return done.promise
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// advanced communication with the database --------------------------------
|
// handle table to class distribution
|
||||||
|
|
||||||
|
addTable(SmartdataCollectionArg: SmartdataCollection<any>) {
|
||||||
|
this.smartdataCollectionMap.add(SmartdataCollectionArg);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets a collection by name: string
|
* Gets a collection's name and returns a SmartdataCollection instance
|
||||||
|
* @param nameArg
|
||||||
|
* @returns DbTable
|
||||||
*/
|
*/
|
||||||
getCollectionByName<T>(nameArg: string): plugins.q.Promise<DbCollection<T>> {
|
async getSmartdataCollectionByName<T>(nameArg: string): Promise<SmartdataCollection<T>> {
|
||||||
let done = plugins.q.defer<DbCollection<any>>()
|
let resultCollection = this.smartdataCollectionMap.find(dbTableArg => {
|
||||||
let resultCollection = this.collections.find((dbCollectionArg) => {
|
return dbTableArg.collectionName === nameArg;
|
||||||
return dbCollectionArg.name === nameArg
|
});
|
||||||
})
|
return resultCollection;
|
||||||
if (resultCollection !== null) {
|
|
||||||
done.resolve(resultCollection)
|
|
||||||
}
|
}
|
||||||
return done.promise
|
|
||||||
};
|
|
||||||
|
|
||||||
addCollection(dbCollectionArg: DbCollection<any>) {
|
|
||||||
this.collections.add(dbCollectionArg)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,144 +0,0 @@
|
|||||||
import * as plugins from './smartdata.plugins'
|
|
||||||
import { Db } from './smartdata.classes.db'
|
|
||||||
|
|
||||||
export interface IFindOptions {
|
|
||||||
limit?: number
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IDocValidation<T> {
|
|
||||||
(doc: T): boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
export function Collection(db: Db) {
|
|
||||||
return function (constructor) {
|
|
||||||
constructor['dbCollection'] = new DbCollection(constructor.name, db)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class DbCollection<T> {
|
|
||||||
/**
|
|
||||||
* the collection that is used, defaults to mongodb collection,
|
|
||||||
* can be nedb datastore (sub api of mongodb)
|
|
||||||
*/
|
|
||||||
collection: plugins.mongodb.Collection
|
|
||||||
name: string
|
|
||||||
db: Db
|
|
||||||
objectValidation: IDocValidation<T> = null
|
|
||||||
|
|
||||||
|
|
||||||
constructor(nameArg: string, dbArg: Db) {
|
|
||||||
this.name = nameArg
|
|
||||||
this.db = dbArg
|
|
||||||
if (this.db.dbType === 'mongodb') {
|
|
||||||
this.collection = dbArg.db.collection(nameArg)
|
|
||||||
} else {
|
|
||||||
this.collection = new plugins.nedb()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* adds a validation function that all newly inserted and updated objects have to pass
|
|
||||||
*/
|
|
||||||
addDocValidation(funcArg: IDocValidation<T>) {
|
|
||||||
this.objectValidation = funcArg
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* finds an object in the DbCollection
|
|
||||||
*/
|
|
||||||
find(docMatchArg: T | any, optionsArg?: IFindOptions): plugins.q.Promise<T[]> {
|
|
||||||
let done = plugins.q.defer<T[]>()
|
|
||||||
if (this.db.dbType === 'mongodb') {
|
|
||||||
let findCursor = this.collection.find(docMatchArg)
|
|
||||||
if (optionsArg) {
|
|
||||||
if (optionsArg.limit) { findCursor = findCursor.limit(1) }
|
|
||||||
}
|
|
||||||
findCursor.toArray((err, docs) => {
|
|
||||||
if (err) {
|
|
||||||
done.reject(err)
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
done.resolve(docs)
|
|
||||||
})
|
|
||||||
} else if (this.db.dbType === 'nedb') {
|
|
||||||
this.collection.find(docMatchArg, (err, docs) => {
|
|
||||||
if (err) {
|
|
||||||
done.reject(err)
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
done.resolve(docs)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return done.promise
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* inserts object into the DbCollection
|
|
||||||
*/
|
|
||||||
insertOne(docArg: T): plugins.q.Promise<void> {
|
|
||||||
let done = plugins.q.defer<void>()
|
|
||||||
this.checkDoc(docArg).then(
|
|
||||||
() => {
|
|
||||||
if (this.db.dbType === 'mongodb') {
|
|
||||||
this.collection.insertOne(docArg)
|
|
||||||
.then(() => { done.resolve() })
|
|
||||||
} else if (this.db.dbType === 'nedb') {
|
|
||||||
this.collection.insert(docArg, (err, newDoc) => {
|
|
||||||
if (err) {
|
|
||||||
done.reject(err)
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
done.resolve()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
},
|
|
||||||
() => {
|
|
||||||
done.reject(new Error('one the docs did not pass validation'))
|
|
||||||
})
|
|
||||||
return done.promise
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* inserts many objects at once into the DbCollection
|
|
||||||
*/
|
|
||||||
insertMany(docArrayArg: T[]): plugins.q.Promise<void> {
|
|
||||||
let done = plugins.q.defer<void>()
|
|
||||||
let checkDocPromiseArray: plugins.q.Promise<void>[] = []
|
|
||||||
for (let docArg of docArrayArg) {
|
|
||||||
checkDocPromiseArray.push(this.checkDoc(docArg))
|
|
||||||
}
|
|
||||||
plugins.q.all(checkDocPromiseArray).then(() => {
|
|
||||||
if (this.db.dbType === 'mongodb') {
|
|
||||||
this.collection.insertMany(docArrayArg)
|
|
||||||
.then(() => { done.resolve() })
|
|
||||||
} else if (this.db.dbType === 'nedb') {
|
|
||||||
let paramArray = plugins.lodash.concat<any>(docArrayArg, (err, newDoc) => {
|
|
||||||
if (err) {
|
|
||||||
done.reject(err)
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
done.resolve()
|
|
||||||
})
|
|
||||||
this.collection.insert.apply(null, paramArray)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return done.promise
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* checks a Doc for constraints
|
|
||||||
*/
|
|
||||||
private checkDoc(docArg: T): plugins.q.Promise<void> {
|
|
||||||
let done = plugins.q.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
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,68 +0,0 @@
|
|||||||
import * as plugins from './smartdata.plugins'
|
|
||||||
|
|
||||||
import { Db } from './smartdata.classes.db'
|
|
||||||
import { DbCollection } from './smartdata.classes.dbcollection'
|
|
||||||
|
|
||||||
export type TDocCreation = 'db' | 'new' | 'mixed'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* saveable - saveable decorator to be used on class properties
|
|
||||||
*/
|
|
||||||
export function svDb() {
|
|
||||||
return (target: DbDoc<any>, key: string) => {
|
|
||||||
console.log('called sva')
|
|
||||||
if (!target.saveableProperties) { target.saveableProperties = [] }
|
|
||||||
target.saveableProperties.push(key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class DbDoc<T> {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* the collection object an Doc belongs to
|
|
||||||
*/
|
|
||||||
collection: DbCollection<T>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* how the Doc in memory was created, may prove useful later.
|
|
||||||
*/
|
|
||||||
creationType: TDocCreation
|
|
||||||
|
|
||||||
/**
|
|
||||||
* an array of saveable properties of a doc
|
|
||||||
*/
|
|
||||||
saveableProperties: string[]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* class constructor
|
|
||||||
*/
|
|
||||||
constructor() {
|
|
||||||
this.collection = this.constructor['dbCollection']
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* saves this instance but not any connected items
|
|
||||||
* may lead to data inconsistencies, but is faster
|
|
||||||
*/
|
|
||||||
save() {
|
|
||||||
let saveableObject = {}
|
|
||||||
for (let propertyNameString of this.saveableProperties) {
|
|
||||||
saveableObject[propertyNameString] = this[propertyNameString]
|
|
||||||
}
|
|
||||||
switch (this.creationType) {
|
|
||||||
case 'db':
|
|
||||||
this.collection // TODO implement collection.update()
|
|
||||||
break
|
|
||||||
case 'new':
|
|
||||||
this.collection.insertOne(saveableObject)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* also store any referenced objects to DB
|
|
||||||
* better for data consistency
|
|
||||||
*/
|
|
||||||
saveDeep() {
|
|
||||||
this.save()
|
|
||||||
}
|
|
||||||
}
|
|
151
ts/smartdata.classes.doc.ts
Normal file
151
ts/smartdata.classes.doc.ts
Normal file
@ -0,0 +1,151 @@
|
|||||||
|
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
|
||||||
|
*/
|
||||||
|
collection: SmartdataCollection<T>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* how the Doc in memory was created, may prove useful later.
|
||||||
|
*/
|
||||||
|
creationStatus: TDocCreation = 'new';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* unique indexes
|
||||||
|
*/
|
||||||
|
uniqueIndexes: string[];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* an array of saveable properties of a doc
|
||||||
|
*/
|
||||||
|
saveableProperties: string[];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* name
|
||||||
|
*/
|
||||||
|
name: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* primary id in the database
|
||||||
|
*/
|
||||||
|
dbDocUniqueId: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* class constructor
|
||||||
|
*/
|
||||||
|
constructor() {
|
||||||
|
this.name = this.constructor['name'];
|
||||||
|
this.collection = this.constructor['smartdataCollection'];
|
||||||
|
}
|
||||||
|
|
||||||
|
static async getInstances<T>(filterArg): Promise<T[]> {
|
||||||
|
let self: any = this; // fool typesystem
|
||||||
|
let referenceMongoDBCollection: SmartdataCollection<T> = self.smartdataCollection;
|
||||||
|
const foundDocs = await referenceMongoDBCollection.find(filterArg);
|
||||||
|
const returnArray = [];
|
||||||
|
for (let item of foundDocs) {
|
||||||
|
let newInstance = new this();
|
||||||
|
for (let key in item) {
|
||||||
|
if (key !== 'id') {
|
||||||
|
newInstance[key] = item[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
returnArray.push(newInstance);
|
||||||
|
}
|
||||||
|
return returnArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
static async getInstance<T>(filterArg): Promise<T> {
|
||||||
|
let 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
|
||||||
|
*/
|
||||||
|
async save() {
|
||||||
|
let self: any = this;
|
||||||
|
switch (this.creationStatus) {
|
||||||
|
case 'db':
|
||||||
|
await this.collection.update(self);
|
||||||
|
break;
|
||||||
|
case 'new':
|
||||||
|
let writeResult = await this.collection.insert(self);
|
||||||
|
this.creationStatus = 'db';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.error('neither new nor in db?');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* also store any referenced objects to DB
|
||||||
|
* better for data consistency
|
||||||
|
*/
|
||||||
|
saveDeep(savedMapArg: Objectmap<SmartDataDbDoc<any>> = null) {
|
||||||
|
if (!savedMapArg) {
|
||||||
|
savedMapArg = new Objectmap<SmartDataDbDoc<any>>();
|
||||||
|
}
|
||||||
|
savedMapArg.add(this);
|
||||||
|
this.save();
|
||||||
|
for (let propertyKey in this) {
|
||||||
|
let property: any = this[propertyKey];
|
||||||
|
if (property instanceof SmartDataDbDoc && !savedMapArg.checkForObject(property)) {
|
||||||
|
property.saveDeep(savedMapArg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async createSavableObject() {
|
||||||
|
let saveableObject: any = {}; // is not exposed to outside, so any is ok here
|
||||||
|
for (let propertyNameString of this.saveableProperties) {
|
||||||
|
saveableObject[propertyNameString] = this[propertyNameString];
|
||||||
|
}
|
||||||
|
return saveableObject;
|
||||||
|
}
|
||||||
|
}
|
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,16 +1,8 @@
|
|||||||
import 'typings-global'
|
import * as assert from 'assert';
|
||||||
import * as assert from 'assert'
|
import * as smartlog from '@pushrocks/smartlog';
|
||||||
import * as beautylog from 'beautylog'
|
import * as lodash from 'lodash';
|
||||||
import * as lodash from 'lodash'
|
import * as mongodb from 'mongodb';
|
||||||
import * as mongodb from 'mongodb'
|
import * as smartq from '@pushrocks/smartpromise';
|
||||||
import * as q from 'q'
|
import * as smartstring from '@pushrocks/smartstring';
|
||||||
let nedb = require('nedb')
|
|
||||||
|
|
||||||
export {
|
export { assert, smartlog, lodash, smartq, mongodb, smartstring };
|
||||||
assert,
|
|
||||||
beautylog,
|
|
||||||
lodash,
|
|
||||||
mongodb,
|
|
||||||
q,
|
|
||||||
nedb
|
|
||||||
}
|
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"experimentalDecorators": true
|
"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