Compare commits

..

8 Commits

Author SHA1 Message Date
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
8 changed files with 147 additions and 14 deletions

View File

@ -2,12 +2,28 @@
> Note: Still in Beta > 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. smartdata is an ODM that adheres to TypeScript practices and uses classes to organize data.
It uses MongoDB or NeDb as persistent storage. It uses MongoDB or NeDb as persistent storage.
## Intention ## Intention
There are many ODMs out there, however when we searched for an ODM that uses TypeScript, There are many ODMs out there, however when we searched for an ODM that uses TypeScript,
acts smart while still embracing an the NoSQL idea... we didn't find a matching solution. acts smart while still embracing the NoSQL idea we didn't find a matching solution.
This is why we started smartdata. This is why we started smartdata.
How MongoDB terms map to smartdata classes How MongoDB terms map to smartdata classes
@ -47,11 +63,11 @@ So to get to get access to a specific collection you document
@Collection(myDb1) @Collection(myDb1)
class myObject extends smartdata.DbDoc<myObject> { // read the next block about DbDoc class myObject extends smartdata.DbDoc<myObject> { // read the next block about DbDoc
property1:string @smartdata.saveable property1: string // @smartdata.saveable marks the property for db save
property2:number property2: number // this one is not marked, so it won't be save upon calling this.save()
constructor(optionsArg:{ constructor(optionsArg:{
property1:string, property1: string,
property2:number property2: number
}) { }) {
super() super()
} }

View File

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

View File

@ -1,12 +1,46 @@
"use strict"; "use strict";
/**
* saveable - saveable decorator to be used on class properties
*/
function saveable(target, key) {
console.log('called sva');
if (!target.saveableProperties) {
target.saveableProperties = [];
}
target.saveableProperties.push(key);
}
exports.saveable = saveable;
class DbDoc { class DbDoc {
/**
* class constructor
*/
constructor() { constructor() {
this.collection = this.constructor['dbCollection']; this.collection = this.constructor['dbCollection'];
} }
/**
* saves this instance but not any connected items
* may lead to data inconsistencies, but is faster
*/
save() { 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() { saveDeep() {
this.save();
} }
} }
exports.DbDoc = DbDoc; exports.DbDoc = DbDoc;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGJkb2MuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEuY2xhc3Nlcy5kYmRvYy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBUUE7SUFHSTtRQUNJLElBQUksQ0FBQyxVQUFVLEdBQUcsSUFBSSxDQUFDLFdBQVcsQ0FBQyxjQUFjLENBQUMsQ0FBQTtJQUN0RCxDQUFDO0lBQ0QsSUFBSTtJQUVKLENBQUM7SUFDRCxRQUFRO0lBRVIsQ0FBQztDQUNKO0FBWkQsc0JBWUMifQ== //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGJkb2MuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGRhdGEuY2xhc3Nlcy5kYmRvYy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBT0E7O0dBRUc7QUFDS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UFFLENBQUE7SUFDZixDQUFDO0NBQ0o7QUFqREQsc0JBaURDIn0=

View File

@ -1,6 +1,6 @@
{ {
"name": "smartdata", "name": "smartdata",
"version": "1.0.17", "version": "1.0.21",
"description": "do more with data", "description": "do more with data",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "dist/index.d.ts", "typings": "dist/index.d.ts",

File diff suppressed because one or more lines are too long

View File

@ -72,6 +72,7 @@ describe('smartdata', function () {
it('should create an extended class', function () { it('should create an extended class', function () {
@smartdata.Collection(testDb) @smartdata.Collection(testDb)
class TestCar extends smartdata.DbDoc<TestCar> { class TestCar extends smartdata.DbDoc<TestCar> {
@smartdata.saveable
color: string color: string
constructor(optionsArg: { constructor(optionsArg: {
color: string, color: string,
@ -85,6 +86,8 @@ describe('smartdata', function () {
color: 'red', color: 'red',
property2: 2 property2: 2
}) })
should(testCarInstance.saveableProperties[0]).equal('color')
console.log(TestCar) console.log(TestCar)
should(testCarInstance.collection).be.instanceof(smartdata.DbCollection) should(testCarInstance.collection).be.instanceof(smartdata.DbCollection)
should(testCarInstance).be.instanceof(smartdata.DbDoc) should(testCarInstance).be.instanceof(smartdata.DbDoc)

View File

@ -3,19 +3,64 @@ import * as plugins from './smartdata.plugins'
import { Db } from './smartdata.classes.db' import { Db } from './smartdata.classes.db'
import { DbCollection } from './smartdata.classes.dbcollection' import { DbCollection } from './smartdata.classes.dbcollection'
export type TDocCreation = 'db' | 'data' | 'mixed' export type TDocCreation = 'db' | 'new' | 'mixed'
/**
* saveable - saveable decorator to be used on class properties
*/
export function saveable(target: DbDoc<any>, key: string) {
console.log('called sva')
if (!target.saveableProperties) { target.saveableProperties = [] }
target.saveableProperties.push(key)
}
export class DbDoc<T> { export class DbDoc<T> {
/**
* the collection object an Doc belongs to
*/
collection: DbCollection<T> collection: DbCollection<T>
/**
* how the Doc in memory was created, may prove useful later.
*/
creationType: TDocCreation creationType: TDocCreation
/**
* an array of saveable properties of a doc
*/
saveableProperties: string[]
/**
* class constructor
*/
constructor() { constructor() {
this.collection = this.constructor['dbCollection'] this.collection = this.constructor['dbCollection']
} }
/**
* saves this instance but not any connected items
* may lead to data inconsistencies, but is faster
*/
save() { 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)
}
} }
saveDeep() {
/**
* also store any referenced objects to DB
* better for data consistency
*/
saveDeep() {
this.save()
} }
} }

5
tsconfig.json Normal file
View File

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