Sails.js: dynamic table name on models

I created a JS logger application in Sails.js and everything looks great. Now I'm going to scale the application: I need to use several tables for the same model (for example, a table for session1 sesson2 , etc. Based on id).

Let's say that for the "Pageview" model, I will use different tables, such as "Pageview1", "Pageview2", etc.

How can I dynamically determine the table name of this model, so I can change it for each query according to a parameter or attribute.

So far i tried this way

 var tableId = 2; Pageview.tableName = "pageview" + tableId; Pageview.create(values, function...); 

This code does not interrupt the application or cause any errors, but the record was saved in the default table, not the one I wanted.

+6
source share
1 answer

I don't think it is possible, but when I read this

I think you can use a custom format to dynamically determine table names.

If you insert a row into a table with a primary key of auto-increment, you can get the insert identifier as follows:

 connection.query('INSERT INTO posts SET ?', {title: 'test'}, function(err, result) { if (err) throw err; console.log(result.insertId); }); 

if this is true for rows, then it may be valid for tables.

0
source

Source: https://habr.com/ru/post/956110/


All Articles