Skip to content

Commit 25b4c71

Browse files
committed
Updated README.md
1 parent aa73f13 commit 25b4c71

2 files changed

Lines changed: 35 additions & 37 deletions

File tree

README.md

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# JoinJS
22

3-
JoinJS is a JavaScript library to map complex database joins to nested objects. It is a simpler alternative that gives you direct control over your database interactions compared to a full-blown Object-Relation Mapper (ORM).
3+
JoinJS is a JavaScript library to map complex database joins to nested objects. It's a simpler alternative to a full-blown Object-Relation Mapper (ORM), but gives you direct control over your database interactions.
44

55
## Motivation
6-
ORMs generally introduce a thick layer of abstraction between objects and the database which usually hinders rather than helps the productivity of the developer. In complex use cases, it is difficult enough to devise queries that will generate the desired results, but now you have to *teach* the ORM to generate the same query. For anyone who has worked with an ORM knows that that's extra time and you may not be able to produce the same query. In the worst case scenario, the ORM may hit the database multiple times for something that you were able to do in a single query.
6+
ORMs generally introduce a thick layer of abstraction between objects and database tables. This usually hinders rather than helps developer productivity. In complex use cases, it is difficult enough to devise efficient queries, but with ORMs, you also have to *teach* them to generate the same query. It takes extra time to do this and you may not be able to produce the same query. In the worst case scenario, the ORM may hit the database multiple times for something that you were able to do in a single query.
77

8-
JoinJS takes a much simpler approach (inspired by a Java library called [MyBatis](http://mybatis.github.io/mybatis-3/)). You can use any database driver or query builder (such as [Kenx.js](http://knexjs.org/)) to query your database, however you use JoinJS to convert the returned result set in to a hierarchy of nested objects.
8+
JoinJS takes a much simple and straightforward approach inspired by a Java library called [MyBatis](http://mybatis.github.io/mybatis-3/). You can use any database driver or query builder (such as [Kenx.js](http://knexjs.org/)) to query your database, however you use JoinJS to convert the returned results to a hierarchy of nested objects.
99

1010
## Example
11-
Suppose you have a one-to-many relationship between a `Team` and its `Player`s. You want to retrieve all teams along with their players. Here's the query for to do this:
11+
Suppose you have a one-to-many relationship between a `Team` and its `Players`. You want to retrieve all teams along with their players. Here's the query for to do this:
1212

1313
```sql
1414
SELECT t.id AS team_id,
@@ -20,7 +20,7 @@ FROM teams t
2020
ON t.id = p.team_id;
2121
```
2222

23-
Assume that this query returns the following result set
23+
Assume that this query returns the following result set:
2424

2525
```javascript
2626
var resultSet = [
@@ -41,21 +41,21 @@ You can use JoinJS to convert this result set in to an array of teams with neste
4141
name: 'New England Patriots'
4242
players: [
4343
{ id: 1, name: 'Tom Brady' },
44-
{ id: 2, name: 'Rob Gronkowski' },
44+
{ id: 2, name: 'Rob Gronkowski' }
4545
]
4646
},
4747
{
4848
id: 2,
4949
name: 'New York Jets'
5050
players: [
5151
{ id: 1, name: 'Tom Brady' },
52-
{ id: 2, name: 'Rob Gronkowski' },
52+
{ id: 2, name: 'Rob Gronkowski' }
5353
]
5454
}
5555
]
5656
```
5757

58-
To teach JoinJS how to do this, you must create result maps that describe your objects:
58+
To teach JoinJS how to do this, you must create two result maps that describe your objects:
5959

6060
```javascript
6161
var resultMaps = [
@@ -79,15 +79,13 @@ var resultMaps = [
7979
]
8080
```
8181

82-
Once you have created these result maps, you can simply call JoinJS to map your result set in to objects:
82+
Once you have created these result maps, you can simply call JoinJS to convert your result set in to objects:
8383

8484
```javascript
8585
var mappedResult = joinjs.map(resultSet, resultMaps, 'teamMap', 'team_');
8686
```
8787

88-
That's it! It doesn't matter how deep or complex your object hierarchy is, JoinJS can map it for you by supplying the appropriate result maps. Read the documentation below for more details. You can find more examples in the [test suite](https://github.com/archfirst/joinjs/tree/master/test). Check out the following project for an example of a real application built with JoinJS:
89-
90-
- [Manage My Money](https://github.com/archfirst/manage-my-money-server) - An application to manage your personal finances
88+
That's it! It doesn't matter how deep or complex your object hierarchy is, JoinJS can map it for you. Read the documentation below for more details. You can find more examples in the [test suite](https://github.com/archfirst/joinjs/tree/master/test). Also check out the [Manage My Money](https://github.com/archfirst/manage-my-money-server) project for an example of a full-fledged application built with JoinJS and other libraries to manage personal finances.
9189

9290
## Installation
9391

@@ -98,56 +96,56 @@ $ npm install join-js
9896
## Documentation
9997

10098
### ResultMap
101-
ResulpMaps are used to teach JoinJS how to map result sets to objects. Each result map focuses on a single object. The properties of a ResultMap are described below. You can find many examples of result maps in the [test suite](https://github.com/archfirst/joinjs/tree/master/test).
99+
ResulpMaps are used to teach JoinJS how to map database results to objects. Each result map focuses on a single object. The properties of a ResultMap are described below. You can find several examples in the [test suite](https://github.com/archfirst/joinjs/tree/master/test).
102100

103-
- `{String} mapId` - A unique identifier for the map
101+
- `mapId {String}` - A unique identifier for the map
104102

105-
- `{function} createNew` (optional) - A function that returns a blank new instance of the mapped object. Use these property to construct your custom objects instead of generic `Object`s.
103+
- `createNew {function} (optional)` - A function that returns a blank new instance of the mapped object. Use this property to construct a custom object instead of generic JavaScript `Object`.
106104

107-
- `{Object} idProperty` (optional) - mapping of id property from result set to mapped object. Default is `{name: 'id', column: 'id'}`.
105+
- `idProperty {Object} (optional)` - specifies the name of the id property in the mapped object and in the result set. Default is `{name: 'id', column: 'id'}`.
108106
- `name` - property that identifies the mapped object
109107
- `column` - property that identifies the database record in the result set
110108

111-
- `{Array} properties` - mapping of other properties from result set to mapped object
109+
- `properties {Array} (optional)` - mappings for other properties. Each mapping contains:
112110
- `name` - property name in the mapped object
113111
- `column` - property name in the result set
114112

115-
- `{Array} associations` - specifies references to other objects
116-
- `name` - property name of the association reference in the mapped object
113+
- `associations {Array} (optional)` - mappings for associations to other objects. Each mapping contains:
114+
- `name` - property name of the association in the mapped object
117115
- `mapId` - identifier of the map for the associated object
118-
- `columnPrefix` (optional) - a column prefix to apply to every element of the associated object. Default is an empty string.
116+
- `columnPrefix (optional)` - a prefix to apply to every column of the associated object. Default is an empty string.
119117

120-
- `{Array} collections` - specifies an array of references to other objects
121-
- `name` - property name of the array in the mapped object
118+
- `collections {Array} (optional)` - mappings for collections of other objects. Each mapping contains:
119+
- `name` - property name of the collection in the mapped object
122120
- `mapId` - identifier of the map for the associated objects
123-
- `columnPrefix` (optional) - a column prefix to apply to every element of the associated objects. Default is an empty string.
121+
- `columnPrefix (optional)` - a prefix to apply to every column of the associated object. Default is an empty string.
124122

125123
### API
126-
JoinJS exposes two very simple functions that give you the power to map any result set to one of more JavaScript objects.
124+
JoinJS exposes two very simple functions that give you the full power to map any result set to one of more JavaScript objects.
127125

128126

129127
#### map(resultSet, maps, mapId, columnPrefix)
130128

131-
Maps a resultSet to a collection.
129+
Maps a resultSet to an array of objects.
132130

133-
- `{Array} resultSet` - an array of database results
134-
- `{Array} maps` - an array of result maps
135-
- `{String} mapId` - mapId of the top-level objects in the resultSet
136-
- `{String} columnPrefix` (optional) - prefix that should be applied to the column names of the top-level objects
131+
- `resultSet {Array}` - an array of database results
132+
- `maps {Array}` - an array of result maps
133+
- `mapId {String}` - mapId of the top-level objects in the resultSet
134+
- `columnPrefix {String} (optional)` - prefix that should be applied to the column names of the top-level objects
137135

138-
Returns a collection of mapped objects.
136+
Returns an array of mapped objects.
139137

140138

141139
#### mapOne(resultSet, maps, mapId, columnPrefix, isRequired)
142140

143141
This is a convenience method that maps a resultSet to a single object.
144142

145-
- `{Array} resultSet` - an array of database results
146-
- `{Array} maps` - an array of result maps
147-
- `{String} mapId` - mapId of the top-level objects in the resultSet
148-
- `{String} columnPrefix` (optional) - prefix that should be applied to the column names of the top-level objects
149-
- `{boolean} isRequired` (optional) - is a mapped object required to be returned? Default is true.
143+
- `resultSet {Array}` - an array of database results
144+
- `maps {Array}` - an array of result maps
145+
- `mapId {String}` - mapId of the top-level objects in the resultSet
146+
- `columnPrefix {String} (optional)` - prefix that should be applied to the column names of the top-level objects
147+
- `isRequired {boolean} (optional)` - is it required to have a mapped object as a return value? Default is `true`.
150148

151149
Returns the mapped object or `null` if no object was mapped.
152150

153-
Throws a NotFoundError if no object is mapped and isRequired is true.
151+
Throws a `NotFoundError` if no object is mapped and `isRequired` is `true`.

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import createError from 'create-error';
55
let NotFoundError = createError('NotFoundError');
66

77
/**
8-
* Maps a resultSet to a collection.
8+
* Maps a resultSet to an array of objects.
99
*
1010
* @param {Array} resultSet - an array of database results
1111
* @param {Array} maps - an array of result maps

0 commit comments

Comments
 (0)