Last active
September 22, 2021 17:49
-
-
Save molda/17642801099abc505626f62844471f22 to your computer and use it in GitHub Desktop.
Quick API server for testing - Total.js, Schemas, embedded flat file database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('total.js').http('release'); // default port 8000 | |
NEWSCHEMA('Product').make(function(schema) { | |
schema.define('name', String, true); | |
schema.define('price', Number, true); | |
schema.define('description', String); | |
// Query products | |
schema.setQuery(function(error, options, callback) { | |
// pagination | |
options.page = U.parseInt(options.page) - 1; | |
options.max = U.parseInt(options.max, 20); | |
// if page not specified set it to 0 | |
if (options.page < 0) | |
options.page = 0; | |
// number of items to return | |
var take = U.parseInt(options.max); | |
// number of items to skip | |
var skip = U.parseInt(options.page * options.max); | |
// NOSQL is total.js embedded database | |
// https://docs.totaljs.com/latest/en.html#api~Database | |
var filter = NOSQL('products').find(); | |
filter.take(take); | |
filter.skip(skip); | |
if(options.sort) filter.sort(options.sort); | |
filter.callback(function(err, docs, count) { | |
// let's create object which will be returned | |
var data = {}; | |
data.count = count; | |
data.items = docs; | |
data.limit = options.max; | |
data.pages = Math.ceil(data.count / options.max) || 1; | |
data.page = options.page + 1; | |
callback(data); | |
}); | |
}); | |
// Get single product by id | |
schema.setGet(function(error, model, id, callback) { | |
NOSQL('products') | |
.one() | |
.where('id', id) | |
.callback(function(err, product){ | |
callback({success: !!product, data: product}); | |
}); | |
}); | |
// Save the product into the database | |
schema.setSave(function(error, model, options, callback) { | |
// if there's no id then it's an insert otherwise update | |
var isNew = model.id ? false : true; | |
// create id if it's new | |
if(isNew) model.id = UID(); //UID returns string such as 16042321110001yfg | |
NOSQL('products') | |
.upsert(model) // update or insert | |
.where('id', model.id) | |
.callback(function() { | |
callback({success: true, id: model.id}); | |
}); | |
}); | |
// Remove a specific product | |
schema.setRemove(function(error, id, callback) { | |
NOSQL('products') | |
.remove() | |
.where('id', id) | |
.callback(function(){ | |
callback({success: true}); | |
}); | |
}); | |
}); | |
// URL Handler Flags Method | |
F.route('/api/products', query, ['*Product']); // GET (default) | |
F.route('/api/products/{id}', get, ['*Product']); // GET (default) | |
F.route('/api/products', save, ['*Product', 'post']); // POST | |
F.route('/api/products/{id}', remove, ['*Product', 'delete']);// DELETE | |
function query() { | |
// `this` is an instance of Controller | |
// https://docs.totaljs.com/latest/en.html#api~FrameworkController | |
var self = this; | |
// $query is Schema method that we defined using `schema.setQuery(...)` | |
self.$query(self.query, self.callback()); | |
// instead of self.callback you can supply your own callback function | |
// self.$query(self.query, function(err, result){ | |
// self.json(result); | |
// }); | |
} | |
function get(id) { | |
// the id is parsed by framework from url `/api/products/{id}` | |
var self = this; | |
// $get is Schema method that we defined using `schema.setGet(...)` | |
this.$get(id, self.callback()); | |
} | |
function save() { | |
var self = this; | |
// $save is Schema method that we defined using `schema.setSave(...)` | |
// the $save method is available on this.body for POST request | |
this.body.$save(self.callback()); | |
// another way to use it is | |
// this.$save(this.body, self.callback()); | |
} | |
function remove(id) { | |
var self = this; | |
// $remove is Schema method that we defined using `schema.setRemove(...)` | |
this.$remove(id, self.callback()); | |
} | |
F.on('request', function(req, res){ | |
console.log(`[${req.method}] ${req.url}`); | |
// outputs e.g. `[GET] /api/products` | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment