Shared Modules
In Less, all of your functions (API routes, Web Socket channels & connection handlers, Topic Subscribers, CRON jobs, and Functions) are isolated serverless functions. This means that you cannot import code between functions.
Less solves this by allowing you to create shared modules. The only change in your code is where you import from.
Creating Shared Module
In order to share code between your Less functions, add your modules to the shared
folder.
mkdir -p less/shared
Here's an example of shared Models that we can use in our functions:
mkdir less/shared/models
With Less you can organize your modules as you normally would.
- Node.js
- Python
─ less
└─ shared
└─ models
└─ index.js
└─ User
└─ index.js
└─ create
└─ index.js
└─ schema.js
─ less
└─ shared
└─ models
└─ __init__.py
└ ─ User
└─ __init__.py
└─ create
└─ __init__.py
└─ schema.py
Importing Shared Modules
Here's how you would import the example Model above:
- Node.js
- Python
less/apis/demo/hello/get.js
const { User } = require('models');
less/apis/demo/hello/get.py
from models import User