gpt4 book ai didi

node.js - 在 FeathersJS 中添加信息以登录数据库

转载 作者:行者123 更新时间:2023-12-03 22:23:53 25 4
gpt4 key购买 nike

我似乎找不到如何在我的 FeathersJS 应用程序中登录数据库。
我更愿意在服务中指定数据库信息和登录信息,但我只需要它工作。
在 myApp/config/default.json 中有以下行:

"postgres": "postgres://postgress:@localhost:5432/feathers_db",

在:

http://docs.sequelizejs.com/en/latest/docs/getting-started/

它说像上面这样的字符串应该是:
"postgres": "postgres://username:user_password@localhost:5432/feathers_db",

但这不起作用。它也不是很像 Feathers,因为现在我的所有 postgress 事务都被锁定在一个 postgress 数据库中。

在 services/index.js 中有以下行:
const sequelize = new Sequelize(app.get('postgres'), {
dialect: 'postgres',
logging: false
});

我可以将上面的行自定义为 Sequelize 在他们的指南中所说的,并将用户名和密码作为参数,但是为什么模板还没有像这样布置呢?
还有这一行:
app.set('sequelize', sequelize);

如果我有多个 postgress 数据库,我该怎么办?我是否制作新的 Sequelize 对象并执行以下操作:
app.set('sequelize_db1', sequelize_db1);
app.set('sequelize_db2', sequelize_db2);

或者我是否指定数据库信息,包括服务模型中的用户信息?
如果使用通用数据库语言而不是 sequelize,Postgress 的登录过程是什么样的?

最佳答案

所以一句话"is"。我在我的问题中所问的一切都是肯定的。
我可以连接到数据库,如 sequelize 文档中所示。我还可以将 config.json 文件配置为具有我的用户和数据库名称的“postgres”配置。在创建新的 sequelize 对象时,我还可以将完整路径放在 services/index.js 文件中。检查是否存在连接的最佳方法是在创建新的 sequelize 对象后使用以下代码:

new_sequelize_object
.authenticate()
.then(function(err) {
console.log('Connection to the DB has been established successfully.');
})
.catch(function (err) {
console.log('Unable to connect to the database:', err);
});

(取自: http://docs.sequelizejs.com/en/latest/docs/getting-started/)

还可以定义多个 sequelize 对象并在应用程序中设置它们。然后在特定服务的 index.js 文件中定义模型时,将新绑定(bind)的名称放在 app.get('new_sequelize_object') 中。

这是定义了两个数据库的 services/index.js 文件:
'use strict';
const service1 = require('./service1');
const authentication = require('./authentication');
const user = require('./user');
const Sequelize = require('sequelize');
module.exports = function() {
const app = this;

const sequelize = new Sequelize('feathers_db1', 'u1', 'upw', {
host: 'localhost',
port: 5432,
dialect: 'postgres',
logging: false
});

const sequelize2 = new Sequelize('postgres://u1:upw@localhost:5432/feathers_db2', {
dialect: 'postgres',
logging: false
});

app.set('sequelize', sequelize);
app.set('sequelize2', sequelize2);

sequelize
.authenticate()
.then(function(err) {
console.log('Connection to sequelize has been established successfully.');
})
.catch(function (err) {
console.log('Unable to connect to the database:', err);
});

sequelize2
.authenticate()
.then(function(err) {
console.log('Connection has been established to sequelize2 successfully.');
})
.catch(function (err) {
console.log('Unable to connect to the database:', err);
});


app.configure(authentication);
app.configure(user);
app.configure(service1);
};

这是使用服务 sequelize2 的 service1/index.js 文件:
'使用严格';
const service = require('feathers-sequelize');
const service1 = require('./service1-model');
const hooks = require('./hooks');

module.exports = function(){
const app = this;

const options = {
//Here is where one sets the name of the differeng sequelize objects
Model: service1(app.get('sequelize2')),
paginate: {
default: 5,
max: 25
}
};

// Initialize our service with any options it requires
app.use('/service1', service(options));

// Get our initialize service to that we can bind hooks
const service1Service = app.service('/service1');


// Set up our before hooks
service1Service.before(hooks.before);
// Set up our after hooks
service1Service.after(hooks.after);
};

关于node.js - 在 FeathersJS 中添加信息以登录数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40842530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com