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
2022-05-17 21:30:53 +02:00
.vscode fix(core): update 2021-09-17 22:34:15 +02:00
test fix(core): update 2022-05-17 21:26:17 +02:00
ts BREAKING CHANGE(core): switch to esm 2022-05-17 21:30:53 +02:00
.gitignore update module structure 2020-06-11 23:05:32 +00:00
.gitlab-ci.yml fix(core): update 2021-11-12 19:02:29 +01:00
npmextra.json update module structure 2020-06-11 23:05:32 +00:00
package-lock.json 5.0.0 2022-05-17 21:30:53 +02:00
package.json 5.0.0 2022-05-17 21:30:53 +02:00
qenv.yml fix(core): update 2019-01-07 02:38:30 +01:00
readme.md fix(core): update 2021-10-16 21:17:02 +02:00
tsconfig.json fix(core): update 2022-05-17 00:33:44 +02:00

@pushrocks/smartdata

do more with data

Status for master

Status Category Status Badge
GitLab Pipelines pipeline status
GitLab Pipline Test Coverage coverage report
npm npm downloads per month
Snyk Known Vulnerabilities
TypeScript Support TypeScript
node Support node
Code Style Code Style
PackagePhobia (total standalone install weight) PackagePhobia
PackagePhobia (package size on registry) PackagePhobia
BundlePhobia (total size when bundled) BundlePhobia
Platform support Supports Windows 10 Supports Mac OS X

Usage

Use TypeScript for best in class instellisense.

smartdata is an ODM that adheres to TypeScript practices and uses classes to organize data. It uses RethinkDB 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 RethinkDB's terms map to the ones of smartdata:

MongoDb term smartdata class
Database smartdata.SmartdataDb
Collection smartdata.SmartdataCollection
Document smartdata.SmartadataDoc

class Db

represents a Database. Naturally it has .connect() etc. methods on it.

// Assuming toplevel await
import * as smartdata from 'smartdata';

const smartdataDb = new smartdata.SmartdataDb({
  mongoDbUrl: '//someurl',
  mongoDbName: 'myDatabase',
  mongoDbPass: 'mypassword',
});

await smartdataDb.connect();

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

// Assuming toplevel await
// continues from the block before...

@smartdata.Collection(smartdataDb)
class MyObject extends smartdata.DbDoc<MyObject /* ,[an optional interface to implement] */> {
  // read the next block about DbDoc
  @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()

  constructor() {
    super(); // the super call is important ;) But you probably know that.
  }
}

// start to instantiate instances of classes from scratch or database

const localObject = new MyObject({
  property1: 'hi',
  property2: {
    deep: 3,
  },
});
await localObject.save(); // saves the object to the database

// start retrieving instances

// .getInstance is staticly inheritied, yet fully typed static function to get instances with fully typed filters
const myInstance = await MyObject.getInstance({
  property1: 'hi',
  property2: {
    deep: {
      $gt: 2,
    } as any,
  },
}); // outputs a new instance of MyObject with the values from db assigned

class DbDoc

represents a individual document in a collection and thereby is ideally suited to extend the class you want to actually store.

CRUD operations

smartdata supports full CRUD operations

Store or Update instances of classes to MongoDB: DbDoc extends your class with the following methods:

  • 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(). 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

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.

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. You don't need to install any additional types for smartdata.

Contribution

We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can contribute one time or contribute monthly. :)

For further information read the linked docs at the top of this readme.

MIT licensed | © Lossless GmbH | By using this npm module you agree to our privacy policy

repo-footer