philwaldmann / openrecord Goto Github PK
View Code? Open in Web Editor NEWMake ORMs great again!
Home Page: https://openrecord.js.org
License: MIT License
Make ORMs great again!
Home Page: https://openrecord.js.org
License: MIT License
see line 240 of validations.js
In the example listed to connect to a MySQL data source, the user ID property is listed as username, however, that doesn't work; if you look in the MySQL connection code in lib, it's actually looking for the "user" parameter.
if find(id)
can't find the record, it returns null
.
use get(id)
to throw an error if the record does not exist!
I have two tables - products and prices. Prices is belong to products via product_id column.
My code:
var config = require('./config.json');
var OpenRecord = require('openrecord');
var db = new OpenRecord({
type: 'mysql',
host: config.host,
database: config.db,
user: config.user,
password: config.password,
port: 3306
});
db.Model('Products', function(){
this.hasMany('Prices');
});
db.Model('Prices', function(){
this.belongsTo('Products')
});
db.ready(function(){
var Prices = db.Model('Prices');
Prices.include('Products').exec(function(prices){
console.log(prices[0].products);
})
});
After execute i got error:
RelationNotFoundError: Can't find relation "Products" for Prices
Why?
Hi again. how to do subquery like this
select from (select from....) left join (select * .....)
Hi! I'm just starting to use this package, but I need some help since the docs are not complete as stated in other issues. I'm getting a "Can't find relation" error. Here is my setup:
// ClutchUsers.model.js
module.exports = function ClutchUsers() {
this.hasMany('clutch_user_permission')
.hasMany('clutch_module', {through: 'clutch_user_permissions', primary_key: 'id', foreign_key: 'user'})
.scope('active', function () {
this.where({status: 1})
})
}
// ClutchUserPermissions.model.js
module.exports = function ClutchUserPermissions () {
this.belongsToMany('clutch_user')
.belongsToMany('clutch_module')
}
// ClutchModules.model.js
module.export = function ClutchModules () {
this.hasMany('clutch_user_permission')
.hasMany('clutch_user', {through: 'clutch_user_permissions', primary_key: 'id', foreign_key: 'module'})
}
I tried following the examples but it is not working when I run something like this:
store.ready(() => {
const ClutchUsers = store.Model('ClutchUsers')
ClutchUsers.active().include('clutch_modules').exec((records) => {
console.log(records)
})
})
For reference: table names are clutch_users
, clutch_user_permissions
, clutch_modules
. Both users and modules have an id
pk column, and the permissions table has user
and module
columns for each of them as fk.
Without include
it works, but I also want the modules that each user has. What am I doing wrong?
Thank you and congratulations on this awesome package!
Model.include()
should support the following:
Model.include('$count').exec(function(records){
console.log(records.$count);
});
or
Model.include('relation.$count').exec(function(records){
console.log(records[0].relation.$count);
});
or just with a custom scope
Model.include('relation.my_scope').exec(function(records){
});
and with params
Model.include({'relation.$sum':'salary'}).exec(function(records){
console.log(records[0].relation.$sum);
});
or with a single record
Model.find(1).include('$count').exec(function(record){
console.log(record.$count);
});
Thanks for a great library. Is there a way to specify the table name? From the relevant code it looks like the inflection can be overridden in the config - but not in the model definition?
if i write myBase.Model('good').select("goods.id AS 'sku'").exec()
i receive result with out field 'sku', and field goods.id = null
This project is interesting but I can't find any update regarding the progress or stable version. Is there any resource I can find ?
It has been a while since the last major update of openrecord
. The reason is, it's running in production for for almost 3 years without problems. And is has all the features that we needed.
openrecord
was originally written for node 0.10 and a lot has changed in the javascript world since then.
Things I like to change/add for version 2.0 (v2 branch):
rest
-store, combined with cross store relations.{ [SQLError: error: select count(*) as "count" from "accounts" where "accounts"."email" = $1 and "accounts"."id" != $2 - invalid input syntax for uuid: "1"]
message:
{ error: invalid input syntax for uuid: "1"
at Connection.parseE (/home/samps/sites/personal/dus/office.downundersports.com/node/node_modules/pg/lib/connection.js:567:11)
at Connection.parseMessage (/home/samps/sites/personal/dus/office.downundersports.com/node/node_modules/pg/lib/connection.js:391:17)
Calling .toSql()
method I'm getting the string with double quotes instead of backquotes, which cause a syntax error. In lib/stores/sql/exec.js in 42 line I found a function, replacing backquotes with double quotes. What's the reason of this replacement? Basically, It turns initially correct SQL string to an incorrect one.
Role.create(params, function(result){
console.log('THIS', this); // should print the created role, instead prints something else
//...
});
workaround:
var role = Role.new(params);
role.save(function(result){
console.log('THIS', role);
//...
});
var role
is now what this
should be.
Hi, I'm getting this error when instantiating a new store with new OpenRecord(...)
/node_modules/openrecord/lib/utils.js:88
var plugin = require(files[i])
^
Error: Cannot find module "."
This is my code:
const OpenRecord = require('openrecord')
module.exports = new OpenRecord({
type: 'mysql',
host: '127.0.0.1',
database: 'database',
username: 'user',
password: 'secret'
})
Am I doing something wrong?
I'm using webpack.
If i try to order by multiple columns
WORKS
School
.select('schools.id as id', 'name', 'address.city as city', 'address_state.abbr as state')
.join({address: 'state'}, 'inner')
.order('name')
ERROR
School
.select('schools.id as id', 'name', 'address.city as city', 'address_state.abbr as state')
.join({address: 'state'}, 'inner')
.order(['name', 'city'])
I get the error:
TypeError: attribute.split is not a function
at Array.<anonymous> (/home/samps/sites/personal/dus/office.downundersports.com/node/node_modules/openrecord/lib/stores/sql/order.js:68:31)
sql/order.js
...
...
order: function(columns, desc){
var self = this.chain()
if(columns){
if(typeof desc === 'boolean'){
if(!Array.isArray(columns)) columns = [columns]
}else{
columns = Utils.args(arguments)
desc = false
}
for(var i in columns){
//MOST LIKELY NOT PROPERLY ADDING TO INTERNAL HERE
self.addInternal('order', {column: columns[i], order: desc ? 'DESC' : 'ASC'})
}
}else{
self.clearInternal('order')
}
return self
},
...
...
exports.definition = {
mixinCallback: function(){
this.beforeFind(function(query){
var order = this.getInternal('order')
...
...
for(i in order){
var attribute = order[i].column
var tmp = attribute.split('.') //ERROR IS HERE
...
...
}
Is there any way to easily rollback a migration from outside of the internal source code?
Hello.
I'm from South East Asia and I have tried installing this over
npm install --save openrecord pg
It hangs and npm won't continue
As a work around, I tried:
cd node_modules
git clone [email protected]:PhilWaldmann/openrecord.git
cd openrecord
npm install
I think one of the packages within your package.json is causing the install to hang. Can I get some help identifying which, and what to do? I'd also like if installing it in your region works, after you have cleaned your npm cache.
... on record.relation.new()
or record.relation.create()
Hi.
When i try to install, these error messages are presented:
npm WARN git config --get remote.origin.url
returned wrong result (https://git
hub.com/tgriesser/knex.git) undefined
npm WARN git config --get remote.origin.url
returned wrong result (https://git
hub.com/tgriesser/knex.git) undefined
npm ERR! git clone https://github.com/tgriesser/knex.git undefined
npm ERR! git clone https://github.com/tgriesser/knex.git undefined
npm ERR! not found: git
npm ERR!
npm ERR! Failed using git.
npm ERR! This is most likely not a problem with npm itself.
npm ERR! Please check if you have git installed and in your PATH.
Something wrong with git url of knex.
I'm very sad, I really liked what I saw, =\
I see there is already hstore support.
But jsonb is better than json and hstore.
Any chance of supporting it?
affected file: validations.js
// ...
self.model.count().where(condition).exec(function(result){
if(result.count > 0){
records.errors.add(field, 'not uniq');
return next(false);
}
next(true);
});
// ...
If no record is found, i.e. the new record is unique, the process crashes because result is null
(null.count
is not possible).
There are only a few DB methods returned by Database Object from Constructor in nativescript-sqlite plugin:
DB.isOpen()
DB.execSQL()
DB.get()
DB.all()
DB.each()
DB.close()
DB.resultType()
DB.valueType()
https://github.com/NathanaelA/nativescript-sqlite/blob/master/src/README.md
Is it possible to implement store for this plugin? Does openrecord could work without nodejs (because NativeScript does not have it... only JS engine)?
Some of my tables use 'bar_code' instead of 'id' for primary key name. and varchar type.
This case,
..
model.bar_code = req.body.bar_code; //--- user input value but "0"
..
model.save(function() {
model.bar_code === "0" //--- true!!!
}
How can I avoid this problem?
Would it be difficult to add/include Oracle's RDBMS support through node-oracledb?
Apparently, Oracle is already supported in Knex.
Thank you.
TypeError: Cannot read property 'scope' of undefined
at exports.definition.sortedList (../node_modules/ah-openrecord-plugin/node_modules/openrecord/lib/stores/sql/plugins/sorted_list.js:15:15)
when no options are provided in the model at this.sortedList();
validatesUniquenessOf
never seems to run. I'm getting database level errors of unique index violations on an email column
module.exports = function (){
this.filteredChanges = function filteredChanges(changes){
return Object.keys(changes)
.filter(key => key.toLowerCase() !== 'password')
.reduce((obj, key) => {
obj[key] = changes[key];
return obj;
}, {})
}
this.forCookie = function forCookie(){
return {id: this.id, email: this.email}
}
this
.validatesUniquenessOf(['email', 'athlete_id'])
.validatesFormatOf('email', /^[^@\s\;]+@[^@\s\;]+\.[^@\s\;]+$/)
.belongsTo('athlete')
.hasMany('accounts_users')
.hasMany('users', {through: 'accounts_users'})
.hasMany('infokits')
.hasMany('account_audits')
.beforeSave(function(){
let action;
if(!this.__exists){
action = 'create';
} else if(this.hasChanges()){
action = 'update';
}
if(action) console.log(this.account_audits.new({action: action, audited_changes: this.filteredChanges(this.changes), remote_address: this.context.current_ip || '127.0.0.1'}))
return true
})
}
Oracle support would allow me to use this
I have a table column id
of type UUID
, as primary key (not null constraint), with a default set to gen_random_uuid()
from pgcrypto extension
It works if i use sql like:
insert into test_table ("id") values ('84df10c9-517a-4dff-a96a-5799c30af0c4');
insert into test_table ("id") values (gen_random_uuid());
insert into test_table ("id") values (DEFAULT);
insert into test_table ("another_column") values ('i am not setting uuid');
Won't work when i do:
var test = new Test({ name: 'i am not setting uuid' })
test.save(function (e) {
console.log(e) // false
console.log(this.id) // null
})
Possibly a bug similar to this issue
Hi there,
We noticed that your repo has a high severity vulnerability:
Here is the test report for this repo.
If you’d like to fix this vulnerability, Snyk lets you generate a pull request that recommends the best upgrade path - there’s a link to fix this vulnerability on the test report.
Stay secure :-)
Ben from the Snyk team
I am evaluating Node based ORM systems for use with a multi-database system that includes both traditional SQL (PG to be specific) and Redis (for caching) and OpenRecord looks great.
The only concern I have is the fact that your test suite is currently flagged as failing. A quick look at the Travis report shows test fails for assigning of primary_keys
which is a bit of a concern.
Thanks
Dave
There is no documentation on how to actually retrieve a relationship.
for instance:
module.exports = function (){
this
.validatesUniquenessOf('athlete_id')
.validatesUniquenessOf('email')
.validatesFormatOf('email', /^[^@\s\;]+@[^@\s\;]+\.[^@\s\;]+$/)
.belongsTo('athlete')
.hasMany('accountsUsers')
.hasMany('users', {through: 'accountUsers'})
.hasMany('infokits')
.hasMany('account_audits')
}
how would I retrieve the 'users' for the above model?
how do I get the athlete
?
I have singled out the reason I was having connection issues in #43
node-pg and node-pg-native do not work with the semi-popular pgpool load balancer. there is something about the way it handles prepared statements that node-pg never becomes aware that an error has been returned
You should add a disclaimer that your library won't work when used with pgpool
given the following code:
Account.create({email: '[email protected]', password: 'password'}))
.then(function(account){
console.log(account) //true, should be {id: NEW_ID, email: ..., etc})
})
the returned account
is true instead of the object that was created
this will create race conditions if I need that newly created record as relying on 'last insert' is not thread safe. postgres has the option RETURNING
in Insert calls. that should be used to return the id of the object at least
How to reproduce the error
Create two models:
models/domain.js
module.exports = function(){
this.hasMany('mailboxes');
}
models/mailbox.js
module.exports = function(){
this.belongsTo('domain');
var _this = this;
this.beforeCreate(function(transaction, done){
var data = JSON.parse(this.context.params.data);
_this.store.models.Domain.find(data.domain_id).exec(function(domain){
console.log('domain found');
done(true);
});
});
}
actions/actions.js
var Actions = require(__dirname + '/../lib/actions.js');
module.exports = Actions.create({
'Domain': {
actions: ['create', 'get', 'list', 'update', 'destroy']
},
'Mailbox': {
actions: ['create', 'get', 'list', 'update', 'destroy']
}
});
The database must contain two tables:
npm start
.writeable
, readable
functions from an inherited Model don't have context
return Model
.join({athlete: 'user'})
.get(invId)
.exec()
.then((result) => {
account = result;
console.log(account.context); //HAS CONTEXT
console.log(account.athlete.context); //HAS CONTEXT
console.log(account.athlete.user.context); //DOES NOT HAVE CONTEXT
return user = account.athlete.user;
})
Server middleware may test if a user is currently logged-in.
But if an application allows different users to have different roles, a database model may want to decide whether the currently logged-in user is allowed to access or write something. Therefore, for instance a before create hook decides if the user may create a new record. This should be the the first thing the model does after the post request came in. But in fact the first thing that happens is the model going through its validation tests. If one of the validation tests fails, e.g. a uniqueness test, even though the user is not authorized to create a new record because of their role, the response to the client contains a uniqueness validation error ("not uniq") instead of an authorization error.
=> Please allow for hooks that execute before the first validation test is applied.
to automatically join tables which are referenced in the where conditions.
User.where({posts:{message_like: 'Foo'}})
e.g. domain
will it support WebSQL adapter?
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.