我在使用sequelize js从数据库中检索数据时遇到问题.我是NODEJS的新手.我不知道Promise和Promise.all是否内置函数所以我安装并在我的代码中也需要npm promise.以下是我的代码.
var Promise = require('promise');
var user_profile = new Promise(function(resolve, reject) {
db.user_profile.findOne({
where: {
profile_id: new_profile_id
}
}).then(user => {
console.log('Summary Result User found.');
resolve(user);
});
});
var all_reports = new Promise(function(resolve, reject) {
db.report.all().then(reports => {
console.log('Summary Result Reports found.');
resolve(reports);
});
});
var report_details = new Promise(function(resolve, reject) {
db.report_detail.findAll({
where: {
profile_id: new_profile_id
}
}).then(report_details => {
console.log('Summary Result Report Details found');
resolve(report_details);
});
});
var all_promises = Promise.all([user_profile, all_reports, report_details]).then(function(data) {
console.log('**********COMPLETE RESULT****************'); …Run Code Online (Sandbox Code Playgroud) 您好我在从GAE连接到Google Cloud SQL时遇到问题.
我的应用程序在docker容器内运行,这里是docker文件
FROM node:8.10.0-alpine
ARG NODE_ENV=production
ENV NODE_ENV=$NODE_ENV
# env like sql user db instance connection name
# Set a working directory
WORKDIR /usr/src/app
COPY ./build/package.json .
COPY ./build/yarn.lock .
# Install Node.js dependencies
RUN yarn install --production --no-progress
# Copy application files
COPY ./build .
COPY ./src/db/seeders ./seeds
COPY ./src/db/migrations ./migrations
COPY ./scripts ./scripts
RUN yarn run db:seed # -> failed to run this line
RUN yarn run db:migrate
# Run the container under "node" user by …Run Code Online (Sandbox Code Playgroud) google-app-engine node.js sequelize.js google-cloud-sql sequelize-cli
我无法让 Sequelize.js 软删除表中的行。我使用 Sequelize cli 来完成所有迁移,并且没有使用同步功能在启动时重新同步数据库。我的迁移和模型中有时间戳字段,甚至有deletedAt字段(模型也有偏执:true),无论如何它仍然删除该行而不是向deletedAt字段添加时间戳。我注意到,当进行任何查询时,它不会像我在一些教程中看到的那样在查询中添加deletedAt = NULL。我正在使用 Sequelize.js v3.29.0。
模型文件:
'use strict';
module.exports = function(sequelize, DataTypes) {
var Collection = sequelize.define('Collection', {
userId: {
type: DataTypes.INTEGER,
allowNull: false,
validate: {
isInt: true
}
},
name: {
type: DataTypes.STRING,
allowNull: false
},
description: DataTypes.TEXT,
createdAt: {
allowNull: false,
type: DataTypes.DATE
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE
},
deletedAt: {
type: DataTypes.DATE
}
}, {
classMethods: {
associate: function(models) {
Collection.belongsTo(models.User, { foreignKey: 'userId' })
}
}
}, {
timestamps: …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用sequilize generateUUID播种uuid,但出现此错误 Seed file failed with error: sequelize.Utils.generateUUID is not a function TypeError: sequelize.Utils.generateUUID is not a function
我如何播种UUID?
return queryInterface.bulkInsert('companies', [{
id: sequelize.Utils.generateUUID(),
name: 'Testing',
updated_at: new Date(),
created_at: new Date()
}]);
Run Code Online (Sandbox Code Playgroud) 我创建了一个迁移文件来添加一个列up,然后将其删除down.
这是迁移文件代码:
module.exports = {
up: (queryInterface, Sequelize) =>
queryInterface.addColumn('Books', 'Rating', {
allowNull: false,
type: Sequelize.ENUM('like', 'dislike'),
}),
down: (queryInterface, Sequelize) => {
queryInterface.removeColumn('Books', 'Rating');
},
};
Run Code Online (Sandbox Code Playgroud)
当我第一次使用db:migrate它时,它成功添加了列但是当我做了一个db:migrate:undo:all然后再次运行迁移时,它给我一个错误sqying
======= 20180211100937-AddedRatingIntoBooks: migrating
======= 2018-02-11 15:42:46.076 IST
[64531] ERROR: type "enum_Books_Rating" already exists 2018-02-11 15:42:46.076 IST
[64531] STATEMENT: CREATE TYPE "public"."enum_Books_Rating" AS ENUM('like', 'dislike');
ALTER TABLE "public"."Boo ks" ADD COLUMN "Rating" "public"."enum_Books_Rating";
ERROR: type "enum_Books_Rating" already exists
Run Code Online (Sandbox Code Playgroud)
该问题仍然住在这里.
我正在使用 docker-compose 建立一个测试基础设施。我想使用 docker-compose 选项--exit-code-from从运行测试的容器中返回退出代码。但是,我还有一个容器,它使用 sequelize cli 在我的数据库容器上运行迁移。当迁移完成然后我的测试运行时,此迁移容器以代码 0 退出。这会导致--exit-code-from和--abort-on-container-exit选项出现问题。迁移容器退出时有没有办法忽略?
尝试使用 Sequelize CLI 创建一个具有特定值的枚举字段的模型。
sequelize model:generate --name user --attributes name:string,login_method:enum('email','google')
Run Code Online (Sandbox Code Playgroud)
错误:bash:意外标记“(”附近出现语法错误
这样做的正确语法是什么?
有没有办法显示已经应用的迁移?
我想知道哪一次迁移是最后一次,以便我可以决定是否撤消它。
我正在开发第三方 NODE 模块,该模块处理发送电子邮件并将其存储在数据库中,所以我们称其为邮件模块。为了让某人使用它的功能,将其导入到他的项目中并使用其功能来发送和存储电子邮件就足够了。
这里的问题是,导入邮件模块的人需要手动创建数据库表来存储电子邮件,因为 Sequelize CLI 在单独的模块中看不到迁移脚本。在 mail-module 中有 Sequelize 迁移脚本,但对于开发人员来说,在模块中查找它比将其复制到自己的项目中并作为其项目的一部分运行更麻烦。
有没有办法避免这种手动工作并进行配置,以便当开发人员(邮件模块的用户)运行自己的迁移脚本时,也会执行邮件模块迁移脚本?
您好,我有一个表,我使用 JSONB 来存储嵌套 JSON 数据,并且需要查询此 JSONB 列下面是表的结构
{
"id": "5810f6b3-fefb-4eb1-befc-7df11a24d997",
"entity": "LocationTypes",
"event_name": "LocationTypes added",
"data": {
"event":{
"id": "b2805163-78f0-4384-bad6-1df8d35b456d",
"name": "builidng",
"company_id": "1dd83f77-fdf1-496d-9e0b-f502788c3a7b",
"is_address_applicable": true,
"is_location_map_applicable": true}
},
"notes": null,
"event_time": "2020-11-05T10:56:34.909Z",
"company_id": "1dd83f77-fdf1-496d-9e0b-f502788c3a7b",
"created_at": "2020-11-05T10:56:34.909Z",
"updated_at": "2020-11-05T10:56:34.909Z"
}
Run Code Online (Sandbox Code Playgroud)
下面的代码给出空白数组作为响应
const dataJson = await database.activity_logs.findAll({
where: {
'data.event.id': {
$eq: 'b2805163-78f0-4384-bad6-1df8d35b456d',
},
},
raw: true,
});
Run Code Online (Sandbox Code Playgroud)
有什么方法可以更好地使用sequelize 完成查询嵌套json 对象。
sequelize-cli ×10
sequelize.js ×9
node.js ×7
express ×2
orm ×2
postgresql ×2
docker ×1
jestjs ×1
mean-stack ×1
node-modules ×1