https://github.com/bdfoster/nomatic-data
Extensible Object-relational Mapping Framework for Node.js
https://github.com/bdfoster/nomatic-data
Last synced: 11 months ago
JSON representation
Extensible Object-relational Mapping Framework for Node.js
- Host: GitHub
- URL: https://github.com/bdfoster/nomatic-data
- Owner: bdfoster
- License: mit
- Created: 2017-09-11T10:45:43.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2020-06-03T03:27:38.000Z (about 6 years ago)
- Last Synced: 2025-07-02T17:01:06.034Z (12 months ago)
- Language: TypeScript
- Homepage: https://bdfoster.github.io/nomatic-data
- Size: 636 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# nomatic-data
[](https://greenkeeper.io/)
[](https://github.com/semantic-release/semantic-release)
[](https://github.com/bdfoster/nomatic-data/releases)
[](https://www.npmjs.com/package/nomatic-data)
[](https://travis-ci.org/bdfoster/nomatic-data)
[](https://coveralls.io/github/bdfoster/nomatic-data)
[](https://snyk.io/test/github/bdfoster/nomatic-data)
[](https://david-dm.org/bdfoster/nomatic-data)
[](https://david-dm.org/bdfoster/nomatic-data?type=dev)
[](https://github.com/bdfoster/nomatic-data/blob/master/LICENSE)
[](https://gitter.im/nomatic-data/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
Extensible Object-relational Mapping Framework for Node.js
### Overview
Written in TypeScript, this package uses Active Record, Data Mapper, and Adapter
patterns to create an Object-Relational Mapping (ORM) tool that provides flexibility and extensibility.
Adapters allow you to connect to a variety of data sources. You can use a pre-built Adapter (see table below),
or you can write your own by implementing either the [Adapter](https://bdfoster.github.io/nomatic-data/classes/adapter.html) or [DatabaseAdapter](https://bdfoster.github.io/nomatic-data/classes/databaseadapter.html) abstract classes.
Each `Adapter` is operated by one or more `Mapper` instances. Each `Mapper` instance is managed by a `Container`
instance. A `Mapper` will generate `Record` instances, which store the state of each document in the collection.
### Installation
You can install from [npm](https://www.npmjs.com/package/nomatic-data) by doing:
```
npm i --save nomatic-data
```
If you want to write your own adapter, you can stop there. Otherwise, you'll need to install an adapter:
| Adapter | Author | Links | Installation |
| :--- | :--- | :--- | :--- |
| [ArangoDB](https://arangodb.com) | [bdfoster](https://github.com/bdfoster) | [npm](https://npmjs.com/package/nomatic-arangodb-adapter), [GitHub](https://github.com/bdfoster/nomatic-arangodb-adapter) | `npm i --save nomatic-arangodb-adapter` |
### Example
This example uses the [ArangoDB](https://npmjs.com/package/nomatic-arangodb-adapter) adapter.
```javascript
import { Container } from 'nomatic-data';
import ArangoDBAdapter from 'nomatic-arangodb-adapter';
const adapter = new ArangoDBAdapter({
name: 'my-database',
host: '127.0.0.1',
port: 8579,
password: 'somethingMoreSecureThanThis'
});
const store = new Container({
adapter: adapter,
/**
* A few hooks are provided for your convenience, including:
* - beforeGet
* - afterGet
* - beforeInsert
* - afterInsert
* - beforeUpdate
* - afterUpdate
* - beforeValidate
* - afterValidate
*/
beforeInsert(mapper, record) {
record.createdAt = new Date();
},
beforeUpdate(mapper, record) {
record.updatedAt = new Date();
},
/**
* Here, we define the schema of all of our mappers. Other options
* for each mapper can also be set here. Schema validation is provided
* by [ajv](https://github.com/epoberezkin/ajv).
*/
mappers: {
people: {
properties: {
/**
* Implicit properties, such as `id` and `rev`, need not be
* defined here.
*/
firstName: {
type: 'string'
},
lastName: {
type: 'string'
},
emailAddress: {
type: 'string',
format: 'email'
}
},
required: ['firstName', 'lastName'],
/**
* By default, additional properties are allowed in the record.
*/
additionalProperties: false
},
accounts: {
properties: {
people: {
type: 'array',
items: {
type: 'string',
/**
* The 'mapper' keyword enforces relational integrity. Each item
* in this array matches an `id` of a record in the collection
* managed by the 'people' mapper.
*/
mapper: 'people'
},
default: []
}
}
}
}
});
/**
* The `load()` method will establish a connection with the database server,
* ensure that the database exists (or it will try and create it), and
* ensure all collection exists (or will create them).
*/
store.load().then(() => {
return store.insertAll('people', [
{ firstName: 'John', lastName: 'Doe' },
{ firstName: 'Jane', lastName: 'Doe' }
]);
}).then(() => {
/**
* This is one way you can query for documents.
*/
store.find('people')
.where('lastName')
.eq('Doe')
.sort('firstName', 1)
.limit(2)
.skip(1)
.run().then((results) => {
//...
});
/**
* You can also query much like how MongoDB query filters. The supported operators are:
* - $and
* Syntax: { $where: { $and: [ , ..., }, ... }
* - $or
* Syntax: { $where: { $or: [ , ..., }, ... }
* - $eq (expression)
* Syntax: { : { $eq: { } }
* - $ne (expression)
* Syntax: { : { $ne: { } }
* - $gt (expression)
* Syntax: { : { $gt: { } }
* - $gte (expression)
* Syntax: { : { $gte: { } }
* - $lt (expression)
* Syntax: { : { $lt: { } }
* - $lte (expression)
* Syntax: { : { $lte: { } }
* - $in (expression)
* Syntax: { : { $in: [ , ..., ] }
* - $nin (expression)
* Syntax: { : { $nin: [ , ..., ] }
* - $exists (expression)
* Syntax: { : { $exists: } }
*
* An expression always follows this form:
* { : { : | [ , ..., ] }
*
* All the operators above can only be used in a $where object.
*/
store.findAll('people', {
$where: {
firstName: 'Jane'
},
$sort: [
['firstName', 1]
],
$limit: 2,
$skip: 1
}).then((results) => {
//...
});
});
```
The API documentation (generated by [TypeDoc](http://typedoc.org/)) can be found
[here](https://bdfoster.github.io/nomatic-data/).
More documentation will be added as moves forward, but if you have a question please feel free to
[open an issue](https://github.com/bdfoster/nomatic-data/issues).