smartdata/README.md

75 lines
2.2 KiB
Markdown
Raw Normal View History

2016-09-11 16:03:47 +00:00
# smartdata
smartdata is a ODM that adheres to TypeScript practices and uses classes to organize data.
2016-09-12 20:11:17 +00:00
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
--- | ---
2016-09-12 21:47:57 +00:00
Database | smartdata.Db
2016-09-12 20:11:17 +00:00
Collection | smartdata.DbCollection
Document | smartdata.DbDoc
2016-09-12 21:47:57 +00:00
### class Db
represents a Database. Naturally it has .connect() etc. methods on it.
Since it is a class you can have multiple DBs defined.
2016-09-12 21:48:58 +00:00
```typescript
2016-09-12 21:47:57 +00:00
import * as smartdata from 'smartdata'
let myDb1 = new smartdata.Db('someConnectionUrl')
let myDb2 = new smartdata.Db('someConnectionUrl')
myDb1.connect()
myDb2.connect()
2016-09-12 22:04:07 +00:00
// continues in next block...
2016-09-12 21:47:57 +00:00
```
2016-09-12 22:13:00 +00:00
### class DbCollection
2016-09-12 21:47:57 +00:00
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
2016-09-12 21:48:58 +00:00
```typescript
2016-09-12 22:04:07 +00:00
// continues from the block before...
2016-09-12 22:13:00 +00:00
class myObject extends smartdata.DbDoc { // read the next block about DbDoc
2016-09-12 21:47:57 +00:00
property1:string
property2:number
constructor(optionsArg:{
queryArg?:any,
dataArg?:{
property1:string,
2016-09-12 22:04:07 +00:00
property2:number
2016-09-12 21:47:57 +00:00
}
}) {
super(this,optionsArg)
}
}
let myCollection = myDb1.getCollection(myObject)
```
> Alert: You NEVER instantiate a collection.
This is done for you!!!
### class DbDoc
2016-09-12 22:13:00 +00:00
represents a individual document in a collection
and thereby is ideally suited to extend the class you want to actually store.
2016-09-12 21:47:57 +00:00
2016-09-12 22:14:50 +00:00
DbDoc extends your class with .save() and saveDeep() methods.
2016-09-12 21:47:57 +00:00
2016-09-12 22:13:00 +00:00
So now we can get store instances of classes to Db...
How do we get a new class instances from a Doc in DB?
2016-09-12 22:14:50 +00:00
Easy! Take a look at the constructor. When you specify `optionsArg.queryArg`
2016-09-12 22:13:00 +00:00
smartdata will fill in the data from the database!
2016-09-12 22:14:50 +00:00
But when you specify a `optionsArg.dataArg` instead
the data is for the class is taken from there :)
2016-09-12 21:47:57 +00:00
2016-09-12 20:11:17 +00:00
[![npm](https://push.rocks/assets/repo-header.svg)](https://push.rocks)