You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Sails.js和sails-MySQL连接MySQL,是否无需定义模型?

关于Sails.js连接现有MySQL数据库的问题解答

Great question! Let's break this down clearly for your use case:

1. 可以不定义模型直接连接MySQL数据库

Absolutely! You don't have to create Sails.js models to interact with your existing MySQL tables, stored procedures, or functions. Sails.js lets you bypass its ORM (Waterline) and use raw database queries directly.

Here's how you can do it:

  • First, make sure your config/datastores.js is properly configured with your MySQL credentials (you probably already have this set up for sails-mysql).
  • Then, in any controller, service, or hook, you can access the raw database connection and run native SQL queries using sendNativeQuery:
// Example in an api/controllers/SomeController.js
async function getUsers(req, res) {
  try {
    // Get the configured datastore (use your datastore name, usually 'default')
    const datastore = sails.getDatastore('default');
    
    // Run a raw SELECT query
    const rawResult = await datastore.sendNativeQuery('SELECT * FROM your_existing_table WHERE status = ?', ['active']);
    
    // Access the returned rows
    return res.ok(rawResult.rows);

    // To call a stored procedure, just use the CALL syntax:
    // const spResult = await datastore.sendNativeQuery('CALL your_stored_procedure(?, ?)', [param1, param2]);
  } catch (err) {
    return res.serverError(err);
  }
}

This approach lets you fully leverage the tables, functions, and stored procedures you've already created in Workbench without touching Sails models at all.

2. 不需要重新创建模型文件并定义数据库Schema

If you do want to use Sails.js's ORM features (like auto-generated CRUD methods, validation, or associations) with your existing database, you still don't need to recreate your schema. You can map Sails models directly to your existing tables:

  • Create a model file (e.g., api/models/YourModel.js) and configure it to point to your existing table:
module.exports = {
  // Explicitly set the table name to match your existing MySQL table
  tableName: 'your_existing_table',
  
  // Disable schema enforcement so Waterline doesn't require model attributes to match the table exactly
  schema: false,
  
  // Optional: Define only the attributes you need to use in your app (you don't have to list every table column)
  attributes: {
    id: {
      type: 'number',
      columnName: 'custom_id' // Use this if your table's primary key has a different name than the model attribute
    },
    username: { type: 'string' },
    // Add other fields as needed
  }
};

With this setup, Sails will use your existing table structure instead of trying to create new tables. You can still use all the standard Waterline methods (like .find(), .create()) while working with your pre-existing database objects.


内容的提问来源于stack exchange,提问作者fariba.j

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:54:41