Comparing version 2.0.0-alpha5 to 2.0.0-alpha6
@@ -20,4 +20,3 @@ var orm = require("../lib/ORM"); | ||
} | ||
}, | ||
autoFetch: true | ||
} | ||
}); | ||
@@ -30,3 +29,6 @@ Person.hasMany("friends", { | ||
// console.log(Jane); | ||
Jane.getFriends().only("name").run(function (err, people) { | ||
console.log(err, people); | ||
}); | ||
}); | ||
}); |
@@ -75,7 +75,25 @@ var InstanceConstructor = require("../Instance").Instance; | ||
value: function () { | ||
var conditions = {}; | ||
var conditions = null; | ||
var options = {}; | ||
var limit; | ||
var cb; | ||
var cb = null; | ||
for (var i = 0; i < arguments.length; i++) { | ||
switch (typeof arguments[i]) { | ||
case "function": | ||
cb = arguments[i]; | ||
break; | ||
case "object": | ||
if (conditions === null) { | ||
conditions = arguments[i]; | ||
} else { | ||
options = arguments[i]; | ||
} | ||
break; | ||
case "number": | ||
limit = arguments[i]; | ||
break; | ||
} | ||
} | ||
options.__merge = { | ||
@@ -93,18 +111,10 @@ from: { table: association.mergeTable, field: association.mergeAssocId }, | ||
for (var i = 0; i < arguments.length; i++) { | ||
switch (typeof arguments[i]) { | ||
case "function": | ||
cb = arguments[i]; | ||
break; | ||
case "object": | ||
conditions = arguments[i]; | ||
break; | ||
case "number": | ||
limit = arguments[i]; | ||
break; | ||
} | ||
if (conditions === null) { | ||
conditions = {}; | ||
} | ||
conditions[association.mergeTable + "." + association.mergeId] = Instance.id; | ||
if (cb === null) { | ||
return association.model.find(conditions, limit, options); | ||
} | ||
association.model.find(conditions, limit, options, cb); | ||
@@ -111,0 +121,0 @@ return this; |
@@ -7,2 +7,10 @@ var Singleton = require("./Singleton"); | ||
return { | ||
only: function () { | ||
if (arguments.length && Array.isArray(arguments[0])) { | ||
opts.only = arguments[0]; | ||
} else { | ||
opts.only = Array.prototype.slice.apply(arguments); | ||
} | ||
return this; | ||
}, | ||
limit: function (limit) { | ||
@@ -21,3 +29,3 @@ opts.limit = limit; | ||
run: function (cb) { | ||
opts.driver.find(opts.table, opts.conditions, { | ||
opts.driver.find(opts.only, opts.table, opts.conditions, { | ||
limit : opts.limit, | ||
@@ -24,0 +32,0 @@ order : opts.order, |
@@ -34,5 +34,6 @@ var mysql = require("mysql"); | ||
Driver.prototype.find = function (table, conditions, opts, cb) { | ||
Driver.prototype.find = function (fields, table, conditions, opts, cb) { | ||
this.QuerySelect | ||
.clear() | ||
.fields(fields) | ||
.table(table); | ||
@@ -39,0 +40,0 @@ if (opts.fields) { |
@@ -29,5 +29,6 @@ var postgres = require("pg"); | ||
Driver.prototype.find = function (table, conditions, opts, cb) { | ||
Driver.prototype.find = function (fields, table, conditions, opts, cb) { | ||
this.QuerySelect | ||
.clear() | ||
.fields(fields) | ||
.table(table); | ||
@@ -34,0 +35,0 @@ if (opts.fields) { |
@@ -30,5 +30,6 @@ var sqlite3 = require("sqlite3"); | ||
Driver.prototype.find = function (table, conditions, opts, cb) { | ||
Driver.prototype.find = function (fields, table, conditions, opts, cb) { | ||
this.QuerySelect | ||
.clear() | ||
.fields(fields) | ||
.table(table); | ||
@@ -35,0 +36,0 @@ if (opts.fields) { |
@@ -49,3 +49,3 @@ var Instance = require("./Instance").Instance; | ||
opts.driver.find(opts.table, conditions, { limit: 1 }, function (err, data) { | ||
opts.driver.find(null, opts.table, conditions, { limit: 1 }, function (err, data) { | ||
if (err) { | ||
@@ -130,2 +130,3 @@ return cb(err); | ||
var chain = new ChainFind({ | ||
only : options.only || null, | ||
table : opts.table, | ||
@@ -132,0 +133,0 @@ driver : opts.driver, |
@@ -5,6 +5,9 @@ { | ||
"description": "NodeJS Object-relational mapping", | ||
"version": "2.0.0-alpha5", | ||
"version": "2.0.0-alpha6", | ||
"repository": { | ||
"url": "" | ||
}, | ||
"contributors": [ | ||
{ "name": "Bramus Van Damme", "email": "bramus@bram.us" } | ||
], | ||
"main": "./lib/ORM", | ||
@@ -11,0 +14,0 @@ "scripts": { |
@@ -8,3 +8,3 @@ ## Object Relational Mapping | ||
```sh | ||
npm install orm@2.0.0-alpha5 | ||
npm install orm@2.0.0-alpha6 | ||
``` | ||
@@ -69,17 +69,23 @@ | ||
After defining a Model you can get a specific element or find one or more based on some conditions. | ||
## Finding Items | ||
After defining a Model you can get a specific element or find one or more based on some conditions. | ||
### Model.get(id, [ options ], cb) | ||
To get a specific element from the database use `Model.get`. | ||
```js | ||
Person.find({ name: "John", surname: "Doe" }, 3, function (err, people) { | ||
// finds people with name='John' AND surname='Doe' and returns the first 3 | ||
Person.get(123, function (err, person) { | ||
// finds person with id = 123 | ||
}); | ||
``` | ||
Or if you know the ID of the item (called Instance): | ||
### Model.find([ conditions ] [, options ] [, limit ] [, order ] [, cb ]) | ||
Finding one or more elements has more options, each one can be given in no specific parameter order. Only `options` has to be after `conditions` (even if it's an empty object). | ||
```js | ||
Person.get(123, function (err, person) { | ||
// finds person with id = 123 | ||
Person.find({ name: "John", surname: "Doe" }, 3, function (err, people) { | ||
// finds people with name='John' AND surname='Doe' and returns the first 3 | ||
}); | ||
@@ -100,4 +106,3 @@ ``` | ||
There are more options that you can pass to find something. These options are passed in a second | ||
object: | ||
There are more options that you can pass to find something. These options are passed in a second object: | ||
@@ -110,6 +115,19 @@ ```js | ||
The order of the parameters is not fixed. You can pass the callback first if you like or mix the | ||
other paramenters. The only parameter that needs to be in order is when you pass 2 objects. The | ||
first one is for conditions (although it can be empty) and the second one is for options. | ||
#### Available options | ||
- `offset`: discards the first `N` elements | ||
- `limit`: although it can be passed as a direct argument, you can use it here if you prefer | ||
- `only`: if you don't want all properties, you can give an array with the list of properties you want | ||
#### Chaining | ||
If you prefer another less complicated syntax you can chain `.find()` by not giving a callback parameter. | ||
```js | ||
Person.find({ surname: "Doe" }).limit(3).offset(2).only("name", "surname").run(function (err, people) { | ||
// finds people with surname='Doe', skips first 2 and limits to 3 elements, | ||
// returning only 'name' and 'surname' properties | ||
}); | ||
``` | ||
## Associations | ||
@@ -116,0 +134,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
102309
73
3180
169