balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.84k stars 1.95k forks source link

Default values for options mentioned in config/global.js file? #7298

Open chauhankiran opened 1 year ago

chauhankiran commented 1 year ago

Node version: 18.17.9 Sails version (sails): 1.5.7 ORM hook version (sails-hook-orm): 4.0.2 Sockets hook version (sails-hook-sockets): N/A Organics hook version (sails-hook-organics): N/A Grunt hook version (sails-hook-grunt): N/A Uploads hook version (sails-hook-uploads): N/A DB adapter & version (e.g. sails-mysql@5.55.5): Default that comes with sails-hook-orm Skipper adapter & version (e.g. skipper-s3@5.55.5):


For the purpose of learning, creating a Sails application from the scratch without sails new command. While doing so, I created a models folder and then create User.js file in it. In order to run this line in Controller,

const users = await User.find();

I need to create a file with name globals.js in config folder to mark models option true. Not just that but also need to mark _ to false, async to false, and sails to true. If the options are missing with the values, it throws error like this.

error: A hook (`userconfig`) failed to load!
error: Failed to lift app: userError: As of Sails v1, `sails.config.globals._` must be either `false` or a locally-installed version of Lodash (typically `require('lodash')`).  For more info, see http://sailsjs.com/config/globals

As _ (or other above mentioned options) can have either false or specific value, Is it possible to have the default value on Sails side e.g. _ is set to by default false by Sails and then using this config/globals.js file, one can override the option? Even further is it possible to work directly with User as model without creating globals.js file? I'm fine if I need to create config/models.js file as it logically make sense.

sailsbot commented 1 year ago

@chauhankiran Thanks for posting! We'll take a look as soon as possible.

In the mean time, there are a few ways you can help speed things along:

Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, disclose it responsibly.

For help with questions about Sails, click here.

chauhankiran commented 1 year ago

Something like this in lib/app/private/exposeGlobals.js. Just a suggestion. Please guide on this if it is incorrect or wrong.

Instead of throwing error, set false to the option.

// `sails.config.globals._` must be false or an object.
// (it's probably a function with lots of extra properties, but to future-proof, we'll allow any type of object)
if (sails.config.globals._ !== false) {
  if (!_.isObject(sails.config.globals._)) {
    // throw flaverr(
    //   { name: "userError", code: "E_BAD_GLOBAL_CONFIG" },
    //   new Error(
    //     "As of Sails v1, `sails.config.globals._` must be either `false` or a locally-installed version of Lodash (typically `require('lodash')`).  For more info, see http://sailsjs.com/config/globals"
    //   )
    // );
    global["_"] = false;
  }
  global["_"] = sails.config.globals._;
}
// `sails.config.globals.async` must be false or an object.
// (it's probably a plain object aka dictionary, but to future-proof, we'll allow any type of object)
if (sails.config.globals.async !== false) {
  if (!_.isObject(sails.config.globals.async)) {
    // throw flaverr(
    //   { name: "userError", code: "E_BAD_GLOBAL_CONFIG" },
    //   new Error(
    //     "As of Sails v1, `sails.config.globals.async` must be either `false` or a locally-installed version of `async` (typically `require('async')`)  For more info, see http://sailsjs.com/config/globals"
    //   )
    // );
    global["async"] = false;
  }
  global["async"] = sails.config.globals.async;
}

// `sails.config.globals.sails` must be a boolean
if (sails.config.globals.sails !== false) {
  if (sails.config.globals.sails !== true) {
    // throw flaverr(
    //   { name: "userError", code: "E_BAD_GLOBAL_CONFIG" },
    //   new Error(
    //     "As of Sails v1, `sails.config.globals.sails` must be either `true` or `false` (Tip: you may need to uncomment the `sails` setting in your `config/globals.js` file).  For more info, see http://sailsjs.com/config/globals"
    //   )
    // );
    global["sails"] = true;
  }
  global["sails"] = sails;
}

// `sails.config.globals.models` must be a boolean.
// `orm` hook takes care of actually globalizing models and adapters (if enabled)
if (
  sails.config.globals.models !== false &&
  sails.config.globals.models !== true
) {
  // throw flaverr(
  //   { name: "userError", code: "E_BAD_GLOBAL_CONFIG" },
  //   new Error(
  //     "As of Sails v1, `sails.config.globals.models` must be either `true` or `false` (you may need to uncomment the `models` setting in your `config/globals.js` file).  For more info, see http://sailsjs.com/config/globals"
  //   )
  // );
  sails.config.globals.models = true;
}

TODO: Also something for globals.js file as well.

DominusKelvin commented 1 year ago

Hey @chauhankiran nice tinkering, so ideally you'd want to scaffold a new Sails project with sails new so you get all the framework code necessary to have a Sails application, which includes setting up required Sails hooks(which include models), and loading core modules.

That said, if you'd like a course to sort of help you understand how to get started with Sails, you can check out the Getting Started with Sails course on Sailscasts(I'll gift you the course for free, just send over your email)

Also you can join the Sailscasts community to ask your Sails questions.

DominusKelvin commented 10 months ago

Hey @chauhankiran would you say this issue has been resolved yet or do you need further help 😃?