sequelize.js

Sequelize Model.prototype.(customFunction) not working

血红的双手。 提交于 2019-12-11 07:04:14
问题 I am trying to define a customFunction in Sequelize model. But I get an error : TypeError: user.getJWT is not a function at User.create.then (/projects/test/a/app/controllers/UserController.js:22:29) Here is the code of models/user.js : module.exports = function(sequelize, Sequelize) { var User = sequelize.define('User', { id: { type: Sequelize.INTEGER(11), allowNull: true, primaryKey: true, autoIncrement: true }, user_id: { type: Sequelize.STRING(255), allowNull: true, defaultValue: '' } });

Sequelize complex aggregate attribute in where and order

倖福魔咒の 提交于 2019-12-11 06:58:47
问题 Hello I'm trying to use Sequelize to perform location based queries, and I'm having a nightmare! I'm trying to generate the SQL: SELECT *, (3959 * acos(cos(radians([user-latitude])) * cos(radians(latitude)) * cos(radians(longitude) - radians([user-longitude])) + sin(radians([user-latitude])) * sin(radians(latitude)))) AS distance FROM myModel HAVING distance <= 25 ORDER BY distance ASC LIMIT 0 , 10; where [user-latitude] and [user-longitude] are variables. So far I've got this: myModel

Is there a way to query a table with a where clause on a related table but still grab all related content to the original table with Sequelize?

女生的网名这么多〃 提交于 2019-12-11 06:54:52
问题 I have two tables that are related. Table A and Table B. Table A has a one-to-many relationship with Table B. I want to retrieve all instances of A that has an instance of B related to it with a status of pending. The query used is shown below, what I'm trying to achieve is to grab all instances of A with an instance of B whose status is pending . await A.findAll({ limit, offset, include: [ { model: B, where: { status: 'pending' } } ] }); The code above returns multiple instances of the same

TypeScript & Sequelize: Pass in generic Model

落爺英雄遲暮 提交于 2019-12-11 06:35:17
问题 I have this code import { Model, DataTypes, Sequelize } from "sequelize"; class User extends Model { public id!: number; public firstName!: string; public readonly createdAt!: Date; public readonly updatedAt!: Date; } function async InitAndDefineModel(model: Model): void { const sequelize = new Sequelize({ dialect: "sqlite", storage: ":memory:", }); model.init{ firstName: { allowNull: false, type: DataTypes.STRING, }, }, { sequelize, }); const tables = await sequelize.showAllSchemas({});

Sequelize - does not contain a string within a PostgreSQL array query

自古美人都是妖i 提交于 2019-12-11 06:33:59
问题 I have a Sequelize model called Staff . On it there's an array field described as follows: const Staff = sequelize.define("staff", { name: { type: DataTypes.STRING, }, email: { type: DataTypes.STRING, unique: true }, password: { type: DataTypes.STRING, }, roles: { type: DataTypes.ARRAY(DataTypes.STRING) }, }); I'm trying to create a GraphQL end point which serves up all staff which don't have 'instructor' in their roles field. I've read this page of the docs, suggesting that I use a

infinite post loop when calling action

你说的曾经没有我的故事 提交于 2019-12-11 06:06:51
问题 This is not so much of a specific error, I'm getting an infinite post loop when calling the function getLikes = (id) => { // console.log(id); this.props.getLikeCount(id) console.log(this.props.likeCount) } visually it looks like this it's an infinite loop. We refactored the code so that a user can add a like to a post, retrieve the amount of likes for a specific post, and update the state of likes. Like.js import React, { Component } from 'react'; import ReactDOM from 'react-dom' import {

how to lock a table for writing

為{幸葍}努か 提交于 2019-12-11 06:03:33
问题 I would like to lock a table for writing during a period of time, while leaving it available for reading. Is that possible ? Ideally I would like to lock the table with a predicate (for example prevent writing rows " where country = france "). 回答1: If you really want to lock against such inserts, i.e. the query should hang and only continue when you allow it, you would have to place a SHARE lock on the table and keep the transaction open. This is usually not a good idea. If you want to

Include unexpected. Element has to be either a Model, an Association or an object

半城伤御伤魂 提交于 2019-12-11 05:54:07
问题 I have a problem trying to configure postrgres with nodeJs, i followed this tutorial and i get a error that doesn't specify anything about what is wrong: here is the stacktrace Unhandled rejection Error: Include unexpected. Element has to be either a Model, an Association or an object. at C:\Users\FilipeCosta\desktop\FloraApp\server\node_modules\sequelize\lib\model.js:1976:15 at Array.map (native) at conformOptions (C:\Users\FilipeCosta\desktop\FloraApp\server\node_modules\sequelize\lib\model

Create a record and an associated record in one go

╄→гoц情女王★ 提交于 2019-12-11 05:49:51
问题 The Problem: Imagine I have two associated models, Library which has many Book s: var Library = sequelize.define('Library', { title: Sequelize.STRING, description: Sequelize.TEXT, address: Sequelize.STRING }); var Book = sequelize.define('Book', { title: Sequelize.STRING, description: Sequelize.TEXT, publish_date: Sequelize.DATE }); Library.hasMany(Book); Now, in order to create a Library and a single associated Book , I do: Library.create({ name: 'Roan Library', address: '123 Any St' }).then

Sequelize condition on joined table doesn't work with limit condition

前提是你 提交于 2019-12-11 05:13:51
问题 I have a Supplier model with associated Calendar models. I want to fetch suppliers who either have a calendar which is set to available don't have a calendar I can do this using the following: Supplier.findAll({ include: [ { model: Calendar, as: 'calendars', required: false, where: { start_time: { [Op.lte]: date }, end_time: { [Op.gte]: date }, }, }, ], where: { '$calendars.state$': { [Op.or]: [ { [Op.in]: ['available'] }, { [Op.eq]: null }, ], }, }, }); This generates the following SQL