├── .gitignore ├── LICENSE ├── README.md ├── config ├── controllers └── api.js ├── database.sql ├── definitions ├── db.js ├── func.js └── service.js ├── index.js ├── package.json └── schemas └── products.js /.gitignore: -------------------------------------------------------------------------------- 1 | /index.js.json 2 | /tmp/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Total.js 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Empty project: PostgreSQL database 2 | 3 | - [Documentation](https://docs.totaljs.com) 4 | - [Join Total.js Telegram](https://t.me/totaljs) 5 | - [Support](https://www.totaljs.com/support/) 6 | 7 | __Instructions__: 8 | 9 | - download example 10 | - create a new database in the PostgreSQL database and run `database.sql` script 11 | - install dependencies `$ npm install` 12 | - update `/config` file by your connection string to PostgreSQL 13 | - run `$ node index.js` 14 | - open browser `http://127.0.0.1:8000` 15 | -------------------------------------------------------------------------------- /config: -------------------------------------------------------------------------------- 1 | // Connection string 2 | database : postgresql://user:pass@localhost:5432/YOUR_DB_NAME -------------------------------------------------------------------------------- /controllers/api.js: -------------------------------------------------------------------------------- 1 | exports.install = function() { 2 | 3 | ROUTE('GET /', index); 4 | 5 | // Products 6 | ROUTE('GET /api/products/ *Products --> list'); 7 | ROUTE('GET /api/products/{id}/ *Products --> read'); 8 | ROUTE('POST /api/products/ *Products --> create'); 9 | ROUTE('PUT /api/products/{id}/ *Products --> update'); 10 | ROUTE('DELETE /api/products/{id}/ *Products --> remove'); 11 | 12 | }; 13 | 14 | function index() { 15 | this.plain('PostgreSQL API example'); 16 | } -------------------------------------------------------------------------------- /database.sql: -------------------------------------------------------------------------------- 1 | -- ============================== 2 | -- TABLES 3 | -- ============================== 4 | 5 | CREATE TABLE "public"."cl_config" ( 6 | "id" text NOT NULL, 7 | "type" text, 8 | "value" text, 9 | "name" text, 10 | "dtcreated" timestamp DEFAULT now(), 11 | "dtupdated" timestamp, 12 | PRIMARY KEY ("id") 13 | ); 14 | 15 | CREATE TABLE "public"."tbl_product" ( 16 | "id" text NOT NULL, 17 | "name" vtext, 18 | "price" numeric DEFAULT 0, 19 | "dtcreated" timestamp DEFAULT now(), 20 | "dtupdated" timestamp, 21 | PRIMARY KEY ("id") 22 | ); 23 | 24 | -- ============================== 25 | -- DATA 26 | -- ============================== 27 | 28 | -- INSERT DEFAULT CONFIGURATION 29 | INSERT INTO "public"."cl_config" ("id", "type", "value", "name", "dtcreated") VALUES 30 | ('author', 'string', 'Peter Sirka', 'Author', NOW()), 31 | ('email', 'string', 'info@totaljs.com', 'Email', NOW()), 32 | ('name', 'string', 'Your app name', 'Name', NOW()), 33 | ('totalapi', 'string', '', 'Total.js API services token', NOW()), 34 | ('mail_smtp', 'string', 'localhost', 'SMTP host', NOW()), 35 | ('mail_smtp_options', 'json', '{"user":"","password":"","timeout":2000}', 'SMTP options', NOW()), 36 | ('url', 'string', 'https://YOURDOMAIN.com', 'URL address', NOW()); 37 | 38 | -- INSERT PRODUCTS 39 | INSERT INTO "public"."tbl_product" ("id", "name", "price") VALUES 40 | ('9h2w001bd41d', 'iPhone', 699), 41 | ('9h2w002bd40d', 'iMac', 1699), 42 | ('9h2w003bd41d', 'iPad', 999), 43 | ('9h2w004bd40d', 'AriPods', 199); -------------------------------------------------------------------------------- /definitions/db.js: -------------------------------------------------------------------------------- 1 | require('querybuilderpg').init('', CONF.database, 1, ERROR('DB')); -------------------------------------------------------------------------------- /definitions/func.js: -------------------------------------------------------------------------------- 1 | // Loads app configuration from database 2 | FUNC.refresh_config = async function(callback) { 3 | var response = await DATA.find('cl_config').fields('id,type,value').promise(); 4 | 5 | // The method below expects Array Object { id:String, type:String, value: string } 6 | // Value is automatically converted to defined type by the framework 7 | // More in documentation: Total.js/Globals section 8 | LOADCONFIG(response); 9 | 10 | callback && callback(); 11 | }; -------------------------------------------------------------------------------- /definitions/service.js: -------------------------------------------------------------------------------- 1 | ON('ready', function() { 2 | 3 | // Loads configuration after the application is ready to use 4 | FUNC.refresh_config(); 5 | 6 | }); 7 | 8 | ON('service', function(counter) { 9 | 10 | // Configuration will be reloaded each 20 minutes 11 | if (counter % 20 === 0) 12 | FUNC.refresh_config(); 13 | 14 | }); -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // =================================================== 2 | // Total.js start script 3 | // https://www.totaljs.com 4 | // =================================================== 5 | 6 | const options = {}; 7 | 8 | // options.ip = '127.0.0.1'; 9 | // options.port = parseInt(process.argv[2]); 10 | // options.unixsocket = require('path').join(require('os').tmpdir(), 'app_name'); 11 | // options.unixsocket777 = true; 12 | // options.config = { name: 'Total.js' }; 13 | // options.sleep = 3000; 14 | // options.inspector = 9229; 15 | // options.watch = ['private']; 16 | // options.livereload = 'https://yourhostname'; 17 | 18 | // Enables cluster: 19 | // options.cluster = 'auto'; 20 | // options.cluster_limit = 10; // max 10. threads (works only with "auto" scaling) 21 | 22 | // Enables threads: 23 | // options.cluster = 'auto'; 24 | // options.cluster_limit = 10; // max 10. threads (works only with "auto" scaling) 25 | // options.timeout = 5000; 26 | // options.threads = '/api/'; 27 | // options.logs = 'isolated'; 28 | 29 | var type = process.argv.indexOf('--release', 1) !== -1 || process.argv.indexOf('release', 1) !== -1 ? 'release' : 'debug'; 30 | require('total4/' + type)(options); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "emptyproject-postgresql", 3 | "description": "Total.js + PostgreSQL empty project", 4 | "version": "1.0.0", 5 | "main": "index.js", 6 | "dependencies": { 7 | "total4": "latest", 8 | "querybuilderpg": "latest" 9 | }, 10 | "scripts": { 11 | "start": "node index.js 8000" 12 | }, 13 | "keywords": ["empty", "project", "postgresql", "total4", "dbms"], 14 | "author": "Peter Širka", 15 | "license": "MIT" 16 | } -------------------------------------------------------------------------------- /schemas/products.js: -------------------------------------------------------------------------------- 1 | NEWSCHEMA('Products', function(schema) { 2 | 3 | schema.action('list', { 4 | name: 'List of products', 5 | action: function($) { 6 | // Performs pagination automatically, sort and all checks 7 | DATA.list('tbl_product').autoquery($.query, 'id:String, name:String, price:Number, dtcreated:Date, dtupdated:Date', 'dtcreated_desc', 50).callback($); 8 | // Or you can use a simple query via: 9 | // DB().find('tbl_product').callback($); 10 | } 11 | }); 12 | 13 | schema.action('read', { 14 | name: 'Read product', 15 | params: '*id:String', 16 | action: async function($) { 17 | 18 | var params = $.params; 19 | 20 | // Performs query 21 | // An error will be returned if no records cannot be read 22 | var item = await DATA.read('tbl_product').id(params.id).error('@(Product not found)').promise($); 23 | 24 | $.callback(item); 25 | } 26 | }); 27 | 28 | schema.action('create', { 29 | name: 'Create product', 30 | input: '*name:String, *price:Number', 31 | action: function($, model) { 32 | 33 | // Assigns additional values 34 | model.id = UID(); 35 | model.dtcreated = new Date(); 36 | 37 | // Performs query 38 | DATA.insert('tbl_product', model).callback($.done(model.id)); 39 | } 40 | }); 41 | 42 | schema.action('update', { 43 | name: 'Update product', 44 | input: '*name:String, *price:Number', 45 | params: '*id:String', 46 | action: function($, model) { 47 | 48 | var params = $.params; 49 | 50 | // Assigns additional values 51 | model.dtupdated = new Date(); 52 | 53 | // Performs update 54 | // An error will be returned if no records cannot be udpated 55 | DATA.modify('tbl_product', model).id(params.id).error('@(Product not found)').callback($.done(params.id)); 56 | } 57 | }); 58 | 59 | schema.action('remove', { 60 | name: 'Remove product', 61 | params: '*id:String', 62 | action: async function($) { 63 | 64 | var params = $.params; 65 | 66 | // An error will be returned if no records cannot be removed 67 | DATA.remove('tbl_product').id(params.id).error('@(Product not found)').callback($.done(params.id)); 68 | } 69 | }); 70 | 71 | }); --------------------------------------------------------------------------------