Compare commits
51 Commits
Author | SHA1 | Date | |
---|---|---|---|
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 |
5
.gitignore
vendored
5
.gitignore
vendored
@ -1,5 +1,4 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
test/data
|
|
||||||
pages/
|
|
||||||
coverage/
|
|
||||||
public/
|
public/
|
||||||
|
coverage/
|
||||||
|
.nogit/
|
||||||
|
128
.gitlab-ci.yml
128
.gitlab-ci.yml
@ -1,43 +1,141 @@
|
|||||||
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
|
||||||
|
script:
|
||||||
|
- npmci git mirror
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
|
snyk:
|
||||||
|
stage: security
|
||||||
|
script:
|
||||||
|
- npmci command npm install -g snyk
|
||||||
|
- npmci command npm install --ignore-scripts
|
||||||
|
- npmci command snyk test
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
|
# ====================
|
||||||
|
# test stage
|
||||||
|
# ====================
|
||||||
testLEGACY:
|
testLEGACY:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci test legacy
|
- npmci node install legacy
|
||||||
|
- npmci npm install
|
||||||
|
- npmci npm test
|
||||||
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
- notpriv
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
|
|
||||||
testLTS:
|
testLTS:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci test lts
|
- 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 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:
|
||||||
|
- tags
|
||||||
|
tags:
|
||||||
|
- 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 npmpage
|
||||||
|
- npmci command npmpage
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
- notpriv
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
|
artifacts:
|
||||||
|
expire_in: 1 week
|
||||||
|
paths:
|
||||||
|
- public
|
||||||
|
allow_failure: true
|
||||||
|
|
||||||
|
windowsCompatibility:
|
||||||
|
image: stefanscherer/node-windows:10-build-tools
|
||||||
|
stage: metadata
|
||||||
|
script:
|
||||||
|
- npm install & npm test
|
||||||
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- windows
|
||||||
|
allow_failure: true
|
||||||
|
131
README.md
131
README.md
@ -1,89 +1,134 @@
|
|||||||
# smartdata
|
# @pushrocks/smartdata
|
||||||
|
|
||||||
> Note: Still in Beta
|
do more with data and RethinkDB
|
||||||
|
|
||||||
|
## Availabililty
|
||||||
|
|
||||||
|
[](https://www.npmjs.com/package/smartdata)
|
||||||
|
[](https://GitLab.com/pushrocks/smartdata)
|
||||||
|
[](https://github.com/pushrocks/smartdata)
|
||||||
|
[](https://pushrocks.gitlab.io/smartdata/)
|
||||||
|
|
||||||
|
## Status for master
|
||||||
|
|
||||||
|
[](https://GitLab.com/pushrocks/smartdata/commits/master)
|
||||||
|
[](https://GitLab.com/pushrocks/smartdata/commits/master)
|
||||||
|
[](https://www.npmjs.com/package/smartdata)
|
||||||
|
[](https://david-dm.org/pushrocks/smartdata)
|
||||||
|
[](https://www.bithound.io/github/pushrocks/smartdata/master/dependencies/npm)
|
||||||
|
[](https://www.bithound.io/github/pushrocks/smartdata)
|
||||||
|
[](https://snyk.io/test/npm/smartdata)
|
||||||
|
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||||
|
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||||
|
[](http://standardjs.com/)
|
||||||
|
|
||||||
|
## 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 an 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
|
| RethinkDB term | smartdata class |
|
||||||
--- | ---
|
| -------------- | ----------------- |
|
||||||
Database | smartdata.Db
|
| Database | smartdata.Db |
|
||||||
Collection | smartdata.DbCollection
|
| Table | smartdata.DbTable |
|
||||||
Document | smartdata.DbDoc
|
| Document | smartdata.DbDoc |
|
||||||
|
|
||||||
### 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
|
let myRethinkDb1 = new smartdata.Db({
|
||||||
let myDb3 = new smartdata('/some/path/for/persistence', 'nedb') // you may set first argument to null for just in memory db
|
db: 'test',
|
||||||
|
host: 'https://some',
|
||||||
|
user: 'testuser',
|
||||||
|
password: 'testpass',
|
||||||
|
port: 1234
|
||||||
|
});
|
||||||
|
|
||||||
myDb1.connect()
|
// in case you need to support a proprietory ssl cert (e.g. compose.com):
|
||||||
myDb2.connect()
|
myRethinkDb1.setSsl(process.env.RDB_CERT, 'base64');
|
||||||
|
|
||||||
// continues in next block...
|
myDb1.connect();
|
||||||
```
|
```
|
||||||
|
|
||||||
### 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.Table(myRethinkDb1)
|
||||||
class myObject extends smartdata.DbDoc<myObject> { // read the next block about DbDoc
|
class MyObject extends smartdata.DbDoc<myObject> {
|
||||||
property1:string
|
// read the next block about DbDoc
|
||||||
property2:number
|
@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
|
||||||
```
|
|
||||||
|
|
||||||
> Alert: You NEVER instantiate a collection.
|
let localObject = new MyObject({
|
||||||
This is done for you!!!
|
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
|
### 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)
|
||||||
|
6
dist/index.d.ts
vendored
6
dist/index.d.ts
vendored
@ -1,3 +1,3 @@
|
|||||||
export * from './smartdata.classes.db';
|
export * from "./smartdata.classes.db";
|
||||||
export * from './smartdata.classes.dbcollection';
|
export * from "./smartdata.classes.dbtable";
|
||||||
export * from './smartdata.classes.dbdoc';
|
export * from "./smartdata.classes.dbdoc";
|
||||||
|
5
dist/index.js
vendored
5
dist/index.js
vendored
@ -2,7 +2,8 @@
|
|||||||
function __export(m) {
|
function __export(m) {
|
||||||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
||||||
}
|
}
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
__export(require("./smartdata.classes.db"));
|
__export(require("./smartdata.classes.db"));
|
||||||
__export(require("./smartdata.classes.dbcollection"));
|
__export(require("./smartdata.classes.dbtable"));
|
||||||
__export(require("./smartdata.classes.dbdoc"));
|
__export(require("./smartdata.classes.dbdoc"));
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7O0FBRUEsNENBQXNDO0FBQ3RDLHNEQUFnRDtBQUNoRCwrQ0FBeUMifQ==
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLDRDQUF1QztBQUN2QyxpREFBNEM7QUFDNUMsK0NBQTBDIn0=
|
41
dist/smartdata.classes.db.d.ts
vendored
41
dist/smartdata.classes.db.d.ts
vendored
@ -1,33 +1,36 @@
|
|||||||
/// <reference types="q" />
|
import * as plugins from "./smartdata.plugins";
|
||||||
import * as plugins from './smartdata.plugins';
|
import { Objectmap } from "lik";
|
||||||
import { Objectmap } from 'lik';
|
import { DbTable } from "./smartdata.classes.dbtable";
|
||||||
import { DbCollection } from './smartdata.classes.dbcollection';
|
import { ConnectionOptions as IConnectionOptions } from "rethinkdb";
|
||||||
/**
|
export { IConnectionOptions };
|
||||||
* interface - indicates the database type
|
|
||||||
*/
|
|
||||||
export declare type TDbType = 'mongodb' | 'nedb';
|
|
||||||
/**
|
/**
|
||||||
* interface - indicates the connection status of the db
|
* interface - indicates the connection status of the db
|
||||||
*/
|
*/
|
||||||
export declare type TConnectionStatus = 'disconnected' | 'connected' | 'failed';
|
export declare type TConnectionStatus = "initial" | "disconnected" | "connected" | "failed";
|
||||||
export declare class Db {
|
export declare class Db {
|
||||||
dbType: TDbType;
|
dbName: string;
|
||||||
dbUrl: string;
|
connectionOptions: plugins.rethinkDb.ConnectionOptions;
|
||||||
db: plugins.mongodb.Db;
|
dbConnection: plugins.rethinkDb.Connection;
|
||||||
status: TConnectionStatus;
|
status: TConnectionStatus;
|
||||||
collections: Objectmap<DbCollection<any>>;
|
dbTablesMap: Objectmap<DbTable<any>>;
|
||||||
constructor(dbUrlArg: string, dbTypeArg?: TDbType);
|
constructor(connectionOptionsArg: IConnectionOptions);
|
||||||
|
/**
|
||||||
|
* supply additional SSl options needed to connect to certain Rethink DB servers (e.g. compose.io)
|
||||||
|
*/
|
||||||
|
setSsl(certificateStringArg: string, formatArg: "base64" | "clearText"): void;
|
||||||
/**
|
/**
|
||||||
* 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>;
|
connect(): Promise<any>;
|
||||||
/**
|
/**
|
||||||
* closes the connection to the databse
|
* closes the connection to the databse
|
||||||
*/
|
*/
|
||||||
close(): plugins.q.Promise<any>;
|
close(): Promise<any>;
|
||||||
|
addTable(dbTableArg: DbTable<any>): void;
|
||||||
/**
|
/**
|
||||||
* gets a collection by name: string
|
* Gets a table's name and returns smartdata's DbTable class
|
||||||
|
* @param nameArg
|
||||||
|
* @returns DbTable
|
||||||
*/
|
*/
|
||||||
getCollectionByName<T>(nameArg: string): plugins.q.Promise<DbCollection<T>>;
|
getDbTableByName<T>(nameArg: string): Promise<DbTable<T>>;
|
||||||
addCollection(dbCollectionArg: DbCollection<any>): void;
|
|
||||||
}
|
}
|
||||||
|
100
dist/smartdata.classes.db.js
vendored
100
dist/smartdata.classes.db.js
vendored
@ -1,64 +1,76 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||||
|
return new (P || (P = Promise))(function (resolve, reject) {
|
||||||
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||||
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||||
|
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
||||||
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||||
|
});
|
||||||
|
};
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
const plugins = require("./smartdata.plugins");
|
const plugins = require("./smartdata.plugins");
|
||||||
const lik_1 = require("lik");
|
const lik_1 = require("lik");
|
||||||
class Db {
|
class Db {
|
||||||
constructor(dbUrlArg, dbTypeArg = 'mongodb') {
|
constructor(connectionOptionsArg) {
|
||||||
this.collections = new lik_1.Objectmap();
|
this.dbTablesMap = new lik_1.Objectmap();
|
||||||
this.dbType = dbTypeArg;
|
this.dbName = connectionOptionsArg.db;
|
||||||
this.dbUrl = dbUrlArg;
|
this.connectionOptions = connectionOptionsArg;
|
||||||
|
this.status = "initial";
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* supply additional SSl options needed to connect to certain Rethink DB servers (e.g. compose.io)
|
||||||
|
*/
|
||||||
|
setSsl(certificateStringArg, formatArg) {
|
||||||
|
let certificateString;
|
||||||
|
if ((formatArg = "base64")) {
|
||||||
|
certificateString = plugins.smartstring.base64.decode(certificateStringArg);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
certificateString = certificateStringArg;
|
||||||
|
}
|
||||||
|
this.connectionOptions["ssl"] = {
|
||||||
|
ca: Buffer.from(certificateString)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
// basic connection stuff ----------------------------------------------
|
// basic connection stuff ----------------------------------------------
|
||||||
/**
|
/**
|
||||||
* connects to the database that was specified during instance creation
|
* connects to the database that was specified during instance creation
|
||||||
*/
|
*/
|
||||||
connect() {
|
connect() {
|
||||||
let done = plugins.q.defer();
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
if (this.dbType === 'mongodb') {
|
this.dbConnection = yield plugins.rethinkDb.connect(this.connectionOptions);
|
||||||
plugins.mongodb.MongoClient.connect(this.dbUrl, (err, db) => {
|
this.dbConnection.use(this.dbName);
|
||||||
if (err) {
|
this.status = "connected";
|
||||||
console.log(err);
|
plugins.beautylog.ok(`Connected to database ${this.dbName}`);
|
||||||
}
|
});
|
||||||
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
|
* closes the connection to the databse
|
||||||
*/
|
*/
|
||||||
close() {
|
close() {
|
||||||
let done = plugins.q.defer();
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
if (this.dbType === 'mongodb') {
|
yield this.dbConnection.close();
|
||||||
this.db.close();
|
this.status = "disconnected";
|
||||||
}
|
plugins.beautylog.ok(`disconnected from database ${this.dbName}`);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
;
|
// handle table to class distribution
|
||||||
addCollection(dbCollectionArg) {
|
addTable(dbTableArg) {
|
||||||
this.collections.add(dbCollectionArg);
|
this.dbTablesMap.add(dbTableArg);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Gets a table's name and returns smartdata's DbTable class
|
||||||
|
* @param nameArg
|
||||||
|
* @returns DbTable
|
||||||
|
*/
|
||||||
|
getDbTableByName(nameArg) {
|
||||||
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
|
let resultCollection = this.dbTablesMap.find(dbTableArg => {
|
||||||
|
return dbTableArg.tableName === nameArg;
|
||||||
|
});
|
||||||
|
return resultCollection;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exports.Db = Db;
|
exports.Db = Db;
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEuY2xhc3Nlcy5kYi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsK0NBQThDO0FBQzlDLDZCQUErQjtBQWMvQjtJQU9JLFlBQVksUUFBZ0IsRUFBRSxZQUFxQixTQUFTO1FBRjVELGdCQUFXLEdBQUcsSUFBSSxlQUFTLEVBQXFCLENBQUE7UUFHNUMsSUFBSSxDQUFDLE1BQU0sR0FBRyxTQUFTLENBQUE7UUFDdkIsSUFBSSxDQUFDLEtBQUssR0FBR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ENBQUMsZ0JBQWdCLENBQUM7UUFDMUIsQ0FBQztLQUFBO0NBQ0Y7QUFwRUQsZ0JBb0VDIn0=
|
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
49
dist/smartdata.classes.dbdoc.d.ts
vendored
49
dist/smartdata.classes.dbdoc.d.ts
vendored
@ -1,9 +1,46 @@
|
|||||||
import { DbCollection } from './smartdata.classes.dbcollection';
|
import { Objectmap } from "lik";
|
||||||
export declare type TDocCreation = 'db' | 'data' | 'mixed';
|
import { DbTable } from "./smartdata.classes.dbtable";
|
||||||
|
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> {
|
export declare class DbDoc<T> {
|
||||||
collection: DbCollection<T>;
|
/**
|
||||||
creationType: TDocCreation;
|
* the collection object an Doc belongs to
|
||||||
|
*/
|
||||||
|
collection: DbTable<T>;
|
||||||
|
/**
|
||||||
|
* how the Doc in memory was created, may prove useful later.
|
||||||
|
*/
|
||||||
|
creationStatus: TDocCreation;
|
||||||
|
/**
|
||||||
|
* an array of saveable properties of a doc
|
||||||
|
*/
|
||||||
|
saveableProperties: string[];
|
||||||
|
/**
|
||||||
|
* name
|
||||||
|
*/
|
||||||
|
name: string;
|
||||||
|
/**
|
||||||
|
* primary id in the database
|
||||||
|
*/
|
||||||
|
dbId: string;
|
||||||
|
/**
|
||||||
|
* class constructor
|
||||||
|
*/
|
||||||
constructor();
|
constructor();
|
||||||
save(): void;
|
static getInstances<T>(filterArg: any): Promise<T[]>;
|
||||||
saveDeep(): void;
|
static getInstance<T>(filterArg: any): Promise<T>;
|
||||||
|
/**
|
||||||
|
* saves this instance but not any connected items
|
||||||
|
* may lead to data inconsistencies, but is faster
|
||||||
|
*/
|
||||||
|
save(): Promise<void>;
|
||||||
|
/**
|
||||||
|
* also store any referenced objects to DB
|
||||||
|
* better for data consistency
|
||||||
|
*/
|
||||||
|
saveDeep(savedMapArg?: Objectmap<DbDoc<any>>): void;
|
||||||
|
createSavableObject(): any;
|
||||||
}
|
}
|
||||||
|
103
dist/smartdata.classes.dbdoc.js
vendored
103
dist/smartdata.classes.dbdoc.js
vendored
@ -1,12 +1,109 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||||
|
return new (P || (P = Promise))(function (resolve, reject) {
|
||||||
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||||
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||||
|
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
||||||
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||||
|
});
|
||||||
|
};
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
const lik_1 = require("lik");
|
||||||
|
/**
|
||||||
|
* 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 DbDoc {
|
||||||
|
/**
|
||||||
|
* class constructor
|
||||||
|
*/
|
||||||
constructor() {
|
constructor() {
|
||||||
this.collection = this.constructor['dbCollection'];
|
/**
|
||||||
|
* how the Doc in memory was created, may prove useful later.
|
||||||
|
*/
|
||||||
|
this.creationStatus = "new";
|
||||||
|
this.name = this.constructor["name"];
|
||||||
|
this.collection = this.constructor["dbTable"];
|
||||||
}
|
}
|
||||||
|
static getInstances(filterArg) {
|
||||||
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
|
let self = this; // fool typesystem
|
||||||
|
let referenceTable = self.dbTable;
|
||||||
|
const foundDocs = yield referenceTable.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 getInstance(filterArg) {
|
||||||
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
|
let result = yield this.getInstances(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
|
||||||
|
*/
|
||||||
save() {
|
save() {
|
||||||
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
|
let self = this;
|
||||||
|
switch (this.creationStatus) {
|
||||||
|
case "db":
|
||||||
|
yield this.collection.update(self);
|
||||||
|
break;
|
||||||
|
case "new":
|
||||||
|
let writeResult = yield this.collection.insert(self);
|
||||||
|
this.creationStatus = "db";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.error("neither new nor in db?");
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
saveDeep() {
|
/**
|
||||||
|
* also store any referenced objects to DB
|
||||||
|
* better for data consistency
|
||||||
|
*/
|
||||||
|
saveDeep(savedMapArg = null) {
|
||||||
|
if (!savedMapArg) {
|
||||||
|
savedMapArg = new lik_1.Objectmap();
|
||||||
|
}
|
||||||
|
savedMapArg.add(this);
|
||||||
|
this.save();
|
||||||
|
for (let propertyKey in this) {
|
||||||
|
let property = this[propertyKey];
|
||||||
|
if (property instanceof DbDoc && !savedMapArg.checkForObject(property)) {
|
||||||
|
property.saveDeep(savedMapArg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
createSavableObject() {
|
||||||
|
let saveableObject = {}; // is not exposed to outside, so any is ok here
|
||||||
|
for (let propertyNameString of this.saveableProperties) {
|
||||||
|
saveableObject[propertyNameString] = this[propertyNameString];
|
||||||
|
}
|
||||||
|
return saveableObject;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exports.DbDoc = DbDoc;
|
exports.DbDoc = DbDoc;
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGJkb2MuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEuY2xhc3Nlcy5kYmRvYy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBUUE7SUFHSTtRQUNJLElBQUksQ0FBQyxVQUFVLEdBQUcsSUFBSSxDQUFDLFdBQVcsQ0FBQyxjQUFjLENBQUMsQ0FBQTtJQUN0RCxDQUFDO0lBQ0QsSUFBSTtJQUVKLENBQUM7SUFDRCxRQUFRO0lBRVIsQ0FBQztDQUNKO0FBWkQsc0JBWUMifQ==
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGJkb2MuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEuY2xhc3Nlcy5kYmRvYy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBRUEsNkJBQWdDO0FBT2hDOztHQUVHO0FBQ0g7SUFDRSxNQUFNLENBQUMsQ0FBQyxNQUFrQixFQUFFLEdBQVcsRUFBRSxFQUFFO1FBQ3pDLE9BQU8sQ0FBQyxHQUFHLENBQUMsWUFBWSxDQUFDLENBQUM7UUFDMUIsRUFBRSxDQUFDLENBQUMsQ0FBQyxNQUFNLENBQUMsa0JBQWtCLENBQUMsQ0FBQyxDQUFDO1lBQy9CLE1BQU0sQ0FBQyxrQkFBa0IsR0FBRyxFQUFFLENBQUM7UUFDakMsQ0F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FDLENBQUMsV0FBVyxDQUFDLENBQUMsQ0FBQztZQUNqQixXQUFXLEdBQUcsSUFBSSxlQUFTLEVBQWMsQ0FBQztRQUM1QyxDQUFDO1FBQ0QsV0FBVyxDQUFDLEdBQUcsQ0FBQyxJQUFJLENBQUMsQ0FBQztRQUN0QixJQUFJLENBQUMsSUFBSSxFQUFFLENBQUM7UUFDWixHQUFHLENBQUMsQ0FBQyxJQUFJLFdBQVcsSUFBSSxJQUFJLENBQUMsQ0FBQyxDQUFDO1lBQzdCLElBQUksUUFBUSxHQUFRLElBQUksQ0FBQyxXQUFXLENBQUMsQ0FBQztZQUN0QyxFQUFFLENBQUMsQ0FBQyxRQUFRLFlBQVksS0FBSyxJQUFJLENBQUMsV0FBVyxDQUFDLGNBQWMsQ0FBQyxRQUFRLENBQUMsQ0FBQyxDQUFDLENBQUM7Z0JBQ3ZFLFFBQVEsQ0FBQyxRQUFRLENBQUMsV0FBVyxDQUFDLENBQUM7WUFDakMsQ0FBQztRQUNILENBQUM7SUFDSCxDQUFDO0lBRUQsbUJBQW1CO1FBQ2pCLElBQUksY0FBYyxHQUFRLEVBQUUsQ0FBQyxDQUFDLCtDQUErQztRQUM3RSxHQUFHLENBQUMsQ0FBQyxJQUFJLGtCQUFrQixJQUFJLElBQUksQ0FBQyxrQkFBa0IsQ0FBQyxDQUFDLENBQUM7WUFDdkQsY0FBYyxDQUFDLGtCQUFrQixDQUFDLEdBQUcsSUFBSSxDQUFDLGtCQUFrQixDQUFDLENBQUM7UUFDaEUsQ0FBQztRQUNELE1BQU0sQ0FBQyxjQUFjLENBQUM7SUFDeEIsQ0FBQztDQUNGO0FBdEdELHNCQXNHQyJ9
|
51
dist/smartdata.classes.dbtable.d.ts
vendored
Normal file
51
dist/smartdata.classes.dbtable.d.ts
vendored
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
import * as plugins from "./smartdata.plugins";
|
||||||
|
import { Db } from "./smartdata.classes.db";
|
||||||
|
import { DbDoc } from "./smartdata.classes.dbdoc";
|
||||||
|
import { WriteResult } from "rethinkdb";
|
||||||
|
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 declare function Table(db: Db): (constructor: any) => void;
|
||||||
|
export declare class DbTable<T> {
|
||||||
|
/**
|
||||||
|
* the collection that is used
|
||||||
|
*/
|
||||||
|
table: plugins.rethinkDb.Table;
|
||||||
|
objectValidation: IDocValidationFunc<T>;
|
||||||
|
tableName: string;
|
||||||
|
db: Db;
|
||||||
|
constructor(collectedClassArg: T & DbDoc<T>, dbArg: Db);
|
||||||
|
init(): Promise<void>;
|
||||||
|
/**
|
||||||
|
* adds a validation function that all newly inserted and updated objects have to pass
|
||||||
|
*/
|
||||||
|
addDocValidation(funcArg: IDocValidationFunc<T>): void;
|
||||||
|
/**
|
||||||
|
* finds an object in the DbCollection
|
||||||
|
*/
|
||||||
|
find(filterObject: any): Promise<any>;
|
||||||
|
/**
|
||||||
|
* create an object in the database
|
||||||
|
*/
|
||||||
|
insert(dbDocArg: T & DbDoc<T>): Promise<WriteResult>;
|
||||||
|
/**
|
||||||
|
* inserts object into the DbCollection
|
||||||
|
*/
|
||||||
|
update(dbDocArg: T & DbDoc<T>): Promise<WriteResult>;
|
||||||
|
/**
|
||||||
|
* checks a Doc for constraints
|
||||||
|
* if this.objectValidation is not set it passes.
|
||||||
|
*/
|
||||||
|
private checkDoc(docArg);
|
||||||
|
extractKey(writeResult: WriteResult): void;
|
||||||
|
}
|
116
dist/smartdata.classes.dbtable.js
vendored
Normal file
116
dist/smartdata.classes.dbtable.js
vendored
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
"use strict";
|
||||||
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||||
|
return new (P || (P = Promise))(function (resolve, reject) {
|
||||||
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||||
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||||
|
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
||||||
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||||
|
});
|
||||||
|
};
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
const plugins = require("./smartdata.plugins");
|
||||||
|
/**
|
||||||
|
* This is a decorator that will tell the decorated class what dbTable to use
|
||||||
|
* @param db
|
||||||
|
*/
|
||||||
|
function Table(db) {
|
||||||
|
return function (constructor) {
|
||||||
|
constructor["dbTable"] = new DbTable(constructor, db);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
exports.Table = Table;
|
||||||
|
class DbTable {
|
||||||
|
constructor(collectedClassArg, dbArg) {
|
||||||
|
this.objectValidation = null;
|
||||||
|
// tell the collection where it belongs
|
||||||
|
this.tableName = collectedClassArg.name;
|
||||||
|
this.db = dbArg;
|
||||||
|
// tell the db class about it (important since Db uses different systems under the hood)
|
||||||
|
this.db.addTable(this);
|
||||||
|
}
|
||||||
|
init() {
|
||||||
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
|
if (!this.table) {
|
||||||
|
// connect this instance to a RethinkDB table
|
||||||
|
const availableTables = yield plugins.rethinkDb
|
||||||
|
.db(this.db.dbName)
|
||||||
|
.tableList()
|
||||||
|
.run(this.db.dbConnection);
|
||||||
|
if (availableTables.indexOf(this.tableName)) {
|
||||||
|
yield plugins.rethinkDb
|
||||||
|
.db(this.db.dbName)
|
||||||
|
.tableCreate(this.tableName)
|
||||||
|
.run(this.db.dbConnection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.table = plugins.rethinkDb.table(this.tableName);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* adds a validation function that all newly inserted and updated objects have to pass
|
||||||
|
*/
|
||||||
|
addDocValidation(funcArg) {
|
||||||
|
this.objectValidation = funcArg;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* finds an object in the DbCollection
|
||||||
|
*/
|
||||||
|
find(filterObject) {
|
||||||
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
|
yield this.init();
|
||||||
|
let cursor = yield plugins.rethinkDb
|
||||||
|
.table(this.tableName)
|
||||||
|
.filter(filterObject)
|
||||||
|
.run(this.db.dbConnection);
|
||||||
|
return yield cursor.toArray();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* create an object in the database
|
||||||
|
*/
|
||||||
|
insert(dbDocArg) {
|
||||||
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
|
yield this.init();
|
||||||
|
yield this.checkDoc(dbDocArg);
|
||||||
|
return yield plugins.rethinkDb
|
||||||
|
.table(this.tableName)
|
||||||
|
.insert(dbDocArg.createSavableObject())
|
||||||
|
.run(this.db.dbConnection);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* inserts object into the DbCollection
|
||||||
|
*/
|
||||||
|
update(dbDocArg) {
|
||||||
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
|
yield this.init();
|
||||||
|
yield this.checkDoc(dbDocArg);
|
||||||
|
console.log(this.tableName, dbDocArg.createSavableObject());
|
||||||
|
return yield plugins.rethinkDb
|
||||||
|
.table(this.tableName)
|
||||||
|
.update(dbDocArg.createSavableObject())
|
||||||
|
.run(this.db.dbConnection);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* checks a Doc for constraints
|
||||||
|
* if this.objectValidation is not set it passes.
|
||||||
|
*/
|
||||||
|
checkDoc(docArg) {
|
||||||
|
let done = plugins.smartq.defer();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
extractKey(writeResult) { }
|
||||||
|
}
|
||||||
|
exports.DbTable = DbTable;
|
||||||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGJ0YWJsZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0ZGF0YS5jbGFzc2VzLmRidGFibGUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7OztBQUFBLCtDQUErQztBQWtCL0M7OztHQUdHO0FBQ0gsZUFBc0IsRUFBTTtJQUMxQixNQUFNLENBQUMsVUFBUyxXQUFXO1FBQ3pCLFdBQVcsQ0FBQyxTQUFTLENBQUMsR0FBRyxJQUFJLE9BQU8sQ0FBQyxXQUFXLEVBQUUsRUFBRSxDQUFDLENBQUM7SUFDeEQsQ0FBQyxDQUFDO0FBQ0osQ0FBQztBQUpELHNCQUlDO0FBRUQ7SUFTRSxZQUFZLGlCQUErQixFQUFFLEtBQVM7UUFKdEQscUJBQWdCLEdBQTBCLElBQUksQ0FBQztRQUs3Qyx1Q0FBdUM7UUFDdkMsSUFBSSxDQUFDLFNBQVMsR0FBRyxpQkFBaUIsQ0FBQyxJQUFJLENBQUM7UUFDeEMsSUFBSSxDQUFDLEVBQUUsR0FBRyxLQUFLLENBQUM7UUFFaEIsd0ZBQXdGO1FBQ3hGLElBQUksQ0FBQyxFQUFFLENBQUMsUUFBUSxDQUFDLElBQUksQ0FBQyxDQUFDO0lBQ3pCLENBQUM7SUFFSyxJQUFJOztZQUNSLEVBQUUsQ0FBQyxDQUFDLENBQUMsSUFBSSxDQUFDLEtBQUssQ0FBQyxDQUFDLENBQUM7Z0JB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BQztRQUMvQixDQUFDO0tBQUE7SUFFRDs7O09BR0c7SUFDSyxRQUFRLENBQUMsTUFBUztRQUN4QixJQUFJLElBQUksR0FBRyxPQUFPLENBQUMsTUFBTSxDQUFDLEtBQUssRUFBUSxDQUFDO1FBQ3hDLElBQUksZ0JBQWdCLEdBQUcsSUFBSSxDQUFDO1FBQzVCLEVBQUUsQ0FBQyxDQUFDLElBQUksQ0FBQyxnQkFBZ0IsQ0FBQyxDQUFDLENBQUM7WUFDMUIsZ0JBQWdCLEdBQUcsSUFBSSxDQUFDLGdCQUFnQixDQUFDLE1BQU0sQ0FBQyxDQUFDO1FBQ25ELENBQUM7UUFDRCxFQUFFLENBQUMsQ0FBQyxnQkFBZ0IsQ0FBQyxDQUFDLENBQUM7WUFDckIsSUFBSSxDQUFDLE9BQU8sRUFBRSxDQUFDO1FBQ2pCLENBQUM7UUFBQyxJQUFJLENBQUMsQ0FBQztZQUNOLElBQUksQ0FBQyxNQUFNLENBQUMsbUNBQW1DLENBQUMsQ0FBQztRQUNuRCxDQUFDO1FBQ0QsTUFBTSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUM7SUFDdEIsQ0FBQztJQUVELFVBQVUsQ0FBQyxXQUF3QixJQUFHLENBQUM7Q0FDeEM7QUFsR0QsMEJBa0dDIn0=
|
15
dist/smartdata.plugins.d.ts
vendored
15
dist/smartdata.plugins.d.ts
vendored
@ -1,8 +1,7 @@
|
|||||||
import 'typings-global';
|
import * as assert from "assert";
|
||||||
import * as assert from 'assert';
|
import * as beautylog from "beautylog";
|
||||||
import * as beautylog from 'beautylog';
|
import * as lodash from "lodash";
|
||||||
import * as lodash from 'lodash';
|
import * as rethinkDb from "rethinkdb";
|
||||||
import * as mongodb from 'mongodb';
|
import * as smartq from "smartq";
|
||||||
import * as q from 'q';
|
import * as smartstring from "smartstring";
|
||||||
declare let nedb: any;
|
export { assert, beautylog, lodash, smartq, rethinkDb, smartstring };
|
||||||
export { assert, beautylog, lodash, mongodb, q, nedb };
|
|
||||||
|
16
dist/smartdata.plugins.js
vendored
16
dist/smartdata.plugins.js
vendored
@ -1,15 +1,15 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
require("typings-global");
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
const assert = require("assert");
|
const assert = require("assert");
|
||||||
exports.assert = assert;
|
exports.assert = assert;
|
||||||
const beautylog = require("beautylog");
|
const beautylog = require("beautylog");
|
||||||
exports.beautylog = beautylog;
|
exports.beautylog = beautylog;
|
||||||
const lodash = require("lodash");
|
const lodash = require("lodash");
|
||||||
exports.lodash = lodash;
|
exports.lodash = lodash;
|
||||||
const mongodb = require("mongodb");
|
const rethinkDb = require("rethinkdb");
|
||||||
exports.mongodb = mongodb;
|
exports.rethinkDb = rethinkDb;
|
||||||
const q = require("q");
|
const smartq = require("smartq");
|
||||||
exports.q = q;
|
exports.smartq = smartq;
|
||||||
let nedb = require('nedb');
|
const smartstring = require("smartstring");
|
||||||
exports.nedb = nedb;
|
exports.smartstring = smartstring;
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLnBsdWdpbnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEucGx1Z2lucy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsMEJBQXVCO0FBQ3ZCLGlDQUFnQztBQVE1Qix3QkFBTTtBQVBWLHVDQUFzQztBQVFsQyw4QkFBUztBQVBiLGlDQUFnQztBQVE1Qix3QkFBTTtBQVBWLG1DQUFrQztBQVE5QiwwQkFBTztBQVBYLHVCQUFzQjtBQVFsQixjQUFDO0FBUEwsSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLE1BQU0sQ0FBQyxDQUFBO0FBUXRCLG9CQUFJIn0=
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLnBsdWdpbnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEucGx1Z2lucy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLGlDQUFpQztBQU94Qix3QkFBTTtBQU5mLHVDQUF1QztBQU10Qiw4QkFBUztBQUwxQixpQ0FBaUM7QUFLTCx3QkFBTTtBQUpsQyx1Q0FBdUM7QUFJSyw4QkFBUztBQUhyRCxpQ0FBaUM7QUFHRyx3QkFBTTtBQUYxQywyQ0FBMkM7QUFFWSxrQ0FBVyJ9
|
@ -1,19 +1,29 @@
|
|||||||
# smartdata
|
# smartdata
|
||||||
|
|
||||||
smartdata is a ODM that adheres to TypeScript practices and uses classes to organize data.
|
do more with data
|
||||||
It uses MongoDB as persistent storage.
|
|
||||||
|
|
||||||
## Intention
|
## Availabililty
|
||||||
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
|
[](https://www.npmjs.com/package/smartdata)
|
||||||
|
[](https://GitLab.com/pushrocks/smartdata)
|
||||||
|
[](https://github.com/pushrocks/smartdata)
|
||||||
|
[](https://pushrocks.gitlab.io/smartdata/)
|
||||||
|
|
||||||
MongoDB term | smartdata class
|
## Status for master
|
||||||
--- | ---
|
|
||||||
Database | smartdata.DbConnection
|
|
||||||
Collection | smartdata.DbCollection
|
|
||||||
Document | smartdata.DbDoc
|
|
||||||
|
|
||||||
[](https://push.rocks)
|
[](https://GitLab.com/pushrocks/smartdata/commits/master)
|
||||||
|
[](https://GitLab.com/pushrocks/smartdata/commits/master)
|
||||||
|
[](https://www.npmjs.com/package/smartdata)
|
||||||
|
[](https://david-dm.org/pushrocks/smartdata)
|
||||||
|
[](https://www.bithound.io/github/pushrocks/smartdata/master/dependencies/npm)
|
||||||
|
[](https://www.bithound.io/github/pushrocks/smartdata)
|
||||||
|
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||||
|
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||||
|
[](http://standardjs.com/)
|
||||||
|
|
||||||
|
For further information read the linked docs at the top of this README.
|
||||||
|
|
||||||
|
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||||
|
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
||||||
|
|
||||||
|
[](https://push.rocks)
|
||||||
|
18
npmextra.json
Normal file
18
npmextra.json
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"npmdocker": {
|
||||||
|
"baseImage": "hosttoday/ht-docker-node:mongo",
|
||||||
|
"command": "npmci test stable",
|
||||||
|
"dockerSock": false
|
||||||
|
},
|
||||||
|
"npmci": {
|
||||||
|
"npmGlobalTools": [
|
||||||
|
"npmts"
|
||||||
|
],
|
||||||
|
"npmAccessLevel": "public"
|
||||||
|
},
|
||||||
|
"npmts": {
|
||||||
|
"testConfig": {
|
||||||
|
"parallel": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1011
package-lock.json
generated
Normal file
1011
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
45
package.json
45
package.json
@ -1,12 +1,14 @@
|
|||||||
{
|
{
|
||||||
"name": "smartdata",
|
"name": "@pushrocks/smartdata",
|
||||||
"version": "1.0.17",
|
"version": "3.0.0",
|
||||||
|
"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": "(npmts)",
|
||||||
"prepareMongo": "(rm -rf ./test/data && mkdir ./test/data/)"
|
"testLocal": "(npmdocker)",
|
||||||
|
"build": "echo \"Not needed for now\""
|
||||||
},
|
},
|
||||||
"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/smartlog": "^1.0.6",
|
||||||
"@types/mongodb": "^2.1.34",
|
"@pushrocks/smartpromise": "^2.0.5",
|
||||||
"@types/nedb": "0.0.31",
|
"@types/lodash": "^4.14.110",
|
||||||
"@types/q": "0.0.32",
|
"@types/mongodb": "^3.1.1",
|
||||||
"beautylog": "^6.0.0",
|
"lik": "^2.0.5",
|
||||||
"lik": "^1.0.24",
|
"lodash": "^4.17.10",
|
||||||
"lodash": "^4.17.2",
|
"mongodb": "^3.1.1",
|
||||||
"mongodb": "^2.2.11",
|
|
||||||
"nedb": "^1.8.0",
|
|
||||||
"q": "^1.4.1",
|
|
||||||
"runtime-type-checks": "0.0.4",
|
"runtime-type-checks": "0.0.4",
|
||||||
"typings-global": "^1.0.14"
|
"smartstring": "^2.0.28"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/shelljs": "^0.3.32",
|
"@types/node": "^10.5.2",
|
||||||
"@types/should": "^8.1.30",
|
"@types/shelljs": "^0.8.0",
|
||||||
"shelljs": "^0.7.5",
|
"qenv": "^1.1.7",
|
||||||
"should": "^11.1.1",
|
"shelljs": "^0.8.2",
|
||||||
"smartstring": "^2.0.22",
|
"tapbundle": "^2.0.2"
|
||||||
"typings-test": "^1.0.3"
|
},
|
||||||
|
"config": {
|
||||||
|
"commitizen": {
|
||||||
|
"path": "./node_modules/cz-conventional-changelog"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
1
test/test.d.ts
vendored
1
test/test.d.ts
vendored
@ -1 +0,0 @@
|
|||||||
import 'typings-test';
|
|
124
test/test.js
124
test/test.js
File diff suppressed because one or more lines are too long
173
test/test.ts
173
test/test.ts
@ -1,130 +1,63 @@
|
|||||||
import 'typings-test'
|
import { tap, expect } from 'tapbundle';
|
||||||
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
|
import { Qenv } from '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';
|
||||||
|
|
||||||
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
|
mongoPass: process.env.MONGO_PASS
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should establish a connection to the rethink Db cluster', async () => {
|
||||||
|
await testDb.connect();
|
||||||
|
});
|
||||||
|
|
||||||
|
// =======================================
|
||||||
|
// The actual tests
|
||||||
|
// =======================================
|
||||||
|
|
||||||
|
// ------
|
||||||
|
// Collections
|
||||||
|
// ------
|
||||||
|
|
||||||
|
@smartdata.Table(testDb)
|
||||||
|
class Car extends smartdata.smartDataDbDoc<Car> {
|
||||||
|
@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 myCar = 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(myCar[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> {
|
|
||||||
color: string
|
|
||||||
constructor(optionsArg: {
|
|
||||||
color: string,
|
|
||||||
property2: number
|
|
||||||
}) {
|
|
||||||
super()
|
|
||||||
this.color = optionsArg.color
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let testCarInstance = new TestCar({
|
|
||||||
color: 'red',
|
|
||||||
property2: 2
|
|
||||||
})
|
|
||||||
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 () {
|
|
||||||
|
|
||||||
})
|
tap.start();
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
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,3 @@
|
|||||||
import * as plugins from './smartdata.plugins'
|
export * from './smartdata.classes.db';
|
||||||
|
export * from './smartdata.classes.dbtable';
|
||||||
export * from './smartdata.classes.db'
|
export * from './smartdata.classes.dbdoc';
|
||||||
export * from './smartdata.classes.dbcollection'
|
|
||||||
export * from './smartdata.classes.dbdoc'
|
|
||||||
|
@ -1,82 +1,94 @@
|
|||||||
import * as plugins from './smartdata.plugins'
|
import * as plugins from './smartdata.plugins';
|
||||||
import { Objectmap } from 'lik'
|
import { Objectmap } from 'lik';
|
||||||
|
|
||||||
import { DbCollection } from './smartdata.classes.dbcollection'
|
import { SmartdataCollection } from './smartdata.classes.dbtable';
|
||||||
|
|
||||||
/**
|
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;
|
||||||
|
|
||||||
// basic connection stuff ----------------------------------------------
|
/**
|
||||||
|
* an optional password that will be replace <PASSWORD> in the connection string
|
||||||
/**
|
*/
|
||||||
* connects to the database that was specified during instance creation
|
mongoDbPass?: string;
|
||||||
*/
|
}
|
||||||
connect(): plugins.q.Promise<any> {
|
|
||||||
let done = plugins.q.defer()
|
export class SmartdataDb {
|
||||||
if (this.dbType === 'mongodb') {
|
smartdataOptions: ISmartdataOptions;
|
||||||
plugins.mongodb.MongoClient.connect(this.dbUrl, (err, db) => {
|
mongoDbClient: plugins.mongodb.MongoClient;
|
||||||
if (err) { console.log(err) }
|
mongoDb: plugins.mongodb.Db;
|
||||||
plugins.assert.equal(null, err)
|
status: TConnectionStatus;
|
||||||
this.db = db
|
smartdataCollectionMap = new Objectmap<SmartdataCollection<any>>();
|
||||||
plugins.beautylog.success(`connected to database at ${this.dbUrl}`)
|
|
||||||
done.resolve(this.db)
|
constructor(smartdataOptions: ISmartdataOptions) {
|
||||||
})
|
this.smartdataOptions = smartdataOptions;
|
||||||
} else if (this.dbType === 'nedb') {
|
this.status = 'initial';
|
||||||
this.db = null
|
}
|
||||||
}
|
|
||||||
return done.promise
|
// basic connection stuff ----------------------------------------------
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* connects to the database that was specified during instance creation
|
||||||
* closes the connection to the databse
|
*/
|
||||||
*/
|
async connect(): Promise<any> {
|
||||||
close(): plugins.q.Promise<any> {
|
let finalConnectionUrl = this.smartdataOptions.mongoDbUrl;
|
||||||
let done = plugins.q.defer()
|
if (this.smartdataOptions.mongoDbPass) {
|
||||||
if (this.dbType === 'mongodb') {
|
finalConnectionUrl = mongoHelpers.addPassword(
|
||||||
this.db.close()
|
this.smartdataOptions.mongoDbName,
|
||||||
}
|
this.smartdataOptions.mongoDbPass
|
||||||
plugins.beautylog.ok(`disconnected to database at ${this.dbUrl}`)
|
);
|
||||||
done.resolve()
|
}
|
||||||
return done.promise
|
this.mongoDbClient = await plugins.mongodb.MongoClient.connect(
|
||||||
}
|
finalConnectionUrl,
|
||||||
|
{}
|
||||||
// advanced communication with the database --------------------------------
|
);
|
||||||
|
this.mongoDb = this.mongoDbClient.db(this.smartdataOptions.mongoDbName);
|
||||||
/**
|
this.status = 'connected';
|
||||||
* gets a collection by name: string
|
plugins.smartlog
|
||||||
*/
|
.getDefaultLogger()
|
||||||
getCollectionByName<T>(nameArg: string): plugins.q.Promise<DbCollection<T>> {
|
.info(`Connected to database ${this.smartdataOptions.mongoDbName}`);
|
||||||
let done = plugins.q.defer<DbCollection<any>>()
|
}
|
||||||
let resultCollection = this.collections.find((dbCollectionArg) => {
|
|
||||||
return dbCollectionArg.name === nameArg
|
/**
|
||||||
})
|
* closes the connection to the databse
|
||||||
if (resultCollection !== null) {
|
*/
|
||||||
done.resolve(resultCollection)
|
async close(): Promise<any> {
|
||||||
}
|
await this.mongoDbClient.close();
|
||||||
return done.promise
|
this.status = 'disconnected';
|
||||||
};
|
plugins.smartlog
|
||||||
|
.getDefaultLogger()
|
||||||
addCollection(dbCollectionArg: DbCollection<any>) {
|
.info(`disconnected from database ${this.smartdataOptions.mongoDbName}`);
|
||||||
this.collections.add(dbCollectionArg)
|
}
|
||||||
}
|
|
||||||
|
// handle table to class distribution
|
||||||
|
|
||||||
|
addTable(SmartdataCollectionArg: SmartdataCollection<any>) {
|
||||||
|
this.smartdataCollectionMap.add(SmartdataCollectionArg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a collection's name and returns a SmartdataCollection instance
|
||||||
|
* @param nameArg
|
||||||
|
* @returns DbTable
|
||||||
|
*/
|
||||||
|
async getSmartdataCollectionByName<T>(nameArg: string): Promise<SmartdataCollection<T>> {
|
||||||
|
let resultCollection = this.smartdataCollectionMap.find(dbTableArg => {
|
||||||
|
return dbTableArg.collectionName === nameArg;
|
||||||
|
});
|
||||||
|
return resultCollection;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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,21 +1,125 @@
|
|||||||
import * as plugins from './smartdata.plugins'
|
import * as plugins from './smartdata.plugins';
|
||||||
|
|
||||||
import { Db } from './smartdata.classes.db'
|
import { Objectmap } from 'lik';
|
||||||
import { DbCollection } from './smartdata.classes.dbcollection'
|
|
||||||
|
|
||||||
export type TDocCreation = 'db' | 'data' | 'mixed'
|
import { SmartdataDb } from './smartdata.classes.db';
|
||||||
|
import { SmartdataCollection } from './smartdata.classes.dbtable';
|
||||||
|
|
||||||
|
export type TDocCreation = 'db' | 'new' | 'mixed';
|
||||||
|
|
||||||
export class DbDoc<T> {
|
/**
|
||||||
collection: DbCollection<T>
|
* saveable - saveable decorator to be used on class properties
|
||||||
creationType: TDocCreation
|
*/
|
||||||
constructor() {
|
export function svDb() {
|
||||||
this.collection = this.constructor['dbCollection']
|
return (target: smartDataDbDoc<any>, key: string) => {
|
||||||
}
|
console.log('called sva');
|
||||||
save() {
|
if (!target.saveableProperties) {
|
||||||
|
target.saveableProperties = [];
|
||||||
}
|
|
||||||
saveDeep() {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
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';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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['dbTable'];
|
||||||
|
}
|
||||||
|
|
||||||
|
static async getInstances<T>(filterArg): Promise<T[]> {
|
||||||
|
let self: any = this; // fool typesystem
|
||||||
|
let referenceTable: SmartdataCollection<T> = self.dbTable;
|
||||||
|
const foundDocs = await referenceTable.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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
113
ts/smartdata.classes.dbtable.ts
Normal file
113
ts/smartdata.classes.dbtable.ts
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
import * as plugins from './smartdata.plugins';
|
||||||
|
import { SmartdataDb } from './smartdata.classes.db';
|
||||||
|
import { smartDataDbDoc } from './smartdata.classes.dbdoc';
|
||||||
|
|
||||||
|
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 Table(db: SmartdataDb) {
|
||||||
|
return function(constructor) {
|
||||||
|
constructor['mongoDbCollection'] = 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;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* create an object in the database
|
||||||
|
*/
|
||||||
|
async insert(dbDocArg: T & smartDataDbDoc<T>): Promise<any> {
|
||||||
|
await this.init();
|
||||||
|
await this.checkDoc(dbDocArg);
|
||||||
|
const saveableObject = await dbDocArg.createSavableObject();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
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 'smartq';
|
||||||
import * as q from 'q'
|
import * as smartstring from 'smartstring';
|
||||||
let nedb = require('nedb')
|
|
||||||
|
|
||||||
export {
|
export { assert, smartlog, lodash, smartq, mongodb, smartstring };
|
||||||
assert,
|
|
||||||
beautylog,
|
|
||||||
lodash,
|
|
||||||
mongodb,
|
|
||||||
q,
|
|
||||||
nedb
|
|
||||||
}
|
|
||||||
|
8
tsconfig.json
Normal file
8
tsconfig.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"lib": [
|
||||||
|
"es2015"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": "tslint-config-standard"
|
|
||||||
}
|
|
Reference in New Issue
Block a user