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
Related
I created an express server for connection to my mongodb. Before I start, everything works fine.
But I dont understand some things.
I made a collection in my mongodb, projects (with an 's' at the end)
In my code I made a project.model.js like: const Project = mongoose.model("Project", ...) module.exports = Project
Elsewhere I have db.project = require("./project.model");
Everything works like a harm, creating, updating, deleting. But I dont understand. My collections in my mongodb don't have the same name. The names are Users, Materials and in my code I use user, material. Maybe it is a stupid question but how does this work?
It is the default behavior of mongoose to convert singular to plural model names.
To fix your issue & to convert your model name from plural to singular or any other name, You can write your code as shown below
const Project = mongoose.model("Project", ProjectSchema, 'Project')
module.exports = Project
Now you could see the collection name as Project.
The third parameter in the mongoose.model is the collection name.
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)
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
Some Sequelize users of SQL databases often use sync({ force: true }) in the early days, and then switch over to migrations. I'm confused because the migrations and models seem to have a similar syntax, but also a number of differences that don't seem to be strongly highlighted.
Some observations (which may be wrong):
the options objects in the two are different: options.indexes object in the model doesn't work in migrations (but works in models)
Custom index names using unique: 'somename' doesn't work in the migration field attributes (but works in models); but a unique: true works in both
foreign keys need to be explicitly created - and a references needs to be set in migrations (but in models, the hasMany/belongsTo automatically generates the foreign key field and foreign key reference)
id, createdAt and updatedAt must be manually created in a migration, but not a model
The last two make sense as it's delineating the migration responsibilities (which sets up the db schema) versus the model - but what the migration supports/how it works seems different for similar items, and the sync option exacerbates my confusion.
Some questions:
What are the main differences to keep in mind between migrations and models in Sequelize (not the general philosophy between the two)? What options in models (when using the sync option) do work in migrations?
Do others simply copy model files into migrations when switching from sync to discrete migrations? Do you cut out parts of the extraneous info that only works in models? Only works in migrations?
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)