[[oktatas:web:back-end_framework:sails|< Sails]] ====== Sails - REST API ====== * **Szerző:** Sallai András * Copyright (c) Sallai András, 2023 * Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC Attribution-Share Alike 4.0 International]] * Web: https://szit.hu ===== Projekt készítése ===== REST API projekt készítése: sails new app01 --no-frontend A kimenet ehhez hasonló: $ sails new app01 sails new app09 --no-frontend info: Installing dependencies... Press CTRL+C to cancel. (to skip this step in the future, use --fast) info: Created a new Sails app `app-09`! A következő könyvtárszerkezet jön létre: app01/ |-api/ | |-controllers/ | |-helpers/ | |-models/ | `-policies/ |-config/ | |-env/ | | `-production.js | |-locales/ | | |-de.json | | |-en.json | | |-es.json | | `-fr.json | |-blueprints.js | |-bootstrap.js | |-custom.js | |-datastores.js | |-globals.js | |-http.js | |-i18n.js | |-local.js | |-log.js | |-models.js | |-policies.js | |-routes.js | |-security.js | |-session.js | |-sockets.js | `-views.js |-node_modules/ |-app.js |-package.js |-package-lock.json `-README.md ===== EmployeesController ===== Az employees kontroller generálása: sails generate api employees Létrejön egy api/controllers/EmployeesController.js fájl. /** * EmployeesController * * @description :: Server-side actions for handling incoming requests. * @help :: See https://sailsjs.com/docs/concepts/actions */ module.exports = { }; Létrejön egy api/models/Employees.js fájl. /** * Employees.js * * @description :: A model definition represents a database table/collection. * @docs :: https://sailsjs.com/docs/concepts/models-and-orm/models */ module.exports = { attributes: { // ╔═╗╦═╗╦╔╦╗╦╔╦╗╦╦ ╦╔═╗╔═╗ // ╠═╝╠╦╝║║║║║ ║ ║╚╗╔╝║╣ ╚═╗ // ╩ ╩╚═╩╩ ╩╩ ╩ ╩ ╚╝ ╚═╝╚═╝ // ╔═╗╔╦╗╔╗ ╔═╗╔╦╗╔═╗ // ║╣ ║║║╠╩╗║╣ ║║╚═╗ // ╚═╝╩ ╩╚═╝╚═╝═╩╝╚═╝ // ╔═╗╔═╗╔═╗╔═╗╔═╗╦╔═╗╔╦╗╦╔═╗╔╗╔╔═╗ // ╠═╣╚═╗╚═╗║ ║║ ║╠═╣ ║ ║║ ║║║║╚═╗ // ╩ ╩╚═╝╚═╝╚═╝╚═╝╩╩ ╩ ╩ ╩╚═╝╝╚╝╚═╝ }, }; ===== A model elkészítése ===== /** * Employees.js * * @description :: A model definition represents a database table/collection. * @docs :: https://sailsjs.com/docs/concepts/models-and-orm/models */ module.exports = { attributes: { name: 'string', city: 'string', salary: 'number' }, }; ===== Indítás ===== A szerver indítása: sails lift Válasszuk az alter lehetőséget. Lehetséges kimenet részlete: 1. FOR DEV: alter wipe/drop and try to re-insert ALL my data (recommended) 2. FOR TESTS: drop wipe/drop ALL my data every time I lift Sails 3. FOR STAGING: safe don't auto-migrate my data. I will do it myself Read more: sailsjs.com/docs/concepts/models-and-orm/model-settings#?migrate -------------------------------------------------------------------------------- What would you like Sails to do this time? ** NEVER CHOOSE "alter" or "drop" IF YOU ARE WORKING WITH PRODUCTION DATA ** prompt: ?: alter ===== Megtekintés böngészőben ===== http://localhost:1337 http://localhost:1337/employees A lehetséges eredmény: [] ===== Alapértelmezett adatbázis ===== Az alapértelmezett adatbázis egy fejlesztői localdisk adatbázis a következő helyen: .tmp/