feat(core): now retrieves classes properly
This commit is contained in:
parent
e2d12f8c9c
commit
d2ca108ef8
3
.prettierrc
Normal file
3
.prettierrc
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"parser": "typescript"
|
||||||
|
}
|
52
README.md
52
README.md
@ -1,13 +1,16 @@
|
|||||||
# smartdata
|
# smartdata
|
||||||
|
|
||||||
do more with data and RethinkDB
|
do more with data and RethinkDB
|
||||||
|
|
||||||
## Availabililty
|
## Availabililty
|
||||||
|
|
||||||
[![npm](https://pushrocks.gitlab.io/assets/repo-button-npm.svg)](https://www.npmjs.com/package/smartdata)
|
[![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-git.svg)](https://GitLab.com/pushrocks/smartdata)
|
||||||
[![git](https://pushrocks.gitlab.io/assets/repo-button-mirror.svg)](https://github.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/)
|
[![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)
|
[![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)
|
[![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)
|
[![npm downloads per month](https://img.shields.io/npm/dm/smartdata.svg)](https://www.npmjs.com/package/smartdata)
|
||||||
@ -20,37 +23,42 @@ do more with data and RethinkDB
|
|||||||
[![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
|
## Usage
|
||||||
|
|
||||||
Use TypeScript for best in class instellisense.
|
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 RethinkDB 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 RethinkDB's terms map to the ones of smartdata:
|
How RethinkDB's terms map to the ones of smartdata:
|
||||||
|
|
||||||
RethinkDB term | smartdata class
|
| RethinkDB term | smartdata class |
|
||||||
--- | ---
|
| -------------- | ----------------- |
|
||||||
Database | smartdata.Db
|
| Database | smartdata.Db |
|
||||||
Table | smartdata.DbTable
|
| 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.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
import * as smartdata from 'smartdata'
|
import * as smartdata from "smartdata";
|
||||||
|
|
||||||
let myRethinkDb1 = new smartdata.Db({
|
let myRethinkDb1 = new smartdata.Db({
|
||||||
// rethinkDb connection options here
|
// rethinkDb connection options here
|
||||||
})
|
});
|
||||||
|
|
||||||
myDb1.connect()
|
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
|
||||||
|
|
||||||
@ -60,31 +68,30 @@ So to get to get access to a specific collection you document
|
|||||||
// continues from the block before...
|
// continues from the block before...
|
||||||
|
|
||||||
@Collection(myRethinkDb1)
|
@Collection(myRethinkDb1)
|
||||||
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 = myRethinkDb1.getCollectionByName<myObject>(myObject)
|
let myCollection = myRethinkDb1.getCollectionByName < myObject > myObject;
|
||||||
|
|
||||||
// start to instantiate instances of classes from scratch or database
|
// start to instantiate instances of classes from scratch or database
|
||||||
|
|
||||||
let localObject = new MyObject({
|
let localObject = new MyObject({
|
||||||
property1: 'hi',
|
property1: "hi",
|
||||||
property2: 2
|
property2: 2
|
||||||
})
|
});
|
||||||
localObject.save() // saves the object to the database
|
localObject.save(); // saves the object to the database
|
||||||
```
|
```
|
||||||
|
|
||||||
> Alert: You NEVER instantiate a collection.
|
> Alert: You NEVER instantiate a collection.
|
||||||
This is done for you!!!
|
> 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.
|
||||||
|
|
||||||
@ -100,6 +107,7 @@ So now we can **store** instances of classes to Db...
|
|||||||
How do we **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?
|
||||||
|
|
||||||
## 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.
|
||||||
@ -109,6 +117,6 @@ So you don't need to install any additional types when importing smartdata.
|
|||||||
For further information read the linked docs at the top of this README.
|
For further information read the linked docs at the top of this README.
|
||||||
|
|
||||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
> | 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)
|
[![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](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";
|
||||||
|
4
dist/index.js
vendored
4
dist/index.js
vendored
@ -4,6 +4,6 @@ function __export(m) {
|
|||||||
}
|
}
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLDRDQUFzQztBQUN0QyxzREFBZ0Q7QUFDaEQsK0NBQXlDIn0=
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLDRDQUF1QztBQUN2QyxpREFBNEM7QUFDNUMsK0NBQTBDIn0=
|
16
dist/smartdata.classes.db.d.ts
vendored
16
dist/smartdata.classes.db.d.ts
vendored
@ -1,11 +1,11 @@
|
|||||||
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.dbcollection';
|
import { DbTable } from "./smartdata.classes.dbtable";
|
||||||
import { ConnectionOptions } from 'rethinkdb';
|
import { ConnectionOptions } from "rethinkdb";
|
||||||
/**
|
/**
|
||||||
* interface - indicates the connection status of the db
|
* interface - indicates the connection status of the db
|
||||||
*/
|
*/
|
||||||
export declare type TConnectionStatus = 'initial' | 'disconnected' | 'connected' | 'failed';
|
export declare type TConnectionStatus = "initial" | "disconnected" | "connected" | "failed";
|
||||||
export declare class Db {
|
export declare class Db {
|
||||||
dbName: string;
|
dbName: string;
|
||||||
connectionOptions: plugins.rethinkDb.ConnectionOptions;
|
connectionOptions: plugins.rethinkDb.ConnectionOptions;
|
||||||
@ -14,9 +14,9 @@ export declare class Db {
|
|||||||
dbTablesMap: Objectmap<DbTable<any>>;
|
dbTablesMap: Objectmap<DbTable<any>>;
|
||||||
constructor(connectionOptionsArg: ConnectionOptions);
|
constructor(connectionOptionsArg: ConnectionOptions);
|
||||||
/**
|
/**
|
||||||
* supply additional SSl options
|
* supply additional SSl options needed to connect to certain Rethink DB servers (e.g. compose.io)
|
||||||
*/
|
*/
|
||||||
setSsl(certificateStringArg: string, formatArg: 'base64' | 'clearText'): void;
|
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
|
||||||
*/
|
*/
|
||||||
@ -25,11 +25,11 @@ export declare class Db {
|
|||||||
* closes the connection to the databse
|
* closes the connection to the databse
|
||||||
*/
|
*/
|
||||||
close(): Promise<any>;
|
close(): Promise<any>;
|
||||||
|
addTable(dbTableArg: DbTable<any>): void;
|
||||||
/**
|
/**
|
||||||
* Gets a table's name and returns smartdata's DbTable class
|
* Gets a table's name and returns smartdata's DbTable class
|
||||||
* @param nameArg
|
* @param nameArg
|
||||||
* @returns DbTable
|
* @returns DbTable
|
||||||
*/
|
*/
|
||||||
getDbTableByName<T>(nameArg: string): Promise<DbTable<T>>;
|
getDbTableByName<T>(nameArg: string): Promise<DbTable<T>>;
|
||||||
addTable(dbCollectionArg: DbTable<any>): void;
|
|
||||||
}
|
}
|
||||||
|
24
dist/smartdata.classes.db.js
vendored
24
dist/smartdata.classes.db.js
vendored
@ -15,20 +15,20 @@ class Db {
|
|||||||
this.dbTablesMap = new lik_1.Objectmap();
|
this.dbTablesMap = new lik_1.Objectmap();
|
||||||
this.dbName = connectionOptionsArg.db;
|
this.dbName = connectionOptionsArg.db;
|
||||||
this.connectionOptions = connectionOptionsArg;
|
this.connectionOptions = connectionOptionsArg;
|
||||||
this.status = 'initial';
|
this.status = "initial";
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* supply additional SSl options
|
* supply additional SSl options needed to connect to certain Rethink DB servers (e.g. compose.io)
|
||||||
*/
|
*/
|
||||||
setSsl(certificateStringArg, formatArg) {
|
setSsl(certificateStringArg, formatArg) {
|
||||||
let certificateString;
|
let certificateString;
|
||||||
if (formatArg = 'base64') {
|
if ((formatArg = "base64")) {
|
||||||
certificateString = plugins.smartstring.base64.decode(certificateStringArg);
|
certificateString = plugins.smartstring.base64.decode(certificateStringArg);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
certificateString = certificateStringArg;
|
certificateString = certificateStringArg;
|
||||||
}
|
}
|
||||||
this.connectionOptions['ssl'] = {
|
this.connectionOptions["ssl"] = {
|
||||||
ca: Buffer.from(certificateString)
|
ca: Buffer.from(certificateString)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -40,7 +40,7 @@ class Db {
|
|||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
this.dbConnection = yield plugins.rethinkDb.connect(this.connectionOptions);
|
this.dbConnection = yield plugins.rethinkDb.connect(this.connectionOptions);
|
||||||
this.dbConnection.use(this.dbName);
|
this.dbConnection.use(this.dbName);
|
||||||
this.status = 'connected';
|
this.status = "connected";
|
||||||
plugins.beautylog.ok(`Connected to database ${this.dbName}`);
|
plugins.beautylog.ok(`Connected to database ${this.dbName}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -50,11 +50,14 @@ class Db {
|
|||||||
close() {
|
close() {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
yield this.dbConnection.close();
|
yield this.dbConnection.close();
|
||||||
this.status = 'disconnected';
|
this.status = "disconnected";
|
||||||
plugins.beautylog.ok(`disconnected from database ${this.dbName}`);
|
plugins.beautylog.ok(`disconnected from database ${this.dbName}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// handle table to class distribution
|
// handle table to class distribution
|
||||||
|
addTable(dbTableArg) {
|
||||||
|
this.dbTablesMap.add(dbTableArg);
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Gets a table's name and returns smartdata's DbTable class
|
* Gets a table's name and returns smartdata's DbTable class
|
||||||
* @param nameArg
|
* @param nameArg
|
||||||
@ -62,15 +65,12 @@ class Db {
|
|||||||
*/
|
*/
|
||||||
getDbTableByName(nameArg) {
|
getDbTableByName(nameArg) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
let resultCollection = this.dbTablesMap.find((dbCollectionArg) => {
|
let resultCollection = this.dbTablesMap.find(dbTableArg => {
|
||||||
return dbCollectionArg.tableName === nameArg;
|
return dbTableArg.tableName === nameArg;
|
||||||
});
|
});
|
||||||
return resultCollection;
|
return resultCollection;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
addTable(dbCollectionArg) {
|
|
||||||
this.dbTablesMap.add(dbCollectionArg);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
exports.Db = Db;
|
exports.Db = Db;
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEuY2xhc3Nlcy5kYi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBQUEsK0NBQThDO0FBQzlDLDZCQUErQjtBQVcvQjtJQU9FLFlBQVksb0JB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sQ0FBQztZQUMxQixPQUFPLENBQUMsU0FBUyxDQUFDLEVBQUUsQ0FBQyx5QkFBeUIsSUFBSSxDQUFDLE1BQU0sRUFBRSxDQUFDLENBQUM7UUFDL0QsQ0FBQztLQUFBO0lBRUQ7O09BRUc7SUFDRyxLQUFLOztZQUNULE1BQU0sSUFBSSxDQUFDLFlBQVksQ0FBQyxLQUFLLEVBQUUsQ0FBQztZQUNoQyxJQUFJLENBQUMsTUFBTSxHQUFHLGNBQWMsQ0FBQztZQUM3QixPQUFPLENBQUMsU0FBUyxDQUFDLEVBQUUsQ0FBQyw4QkFBOEIsSUFBSSxDQUFDLE1BQU0sRUFBRSxDQUFDLENBQUM7UUFDcEUsQ0FBQztLQUFBO0lBRUQscUNBQXFDO0lBRXJDLFFBQVEsQ0FBQyxVQUF3QjtRQUMvQixJQUFJLENBQUMsV0FBVyxDQUFDLEdBQUcsQ0FBQyxVQUFVLENBQUMsQ0FBQztJQUNuQyxDQUFDO0lBRUQ7Ozs7T0FJRztJQUNHLGdCQUFnQixDQUFJLE9BQWU7O1lBQ3ZDLElBQUksZ0JBQWdCLEdBQUcsSUFBSSxDQUFDLFdBQVcsQ0FBQyxJQUFJLENBQUMsVUFBVSxDQUFDLEVBQUU7Z0JBQ3hELE1BQU0sQ0FBQyxVQUFVLENBQUMsU0FBUyxLQUFLLE9BQU8sQ0FBQztZQUMxQyxDQUFDLENBQUMsQ0FBQztZQUNILE1BQU0sQ0FBQyxnQkFBZ0IsQ0FBQztRQUMxQixDQUFDO0tBQUE7Q0FDRjtBQXBFRCxnQkFvRUMifQ==
|
103
dist/smartdata.classes.dbcollection.js
vendored
103
dist/smartdata.classes.dbcollection.js
vendored
@ -1,103 +0,0 @@
|
|||||||
"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");
|
|
||||||
function Collection(db) {
|
|
||||||
return function (constructor) {
|
|
||||||
constructor['dbCollection'] = new DbTable(constructor, db);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
exports.Collection = Collection;
|
|
||||||
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() {
|
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
|
||||||
yield this.init();
|
|
||||||
return yield plugins.rethinkDb.table(this.tableName).filter({}).run(this.db.dbConnection);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGJjb2xsZWN0aW9uLmpz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0FBQyxDQUFDO1lBQ3JCLElBQUksQ0FBQyxPQUFPLEVBQUUsQ0FBQTtRQUNoQixDQUFDO1FBQUMsSUFBSSxDQUFDLENBQUM7WUFDTixJQUFJLENBQUMsTUFBTSxDQUFDLG1DQUFtQyxDQUFDLENBQUE7UUFDbEQsQ0FBQztRQUNELE1BQU0sQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFBO0lBQ3JCLENBQUM7SUFFRCxVQUFVLENBQUUsV0FBd0I7SUFFcEMsQ0FBQztDQUNGO0FBakdELDBCQWlHQyJ9
|
|
7
dist/smartdata.classes.dbdoc.d.ts
vendored
7
dist/smartdata.classes.dbdoc.d.ts
vendored
@ -1,6 +1,6 @@
|
|||||||
import { Objectmap } from 'lik';
|
import { Objectmap } from "lik";
|
||||||
import { DbTable } from './smartdata.classes.dbcollection';
|
import { DbTable } from "./smartdata.classes.dbtable";
|
||||||
export declare type TDocCreation = 'db' | 'new' | 'mixed';
|
export declare type TDocCreation = "db" | "new" | "mixed";
|
||||||
/**
|
/**
|
||||||
* saveable - saveable decorator to be used on class properties
|
* saveable - saveable decorator to be used on class properties
|
||||||
*/
|
*/
|
||||||
@ -30,6 +30,7 @@ export declare class DbDoc<T> {
|
|||||||
* class constructor
|
* class constructor
|
||||||
*/
|
*/
|
||||||
constructor();
|
constructor();
|
||||||
|
static getInstances<T>(filterArg: any): Promise<T[]>;
|
||||||
/**
|
/**
|
||||||
* saves this instance but not any connected items
|
* saves this instance but not any connected items
|
||||||
* may lead to data inconsistencies, but is faster
|
* may lead to data inconsistencies, but is faster
|
||||||
|
36
dist/smartdata.classes.dbdoc.js
vendored
36
dist/smartdata.classes.dbdoc.js
vendored
@ -14,7 +14,7 @@ const lik_1 = require("lik");
|
|||||||
*/
|
*/
|
||||||
function svDb() {
|
function svDb() {
|
||||||
return (target, key) => {
|
return (target, key) => {
|
||||||
console.log('called sva');
|
console.log("called sva");
|
||||||
if (!target.saveableProperties) {
|
if (!target.saveableProperties) {
|
||||||
target.saveableProperties = [];
|
target.saveableProperties = [];
|
||||||
}
|
}
|
||||||
@ -30,9 +30,27 @@ class DbDoc {
|
|||||||
/**
|
/**
|
||||||
* how the Doc in memory was created, may prove useful later.
|
* how the Doc in memory was created, may prove useful later.
|
||||||
*/
|
*/
|
||||||
this.creationStatus = 'new';
|
this.creationStatus = "new";
|
||||||
this.name = this.constructor['name'];
|
this.name = this.constructor["name"];
|
||||||
this.collection = this.constructor['dbCollection'];
|
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;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* saves this instance but not any connected items
|
* saves this instance but not any connected items
|
||||||
@ -42,15 +60,15 @@ class DbDoc {
|
|||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
let self = this;
|
let self = this;
|
||||||
switch (this.creationStatus) {
|
switch (this.creationStatus) {
|
||||||
case 'db':
|
case "db":
|
||||||
yield this.collection.update(self);
|
yield this.collection.update(self);
|
||||||
break;
|
break;
|
||||||
case 'new':
|
case "new":
|
||||||
let writeResult = yield this.collection.insert(self);
|
let writeResult = yield this.collection.insert(self);
|
||||||
this.creationStatus = 'db';
|
this.creationStatus = "db";
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.error('neither new nor in db?');
|
console.error("neither new nor in db?");
|
||||||
}
|
}
|
||||||
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Q0FBQyxDQUFDLENBQUM7Z0JBQ3ZFLFFBQVEsQ0FBQyxRQUFRLENBQUMsV0FBVyxDQUFDLENBQUM7WUFDakMsQ0FBQztRQUNILENBQUM7SUFDSCxDQUFDO0lBRUQsbUJBQW1CO1FBQ2pCLElBQUksY0FBYyxHQUFRLEVBQUUsQ0FBQyxDQUFDLCtDQUErQztRQUM3RSxHQUFHLENBQUMsQ0FBQyxJQUFJLGtCQUFrQixJQUFJLElBQUksQ0FBQyxrQkFBa0IsQ0FBQyxDQUFDLENBQUM7WUFDdkQsY0FBYyxDQUFDLGtCQUFrQixDQUFDLEdBQUcsSUFBSSxDQUFDLGtCQUFrQixDQUFDLENBQUM7UUFDaEUsQ0FBQztRQUNELE1BQU0sQ0FBQyxjQUFjLENBQUM7SUFDeEIsQ0FBQztDQUNGO0FBL0ZELHNCQStGQyJ9
|
@ -1,21 +1,27 @@
|
|||||||
import * as plugins from './smartdata.plugins';
|
import * as plugins from "./smartdata.plugins";
|
||||||
import { Db } from './smartdata.classes.db';
|
import { Db } from "./smartdata.classes.db";
|
||||||
import { DbDoc } from './smartdata.classes.dbdoc';
|
import { DbDoc } from "./smartdata.classes.dbdoc";
|
||||||
import { WriteResult, Cursor } from 'rethinkdb';
|
import { WriteResult } from "rethinkdb";
|
||||||
export interface IFindOptions {
|
export interface IFindOptions {
|
||||||
limit?: number;
|
limit?: number;
|
||||||
}
|
}
|
||||||
export interface IDocValidation<T> {
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
export interface IDocValidationFunc<T> {
|
||||||
(doc: T): boolean;
|
(doc: T): boolean;
|
||||||
}
|
}
|
||||||
export declare function Collection(db: Db): (constructor: any) => void;
|
/**
|
||||||
|
* 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> {
|
export declare class DbTable<T> {
|
||||||
/**
|
/**
|
||||||
* the collection that is used, defaults to mongodb collection,
|
* the collection that is used
|
||||||
* can be nedb datastore (sub api of mongodb)
|
|
||||||
*/
|
*/
|
||||||
table: plugins.rethinkDb.Table;
|
table: plugins.rethinkDb.Table;
|
||||||
objectValidation: IDocValidation<T>;
|
objectValidation: IDocValidationFunc<T>;
|
||||||
tableName: string;
|
tableName: string;
|
||||||
db: Db;
|
db: Db;
|
||||||
constructor(collectedClassArg: T & DbDoc<T>, dbArg: Db);
|
constructor(collectedClassArg: T & DbDoc<T>, dbArg: Db);
|
||||||
@ -23,11 +29,11 @@ export declare class DbTable<T> {
|
|||||||
/**
|
/**
|
||||||
* adds a validation function that all newly inserted and updated objects have to pass
|
* adds a validation function that all newly inserted and updated objects have to pass
|
||||||
*/
|
*/
|
||||||
addDocValidation(funcArg: IDocValidation<T>): void;
|
addDocValidation(funcArg: IDocValidationFunc<T>): void;
|
||||||
/**
|
/**
|
||||||
* finds an object in the DbCollection
|
* finds an object in the DbCollection
|
||||||
*/
|
*/
|
||||||
find(): Promise<Cursor>;
|
find(filterObject: any): Promise<any>;
|
||||||
/**
|
/**
|
||||||
* create an object in the database
|
* create an object in the database
|
||||||
*/
|
*/
|
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGJ0YWJsZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL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0FBQyxDQUFDO1FBQ25ELENBQUM7UUFDRCxFQUFFLENBQUMsQ0FBQyxnQkFBZ0IsQ0FBQyxDQUFDLENBQUM7WUFDckIsSUFBSSxDQUFDLE9BQU8sRUFBRSxDQUFDO1FBQ2pCLENBQUM7UUFBQyxJQUFJLENBQUMsQ0FBQztZQUNOLElBQUksQ0FBQyxNQUFNLENBQUMsbUNBQW1DLENBQUMsQ0FBQztRQUNuRCxDQUFDO1FBQ0QsTUFBTSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUM7SUFDdEIsQ0FBQztJQUVELFVBQVUsQ0FBQyxXQUF3QixJQUFHLENBQUM7Q0FDeEM7QUFsR0QsMEJBa0dDIn0=
|
12
dist/smartdata.plugins.d.ts
vendored
12
dist/smartdata.plugins.d.ts
vendored
@ -1,7 +1,7 @@
|
|||||||
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 rethinkDb from "rethinkdb";
|
||||||
import * as smartq from 'smartq';
|
import * as smartq from "smartq";
|
||||||
import * as smartstring from 'smartstring';
|
import * as smartstring from "smartstring";
|
||||||
export { assert, beautylog, lodash, smartq, rethinkDb, smartstring };
|
export { assert, beautylog, lodash, smartq, rethinkDb, smartstring };
|
||||||
|
2
dist/smartdata.plugins.js
vendored
2
dist/smartdata.plugins.js
vendored
@ -12,4 +12,4 @@ const smartq = require("smartq");
|
|||||||
exports.smartq = smartq;
|
exports.smartq = smartq;
|
||||||
const smartstring = require("smartstring");
|
const smartstring = require("smartstring");
|
||||||
exports.smartstring = smartstring;
|
exports.smartstring = smartstring;
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLnBsdWdpbnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEucGx1Z2lucy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLGlDQUFnQztBQVE1Qix3QkFBTTtBQVBWLHVDQUFzQztBQVFsQyw4QkFBUztBQVBiLGlDQUFnQztBQVE1Qix3QkFBTTtBQVBWLHVDQUFzQztBQVNsQyw4QkFBUztBQVJiLGlDQUFnQztBQU81Qix3QkFBTTtBQU5WLDJDQUEwQztBQVF0QyxrQ0FBVyJ9
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLnBsdWdpbnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEucGx1Z2lucy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLGlDQUFpQztBQU94Qix3QkFBTTtBQU5mLHVDQUF1QztBQU10Qiw4QkFBUztBQUwxQixpQ0FBaUM7QUFLTCx3QkFBTTtBQUpsQyx1Q0FBdUM7QUFJSyw4QkFBUztBQUhyRCxpQ0FBaUM7QUFHRyx3QkFBTTtBQUYxQywyQ0FBMkM7QUFFWSxrQ0FBVyJ9
|
10
test/test.ts
10
test/test.ts
@ -34,8 +34,9 @@ tap.test('should establish a connection to the rethink Db cluster', async () =>
|
|||||||
// Collections
|
// Collections
|
||||||
// ------
|
// ------
|
||||||
|
|
||||||
@smartdata.Collection(testDb)
|
@smartdata.Table(testDb)
|
||||||
class Car extends smartdata.DbDoc<Car> {
|
class Car extends smartdata.DbDoc<Car> {
|
||||||
|
|
||||||
@smartdata.svDb() color: string
|
@smartdata.svDb() color: string
|
||||||
@smartdata.svDb() brand: string
|
@smartdata.svDb() brand: string
|
||||||
constructor (colorArg: string, brandArg: string) {
|
constructor (colorArg: string, brandArg: string) {
|
||||||
@ -50,6 +51,13 @@ tap.test('should save the car to the db', async () => {
|
|||||||
await myCar.save()
|
await myCar.save()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
tap.test('expect to get instance of Car', async () => {
|
||||||
|
let myCar = await Car.getInstances<Car>({
|
||||||
|
brand: 'Volvo'
|
||||||
|
})
|
||||||
|
expect(myCar[0].color).to.equal('red')
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
// =======================================
|
// =======================================
|
||||||
// close the database connection
|
// close the database connection
|
||||||
|
@ -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";
|
||||||
|
@ -1,41 +1,47 @@
|
|||||||
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.dbcollection'
|
import { DbTable } from "./smartdata.classes.dbtable";
|
||||||
|
|
||||||
import { Connection as dbConnection, ConnectionOptions } from 'rethinkdb'
|
import { Connection as dbConnection, ConnectionOptions } from "rethinkdb";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* interface - indicates the connection status of the db
|
* interface - indicates the connection status of the db
|
||||||
*/
|
*/
|
||||||
export type TConnectionStatus = 'initial' | 'disconnected' | 'connected' | 'failed'
|
export type TConnectionStatus =
|
||||||
|
| "initial"
|
||||||
|
| "disconnected"
|
||||||
|
| "connected"
|
||||||
|
| "failed";
|
||||||
|
|
||||||
export class Db {
|
export class Db {
|
||||||
dbName: string
|
dbName: string;
|
||||||
connectionOptions: plugins.rethinkDb.ConnectionOptions
|
connectionOptions: plugins.rethinkDb.ConnectionOptions;
|
||||||
dbConnection: plugins.rethinkDb.Connection
|
dbConnection: plugins.rethinkDb.Connection;
|
||||||
status: TConnectionStatus
|
status: TConnectionStatus;
|
||||||
dbTablesMap = new Objectmap<DbTable<any>>()
|
dbTablesMap = new Objectmap<DbTable<any>>();
|
||||||
|
|
||||||
constructor(connectionOptionsArg: ConnectionOptions) {
|
constructor(connectionOptionsArg: ConnectionOptions) {
|
||||||
this.dbName = connectionOptionsArg.db
|
this.dbName = connectionOptionsArg.db;
|
||||||
this.connectionOptions = connectionOptionsArg
|
this.connectionOptions = connectionOptionsArg;
|
||||||
this.status = 'initial'
|
this.status = "initial";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* supply additional SSl options
|
* supply additional SSl options needed to connect to certain Rethink DB servers (e.g. compose.io)
|
||||||
*/
|
*/
|
||||||
setSsl (certificateStringArg: string, formatArg: 'base64' | 'clearText') {
|
setSsl(certificateStringArg: string, formatArg: "base64" | "clearText") {
|
||||||
let certificateString: string
|
let certificateString: string;
|
||||||
if(formatArg = 'base64') {
|
if ((formatArg = "base64")) {
|
||||||
certificateString = plugins.smartstring.base64.decode(certificateStringArg)
|
certificateString = plugins.smartstring.base64.decode(
|
||||||
|
certificateStringArg
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
certificateString = certificateStringArg
|
certificateString = certificateStringArg;
|
||||||
}
|
}
|
||||||
this.connectionOptions['ssl'] = {
|
this.connectionOptions["ssl"] = {
|
||||||
ca: Buffer.from(certificateString)
|
ca: Buffer.from(certificateString)
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// basic connection stuff ----------------------------------------------
|
// basic connection stuff ----------------------------------------------
|
||||||
@ -44,36 +50,36 @@ export class Db {
|
|||||||
* connects to the database that was specified during instance creation
|
* connects to the database that was specified during instance creation
|
||||||
*/
|
*/
|
||||||
async connect(): Promise<any> {
|
async connect(): Promise<any> {
|
||||||
this.dbConnection = await plugins.rethinkDb.connect(this.connectionOptions)
|
this.dbConnection = await plugins.rethinkDb.connect(this.connectionOptions);
|
||||||
this.dbConnection.use(this.dbName)
|
this.dbConnection.use(this.dbName);
|
||||||
this.status = 'connected'
|
this.status = "connected";
|
||||||
plugins.beautylog.ok(`Connected to database ${this.dbName}`)
|
plugins.beautylog.ok(`Connected to database ${this.dbName}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* closes the connection to the databse
|
* closes the connection to the databse
|
||||||
*/
|
*/
|
||||||
async close(): Promise<any> {
|
async close(): Promise<any> {
|
||||||
await this.dbConnection.close()
|
await this.dbConnection.close();
|
||||||
this.status = 'disconnected'
|
this.status = "disconnected";
|
||||||
plugins.beautylog.ok(`disconnected from database ${this.dbName}`)
|
plugins.beautylog.ok(`disconnected from database ${this.dbName}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle table to class distribution
|
// handle table to class distribution
|
||||||
|
|
||||||
|
addTable(dbTableArg: DbTable<any>) {
|
||||||
|
this.dbTablesMap.add(dbTableArg);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a table's name and returns smartdata's DbTable class
|
* Gets a table's name and returns smartdata's DbTable class
|
||||||
* @param nameArg
|
* @param nameArg
|
||||||
* @returns DbTable
|
* @returns DbTable
|
||||||
*/
|
*/
|
||||||
async getDbTableByName<T>(nameArg: string): Promise<DbTable<T>> {
|
async getDbTableByName<T>(nameArg: string): Promise<DbTable<T>> {
|
||||||
let resultCollection = this.dbTablesMap.find((dbCollectionArg) => {
|
let resultCollection = this.dbTablesMap.find(dbTableArg => {
|
||||||
return dbCollectionArg.tableName === nameArg
|
return dbTableArg.tableName === nameArg;
|
||||||
})
|
});
|
||||||
return resultCollection
|
return resultCollection;
|
||||||
}
|
|
||||||
|
|
||||||
addTable (dbCollectionArg: DbTable<any>) {
|
|
||||||
this.dbTablesMap.add(dbCollectionArg)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,119 +0,0 @@
|
|||||||
import * as plugins from './smartdata.plugins'
|
|
||||||
import { Db } from './smartdata.classes.db'
|
|
||||||
import { DbDoc } from './smartdata.classes.dbdoc'
|
|
||||||
|
|
||||||
// RethinkDb Interfaces
|
|
||||||
import { WriteResult, Cursor } from 'rethinkdb'
|
|
||||||
|
|
||||||
export interface IFindOptions {
|
|
||||||
limit?: number
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IDocValidation<T> {
|
|
||||||
(doc: T): boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
export function Collection (db: Db) {
|
|
||||||
return function (constructor) {
|
|
||||||
constructor[ 'dbCollection' ] = new DbTable(constructor, db)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class DbTable<T> {
|
|
||||||
/**
|
|
||||||
* the collection that is used, defaults to mongodb collection,
|
|
||||||
* can be nedb datastore (sub api of mongodb)
|
|
||||||
*/
|
|
||||||
table: plugins.rethinkDb.Table
|
|
||||||
objectValidation: IDocValidation<T> = null
|
|
||||||
tableName: string
|
|
||||||
db: Db
|
|
||||||
|
|
||||||
constructor (collectedClassArg: T & DbDoc<T>, dbArg: Db) {
|
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
async init() {
|
|
||||||
if(!this.table) {
|
|
||||||
// connect this instance to a RethinkDB table
|
|
||||||
const availableTables = await plugins.rethinkDb
|
|
||||||
.db(this.db.dbName)
|
|
||||||
.tableList()
|
|
||||||
.run(this.db.dbConnection)
|
|
||||||
if(availableTables.indexOf(this.tableName)) {
|
|
||||||
await 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: IDocValidation<T>) {
|
|
||||||
this.objectValidation = funcArg
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* finds an object in the DbCollection
|
|
||||||
*/
|
|
||||||
async find (): Promise<Cursor> {
|
|
||||||
await this.init()
|
|
||||||
return await plugins.rethinkDb.table(this.tableName).filter({
|
|
||||||
/* TODO: */
|
|
||||||
}).run(this.db.dbConnection)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* create an object in the database
|
|
||||||
*/
|
|
||||||
async insert (dbDocArg: T & DbDoc<T>): Promise<WriteResult> {
|
|
||||||
await this.init()
|
|
||||||
await this.checkDoc(dbDocArg)
|
|
||||||
return await plugins.rethinkDb.table(this.tableName).insert(
|
|
||||||
dbDocArg.createSavableObject()
|
|
||||||
).run(this.db.dbConnection)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* inserts object into the DbCollection
|
|
||||||
*/
|
|
||||||
async update (dbDocArg: T & DbDoc<T>): Promise<WriteResult> {
|
|
||||||
await this.init()
|
|
||||||
await this.checkDoc(dbDocArg)
|
|
||||||
console.log(this.tableName, dbDocArg.createSavableObject())
|
|
||||||
return await 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.
|
|
||||||
*/
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
extractKey (writeResult: WriteResult) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,56 +1,81 @@
|
|||||||
import * as plugins from './smartdata.plugins'
|
import * as plugins from "./smartdata.plugins";
|
||||||
|
|
||||||
import { Objectmap } from 'lik'
|
import { Objectmap } from "lik";
|
||||||
|
|
||||||
import { Db } from './smartdata.classes.db'
|
import { Db } from "./smartdata.classes.db";
|
||||||
import { DbTable } from './smartdata.classes.dbcollection'
|
import { DbTable } from "./smartdata.classes.dbtable";
|
||||||
|
|
||||||
export type TDocCreation = 'db' | 'new' | 'mixed'
|
export type TDocCreation = "db" | "new" | "mixed";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* saveable - saveable decorator to be used on class properties
|
* saveable - saveable decorator to be used on class properties
|
||||||
*/
|
*/
|
||||||
export function svDb() {
|
export function svDb() {
|
||||||
return (target: DbDoc<any>, key: string) => {
|
return (target: DbDoc<any>, key: string) => {
|
||||||
console.log('called sva')
|
console.log("called sva");
|
||||||
if (!target.saveableProperties) { target.saveableProperties = [] }
|
if (!target.saveableProperties) {
|
||||||
target.saveableProperties.push(key)
|
target.saveableProperties = [];
|
||||||
}
|
}
|
||||||
|
target.saveableProperties.push(key);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export class DbDoc<T> {
|
export class DbDoc<T> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* the collection object an Doc belongs to
|
* the collection object an Doc belongs to
|
||||||
*/
|
*/
|
||||||
collection: DbTable<T>
|
collection: DbTable<T>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* how the Doc in memory was created, may prove useful later.
|
* how the Doc in memory was created, may prove useful later.
|
||||||
*/
|
*/
|
||||||
creationStatus: TDocCreation = 'new'
|
creationStatus: TDocCreation = "new";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* an array of saveable properties of a doc
|
* an array of saveable properties of a doc
|
||||||
*/
|
*/
|
||||||
saveableProperties: string[]
|
saveableProperties: string[];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* name
|
* name
|
||||||
*/
|
*/
|
||||||
name: string
|
name: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* primary id in the database
|
* primary id in the database
|
||||||
*/
|
*/
|
||||||
dbId: string
|
dbId: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* class constructor
|
* class constructor
|
||||||
*/
|
*/
|
||||||
constructor() {
|
constructor() {
|
||||||
this.name = this.constructor['name']
|
this.name = this.constructor["name"];
|
||||||
this.collection = this.constructor[ 'dbCollection' ]
|
this.collection = this.constructor["dbTable"];
|
||||||
|
}
|
||||||
|
|
||||||
|
static async getInstances<T>(filterArg): Promise<T[]> {
|
||||||
|
let self: any = this; // fool typesystem
|
||||||
|
let referenceTable: DbTable<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]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -58,17 +83,17 @@ export class DbDoc<T> {
|
|||||||
* may lead to data inconsistencies, but is faster
|
* may lead to data inconsistencies, but is faster
|
||||||
*/
|
*/
|
||||||
async save() {
|
async save() {
|
||||||
let self: any = this
|
let self: any = this;
|
||||||
switch (this.creationStatus) {
|
switch (this.creationStatus) {
|
||||||
case 'db':
|
case "db":
|
||||||
await this.collection.update(self)
|
await this.collection.update(self);
|
||||||
break
|
break;
|
||||||
case 'new':
|
case "new":
|
||||||
let writeResult = await this.collection.insert(self)
|
let writeResult = await this.collection.insert(self);
|
||||||
this.creationStatus = 'db'
|
this.creationStatus = "db";
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.error('neither new nor in db?')
|
console.error("neither new nor in db?");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,23 +103,23 @@ export class DbDoc<T> {
|
|||||||
*/
|
*/
|
||||||
saveDeep(savedMapArg: Objectmap<DbDoc<any>> = null) {
|
saveDeep(savedMapArg: Objectmap<DbDoc<any>> = null) {
|
||||||
if (!savedMapArg) {
|
if (!savedMapArg) {
|
||||||
savedMapArg = new Objectmap<DbDoc<any>>()
|
savedMapArg = new Objectmap<DbDoc<any>>();
|
||||||
}
|
}
|
||||||
savedMapArg.add(this)
|
savedMapArg.add(this);
|
||||||
this.save()
|
this.save();
|
||||||
for (let propertyKey in this) {
|
for (let propertyKey in this) {
|
||||||
let property: any = this[ propertyKey ]
|
let property: any = this[propertyKey];
|
||||||
if (property instanceof DbDoc && !savedMapArg.checkForObject(property)) {
|
if (property instanceof DbDoc && !savedMapArg.checkForObject(property)) {
|
||||||
property.saveDeep(savedMapArg)
|
property.saveDeep(savedMapArg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
createSavableObject() {
|
createSavableObject() {
|
||||||
let saveableObject: any = {} // is not exposed to outside, so any is ok here
|
let saveableObject: any = {}; // is not exposed to outside, so any is ok here
|
||||||
for (let propertyNameString of this.saveableProperties) {
|
for (let propertyNameString of this.saveableProperties) {
|
||||||
saveableObject[ propertyNameString ] = this[ propertyNameString ]
|
saveableObject[propertyNameString] = this[propertyNameString];
|
||||||
}
|
}
|
||||||
return saveableObject
|
return saveableObject;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
127
ts/smartdata.classes.dbtable.ts
Normal file
127
ts/smartdata.classes.dbtable.ts
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
import * as plugins from "./smartdata.plugins";
|
||||||
|
import { Db } from "./smartdata.classes.db";
|
||||||
|
import { DbDoc } from "./smartdata.classes.dbdoc";
|
||||||
|
|
||||||
|
// RethinkDb Interfaces
|
||||||
|
import { WriteResult, Cursor } 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 function Table(db: Db) {
|
||||||
|
return function(constructor) {
|
||||||
|
constructor["dbTable"] = new DbTable(constructor, db);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export class DbTable<T> {
|
||||||
|
/**
|
||||||
|
* the collection that is used
|
||||||
|
*/
|
||||||
|
table: plugins.rethinkDb.Table;
|
||||||
|
objectValidation: IDocValidationFunc<T> = null;
|
||||||
|
tableName: string;
|
||||||
|
db: Db;
|
||||||
|
|
||||||
|
constructor(collectedClassArg: T & DbDoc<T>, dbArg: Db) {
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
async init() {
|
||||||
|
if (!this.table) {
|
||||||
|
// connect this instance to a RethinkDB table
|
||||||
|
const availableTables = await plugins.rethinkDb
|
||||||
|
.db(this.db.dbName)
|
||||||
|
.tableList()
|
||||||
|
.run(this.db.dbConnection);
|
||||||
|
if (availableTables.indexOf(this.tableName)) {
|
||||||
|
await 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: IDocValidationFunc<T>) {
|
||||||
|
this.objectValidation = funcArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* finds an object in the DbCollection
|
||||||
|
*/
|
||||||
|
async find(filterObject: any): Promise<any> {
|
||||||
|
await this.init();
|
||||||
|
let cursor = await plugins.rethinkDb
|
||||||
|
.table(this.tableName)
|
||||||
|
.filter(filterObject)
|
||||||
|
.run(this.db.dbConnection);
|
||||||
|
return await cursor.toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* create an object in the database
|
||||||
|
*/
|
||||||
|
async insert(dbDocArg: T & DbDoc<T>): Promise<WriteResult> {
|
||||||
|
await this.init();
|
||||||
|
await this.checkDoc(dbDocArg);
|
||||||
|
return await plugins.rethinkDb
|
||||||
|
.table(this.tableName)
|
||||||
|
.insert(dbDocArg.createSavableObject())
|
||||||
|
.run(this.db.dbConnection);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* inserts object into the DbCollection
|
||||||
|
*/
|
||||||
|
async update(dbDocArg: T & DbDoc<T>): Promise<WriteResult> {
|
||||||
|
await this.init();
|
||||||
|
await this.checkDoc(dbDocArg);
|
||||||
|
console.log(this.tableName, dbDocArg.createSavableObject());
|
||||||
|
return await 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.
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
extractKey(writeResult: WriteResult) {}
|
||||||
|
}
|
@ -1,15 +1,8 @@
|
|||||||
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 rethinkDb from "rethinkdb";
|
||||||
import * as smartq from 'smartq'
|
import * as smartq from "smartq";
|
||||||
import * as smartstring from 'smartstring'
|
import * as smartstring from "smartstring";
|
||||||
|
|
||||||
export {
|
export { assert, beautylog, lodash, smartq, rethinkDb, smartstring };
|
||||||
assert,
|
|
||||||
beautylog,
|
|
||||||
lodash,
|
|
||||||
smartq,
|
|
||||||
rethinkDb,
|
|
||||||
smartstring
|
|
||||||
}
|
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": "tslint-config-standard"
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user