smartdata/README.md

138 lines
5.2 KiB
Markdown
Raw Normal View History

# @pushrocks/smartdata
2019-02-13 21:08:58 +00:00
do more with data
2019-02-13 21:08:58 +00:00
## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartdata)
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartdata)
* [github.com (source mirror)](https://github.com/pushrocks/smartdata)
* [docs (typedoc)](https://pushrocks.gitlab.io/smartdata/)
2016-11-17 21:41:01 +00:00
## Status for master
2019-02-13 21:08:58 +00:00
[![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)
[![npm downloads per month](https://img.shields.io/npm/dm/@pushrocks/smartdata.svg)](https://www.npmjs.com/package/@pushrocks/smartdata)
[![Known Vulnerabilities](https://snyk.io/test/npm/@pushrocks/smartdata/badge.svg)](https://snyk.io/test/npm/@pushrocks/smartdata)
[![TypeScript](https://img.shields.io/badge/TypeScript->=%203.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
2019-09-02 14:51:22 +00:00
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-prettier-ff69b4.svg)](https://prettier.io/)
2016-11-17 21:41:01 +00:00
## Usage
Use TypeScript for best in class instellisense.
2016-09-12 21:47:57 +00:00
2018-01-07 16:43:02 +00:00
smartdata is an ODM that adheres to TypeScript practices and uses classes to organize data.
It uses RethinkDB as persistent storage.
## Intention
2018-01-07 16:43:02 +00:00
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 RethinkDB's terms map to the ones of smartdata:
2018-01-07 16:43:02 +00:00
2019-01-07 01:41:38 +00:00
| MongoDb term | smartdata class |
| ------------ | ----------------------------- |
| Database | smartdata.SmartdataDb |
| Collection | smartdata.SmartdataCollection |
| Document | smartdata.SmartadataDoc |
2018-01-07 16:43:02 +00:00
### class Db
2018-01-07 16:43:02 +00:00
represents a Database. Naturally it has .connect() etc. methods on it.
2018-01-14 16:57:57 +00:00
```typescript
import * as smartdata from 'smartdata';
2018-01-07 16:43:02 +00:00
2018-07-09 22:02:04 +00:00
const smartdataDb = new smartdata.SmartdataDb({
mongoDbUrl: '//someurl',
mongoDbName: 'myDatabase',
mongoDbPass: 'mypassword'
});
2018-01-07 16:43:02 +00:00
2018-07-09 22:02:04 +00:00
smartdataDb.connect();
2018-01-07 16:43:02 +00:00
```
### class DbCollection
2018-01-07 16:43:02 +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
2018-01-14 16:57:57 +00:00
```typescript
2018-01-07 16:43:02 +00:00
// continues from the block before...
2018-07-09 22:02:04 +00:00
@smartdata.Collection(smartdataDb)
class MyObject extends smartdata.DbDoc<MyObject> {
// read the next block about DbDoc
2019-09-04 15:41:53 +00:00
@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()
2019-09-04 15:41:53 +00:00
constructor() {
super(); // the super call is important ;) But you probably know that.
}
2018-01-07 16:43:02 +00:00
}
// start to instantiate instances of classes from scratch or database
2019-09-04 15:41:53 +00:00
const localObject = new MyObject({
property1: 'hi',
property2: 2
});
localObject.save(); // saves the object to the database
2018-01-07 16:43:02 +00:00
2018-01-14 16:57:57 +00:00
// start retrieving instances
MyObject.getInstance<MyObject>({
property: 'hi'
2018-01-14 16:57:57 +00:00
}); // outputs a new instance of MyObject with the values from db assigned
```
2018-01-07 16:43:02 +00:00
### class DbDoc
2018-01-07 16:43:02 +00:00
represents a individual document in a collection
and thereby is ideally suited to extend the class you want to actually store.
2019-09-04 15:25:50 +00:00
### CRUD operations
smartdata supports full CRUD operations
**Store** or **Update** instances of classes to MongoDB:
2018-01-07 16:43:02 +00:00
DbDoc extends your class with the following methods:
2019-09-04 15:25:50 +00:00
- async `.save()` will save (or update) the object you call it on only. Any referenced non-savable objects will not get stored.
- async `.saveDeep()` does the same like `.save()`.
2018-01-07 16:43:02 +00:00
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
2019-09-04 15:25:50 +00:00
**Get** a new class instance from MongoDB:
DbDoc exposes a static method that allows you specify a filter to retrieve a cloned class of the one you used to that doc at some point later in time:
* static async `.getInstance({ /* filter props here */ })` gets you an instance that has the data of the first matched document as properties.
* static async `getInstances({ /* filter props here */ })` get you an array instances (one instance for every matched document).
**Delete** instances from MongoDb:
smartdata extends your class with a method to easily delete the doucment from DB:
* async `.delete()`will delete the document from DB.
2018-01-14 17:03:57 +00:00
2018-01-07 16:43:02 +00:00
## TypeScript
2018-01-07 16:43:02 +00:00
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.
2019-09-04 15:25:50 +00:00
smartdata itself also bundles typings. You don't need to install any additional types for smartdata.
2018-01-07 16:43:02 +00:00
2019-02-13 21:08:58 +00:00
For further information read the linked docs at the top of this readme.
2016-09-13 08:23:46 +00:00
> MIT licensed | **&copy;** [Lossless GmbH](https://lossless.gmbh)
2019-09-02 14:51:22 +00:00
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
2016-09-12 21:47:57 +00:00
2019-09-02 14:51:22 +00:00
[![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com)