Comparing version 2.0.0 to 2.0.1
@@ -19,3 +19,2 @@ module.exports = function (db, cb) { | ||
}); | ||
db.models.person.hasOne("favpet", db.models.pet); | ||
@@ -22,0 +21,0 @@ db.models.pet.hasOne("owner", db.models.person, { |
var orm = require("../lib/ORM"); | ||
// orm.connect("postgres://dresende@localhost:/orm_test", function (err, db) { | ||
orm.connect("mysql://root:tedua@localhost/orm?pool=true", function (err, db) { | ||
orm.connect("redshift://dresende@localhost:/orm_test", function (err, db) { | ||
// orm.connect("mysql://root:tedua@localhost/orm?pool=true", function (err, db) { | ||
// orm.connect("mongodb:///test", function (err, db) { | ||
@@ -22,16 +22,16 @@ // orm.connect("sqlite://", function (err, db) { | ||
db.sync(function () { | ||
db.serial( | ||
Person.find({ surname: "Doe" }), | ||
Pet.find() | ||
).get(function (err, people, pets) { | ||
console.log(err); | ||
for (var k in people) { | ||
console.log("person", people[k].id, people[k].name, people[k].surname); | ||
} | ||
for (k in pets) { | ||
console.log(" pet", pets[k].id, pets[k].name); | ||
} | ||
var John = new Person({ | ||
name : "Jane", | ||
surname : "Dean", | ||
male : true | ||
}); | ||
John.save(function (err) { | ||
console.log("err", err); | ||
Person.find(function (err, people) { | ||
console.log(people); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -45,2 +45,3 @@ exports.drop = function (driver, opts, cb) { | ||
for (i = 0; i < opts.one_associations.length; i++) { | ||
if (opts.one_associations[i].reversed) continue; | ||
definitions.push(driver.escapeId(opts.one_associations[i].field) + " INT(11) UNSIGNED NOT NULL"); | ||
@@ -56,2 +57,3 @@ } | ||
for (i = 0; i < opts.one_associations.length; i++) { | ||
if (opts.one_associations[i].reversed) continue; | ||
definitions.push("INDEX (" + driver.escapeId(opts.one_associations[i].field) + ")"); | ||
@@ -58,0 +60,0 @@ } |
@@ -40,2 +40,3 @@ exports.drop = function (driver, opts, cb) { | ||
for (i = 0; i < opts.one_associations.length; i++) { | ||
if (opts.one_associations[i].reversed) continue; | ||
definitions.push(driver.escapeId(opts.one_associations[i].field) + " INTEGER NOT NULL"); | ||
@@ -61,2 +62,3 @@ } | ||
for (i = 0; i < opts.one_associations.length; i++) { | ||
if (opts.one_associations[i].reversed) continue; | ||
tables[tables.length - 1].subqueries.push( | ||
@@ -158,5 +160,5 @@ "CREATE INDEX ON " + driver.escapeId(opts.table) + | ||
if (prop.hasOwnProperty("defaultValue")) { | ||
def += " DEFAULT " + driver.db.escape(prop.defaultValue); | ||
def += " DEFAULT " + driver.escape(prop.defaultValue); | ||
} | ||
return def; | ||
} |
@@ -40,2 +40,3 @@ exports.drop = function (driver, opts, cb) { | ||
for (i = 0; i < opts.one_associations.length; i++) { | ||
if (opts.one_associations[i].reversed) continue; | ||
definitions.push(driver.escapeId(opts.one_associations[i].field) + " INTEGER NOT NULL"); | ||
@@ -61,2 +62,3 @@ } | ||
for (i = 0; i < opts.one_associations.length; i++) { | ||
if (opts.one_associations[i].reversed) continue; | ||
tables[tables.length - 1].subqueries.push( | ||
@@ -63,0 +65,0 @@ "CREATE INDEX ON " + driver.escapeId(opts.table) + |
@@ -32,2 +32,3 @@ exports.drop = function (driver, opts, cb) { | ||
for (i = 0; i < opts.one_associations.length; i++) { | ||
if (opts.one_associations[i].reversed) continue; | ||
definitions.push(driver.escapeId(opts.one_associations[i].field) + " INTEGER UNSIGNED NOT NULL"); | ||
@@ -51,2 +52,3 @@ } | ||
for (i = 0; i < opts.one_associations.length; i++) { | ||
if (opts.one_associations[i].reversed) continue; | ||
queries.push( | ||
@@ -53,0 +55,0 @@ "CREATE INDEX IF NOT EXISTS " + driver.escapeId(opts.table + "_" + opts.one_associations[i].field) + |
@@ -189,2 +189,4 @@ var postgres = require("pg"); | ||
return value; | ||
case "boolean": | ||
return value ? "true" : "false"; | ||
} | ||
@@ -191,0 +193,0 @@ |
@@ -20,2 +20,3 @@ var Instance = require("./Instance").Instance; | ||
var model = function (data) { | ||
var instance; | ||
if (typeof data == "number") { | ||
@@ -25,3 +26,3 @@ var data2 = {}; | ||
var instance = new Instance({ | ||
instance = new Instance({ | ||
id : opts.id, | ||
@@ -55,3 +56,3 @@ data : data2, | ||
} | ||
return new Instance({ | ||
instance = new Instance({ | ||
id : opts.id, | ||
@@ -70,2 +71,12 @@ is_new : !data.hasOwnProperty(opts.id), | ||
}); | ||
OneAssociation.extend(instance, opts.driver, one_associations, { | ||
autoFetch : false | ||
}, function () { | ||
ManyAssociation.extend(instance, opts.driver, many_associations, { | ||
autoFetch : false | ||
}, function () { | ||
// .. | ||
}); | ||
}); | ||
return instance; | ||
}; | ||
@@ -72,0 +83,0 @@ |
@@ -8,8 +8,16 @@ var map = {}; | ||
if (map.hasOwnProperty(key)) { | ||
return returnCb(map[key]); | ||
if (map[key].t !== null && map[key].t <= Date.now()) { | ||
delete map[key]; | ||
} else { | ||
return returnCb(map[key].o); | ||
} | ||
} | ||
createCb(function (value) { | ||
return returnCb(map[key] = value); | ||
map[key] = { // object , timeout | ||
o : value, | ||
t : (opts && typeof opts.cache == "number" ? Date.now() + (opts.cache * 1000) : null) | ||
}; | ||
return returnCb(map[key].o); | ||
}); | ||
}; |
@@ -11,5 +11,6 @@ { | ||
"postgres", | ||
"redshift", | ||
"sqlite" | ||
], | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"license": "MIT", | ||
@@ -16,0 +17,0 @@ "repository": { |
@@ -11,3 +11,3 @@ ## Object Relational Mapping | ||
Despite the alpha tag, this is the recommended version for new applications. | ||
Current stable version: **2.0.1** | ||
@@ -18,2 +18,3 @@ ## DBMS Support | ||
- PostgreSQL | ||
- Amazon Redshift | ||
- SQLite | ||
@@ -237,3 +238,3 @@ | ||
#### Available options | ||
### Available options | ||
@@ -325,2 +326,20 @@ - `offset`: discards the first `N` elements | ||
### Singleton | ||
Each model instances is cached, so if you fetch the same record using 2 or more different queries, you will | ||
get the same object. If you have other systems that can change your database (or you're developing and need | ||
to make some manual changes) you should remove this feature by disabling cache. You do this when you're | ||
defining each Model. | ||
```js | ||
var Person = db.define('person', { | ||
name : String | ||
}, { | ||
cache : false | ||
}); | ||
``` | ||
If you want singletons but want cache to expire after a period of time, you can pass a number instead of a | ||
boolean. The number will be considered the cache timeout in seconds (you can use floating point). | ||
## Associations | ||
@@ -330,4 +349,15 @@ | ||
## hasOne vs. hasMany Associations | ||
## hasOne vs. hasMany | ||
Since this topic brings some confusion to many people including myself, here's a list of the possibilities | ||
supported by both types of association. | ||
- `hasOne` : it's a **Many-to-One** relationship. A.hasOne(B) means A will have one (or none) of B, but B can be | ||
associated with many A; | ||
- `hasMany`: it's a **One-to-Many** relationship. A.hasMany(B) means A will have none, one or more of B. Actually | ||
B will be associated with possibly many A but you don't have how to find it easily (see next); | ||
- `hasMany` + reverse: it's a **Many-to-Many** relationship. A.hasMany(B, { reverse: A }) means A can have none or | ||
many B and also B can have none or many A. Accessors will be created in both models so you can manage them from | ||
both sides. | ||
If you have a relation of 1 to 0 or 1 to 1, you should use `hasOne` association. This assumes a column in the model that has the id of the other end of the relation. | ||
@@ -353,2 +383,10 @@ | ||
If you prefer to use another name for the field (owner_id) you can change this parameter in the settings. | ||
```js | ||
db.settings.set("properties.association_key", "id_{name}"); // {name} will be replaced by 'owner' in this case | ||
``` | ||
**Note: This has to be done prior to the association creation.** | ||
For relations of 1 to many you have to use `hasMany` associations. This assumes another table that has 2 columns, one for each table in the association. | ||
@@ -417,5 +455,5 @@ | ||
For `hasOne` associations you can make calls to the associated Model by using the `reverse` option. For example, | ||
if you have an association from ModelA to ModelB, you can create an accessor in ModelB to get instances from ModelA. | ||
Confusin? Look at the next example. | ||
Associations can make calls to the associated Model by using the `reverse` option. For example, if you have an | ||
association from ModelA to ModelB, you can create an accessor in ModelB to get instances from ModelA. | ||
Confusing? Look at the next example. | ||
@@ -441,1 +479,22 @@ ```js | ||
``` | ||
This makes even more sense when having `hasMany` associations since you can manage the Many-to-Many associations | ||
from both sides. | ||
```js | ||
var Pet = db.define('pet', { | ||
name : String | ||
}); | ||
var Person = db.define('person', { | ||
name : String | ||
}); | ||
Person.hasMany("pets", Person, { | ||
bought : Date | ||
}, { | ||
reverse : "owners" | ||
}); | ||
Person(1).getPets(...); | ||
Pet(2).getOwners(...); | ||
``` |
@@ -27,2 +27,3 @@ var common = exports; | ||
case 'postgres': | ||
case 'redshift': | ||
return 'postgres://postgres@localhost/orm_test'; | ||
@@ -50,2 +51,8 @@ case 'sqlite': | ||
'/' + (config.database || 'orm_test'); | ||
case 'redshift': | ||
return 'redshift://' + | ||
(config.user || 'postgres') + | ||
(config.password ? ':' + config.password : '') + | ||
'@' + (config.host || 'localhost') + | ||
'/' + (config.database || 'orm_test'); | ||
case 'sqlite': | ||
@@ -69,2 +76,3 @@ return 'sqlite://' + (config.pathname || ""); | ||
case "postgres": | ||
case "redshift": | ||
db.query("CREATE TEMPORARY TABLE " + table + " (id SERIAL PRIMARY KEY, name VARCHAR(100) NOT NULL)", cb); | ||
@@ -86,2 +94,3 @@ break; | ||
case "postgres": | ||
case "redshift": | ||
db.query("CREATE TEMPORARY TABLE " + table + " (id SERIAL PRIMARY KEY, name VARCHAR(100) NOT NULL, assoc_id BIGINT NOT NULL)", cb); | ||
@@ -103,2 +112,3 @@ break; | ||
case "postgres": | ||
case "redshift": | ||
db.query("CREATE TEMPORARY TABLE " + table + "_" + assoc + " (" + table + "_id BIGINT NOT NULL, " + assoc + "_id BIGINT NOT NULL, extra_field BIGINT)", cb); | ||
@@ -122,2 +132,3 @@ break; | ||
case "postgres": | ||
case "redshift": | ||
case "mysql": | ||
@@ -152,2 +163,3 @@ query = []; | ||
case "postgres": | ||
case "redshift": | ||
case "mysql": | ||
@@ -182,2 +194,3 @@ query = []; | ||
case "postgres": | ||
case "redshift": | ||
case "mysql": | ||
@@ -184,0 +197,0 @@ query = []; |
Sorry, the diff of this file is not supported yet
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
186387
110
5597
493