You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,11 @@
1
1
# JoinJS
2
2
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.
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), and gives you direct control over your database interactions.
4
4
5
5
## Motivation
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.
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.
7
7
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.
8
+
JoinJS takes a much simpler 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 [Knex.js](http://knexjs.org/)) to query your database, however you use JoinJS to convert the returned results to a hierarchy of nested objects.
9
9
10
10
## Example
11
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:
@@ -48,8 +48,8 @@ You can use JoinJS to convert this result set in to an array of teams with neste
48
48
id:2,
49
49
name:'New York Jets'
50
50
players: [
51
-
{ id:1, name:'Tom Brady' },
52
-
{ id:2, name:'Rob Gronkowski' }
51
+
{ id:3, name:'Geno Smith' },
52
+
{ id:4, name:'Darrelle Revis' }
53
53
]
54
54
}
55
55
]
@@ -96,11 +96,11 @@ $ npm install join-js
96
96
## Documentation
97
97
98
98
### ResultMap
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).
99
+
ResultMaps 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).
100
100
101
101
-`mapId {String}` - A unique identifier for the map
102
102
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`.
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 a generic JavaScript `Object`.
104
104
105
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'}`.
106
106
-`name` - property that identifies the mapped object
@@ -112,12 +112,12 @@ ResulpMaps are used to teach JoinJS how to map database results to objects. Each
112
112
113
113
-`associations {Array} (optional)` - mappings for associations to other objects. Each mapping contains:
114
114
-`name` - property name of the association in the mapped object
115
-
-`mapId` - identifier of the map for the associated object
115
+
-`mapId` - identifier of the result map of the associated object
116
116
-`columnPrefix (optional)` - a prefix to apply to every column of the associated object. Default is an empty string.
117
117
118
118
-`collections {Array} (optional)` - mappings for collections of other objects. Each mapping contains:
119
119
-`name` - property name of the collection in the mapped object
120
-
-`mapId` - identifier of the map for the associated objects
120
+
-`mapId` - identifier of the result map of the associated objects
121
121
-`columnPrefix (optional)` - a prefix to apply to every column of the associated object. Default is an empty string.
122
122
123
123
### API
@@ -138,11 +138,11 @@ Returns an array of mapped objects.
This is a convenience method that maps a resultSet to a single object.
141
+
This is a convenience method that maps a resultSet to a single object. It is used when your select query is expected to return only one result (e.g. `SELECT * FROM table WHERE id = 1234`).
142
142
143
143
-`resultSet {Array}` - an array of database results
144
144
-`maps {Array}` - an array of result maps
145
-
-`mapId {String}` - mapId of the top-level objects in the resultSet
145
+
-`mapId {String}` - mapId of the top-level object in the resultSet
146
146
-`columnPrefix {String} (optional)` - prefix that should be applied to the column names of the top-level objects
147
147
-`isRequired {boolean} (optional)` - is it required to have a mapped object as a return value? Default is `true`.
0 commit comments