bookshelf.js

app.use inside a promise (bookshelf.js & express-basic-auth)

元气小坏坏 提交于 2019-12-23 04:13:13
问题 Api.fetchAll({columns: ['username','password']}) .then(function(employee) { return employee.toJSON(); }) .then(function(employee){ app.use(basicAuth({ users: {employee} })); }); I need my middleware (app.use) to run before my node starts so it registers. It doesn't, so when I start my node, my basic auth never registers. I'm using express-basic-auth to do the basic authentication for my api, and bookshelf.js for retrieving the values in the database. 回答1: Okay so here is how I solved it.

Can we always fetch date column as string (varchar) with knex and postgres?

微笑、不失礼 提交于 2019-12-22 09:56:35
问题 I have a column in postgres database with type date . It is a column like birthday, which is just a date and does not need to have a time part. When fetching this column with knex, the result is a javascript Date object. It is presumably doing new Date(row.birthday) , and this is the result that is sent to the client. The problem now is that the value that the client receives is in the standard ISO 8601 format with the time part and the Z . When the client tries to create a new Date object

KnexJS Migration With Associated Seed Data

喜夏-厌秋 提交于 2019-12-22 08:37:35
问题 I'm, in the process of learning BookshelfJS/KnexJS (switching from SequelizeJS), and I'm running into an issue with importing data into multiple tables that were created via the migrations feature within KnexJS. There's 4 tables: servers operating_systems applications applications_servers With the following constraints: servers . operating_system_id references operating_systems . id applications_servers . server_id references servers . id applications_servers . application_id references

How to do raw query in Bookshelf.js

空扰寡人 提交于 2019-12-21 11:34:08
问题 I want to achieve this SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20; from https://developers.google.com/maps/articles/phpsqlsearch_v3?hl=fr#createtable How can I make this query with Bookshelf. I have this now: var raw = '( 3959 * acos( cos( radians(37) ) * cos( radians( '+ req.params.lat + ' ) ) * cos(

Bookshelf.js - how to save many-to-many relationship?

北战南征 提交于 2019-12-21 08:23:41
问题 I'm having troubles saving data in a "many-to-many" relationship. Here are my models: var CoursePeople = bookshelf.Model.extend({ tableName: 'course_people' }); var Course = bookshelf.Model.extend({ tableName: 'course', users: function(){ return this.belongsToMany(User); } }); var City = bookshelf.Model.extend({ tableName: 'city', users: function(){ return this.hasMany(User); } }); var User = bookshelf.Model.extend({ tableName: 'people', city: function(){ return this.belongsTo(City); },

Bookshelf.js - how to save many-to-many relationship?

断了今生、忘了曾经 提交于 2019-12-21 08:23:07
问题 I'm having troubles saving data in a "many-to-many" relationship. Here are my models: var CoursePeople = bookshelf.Model.extend({ tableName: 'course_people' }); var Course = bookshelf.Model.extend({ tableName: 'course', users: function(){ return this.belongsToMany(User); } }); var City = bookshelf.Model.extend({ tableName: 'city', users: function(){ return this.hasMany(User); } }); var User = bookshelf.Model.extend({ tableName: 'people', city: function(){ return this.belongsTo(City); },

Tracking DB querying time - Bookshelf/knex

送分小仙女□ 提交于 2019-12-19 11:06:43
问题 I would like to monitor the time taken by a query on my API's db. I so created the following function, using bookshelf-signals, a Bookshelf plugin. : bookshelf.on('fetching', () => { server.app.fetching = new Date().valueOf(); }); bookshelf.on('counting', () => { server.app.fetching = new Date().valueOf(); }); bookshelf.on('fetched', () => { server.statsd.gauge('db_query', new Date().valueOf() - server.app.fetching); }); ... so that I can retrieve the time just before and just after a fetch

KNEX Undefined binding(s) detected when compiling SELECT query

纵然是瞬间 提交于 2019-12-13 14:53:46
问题 var knex = require('knex')(config); var bookshelf = require('bookshelf')(knex); var SKU = bookshelf.Model.extend({ tableName: 'skus', }); SKU.where('id', undefined).fetch().then(function (skus) { if (skus) console.log(skus.toJSON()); }).catch(function (err) { console.error(err); }); It throws Undefined binding(s) detected when compiling SELECT query. It is working fine with 0.11.5, it stopped working with 0.11.6 onwards. I am pointing to 0.13.0 right now. useNullAsDefault: true works fine

Bookshelf JS function inside extend()

女生的网名这么多〃 提交于 2019-12-12 18:29:38
问题 I am using http://bookshelfjs.org/ I added a function inside var User = Bookshelf.Model.extend({ ... // verify the password verifyPassword: function (password, hash, done) { // Load hash from your password DB. bcrypt.compare(password, hash.replace('$2y$', '$2a$'), function(err, result) { return done(err, result); }); } ... module.exports = User; from my user controller, I call it like: var User = require('../models/user'); User.verifyPassword(req.body.password, user.password, function (err,

How to get SQLSTATE in bookshelf.js?

99封情书 提交于 2019-12-12 16:42:52
问题 How does one detect the type of an error that occurs in bookshelf.js when talking to PostgreSQL (or another RDBMS)? Prompted by this question; I don't use bookshelf.js myself. A search of the manual did not reveal much that was enlightening. 来源: https://stackoverflow.com/questions/43827704/how-to-get-sqlstate-in-bookshelf-js