Compare commits

..

54 Commits

Author SHA1 Message Date
f763de2403 3.1.1 2018-07-10 21:33:49 +02:00
cba1d031b6 fix(ci): fix tools install 2018-07-10 21:33:49 +02:00
d31e6a5931 3.1.0 2018-07-10 21:27:17 +02:00
46514a2743 feat(SmartdataDoc): add custom unique indexes 2018-07-10 21:27:16 +02:00
1cf02157c4 3.0.5 2018-07-10 00:19:23 +02:00
72efa14da8 fix(test): update npmts 2018-07-10 00:19:22 +02:00
a73c2084c0 3.0.4 2018-07-10 00:17:17 +02:00
5ed557a21a fix(test): test now throws correctly 2018-07-10 00:17:16 +02:00
2b6c30ff55 3.0.3 2018-07-10 00:02:39 +02:00
12ef599333 3.0.2 2018-07-10 00:02:10 +02:00
191ea5d3c6 update to mongodb 2018-07-10 00:02:04 +02:00
bdf33ed1ca 3.0.1 2018-07-08 23:49:05 +02:00
1c76905dcd fix(structure): clean 2018-07-08 23:49:05 +02:00
4d88cea69d 3.0.0 2018-07-08 23:48:15 +02:00
8b4f7169ff BREAKING CHANGE(scope/db driver): switched to pushrocks scope and from rethinkdb to mongodb as db 2018-07-08 23:48:14 +02:00
2a1c45608f 2.0.7 2018-01-16 00:40:12 +01:00
85a6444263 feat(IConnectionOptions): export ConnectionOptions 2018-01-16 00:40:08 +01:00
766ae1d1ff 2.0.6 2018-01-14 18:07:35 +01:00
7630882312 docs(README): update 2018-01-14 18:07:29 +01:00
4aec47f207 2.0.5 2018-01-14 18:04:00 +01:00
c2e3a1ae6e docs(README): update 2018-01-14 18:03:57 +01:00
3d8f8646b1 2.0.4 2018-01-14 17:58:35 +01:00
aca9817c56 docs(README): update 2018-01-14 17:57:57 +01:00
7e341f2b50 2.0.3 2018-01-14 17:32:07 +01:00
d2ca108ef8 feat(core): now retrieves classes properly 2018-01-14 17:32:04 +01:00
e2d12f8c9c 2.0.2 2018-01-12 01:36:12 +01:00
cfcd9ab386 fix(CI): npmts now installing correctly during CI 2018-01-12 01:36:08 +01:00
3048035374 2.0.1 2018-01-12 01:34:00 +01:00
6c70cf05c4 update ci 2018-01-12 01:33:54 +01:00
04b0910883 2.0.0 2018-01-12 01:23:02 +01:00
3671fe4df4 feat(add RethinkDB as main driver and revert to docs in README): 2018-01-12 01:22:58 +01:00
bd30da1c4a fix(core): remove old code for outdated objectstorage 2018-01-07 18:15:14 +01:00
05938bf2af feat(ci): add commitizen for more consistent commit messages 2018-01-07 18:10:16 +01:00
cddb0caf1f feat(ci): add commitizen for more consistent commit messages 2018-01-07 18:08:42 +01:00
1af51dd989 update rethink integration 2018-01-07 17:58:30 +01:00
8101e49026 update deps and docs 2018-01-07 17:43:02 +01:00
2d97ab9dc5 prepare v2 of smartdata 2018-01-07 17:26:34 +01:00
3ba2bc8446 add rethink 2018-01-07 14:45:43 +01:00
5735ab8577 1.0.28 2017-11-16 14:35:30 +01:00
def671cbe4 update 2017-11-16 14:23:06 +01:00
14042151ba update tests 2017-09-13 13:47:38 +02:00
c74873d02c 1.0.27 2017-07-16 00:11:06 +02:00
800cdd655a fix rxjs dependency 2017-07-16 00:11:03 +02:00
68c0ceeb14 update 2017-07-16 00:09:54 +02:00
154dc5c1b3 Merge branch '3-plugins-are-not-exported' 2017-06-24 09:57:29 +02:00
d5c027caf0 fix import 2017-06-24 09:57:11 +02:00
30776a7da0 1.0.26 2017-06-23 16:42:08 +02:00
fedc0bd8c8 fix import 2017-06-23 16:42:03 +02:00
7b78b47a1b 1.0.25 2017-06-23 16:38:11 +02:00
a4acfc0f6e 1.0.24 2017-06-23 11:40:26 +02:00
a934f3608f remove NeDB and update to use MongoDB Atlas for testing 2017-06-23 11:40:20 +02:00
a4f3f23bed update npmextra 2017-06-18 19:53:29 +02:00
2039f1c807 update ci 2017-06-18 19:52:54 +02:00
57d1a6dd0a added npmdocker for local development 2017-02-25 11:37:05 +01:00
31 changed files with 2255 additions and 1046 deletions

5
.gitignore vendored
View File

@ -1,5 +1,4 @@
node_modules/ node_modules/
test/data
pages/
coverage/
public/ public/
coverage/
.nogit/

View File

@ -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: 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 npmpage
- npmci command npmpage
tags:
- docker
- notpriv
only:
- 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:
- windows
allow_failure: true

118
README.md
View File

@ -1,105 +1,129 @@
# smartdata # @pushrocks/smartdata
> Note: Still in Beta do more with data and RethinkDB
## Availabililty ## Availabililty
[![npm](https://push.rocks/assets/repo-button-npm.svg)](https://www.npmjs.com/package/smartdata)
[![git](https://push.rocks/assets/repo-button-git.svg)](https://gitlab.com/pushrocks/smartdata) [![npm](https://pushrocks.gitlab.io/assets/repo-button-npm.svg)](https://www.npmjs.com/package/smartdata)
[![git](https://push.rocks/assets/repo-button-mirror.svg)](https://github.com/pushrocks/smartdata) [![git](https://pushrocks.gitlab.io/assets/repo-button-git.svg)](https://GitLab.com/pushrocks/smartdata)
[![docs](https://push.rocks/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/smartdata/) [![git](https://pushrocks.gitlab.io/assets/repo-button-mirror.svg)](https://github.com/pushrocks/smartdata)
[![docs](https://pushrocks.gitlab.io/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/smartdata/)
## Status for master ## Status for master
[![build status](https://gitlab.com/pushrocks/smartdata/badges/master/build.svg)](https://gitlab.com/pushrocks/smartdata/commits/master)
[![coverage report](https://gitlab.com/pushrocks/smartdata/badges/master/coverage.svg)](https://gitlab.com/pushrocks/smartdata/commits/master) [![build status](https://GitLab.com/pushrocks/smartdata/badges/master/build.svg)](https://GitLab.com/pushrocks/smartdata/commits/master)
[![coverage report](https://GitLab.com/pushrocks/smartdata/badges/master/coverage.svg)](https://GitLab.com/pushrocks/smartdata/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/smartdata.svg)](https://www.npmjs.com/package/smartdata)
[![Dependency Status](https://david-dm.org/pushrocks/smartdata.svg)](https://david-dm.org/pushrocks/smartdata) [![Dependency Status](https://david-dm.org/pushrocks/smartdata.svg)](https://david-dm.org/pushrocks/smartdata)
[![bitHound Dependencies](https://www.bithound.io/github/pushrocks/smartdata/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/smartdata/master/dependencies/npm) [![bitHound Dependencies](https://www.bithound.io/github/pushrocks/smartdata/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/smartdata/master/dependencies/npm)
[![bitHound Code](https://www.bithound.io/github/pushrocks/smartdata/badges/code.svg)](https://www.bithound.io/github/pushrocks/smartdata) [![bitHound Code](https://www.bithound.io/github/pushrocks/smartdata/badges/code.svg)](https://www.bithound.io/github/pushrocks/smartdata)
[![Known Vulnerabilities](https://snyk.io/test/npm/smartdata/badge.svg)](https://snyk.io/test/npm/smartdata)
[![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/) [![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%206.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/) [![node](https://img.shields.io/badge/node->=%206.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/) [![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](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.
[![npm](https://push.rocks/assets/repo-header.svg)](https://push.rocks) For further information read the linked docs at the top of this README.
> MIT licensed | **&copy;** [Lossless GmbH](https://lossless.gmbh)
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
[![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://push.rocks)

3
dist/index.d.ts vendored
View File

@ -1,3 +0,0 @@
export * from './smartdata.classes.db';
export * from './smartdata.classes.dbcollection';
export * from './smartdata.classes.dbdoc';

8
dist/index.js vendored
View File

@ -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==

View File

@ -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;
}

View File

@ -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==

View File

@ -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);
}

File diff suppressed because one or more lines are too long

View File

@ -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;
}

View File

@ -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

View File

@ -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 };

View File

@ -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=

View File

@ -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 [![npm](https://pushrocks.gitlab.io/assets/repo-button-npm.svg)](https://www.npmjs.com/package/smartdata)
[![git](https://pushrocks.gitlab.io/assets/repo-button-git.svg)](https://GitLab.com/pushrocks/smartdata)
[![git](https://pushrocks.gitlab.io/assets/repo-button-mirror.svg)](https://github.com/pushrocks/smartdata)
[![docs](https://pushrocks.gitlab.io/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/smartdata/)
MongoDB term | smartdata class ## Status for master
--- | ---
Database | smartdata.DbConnection
Collection | smartdata.DbCollection
Document | smartdata.DbDoc
[![npm](https://push.rocks/assets/repo-header.svg)](https://push.rocks) [![build status](https://GitLab.com/pushrocks/smartdata/badges/master/build.svg)](https://GitLab.com/pushrocks/smartdata/commits/master)
[![coverage report](https://GitLab.com/pushrocks/smartdata/badges/master/coverage.svg)](https://GitLab.com/pushrocks/smartdata/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/smartdata.svg)](https://www.npmjs.com/package/smartdata)
[![Dependency Status](https://david-dm.org/pushrocks/smartdata.svg)](https://david-dm.org/pushrocks/smartdata)
[![bitHound Dependencies](https://www.bithound.io/github/pushrocks/smartdata/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/smartdata/master/dependencies/npm)
[![bitHound Code](https://www.bithound.io/github/pushrocks/smartdata/badges/code.svg)](https://www.bithound.io/github/pushrocks/smartdata)
[![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%206.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
For further information read the linked docs at the top of this README.
> MIT licensed | **&copy;** [Lossless GmbH](https://lossless.gmbh)
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
[![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://push.rocks)

18
npmextra.json Normal file
View File

@ -0,0 +1,18 @@
{
"npmdocker": {
"baseImage": "hosttoday/ht-docker-node:mongo",
"command": "npmci test stable",
"dockerSock": false
},
"npmci": {
"npmGlobalTools": [
"@gitzone/npmts"
],
"npmAccessLevel": "public"
},
"npmts": {
"testConfig": {
"parallel": false
}
}
}

1558
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,12 +1,14 @@
{ {
"name": "smartdata", "name": "@pushrocks/smartdata",
"version": "1.0.22", "version": "3.1.1",
"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": "tsrun test/test.ts",
"prepareMongo": "(rm -rf ./test/data && mkdir ./test/data/)" "testLocal": "(npmdocker)",
"build": "(npmts)"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -19,25 +21,23 @@
}, },
"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", "@gitzone/tsrun": "^1.1.2",
"@types/should": "^8.1.30", "@types/node": "^10.5.2",
"shelljs": "^0.7.5", "@types/shortid": "0.0.29",
"should": "^11.1.1", "mongodb-memory-server": "^1.9.0",
"smartstring": "^2.0.22", "qenv": "^1.1.7",
"typings-test": "^1.0.3" "shortid": "^2.2.11",
"tapbundle": "^2.0.2"
} }
} }

4
qenv.yml Normal file
View File

@ -0,0 +1,4 @@
vars:
- MONGO_URL
- MONGO_DBNAME
- MONGO_PASS

1
test/test.d.ts vendored
View File

@ -1 +0,0 @@
import 'typings-test';

File diff suppressed because one or more lines are too long

View File

@ -1,133 +1,65 @@
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';
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
} });
let testDbCollection: smartdata.DbCollection<ITestObject1> tap.test('should establish a connection to the rethink Db cluster', async () => {
await testDb.connect();
});
describe('mongodb', function () { // =======================================
it('should start mongodb', function (done) { // The actual tests
this.timeout(30000) // =======================================
mongoChildProcess = shelljs.exec('mongod --dbpath=./test/data --port 27017', { async: true, silent: true })
let doneCalled = false // ------
mongoChildProcess.stdout.on('data', function (data) { // Collections
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) {
testDb = new smartdata.Db('mongodb://localhost:27017/smartdata')
testDb.connect().then(() => { done() })
})
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) @smartdata.Collection(testDb)
class TestCar extends smartdata.DbDoc<TestCar> { class Car extends smartdata.SmartDataDbDoc<Car> {
@smartdata.svDb() @smartdata.unI() index: string = shortid();
color: string @smartdata.svDb() color: string;
constructor(optionsArg: { @smartdata.svDb() brand: string;
color: string, constructor(colorArg: string, brandArg: string) {
property2: number super();
}) { this.color = colorArg;
super() this.brand = brandArg;
this.color = optionsArg.color
} }
};
let testCarInstance = new TestCar({
color: 'red',
property2: 2
})
should(testCarInstance.saveableProperties[0]).equal('color')
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 () { tap.test('should save the car to the db', async () => {
let testDb: smartdata.Db const myCar = new Car('red', 'Volvo');
let testCollection: smartdata.DbCollection<ITestObject1> await myCar.save();
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){ tap.test('expect to get instance of Car', async () => {
testCollection.insertOne({ value1: 'hi' }).then(() => {done()}) let myCars = await Car.getInstances<Car>({
}) brand: 'Volvo'
});
expect(myCars[0].color).to.equal('red');
});
it('should find the inserted document', function(done){ // =======================================
testCollection.find({ value1: 'hi' }).then(x => { // close the database connection
console.log(x) // =======================================
done() tap.test('should close the database connection', async tools => {
}) await testDb.close();
}) });
})
tap.start({throwOnError: true});

View File

@ -1,5 +1,3 @@
import * as plugins from './smartdata.plugins' export * from './smartdata.classes.db';
export * from './smartdata.classes.collection';
export * from './smartdata.classes.db' export * from './smartdata.classes.doc';
export * from './smartdata.classes.dbcollection'
export * from './smartdata.classes.dbdoc'

View 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;
}
}

View File

@ -1,28 +1,42 @@
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.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,52 @@ 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
} .getDefaultLogger()
plugins.beautylog.ok(`disconnected to database at ${this.dbUrl}`) .info(`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)
}
} }

View File

@ -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
}
}

View File

@ -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
View File

@ -0,0 +1,151 @@
import * as plugins from './smartdata.plugins';
import { Objectmap } from '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;
}
}

View File

@ -0,0 +1,3 @@
export const addPassword = (mongoUrlArg: string, passwordArg: string): string => {
return mongoUrlArg.replace('<PASSWORD>', passwordArg);
};

View File

@ -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
}

View File

@ -1,5 +1,7 @@
{ {
"compilerOptions": { "compilerOptions": {
"experimentalDecorators": true "experimentalDecorators": true,
"target": "es2017",
"module": "commonjs"
} }
} }

View File

@ -1,3 +0,0 @@
{
"extends": "tslint-config-standard"
}