帆 js / waterline / mysql table 名称和 table 前缀

sails js / waterline / mysql table name and table prefix

我是 sails.js 的新手。我想 select 来自 table 的所有记录。如何使用 .find() .

特别是 waterline 如何知道我要从哪个 table 获取数据?因为我们没有在模型中提及任何 table 名称。我知道有 .query()。但这在水线基本创建/更新/查找/删除方法中可能吗?

另一个问题如何在 sails.js 中为 table 名称使用前缀?就像我想使用 sails_product 作为 table 名称。

I am new to sails.js. I want to select all record from a table. How to use .find() .

如果您的型号名称是,例如,Book,您将 select 所有 Book 记录

Book.find()
    .exec(function(err, books) {
        if (err) return res.serverError();
        console.log(books); // 'books' is an array of the found records
    })

Specially how waterline will know from which table i want data ? Because we are not mentioning any table name in model. I know there is .query(). But is this possible within waterline basic create / update / find / delete method ?

是的,这是可能的。您根本不必处理 table 名称等水线,您所需要的只是您的 模型名称。 创建、更新、删除所有工作的方式与上面的 find 示例 - 所以 ModelName.actionName().

Another question how to use prefix for table name in sails.js ? Like i want to use sails_product as table name.

默认情况下,waterline 使用小写的模型名称作为相应的 table 名称。但是,您可以在模型设置中覆盖它。例如,如果您在名为 Book.js 的文件中定义了模型,其内容将如下所示:

module.exports = {

    attributes: {
        name: {
            type: 'String',
            required: true
        },
        price: {
            type: 'float'
        }
    },

    tableName: 'custom_book_table'

}

这样,在数据库中创建的实际 table 将被称为 custom_book_table,而您仍然会在 find 查询等中将您的模型称为 Book.

这里有指向两个 Waterline and Sails 文档的链接,可以帮助您进行操作。在 model/query 相关问题中,我肯定会先从 Waterline 文档中搜索。