bookshelf.js

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

元气小坏坏 提交于 2019-12-05 20:51:04
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 from this string, the client may have an erroneous date value based on where the client is located. For

Accessing nested relations in Bookshelf.js

 ̄綄美尐妖づ 提交于 2019-12-05 19:56:09
I guess you could say I'm building a reddit style app. So I have a topic, and that topic has comments, and those comments have parent comments, etc. Here is my Comment model: var Comment = bookshelf.Model.extend({ tableName: 'comments', topic: function() { return this.belongsTo(Topic, 'topic_id'); }, children: function() { return this.hasMany(Comment, 'parent_id') } }); And so in my .get('/topic') page, i load my comments like this: new Comment() .query({where: {topic_id: topic_id}}) .query({where: {parent_id: null}}) .fetchAll({ withRelated: ['children.children.children.children'] }) So what

KnexJS Migration With Associated Seed Data

独自空忆成欢 提交于 2019-12-05 14:08:56
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 applications . id The tables get created just fine when I run knex migrate:latest --env development servers

Bookshelf.js set attribute not in database

一笑奈何 提交于 2019-12-05 13:06:51
I have a Bookshelf.js model. I want to be able to set and get attributes for this model that are not persistent in the database. For instance lets say I have a model that looks like this: var Domain = bookshelf.Model.extend({ tableName: 'domains', initialize: function() { this.on('creating', this.setDomainName); }, setDomainName: function() { this.set('name', getDomainFromUrl(this.url)); } }); With a schema that looks like this: knex.schema.createTable('domains', function (table) { table.increments().index(); table.text('name').index(); table.timestamps(); }); I want to be able to save an

bookshelf.js count method

蓝咒 提交于 2019-12-04 18:30:28
问题 I was searching high and low to find how to do basic counting (like SELECT COUNT(something) FROM table) with Bookshelf.js, but to no avail. Is there anything I'm missing? Or is it just used with a manual select query? Thanks! 回答1: For now it's a manual query... e.g: bookshelf.knex(tableName).count('columnName').then(... Long story as to why that's the case, but it's mainly because of a few complexities around counts in relations, I don't want to hack a half working one on there just for the

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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 01:51:40
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); }, courses: function(){ return this.belongsToMany(Course); } }); The challenge is, how to insert IDs that I get

Sort Bookshelf.js results with .orderBy()

依然范特西╮ 提交于 2019-12-03 06:04:33
I am working on a personal project to learn Node.js + express + Bookshelf.js. Where do I build queries? In particular, how do I simply set an 'ORDER BY' or 'WHERE' in the following code? var Accounts = require('../collections/accounts').collection; new Accounts().fetch({ withRelated: ['folders'] }).then(function(collection) { // process results }); I would like to learn Bookshelf.js because it seems to offer the features I am used to with Laravel's Elequent (PHP), such as polymorphic relationships and sub expressions. However, I'm finding that the documentation is not very in-depth and trying

How to access data on a `through` table with Bookshelf

半腔热情 提交于 2019-12-02 21:21:03
问题 I am using [BookshelfJS][bookshelfjs] for my ORM and am wondering how to access data on a though table. I have 3 Models, Recipe , Ingredient and RecipeIngredient which joins the two. var Recipe = BaseModel.extend({ tableName: 'recipe', defaults: { name: null }, ingredients: function () { return this .belongsToMany('Ingredient') .through('RecipeIngredient') .withPivot(['measurement']); } })); var Ingredient = BaseModel.extend({ tableName: 'ingredients', defaults: { name: null }, recipes:

How to access data on a `through` table with Bookshelf

偶尔善良 提交于 2019-12-02 09:49:24
I am using [BookshelfJS][bookshelfjs] for my ORM and am wondering how to access data on a though table. I have 3 Models, Recipe , Ingredient and RecipeIngredient which joins the two. var Recipe = BaseModel.extend({ tableName: 'recipe', defaults: { name: null }, ingredients: function () { return this .belongsToMany('Ingredient') .through('RecipeIngredient') .withPivot(['measurement']); } })); var Ingredient = BaseModel.extend({ tableName: 'ingredients', defaults: { name: null }, recipes: function () { return this .belongsToMany('Recipe') .through('RecipeIngredient'); } })); var RecipeIngredient

How to add simple Where-clause with bookshelf-pagemaker

感情迁移 提交于 2019-12-02 08:23:47
问题 Using the bookshelf-pagemaker NodeJS Module: https://www.npmjs.com/package/bookshelf-pagemaker https://github.com/bhoriuchi/bookshelf-pagemaker .. I was able to get basic paginate working where it selects all rows from a table. But I am not seeing how to add a WHERE clause to the query. QUESTION: Can someone please share a small example of using bookshelf-pagemaker with search criteria - e.g. SELECT * FROM user WHERE id > 10 ? var Bookshelf = require('bookshelf').mysqlAuth; var pagemaker =