Compare commits

..

51 Commits

Author SHA1 Message Date
d504e90e47 1.0.22 2016-11-18 13:56:18 +01:00
7079340657 refactor @saveable to @svDb() 2016-11-18 13:56:15 +01:00
63ceded1b8 1.0.21 2016-11-18 13:20:47 +01:00
b45e7ee206 fix mongodb startup 2016-11-18 13:20:43 +01:00
715709f3ec 1.0.20 2016-11-18 01:00:06 +01:00
0b8adea736 improve README 2016-11-18 00:59:57 +01:00
90d9a25b3f 1.0.19 2016-11-18 00:42:29 +01:00
e49811eecd now allows adding a saveable decorator 2016-11-18 00:42:25 +01:00
9cbdf317dc 1.0.18 2016-11-17 22:41:05 +01:00
d55339013a improve README 2016-11-17 22:41:01 +01:00
9acdbd2842 1.0.17 2016-11-17 22:36:16 +01:00
dfce8cfcc0 add NeDB support 2016-11-17 22:36:12 +01:00
38446fc79b 1.0.16 2016-11-17 12:20:56 +01:00
af8c41ad6c added nedb to the mix 2016-11-17 12:20:52 +01:00
3a24f829b4 improve README 2016-09-14 01:03:25 +02:00
67db1325c1 1.0.15 2016-09-14 01:02:23 +02:00
6683c05c22 added Decorator support for easy connection between classes and collections 2016-09-14 01:02:11 +02:00
f7f7852b7f 1.0.14 2016-09-13 22:53:27 +02:00
df4346e0a2 implenting dbDoc 2016-09-13 22:53:21 +02:00
eb54fbcd0d improve README 2016-09-13 10:38:00 +02:00
f13c12cd47 improve README 2016-09-13 10:35:51 +02:00
08d53f22f5 improve README 2016-09-13 10:31:14 +02:00
5b6523e6f4 improve README 2016-09-13 10:26:17 +02:00
138290a7b8 1.0.13 2016-09-13 10:23:49 +02:00
036cafc282 improve README 2016-09-13 10:23:46 +02:00
7764ba5cb8 1.0.12 2016-09-13 10:17:46 +02:00
7f77634ea1 imrpove README 2016-09-13 10:17:34 +02:00
acf3c29939 1.0.11 2016-09-13 00:14:54 +02:00
925cd639b4 imrpove README 2016-09-13 00:14:50 +02:00
af8de58dcd improve README 2016-09-13 00:13:00 +02:00
f39703b8d9 1.0.10 2016-09-13 00:04:20 +02:00
fa1951b4dd imrprove README 2016-09-13 00:04:07 +02:00
8c37a367a0 1.0.9 2016-09-12 23:50:31 +02:00
37f388e998 fix tests 2016-09-12 23:50:25 +02:00
78bdb6760e imrove README 2016-09-12 23:48:58 +02:00
ce1dceb60c 1.0.8 2016-09-12 23:48:02 +02:00
16add8bff0 update README.md 2016-09-12 23:47:57 +02:00
11149b2712 improve README 2016-09-12 22:11:17 +02:00
e508a942e3 1.0.7 2016-09-12 21:36:41 +02:00
5b093c68e4 add find logic 2016-09-12 21:36:26 +02:00
d1b0a65993 1.0.6 2016-09-12 18:14:05 +02:00
316030be7c introduce checkDoc method 2016-09-12 18:14:01 +02:00
411b0b1ae9 update structure 2016-09-12 17:31:23 +02:00
50a6a1a690 now wating correctly for mongo by checking logMessage 2016-09-12 14:45:08 +02:00
ee3c3580d3 1.0.5 2016-09-11 18:03:56 +02:00
6dd82994a6 add basic README 2016-09-11 18:03:47 +02:00
295096de24 split into class files 2016-09-11 18:01:46 +02:00
50b5f84955 1.0.4 2016-09-11 17:04:57 +02:00
1557c713cd reduce time 2016-09-11 17:04:56 +02:00
4ce457fadf 1.0.3 2016-09-11 16:58:59 +02:00
56d6e0fd45 double time for mongo startup 2016-09-11 16:58:53 +02:00
22 changed files with 1064 additions and 140 deletions

105
README.md Normal file
View File

@ -0,0 +1,105 @@
# smartdata
> Note: Still in Beta
## 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)
[![git](https://push.rocks/assets/repo-button-mirror.svg)](https://github.com/pushrocks/smartdata)
[![docs](https://push.rocks/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/smartdata/)
## 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)
[![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/)
smartdata is an ODM that adheres to TypeScript practices and uses classes to organize data.
It uses MongoDB or NeDb as persistent storage.
## Intention
There are many ODMs out there, however when we searched for an ODM that uses TypeScript,
acts smart while still embracing the NoSQL idea we didn't find a matching solution.
This is why we started smartdata.
How MongoDB terms map to smartdata classes
MongoDB term | smartdata class
--- | ---
Database | smartdata.Db
Collection | smartdata.DbCollection
Document | smartdata.DbDoc
### class Db
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
let myDb1 = new smartdata.Db('someConnectionUrl')
let myDb2 = new smartdata.Db('someConnectionUrl')
// nedb
let myDb3 = new smartdata('/some/path/for/persistence', 'nedb') // you may set first argument to null for just in memory db
myDb1.connect()
myDb2.connect()
// continues in next block...
```
### class DbCollection
represents a collection of objects.
A collection is defined by the object class (that is extending smartdata.dbdoc) it respresents
So to get to get access to a specific collection you document
```javascript
// continues from the block before...
@Collection(myDb1)
class myObject extends smartdata.DbDoc<myObject> { // read the next block about DbDoc
@smartdata.svDb() property1: string // @smartdata.svDb() marks the property for db save
property2: number // this one is not marked, so it won't be save upon calling this.save()
constructor(optionsArg:{
property1: string,
property2: number
}) {
super()
}
}
let myCollection = myDb1.getCollectionByName<myObject>(myObject)
// start to instantiate classes from scratch or database
```
> Alert: You NEVER instantiate a collection.
This is done for you!!!
### class DbDoc
represents a individual document in a collection
and thereby is ideally suited to extend the class you want to actually store.
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.
* `.saveDeep()` does the same like `.save()`.
In addition it will look for properties that reference an object
that extends DbDoc as well and call .saveDeep() on them as well.
Loops are prevented
So now we can **store** instances of classes to Db...
How do we **get** a new class instance from a Doc in the DB?
## TypeScript
How does TypeScript play into this?
Since you define your classes in TypeScript and types flow through smartdata in a generic way
you should get all the Intellisense and type checking you love when using smartdata.
smartdata itself also bundles typings.
So you don't need to install any additional types when importing smartdata.
[![npm](https://push.rocks/assets/repo-header.svg)](https://push.rocks)

17
dist/index.d.ts vendored
View File

@ -1,14 +1,3 @@
/// <reference types="q" />
import * as plugins from './smartdata.plugins';
export declare class DbCollection<T> {
constructor(nameArg: string, db: plugins.mongodb.Db);
}
export declare type TDbConnectionStatus = 'disconnected' | 'connected' | 'failed';
export declare class DbConnection {
dbUrl: string;
db: plugins.mongodb.Db;
status: TDbConnectionStatus;
constructor(dbUrl: string);
connect(): plugins.q.Promise<any>;
close(): plugins.q.Promise<any>;
}
export * from './smartdata.classes.db';
export * from './smartdata.classes.dbcollection';
export * from './smartdata.classes.dbdoc';

39
dist/index.js vendored
View File

@ -1,35 +1,8 @@
"use strict";
const plugins = require('./smartdata.plugins');
class DbCollection {
constructor(nameArg, db) {
let collection = db.collection(nameArg);
}
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
exports.DbCollection = DbCollection;
class DbConnection {
constructor(dbUrl) {
this.dbUrl = dbUrl;
}
connect() {
let done = plugins.q.defer();
plugins.mongodb.MongoClient.connect(this.dbUrl, (err, db) => {
if (err) {
console.log(err);
}
plugins.assert.equal(null, err);
this.db = db;
plugins.beautylog.success(`connected to database at ${this.dbUrl}`);
done.resolve(this.db);
});
return done.promise;
}
close() {
let done = plugins.q.defer();
this.db.close();
plugins.beautylog.ok(`disconnected to database at ${this.dbUrl}`);
done.resolve();
return done.promise;
}
}
exports.DbConnection = DbConnection;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsTUFBWSxPQUFPLFdBQU0scUJBRXpCLENBQUMsQ0FGNkM7QUFFOUM7SUFDSSxZQUFZLE9BQWUsRUFBRSxFQUFzQjtRQUMvQyxJQUFJLFVBQVUsR0FBRyxFQUFFLENBQUMsVUFBVSxDQUFDLE9BQU8sQ0FBQyxDQUFBO0lBQzNDLENBQUM7QUFDTCxDQUFDO0FBSlksb0JBQVksZUFJeEIsQ0FBQTtBQUlEO0lBS0ksWUFBWSxLQUFhO1FBQ3JCLElBQUksQ0FBQyxLQUFLLEdBQUcsS0FBSyxDQUFBO0lBQ3RCLENBQUM7SUFFRCxPQUFPO1FBQ0gsSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQTtRQUM1QixPQUFPLENBQUMsT0FBTyxDQUFDLFdBQVcsQ0FBQyxPQUFPLENBQUMsSUFBSSxDQUFDLEtBQUssRUFBRSxDQUFDLEdBQUcsRUFBRSxFQUFFO1lBQ3BELEVBQUUsQ0FBQyxDQUFDLEdBQUcsQ0FBQyxDQUFDLENBQUM7Z0JBQUMsT0FBTyxDQUFDLEdBQUcsQ0FBQyxHQUFHLENBQUMsQ0FBQTtZQUFDLENBQUM7WUFDN0IsT0FBTyxDQUFDLE1BQU0sQ0FBQyxLQUFLLENBQUMsSUFBSSxFQUFFLEdBQUcsQ0FBQyxDQUFBO1lBQy9CLElBQUksQ0FBQyxFQUFFLEdBQUcsRUFBRSxDQUFBO1lBQ1osT0FBTyxDQUFDLFNBQVMsQ0FBQyxPQUFPLENBQUMsNEJBQTRCLElBQUksQ0FBQyxLQUFLLEVBQUUsQ0FBQyxDQUFBO1lBQ25FLElBQUksQ0FBQyxPQUFPLENBQUMsSUFBSSxDQUFDLEVBQUUsQ0FBQyxDQUFBO1FBQ3pCLENBQUMsQ0FBQyxDQUFBO1FBQ0YsTUFBTSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUE7SUFDdkIsQ0FBQztJQUVELEtBQUs7UUFDRCxJQUFJLElBQUksR0FBRyxPQUFPLENBQUMsQ0FBQyxDQUFDLEtBQUssRUFBRSxDQUFBO1FBQzVCLElBQUksQ0FBQyxFQUFFLENBQUMsS0FBSyxFQUFFLENBQUE7UUFDZixPQUFPLENBQUMsU0FBUyxDQUFDLEVBQUUsQ0FBQywrQkFBK0IsSUFBSSxDQUFDLEtBQUssRUFBRSxDQUFDLENBQUE7UUFDakUsSUFBSSxDQUFDLE9BQU8sRUFBRSxDQUFBO1FBQ2QsTUFBTSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUE7SUFDdkIsQ0FBQztBQUNMLENBQUM7QUE1Qlksb0JBQVksZUE0QnhCLENBQUEifQ==
__export(require("./smartdata.classes.db"));
__export(require("./smartdata.classes.dbcollection"));
__export(require("./smartdata.classes.dbdoc"));
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7O0FBRUEsNENBQXNDO0FBQ3RDLHNEQUFnRDtBQUNoRCwrQ0FBeUMifQ==

33
dist/smartdata.classes.db.d.ts vendored Normal file
View File

@ -0,0 +1,33 @@
/// <reference types="q" />
import * as plugins from './smartdata.plugins';
import { Objectmap } from 'lik';
import { DbCollection } from './smartdata.classes.dbcollection';
/**
* interface - indicates the database type
*/
export declare type TDbType = 'mongodb' | 'nedb';
/**
* interface - indicates the connection status of the db
*/
export declare type TConnectionStatus = 'disconnected' | 'connected' | 'failed';
export declare class Db {
dbType: TDbType;
dbUrl: string;
db: plugins.mongodb.Db;
status: TConnectionStatus;
collections: Objectmap<DbCollection<any>>;
constructor(dbUrlArg: string, dbTypeArg?: TDbType);
/**
* connects to the database that was specified during instance creation
*/
connect(): plugins.q.Promise<any>;
/**
* closes the connection to the databse
*/
close(): plugins.q.Promise<any>;
/**
* gets a collection by name: string
*/
getCollectionByName<T>(nameArg: string): plugins.q.Promise<DbCollection<T>>;
addCollection(dbCollectionArg: DbCollection<any>): void;
}

64
dist/smartdata.classes.db.js vendored Normal file
View File

@ -0,0 +1,64 @@
"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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FBTSxDQUFDLGVBQWUsQ0FBQyxJQUFJLEtBQUssT0FBTyxDQUFBO1FBQzNDLENBQUMsQ0FBQyxDQUFBO1FBQ0YsRUFBRSxDQUFDLENBQUMsZ0JBQWdCLEtBQUssSUFBSSxDQUFDLENBQUMsQ0FBQztZQUM1QixJQUFJLENBQUMsT0FBTyxDQUFDLGdCQUFnQixDQUFDLENBQUE7UUFDbEMsQ0FBQztRQUNELE1BQU0sQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFBO0lBQ3ZCLENBQUM7SUFBQSxDQUFDO0lBRUYsYUFBYSxDQUFDLGVBQWtDO1FBQzVDLElBQUksQ0FBQyxXQUFXLENBQUMsR0FBRyxDQUFDLGVBQWUsQ0FBQyxDQUFBO0lBQ3pDLENBQUM7Q0FFSjtBQWxFRCxnQkFrRUMifQ==

View File

@ -0,0 +1,41 @@
/// <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 Normal file

File diff suppressed because one or more lines are too long

34
dist/smartdata.classes.dbdoc.d.ts vendored Normal file
View File

@ -0,0 +1,34 @@
import { DbCollection } from './smartdata.classes.dbcollection';
export declare type TDocCreation = 'db' | 'new' | 'mixed';
/**
* saveable - saveable decorator to be used on class properties
*/
export declare function svDb(): (target: DbDoc<any>, key: string) => void;
export declare class DbDoc<T> {
/**
* the collection object an Doc belongs to
*/
collection: DbCollection<T>;
/**
* how the Doc in memory was created, may prove useful later.
*/
creationType: TDocCreation;
/**
* an array of saveable properties of a doc
*/
saveableProperties: string[];
/**
* class constructor
*/
constructor();
/**
* saves this instance but not any connected items
* may lead to data inconsistencies, but is faster
*/
save(): void;
/**
* also store any referenced objects to DB
* better for data consistency
*/
saveDeep(): void;
}

48
dist/smartdata.classes.dbdoc.js vendored Normal file
View File

@ -0,0 +1,48 @@
"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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGJkb2MuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEuY2xhc3Nlcy5kYmRvYy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBT0E7O0dBRUc7QUFDSDtJQUNJLE1BQU0sQ0FBQyxDQUFDLE1BQWtCLEVBQUUsR0FBVztRQUNuQ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2YsQ0FBQztDQUNKO0FBakRELHNCQWlEQyJ9

View File

@ -1,5 +1,8 @@
import 'typings-global';
export import assert = require('assert');
export import beautylog = require('beautylog');
export import mongodb = require('mongodb');
export import q = require('q');
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,7 +1,15 @@
"use strict";
require('typings-global');
exports.assert = require('assert');
exports.beautylog = require('beautylog');
exports.mongodb = require('mongodb');
exports.q = require('q');
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLnBsdWdpbnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEucGx1Z2lucy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsUUFBTyxnQkFDUCxDQUFDLENBRHNCO0FBQ1QsY0FBTSxXQUFXLFFBQVEsQ0FBQyxDQUFBO0FBQzFCLGlCQUFTLFdBQVcsV0FBVyxDQUFDLENBQUE7QUFDaEMsZUFBTyxXQUFXLFNBQVMsQ0FBQyxDQUFBO0FBQzVCLFNBQUMsV0FBVyxHQUFHLENBQUMsQ0FBQSJ9
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=

19
docs/index.md Normal file
View File

@ -0,0 +1,19 @@
# smartdata
smartdata is a ODM that adheres to TypeScript practices and uses classes to organize data.
It uses MongoDB as persistent storage.
## Intention
There are many ODMs out there, however when we searched for a ODM that uses TypeScript,
acts smart while still embracing an easy the NoSQL idea we didn't find a matching solution.
This is why we started smartdata
How MongoDB terms map to smartdata classes
MongoDB term | smartdata class
--- | ---
Database | smartdata.DbConnection
Collection | smartdata.DbCollection
Document | smartdata.DbDoc
[![npm](https://push.rocks/assets/repo-header.svg)](https://push.rocks)

View File

@ -1,11 +1,12 @@
{
"name": "smartdata",
"version": "1.0.2",
"version": "1.0.22",
"description": "do more with data",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"scripts": {
"test": "(npmts)"
"test": "(npm run prepareMongo && npmts)",
"prepareMongo": "(rm -rf ./test/data && mkdir ./test/data/)"
},
"repository": {
"type": "git",
@ -18,18 +19,25 @@
},
"homepage": "https://gitlab.com/pushrocks/smartdata#README",
"dependencies": {
"@types/mongodb": "^2.1.32",
"@types/q": "0.0.30",
"beautylog": "^5.0.23",
"mongodb": "^2.2.9",
"@types/lodash": "^4.14.39",
"@types/mongodb": "^2.1.34",
"@types/nedb": "0.0.31",
"@types/q": "0.0.32",
"beautylog": "^6.0.0",
"lik": "^1.0.24",
"lodash": "^4.17.2",
"mongodb": "^2.2.11",
"nedb": "^1.8.0",
"q": "^1.4.1",
"runtime-type-checks": "0.0.4",
"typings-global": "^1.0.14"
},
"devDependencies": {
"@types/shelljs": "^0.3.30",
"@types/should": "^8.1.29",
"shelljs": "^0.7.4",
"should": "^11.1.0",
"@types/shelljs": "^0.3.32",
"@types/should": "^8.1.30",
"shelljs": "^0.7.5",
"should": "^11.1.1",
"smartstring": "^2.0.22",
"typings-test": "^1.0.3"
}
}

View File

File diff suppressed because one or more lines are too long

View File

@ -1,36 +1,133 @@
import 'typings-test'
import * as shelljs from 'shelljs'
import * as should from 'should'
import * as smartstring from 'smartstring'
// the tested module
import * as smartdata from '../dist/index'
let mongoChildProcess
let testDbConnection: smartdata.DbConnection
let testDb: smartdata.Db
interface ITestObject1 {
value1: string
value2?: number
value3?: string
}
describe('mongodb',function(){
it('should start mongodb',function(done){
this.timeout(10000)
mongoChildProcess = shelljs.exec('mongod --dbpath=./test/data --port 27017',{async: true})
setTimeout(() => { done() }, 5000) // give mongodb it some time to complete startup
})
})
describe('smartdata',function(){
it('should establish a connection to mongodb',function(done){
testDbConnection = new smartdata.DbConnection('mongodb://localhost:27017/smartdata')
testDbConnection.connect().then(() => { done() })
})
it('should create a collection',function(){
let testDbCollection: smartdata.DbCollection<ITestObject1>
})
it('should close the db Connection',function(){
testDbConnection.close()
describe('mongodb', function () {
it('should start mongodb', function (done) {
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) {
console.log(smartstring.indent.indentWithPrefix(data, '*** MongoDB Process *** : '))
if (!doneCalled) {
if (/waiting for connections on port 27017/.test(data)) {
doneCalled = true
done()
}
}
})
})
})
describe('mongodb',function(){
it('should kill mongodb',function(){
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)
class TestCar extends smartdata.DbDoc<TestCar> {
@smartdata.svDb()
color: string
constructor(optionsArg: {
color: string,
property2: number
}) {
super()
this.color = optionsArg.color
}
};
let testCarInstance = new TestCar({
color: 'red',
property2: 2
})
should(testCarInstance.saveableProperties[0]).equal('color')
console.log(TestCar)
should(testCarInstance.collection).be.instanceof(smartdata.DbCollection)
should(testCarInstance).be.instanceof(smartdata.DbDoc)
testCarInstance.save()
it('should get a collection for testCar', function () {
})
})
})
describe('mongodb', function () {
it('should kill mongodb', function (done) {
this.timeout(30000)
mongoChildProcess.stdout.on('data', function (data) {
if (/dbexit: rc: 0/.test(data)) {
done()
}
})
shelljs.exec('mongod --dbpath=./test/data --shutdown')
mongoChildProcess.kill('SIGTERM')
})
})
describe('smartdata with nedb', function () {
let testDb: smartdata.Db
let testCollection: smartdata.DbCollection<ITestObject1>
it('should create a DB with nedb', function () {
testDb = new smartdata.Db('any', 'nedb')
testDb.connect()
testCollection = new smartdata.DbCollection<ITestObject1>('anyName', testDb)
})
it('should insert a doc', function(done){
testCollection.insertOne({ value1: 'hi' }).then(() => {done()})
})
it('should find the inserted document', function(done){
testCollection.find({ value1: 'hi' }).then(x => {
console.log(x)
done()
})
})
})

View File

@ -1,39 +1,5 @@
import * as plugins from './smartdata.plugins'
export class DbCollection<T> {
constructor(nameArg: string, db: plugins.mongodb.Db) {
let collection = db.collection(nameArg)
}
}
export type TDbConnectionStatus = 'disconnected' | 'connected' | 'failed'
export class DbConnection {
dbUrl: string
db: plugins.mongodb.Db
status: TDbConnectionStatus
constructor(dbUrl: string) {
this.dbUrl = dbUrl
}
connect(): plugins.q.Promise<any> {
let done = plugins.q.defer()
plugins.mongodb.MongoClient.connect(this.dbUrl, (err, db) => {
if (err) { console.log(err) }
plugins.assert.equal(null, err)
this.db = db
plugins.beautylog.success(`connected to database at ${this.dbUrl}`)
done.resolve(this.db)
})
return done.promise
}
close(): plugins.q.Promise<any> {
let done = plugins.q.defer()
this.db.close()
plugins.beautylog.ok(`disconnected to database at ${this.dbUrl}`)
done.resolve()
return done.promise
}
}
export * from './smartdata.classes.db'
export * from './smartdata.classes.dbcollection'
export * from './smartdata.classes.dbdoc'

View File

@ -0,0 +1,82 @@
import * as plugins from './smartdata.plugins'
import { Objectmap } from 'lik'
import { DbCollection } from './smartdata.classes.dbcollection'
/**
* interface - indicates the database type
*/
export type TDbType = 'mongodb' | 'nedb'
/**
* interface - indicates the connection status of the db
*/
export type TConnectionStatus = 'disconnected' | 'connected' | 'failed'
export class Db {
dbType: TDbType
dbUrl: string
db: plugins.mongodb.Db
status: TConnectionStatus
collections = new Objectmap<DbCollection<any>>()
constructor(dbUrlArg: string, dbTypeArg: TDbType = 'mongodb') {
this.dbType = dbTypeArg
this.dbUrl = dbUrlArg
}
// basic connection stuff ----------------------------------------------
/**
* connects to the database that was specified during instance creation
*/
connect(): plugins.q.Promise<any> {
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(): plugins.q.Promise<any> {
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<T>(nameArg: string): plugins.q.Promise<DbCollection<T>> {
let done = plugins.q.defer<DbCollection<any>>()
let resultCollection = this.collections.find((dbCollectionArg) => {
return dbCollectionArg.name === nameArg
})
if (resultCollection !== null) {
done.resolve(resultCollection)
}
return done.promise
};
addCollection(dbCollectionArg: DbCollection<any>) {
this.collections.add(dbCollectionArg)
}
}

View File

@ -0,0 +1,144 @@
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

@ -0,0 +1,68 @@
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()
}
}

View File

@ -1,5 +1,16 @@
import 'typings-global'
export import assert = require('assert')
export import beautylog = require('beautylog')
export import mongodb = require('mongodb')
export import q = require('q')
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'
let nedb = require('nedb')
export {
assert,
beautylog,
lodash,
mongodb,
q,
nedb
}

5
tsconfig.json Normal file
View File

@ -0,0 +1,5 @@
{
"compilerOptions": {
"experimentalDecorators": true
}
}