Auto-generate model definitions from existing ArangoDB database - javascript

I'm using ArangoDB 3.4 and planning to use an MVC framework like Backbone.js (or whatever is recommended). Is there a way to auto-generate the models from the existing database to reduce the amount of boilerplate code I have to write by hand?
For example, I am looking at the aye-aye TodoMVC demo. It has this model:
const joi = require('joi');
exports.Model = {
_key: joi.string().optional(),
_id: joi.string().optional(),
_rev: joi.string().optional(),
completed: joi.boolean().optional(),
order: joi.number().optional(),
title: joi.string().optional()
};
Writing a few by hand is no problem. My database will ultimately require many of these models. Are there any tools that I can use with ArangoDB that will help automate this by producing scaffold code?
What I have in mind is possibly something like Python's inspectdb command:
http://docs.djangoproject.com/en/dev/ref/django-admin/#inspectdb
inspectdb
Introspects the database tables in the database pointed-to by the DATABASE_NAME setting and outputs a Django model module (a models.py file) to standard output.
Use this if you have a legacy database with which you'd like to use Django. The script will inspect the database and create a model for each table within it.
As you might expect, the created models will have an attribute for every field in the table.
If there are entirely different approaches to doing this with ArangoDB and javascript please point me in the right direction.

django-admin inspectdb [table [table ...]] targets relational databases where tables have schema and because of that it's possible to generate model
ArangoDB is NoSQL with schemaless collections with ability to store various JSON documents types and because of that you'll need to get schema per document type.
While using fullstack javascript approach you can put your model in js module and use it on both front and backend.
for us, most reliable and scalable approach is based on Typescript as master with following-ish workflow
turn JSON to TS via VS Code extension json2ts.com
then you can
generate JSON Schema via typescript-json-schema
generate UML diagram with tsviz
convert JSON Schema to joi with enjoi
generate forms from JSON schema (front-end framework specific)

Related

How to use one relational(postGreSql) and one non-relational(MongoDb) database in single node.js application

I have to work on one Node Js project where I have to use two database in single application, One database is MongoDb and the other one is postgreSql. Till now I have only used only one dababase in one project. I just want to know that "Is it possible to use two different database as mentioned above in one Node Js project". If yes can you please provide me essential configs and plugin required to setup the project ?
yeah... NoSQL & another RDBMS together...because
NoSQL: fast and simple, but has little to none structure to enforce constraints on data.
RDBMS: satisfies all ACID properties, keeping your data safe and clean. But performance goes down rapidly as traffic and data set size grow.
for doing so ..
you can use an ODM (Object Document Mapper) like mongoose to deal with mongoDB
& an ORM (Object Relational Mapper) like sequalize to deal with mysql , postgre
As provided in the docs.. you got to install both
npm i -s mongoose sequelize

Db versioning/new changes with loopback

My service is built using Node + Loopback + Mongo.
I would like to drop the db at the startup if and only if at least one of the following two asserts is true:
any model schema has changed (note that I cannot use isActual() because that's usable only with relational DBs)
new data has been added/removed from the default data-set definition files (data folder)
I'm currently versioning the db using a custom logic comparing version table and a version unique var but I wonder if there is an easy way to check the two asserts above.
Any idea? Ty

Parse CSV into SQL Database

I've been tasked with parsing a CSV file and storing its data into an SQL database using Node.js. I'm a complete beginner with Node.js but have done similar tasks before in Rails. The CSV file given isn't like the previous ones I've used however and is in a different format.
Format of csv: http://imgur.com/a/gsQkl
I'm looking for any pointers on how to handle this task. Thanks
This question has 2 aspects,
How to do the task
How to do it with Node.JS
Regarding the first aspect - if you know how to do it with Rails it means that you should already know that the CSV example that you've provided is not just a table - it includes hierarchy which can be treated in multiple ways - either add category indicator and date fields to every row in order to flatten the table - or, create separate tables and connect them with external keys. Anyway - this has nothing to do with Node.JS, and you'll most probably have to "massage" your data before you enter it into SQL the database.
Regarding the second questions - in Node.JS you'll find modules to handle almost every task that you can imagine (some things can be done natively with the core modules, Google would be a good start in most cases)
in your case you'd need modules to handle CSV parsing, and SQL server connection
For CSV parsing you can use: https://github.com/wdavidw/node-csv
For SQL - you didn't mention which server are you using (SQL is a language used by many different database servers), assuming that you use one of the populars - this are the relevant modules:
MySQL - https://github.com/mysqljs/mysql
Microsoft SQL Server - https://github.com/patriksimek/node-mssql
PostgreSQL - https://github.com/brianc/node-postgres
Each one has its own interface - read the docs for further information

Search API between client and server in mongodb

I have my server in nodejs and client in angularjs, mongodb(mongoose) as database. I want my client to be able to make a search query with all the normal consitions put in like fixing few fields to certain values, search string contained in few of the fields etc
query can be
(value of field A can be 'x' or 'y') and
(value of field B can range between dates P and Q) and
(string s contained in fields C or D or E) few more
is there any npm plugin or standardisation I can follow to enrich my server side API and also directly put the expression while querying with out doing a map reduce with multiple queries(with lots of code).
Yes, there are a few. I only know of those that work with Mongoose so if you're using another driver you may need to modify them a bit.
With each of these you'll need to determine which to filter locally and which to let the server handle it. Where you filter should depend largely on how much data you have. Personally, it made more sense to abandon local filtering for my largest sets and simply rely on MongoDB to handle the workload.
angoose
Connecting Mongoose and Angular and More
The original motive for Angoose project is to do away with the dual
model declarations(server and client side) if we are building a rich
model based SPA application using modern Javascript framework such as
Angular and node.js. With both front end and backend using Javascript,
Angoose allows the server side models and services to be used in the
client side as if they reside in the client side.
Angoose depends on following frameworks and assumes you have basic
familarities with them:
mongoose
express
angular (optional, for non-angular app, jQuery is required)
Mongoose Api Query
Mongoose plugin that takes a set of URL params and constructs a query for use in a search API. Also, worst project name ever.
If you use Mongoose to help serve results for API calls, you might be
used to handling calls like:
/monsters?color=purple&eats_humans=true
mongoose-api-query handles
some of that busywork for you. Pass in a vanilla object (e.g.
req.query) and query conditions will be cast to their appropriate
types according to your Mongoose schema. For example, if you have a
boolean defined in your schema, we'll convert the eats_humans=true to
a boolean for searching.
It also adds a ton of additional search operators, like less than,
greater than, not equal, near (for geosearch), in, and all. You can
find a full list below.
When searching strings, by default it does a partial, case-insensitive
match. (Which is not the default in MongoDB.)
Angular Bridge
Link models easily via a REST interface between Mongoose/Node-Express/Angular.js
Create, read, update, delete MongoDB collections via AngularJS.
It uses :
Mongoose for accessing to Mongodb
Express for the routing
AngularJS (with the $resource method)

How to generate HTML Forms for a CouchApp ( or for any JSON documents )

Traditional web frameworks like Rails and Django have ways to generate HTML forms based on the "models", which correspond to the schema -- the TABLEs in the database.
How does that work with CouchDB? ( which has no tables and no schema )
Do you just create custom forms by hand, and let those act as your "model/schema"?
Do you have "models" defined in javascript? Does that allow auto-generated forms?
Do you have "models" or something similar defined as JSON documents?
Also, traditional HTML forms handle a single "record" -- with one value per "key".
JSON documents can have a list of values under one key, or another nested document! -- traditional HTML forms don't handle that!
JSON documents ( even documents that are of a known "doctype" ) could have "extra" ( undefined ) fields on them -- they could have missing fields -- they could have fields whose value is of a different type than what is expected.
All of these things are not handled by traditional HTML forms.
Is there a better way for a user to interact with a JSON document than traditional HTML forms?
The Kanso CouchApp framework provides the most comprehensive schema and form generating tools for CouchDB: http://kansojs.org
Take a look at the tutorial for example usage. The schema definitions were created from the group up to work with CouchDB and allow embedded objects and other complex JSON-like structures, it's an incredibly flexible format (much more so than JSON-schema).
You can also automatically generate forms from your schema definitions, or extend these with custom fields, widgets, validators, permissions, etc.
Although CouchDB holds json with no schema, you could define a json schema on the presentation side. And then you can feed this json schema to inputEx which will create a form for you.
See here:
http://neyric.github.com/inputex/examples/json-schema.html
InputEx is very flexible, and will handle arrays, objects, and special editors for properties. Just understand that the json schema standard is not well defined. If you are just using it to do CRUD operations, you should be fine.
Over time, if your form evolves and the schema changes, it really does not matter to couchdb. But you may need to provide users with a way to migrate data to a new schema.

Categories