joeferner / node-persist Goto Github PK
View Code? Open in Web Editor NEWNode.js ORM framework supporting MySQL and SQLite 3 relational databases.
Node.js ORM framework supporting MySQL and SQLite 3 relational databases.
JSON.stringify currently prints out all the connection, model, query information when run against a model object retrieved from the database. It would be nice to clean this up and only print out relevant fields.
I looked over the example app and through the documentation, but couldn't find any reference to the LIKE operator.
Is this included, in the works, or am I complete moron and missed it someplace? :)
With node 0.8.4, I issue npm install on my package.json and I have the following error regarding "persist" installation.
The persist version I'm using is 0.2.5
The error message I got within my npm-debug.log file:
2269 silly exec git "clone" "git://github.com/nearinfinity/node-over.git" "/home/gp/tmp/npm-16682/1343820679216-0.5214699034113437"
2270 silly git,clone,git://github.com/nearinfinity/node-over.git,/home/gp/tmp/npm-16682/1343820679216-0.5214699034113437, spawning
2271 error git clone git://github.com/nearinfinity/node-over.git execvp(): No such file or directory
2272 silly lockFile 3ab471ba-b-com-nearinfinity-node-over-git git://github.com/nearinfinity/node-over.git
2273 verbose about to build /home/gp/test/node_modules/persist
2274 info /home/gp/test/node_modules/persist unbuild
2275 verbose from cache /home/gp/test/node_modules/persist/package.json
2276 info preuninstall [email protected]
2277 info uninstall [email protected]
2278 verbose true,/home/gp/test/node_modules,/home/gp/test/node_modules unbuild [email protected]
2279 info postuninstall [email protected]
2280 error Error: git "clone" "git://github.com/nearinfinity/node-over.git" "/home/gp/tmp/npm-16682/1343820679216-0.5214699034113437"
failed with 127
2280 error at ChildProcess. (/home/gp/.nave/installed/0.8.4/lib/node_modules/npm/lib/utils/exec.js:56:20)
2280 error at ChildProcess.EventEmitter.emit (events.js:91:17)
2280 error at Process._handle.onexit (child_process.js:674:10)
2281 error If you need help, you may report this log at:
2281 error http://github.com/isaacs/npm/issues
2281 error or email it to:
2281 error [email protected]
2282 error System Linux 2.6.32-5-xen-amd64
2283 error command "/home/gp/.nave/installed/0.8.4/bin/node" "/home/gp/.nave/installed/0.8.4/bin/npm" "install"
2284 error cwd /home/gp/test
2285 error node -v v0.8.4
2286 error npm -v 1.1.45
Any idea of the problem ?
Is it safe to say that persist will not work with the latest version of node-mysql?
I am running the following code:
var persist = require("persist");
var opts = {
driver: 'mysql',
host: 'localhost',
database: 'test2',
user: 'root',
password: 'xxxxxx'
};
persist.connect(opts, function (err, connection) {
});
and getting the following error:
node_modules/persist/lib/drivers/mysql.js:97
var db = new mysql.createClient(opts);
^
TypeError: undefined is not a function
at Class.Driver.extend.connect (node_modules/persist/lib/drivers/mysql.js:97:16)
at Object.exports.connect (node_modules/persist/lib/persist.js:86:10)
at Object. (test.js:9:9)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
Turns out that node-mysql has not had a createClient method in a while. This is from the readme:
v2.0.0-alpha (2012-05-15)
This release is a rewrite. You should carefully test your application after
upgrading to avoid problems.
...
The first thing you will run into is that the old Client
class is gone and
has been replaced with a less ambitious Connection
class. So instead of
mysql.createClient()
, you now have to:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret',
});
User = persist.define("User", {
'email': {'type': 'string', 'primaryKey': true},
'name': 'string',
})
.on("beforeSave", function(self) {
// have to save and restore email, because
// last_id is assigned to it after insert
self._email = self.email;
})
.on("afterSave", function(self) {
self.email = self._email;
delete self._email;
});
var Comment = persist.define("Comment", {
'text': 'string',
})
.hasOne(User, { 'foreignKey': 'user_email' });
The hack with save and restore email is for this line:
https://github.com/nearinfinity/node-persist/blob/master/lib/connection.js#L115
When I try to select user.comments
I get nothing, query is:
select t0."text" AS c0, t0."id" AS c1, t0."user_email" AS c2
FROM Comments AS t0
WHERE user_id = ? LIMIT 1
[ '[email protected]' ]
As you can see it uses user_id instead of user_email.
I get the results if I delete this line:
https://github.com/nearinfinity/node-persist/blob/master/lib/model.js#L334
What reasons do you have to delete foreignKey when you make reversed association in parent model?
If child.key references parent.id, then parent.id must reference child.key, not child.id.
Is pooling enabled by default, or does it have to be manually enabled?
Could we provide a database.json
or similar example of how to enable/disable pooling?
https://github.com/nearinfinity/node-persist#connectionPoolingUsing
When adding foreign keys using "hasMany" support changing the property, database column name, etc on that add.
It would be nice to have a simple way to update an object given its id. Consider the following code below, where Event is the model object to be updated:
// Model.update(primaryKey, params[, callback])
Event.update(2, {
name: 'new name',
location: 'new location'
}, function(err, updatedEvent) {
})
i.e. having a User which has Items and each Item has ItemHistories and I want to pull back all users with their items and each item to include its ItemHistories
Is that coming?
Thanks!
Hello,
I have a strange behaviour that I do not manage to fix.
I have the following model:
var persist = require("persist");
var type = persist.type;
module.exports = persist.define("Car", {
"brand": { type: type.STRING }
});
In Sqlite, I have created the cars table like:
CREATE TABLE cars(id integer PRIMARY KEY AUTOINCREMENT, brand varchar(50));
In Postgres, I have created the cars table like:
CREATE TABLE cars(id serial PRIMARY KEY, brand varchar(50));
In both database, I manage to create a car from my code:
app = new models.Car({'brand' : req.query.brand});
app.save(req.conn, function(err){
if(!err){
h.respond(res, { 'code' : 201 });
} else {
h.respond(res, { 'code' : 500, 'error' : err.message });
}
});
But the retrieval of data only works if I use sqlite:
models.Car.all(req.conn, function(err,cars){
async.map(cars,
function(car, callback){
callback(err, car.brand);
},
function(err, results){
if(!err){
h.respond(res, results);
} else {
h.respond(res, { 'code' : 500, 'error' : err.message });
}
}
);
});
No error but nothing is returned from when I use Postgres.
Is there a step I missed to have it working with postgres ?
Thanks a lot,
Luc
adding these events would allow better debugging and auditing support
Seems like it is missing, specifically for mysql?
Use db-info to get database information and at run time create columns in the model object. Like active record does.
allow adding a include clause to a query to eagerly fetch a relationship. This could be really hard with the each method.
Person
.include(Phone)
.all(...)
Can someone explain:
err
in all the callbacks contain? Is this a string or an object? Is this driver-specific? Et cetera.It would be useful to document this in the wiki or readme.
The error:
_node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module './drivers/postgresql.js'
at Function._resolveFilename (module.js:332:11)
at Function._load (module.js:279:25)
at Module.require (module.js:354:17)
at require (module.js:370:17)
at Object.connect (/home/adam/Programing/geddyBlog/node_modules/persist/lib/persist.js:40:16)
at Object. (/home/adam/Programing/geddyBlog/config/init.js:28:9)
at Module._compile (module.js:441:26)
at Object..js (module.js:459:10)
at Module.load (module.js:348:31)
at Function._load (module.js:308:12)
[Sun, 29 Apr 2012 23:13:28 GMT] ERROR Worker 13002 died._
The fix was copy pg.js and rename it to postgresql.js
can support "or" clause?
if i wanna get the people named 'bob' or whose age is 23, how can I use...
.....
Person.where({'name': 'bob', 'age': 23}).all(connection, function(err, people) {
// All the people named 'bob' with the age of 23
});
The delete method does not work for primary keys not called "id" and does not work for composite keys
allow loading app wide connection settings from a json file similar to node-db-migrate. allow the settings to have different environments along with a default environment.
{
"default": "dev"
"dev": {
"driver": "sqlite3",
"filename": ":memory:"
},
"test": {
"driver": "sqlite3",
"filename": ":memory:"
},
"prod": {
"driver": "mysql",
"username": "root",
"password": "root",
"database": "testdbmigrate"
}
}
Currently it seems that table and column names are converted to lower case before they're sent to the db. I think it would be worth adding an option to the connect parameters to turn off lowercasing. This will be especially useful for people who already have databases where tables and columns are not all lowercase. Bonus points for a similar option to use property names as is (so that 'userName' wouldn't get converted to 'user_name').
Currently only one primary key is allowed.
If Person has Phones and I have a phone I would like to get the Person that phone belongs to. (ie phone.person)
Hi,
I'm doing some test with node-persist module and sqlite3 but do not manage to have the 'hasMany' relationship working as expected.
In my db I have created 2 table (Cars and Options, one car having many options)
CREATE TABLE options(id integer, car_id integer, name varchar(50));
CREATE TABLE cars(id integer, name varchar(50));
In my node-persist models, I have the following ones:
var persist = require("persist");
var type = persist.type;
module.exports = persist.define("Option", {
"name": { type: type.STRING }
});
module.exports = persist.define("Car", {
"name": { type: type.STRING }
}).hasMany(Option);
In the code, I cannot see how to create a new options and associate it to the Car.
In the DB, when I manually create an option and associate it with the car (filling the car_id field), the association is not done.
option = new models.OPtion({'car_id': req.app.id, 'name' : req.query.option_name});
option.save(req.conn, function(err){
....
}
results in a:
INSERT INTO Options (name,car_id) VALUES ('TEST OPTION',NULL)
the value of car_id is set to NULL but I know it's not (req.app.id is 1 in this example).
Any idea ?
Thanks,
Luc
UPDATE
juste adding the following line before saving the option makes the link into the DB.
option = new models.Option({'car_id': req.app.id, 'name' : req.query.option_name});
option.car = my_car_object; // add ref to the car object
option.save(req.conn, function(err){
....
}
But if I request the car and then issue an car.options the newly created option is not listed.
For example can't do User.whereIn("name", ["bob", "sally"]).all
Have to do User.using(conn).whereIn("name", ["bob", "sally"]).all
allow different connection pooling strategies to be used.
Hi,
is there any way to do an update on more than one column??
Something like "UPDATE foos SET pos = pos + 1 WHERE pos > 4"
If persist supports INNER JOIN queries, could we supply an example query in the README?
If not, could we add it?
I'm was getting "Error: SQLITE_CONSTRAINT: constraint failed" when saving an object to a sqlite db. A looked at dev.db in the example and it seems that the id column must be allowed to be null. Is this a bug or a feature?
Edit: Turns out the wasn't quite it. The id of my model wasn't being written to the DB. Without that constraint, the app no longer crashes but no id is written to the record being inserted in the database. The row is created and the title, body and dateCreated are all correct. But the id in the database always comes out as null. Any ideas what's going on? Thanks in advance!
Edit: Closer inspection shows that the object returns null for getId()
//// The model adapter
PostTable = persist.define("post", {
"title" : type.STRING,
"body" : type.STRING,
"dateCreated": type.INTEGER
});
.....................
this.create = function (post, callback) {
geddy.log.info("Post adapter.create: " + sys.inspect(post));
persist.connect(function(err, connection) {
geddy.log.info('error: ' + err);
var data = [];
data.push(object = new PostTable(
{ title: post.title
, body: post.body
, dateCreated: post.dateCreated
// , id: post.id
})
);
geddy.log.info('data: ' + sys.inspect(data));
geddy.log.info('._getModel: ' + object.getId()); // returns null
connection.save(data, function(err2) {
if(err2) {
geddy.log.info('error: ' + err2);
throw err2;
}
});
callback(post);
});
};
// My model
var Post = function () {
this.defineProperties({
title: {type: 'string', required: true}
,body: {type: 'string', required: true}
,dateCreated: {type: 'int'}
,id: {type: 'int', required: true}
,status: {type: 'string', required: true}
});
this.validatesPresent('title');
this.validatesLength('title', {min: 5});
this.validatesWithFunction('status', function (status) {
return status == 'open' || status == 'done';
});
this.isValid = function(){
return true;
};
};
...
If I try to create an object that has a many to many with an object it fails.
var person = new Person({ phones: [ {number: '123-1234'}, {number: '234-3456'} ]} );
Could we extend include() to go arbitrarily deep?
The events do however get fired when calling update() on a model instance.
I understand that in the Model.update() case we don't have an instance to pass to the event methods but maybe we could pass the 'data' argument? And maybe add an extra param to the event method to indicate the given obj is a partial or a full instance?
Currently this make the use of Model.update() limited and not practical when registering those events.
In my case, I have defined a beforeSave event to update a 'updated_at' datetime field on model instances. The fact that Model.update() does not fire the event makes this method essentially useless to me. Or should this be handled/implemented in a different way?
Happy to send a pull request if you guys think this could make sense?
Hello,
I need to get the last value inserted in my table. I saw node-persist provides a "first" method, but no "last" neither "reverse" option in the "OrderBy" method. I could use the sql request but it will not be compatible with Postgres and Oracle in this case. Is that something planned or do you have another solution ?
Thanks for your help,
Luc
npm-release tags would be awesome so developers can access the code + documentation that correspondents to the npm-packaged version they use.
It appears that the current module will only work if the table has a column of "id" as the primary key, would be great to allow for an alternate name
If you have say a User which has a property "manager" which references another User you can't get the manager back properly using .include(). When you do User.include("manager").getById(id) for example it will return the manager (not the original user whose id you passed into getById) and that manager will have the property "manager" set to itself
Add support for blob data type
add an easy way to traverse many-to-many relationships without needing to load the join tables explicitly.
Do we have a way to use pooling in order to reduce db connection overload and to increase speed ?
In your webapps ex, each http request generate a mysql connexion, how can we avoid that ?
Regards.
Eric
support active record style hash based wheres
.where({"name": "bob", "age": 25}) // find all bobs that are 25
In a perfect world, it would be great to identify which fields are required in the model and call a function to verify that all of the required fields are present.
If you don't think it's valuable, let me know.
example where "name in ?", ['bob', 'alice']
Hello,
I have installed node-persist with node 0.6.19. When I run the basic example provided in the README, I got the following error:
/Users/luc/Projects/test/node_modules/persist/lib/model.js:288
pts.name || inflection.camelize(inflection.pluralize(associatedModel.modelName
^
TypeError: Cannot read property 'modelName' of undefined
at Function.hasMany (/Users/luc/Projects/test/node_modules/persist/lib/model.js:288:85)
at Object.setup (/Users/luc/Projects/test/lib/db.js:11:4)
at Object. (/Users/luc/Projects/test/test.js:7:24)
at Module._compile (module.js:446:26)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
at Function._load (module.js:311:12)
at Array.0 (module.js:484:10)
at EventEmitter._tickCallback (node.js:190:38)
I checked with node 0.8.4 and I have the same error.
Any idea of the problem ?
Thanks a lot,
Regards,
Luc
Currently, you have to manually change the properties of an object you want to update and then call save, such as:
Model.where({id: 1}).first(err, model) {
model.name = 'Jenny';
model.phone = '867-5309';
model.save(connection, function(err) {
if (!err) { // saved }
});
});
What I'd like to do in instead is:
Model.where({id: 1}).first(err, model) {
model.update(connection, { name: 'Jenny', phone: '867-5309' }, function(err) {
if (!err) { // saved }
});
});
add support for a json datatype. if it's JSON it'll select the value and JSON.parse the column value
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.