Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 20, 2020 12:59 am GMT

rapid prototyping with json file database

What do you think? How many good ideas never got started? Rapid Prototyping, is the art of getting up an initial version of an app. A version good enough for showing an idea, proving that an idea is feasible.

That often is a frontend. How fast can you run create react app, add a material UI, maybe with the help of material or ant design let users login, insert and present data.

For me as a backend engineer it is often more about the nodejs app. How quick can I create an express app with session middleware and some endpoints to insert and query data. Can it be faster setup using lookback?

Personally I never used loopback, sails or such productive, they both have an inMemory datastore. I mostly use more unopinionated libraries.

The part that stoped most of my projects from actually getting started is setting up the DB. Docker is not stable on my mac nor on my Windows PC. That is why I sometimes look into file datastores.

For frontend developers there is the fantastic json-server. It provides rest API to a json file very quickly in a single command. As a backend developer we can use SQLite, nedb (outdated/not maintained), low-db that is inside json-server.

But the problem I found is, that the code written with these solutions is much different from actual production applications made with a sophisticated database.

Solution

That is why I created the node module trdb. A json file database that feels like a real db with asyncronouse API and the data inside the db are isolated objects, that will not unexpectedly mutate.

While json-server let you build the frontend before the API is ready, With trdb you can implement the API before the db is ready.

//createadbconstdb=newFileDB('./db.json');//createacollectionconstusers=db.collection('users');//insertauser(seewithoutid!)constuser=awaitusers.insert({name:'TobiasNickel',job:'technicallead',});//updateandsavetheuseruser.favoriteHobby='programming';awaitusers.save(user);awaitusers.insertMany([{ name:'SebastianSanchez' },{ name:'FiratEvander' },{name:'SilpaJanz' }]);//Wheninsertingorsaving,internallyanewcopyoftheobjectiscreated//nextthereloadeduserrepresentsthesameuserinDB,butisaseparateobject.//thisisthesamebehavioryougetfromarealdatabasesorAPI.constuserReloaded=awaitusers.findOne({id:user.id});console.log(user!==userReloaded);//true//passinthepropertiesalltheresultingitemsshouldhave.//alsoallowtopassarraysforthentheobjectsvalueneedtobeincluded.constsearchedUsers=awaitusers.find({name:['FiratEvander','TobiasNickel']});console.log(searchedUsers.length);//removingitemsjustworksjustlikesearch,iffirstsearchthenremoves.awaitusers.remove({name:'TobiasNickel'});awaitusers.remove({id:user.id});//Feelfreetocreatemultipledbstoputcollectionsinseparatefiles.//ThisexamplealsoshowstheoptionsforcustomidNameand//newId.ThisthenewIddefaultstouuidv4,andprovidesamethodfor//generatingautoIncrementIds.YoucanimplementyourownIDfunctions,//returningandprimitivelikenumbersandstrings.constpostsDB=newFileDB('posts.json',{idName:'postId',newId:newAutoIncrementId});constposts=db.collection('posts');
Enter fullscreen mode Exit fullscreen mode

The db also watches the file. during development and testing you can always see what is inside the db file. When you edit that file, it is instandly loaded into the server process.

Result

I was about to study oauth2 and open connectId. To play with that, you need three services. The authentication/authorization service, an API service and the app service. Each want to have its own db. With trdb setting up these services was quick and I can concentrate on the logic, rather than setup.

What do you think of my solution? How do you build quick prototypes.


Original Link: https://dev.to/bias/rapid-prototyping-with-json-file-database-5821

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To