An advanced library for NoSQL data organization and manipulation using TypeScript with support for MongoDB, data validation, collections, and custom data types.
Go to file
2016-11-18 00:42:29 +01:00
dist now allows adding a saveable decorator 2016-11-18 00:42:25 +01:00
docs improve README 2016-09-12 22:11:17 +02:00
test now allows adding a saveable decorator 2016-11-18 00:42:25 +01:00
ts now allows adding a saveable decorator 2016-11-18 00:42:25 +01:00
.gitignore add tests and gitlab ci 2016-09-11 16:22:53 +02:00
.gitlab-ci.yml fix gitlab ci 2016-09-11 16:26:09 +02:00
package.json 1.0.19 2016-11-18 00:42:29 +01:00
README.md improve README 2016-11-17 22:41:01 +01:00
tsconfig.json now allows adding a saveable decorator 2016-11-18 00:42:25 +01:00
tslint.json add tests and gitlab ci 2016-09-11 16:22:53 +02:00

smartdata

Note: Still in Beta

Availabililty

npm git git docs

Status for master

build status coverage report Dependency Status bitHound Dependencies bitHound Code TypeScript node JavaScript Style Guide

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.

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

// continues from the block before...

@Collection(myDb1)
class myObject extends smartdata.DbDoc<myObject> { // read the next block about DbDoc
    property1:string
    property2:number
    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