I want to drop only one table when ever sailsjs lift - mongodb

I want to migrate only one table to drop whenever sails lift I wanted to delete that table. How it is possible to do? Or is there any way to delete table after sails lift?
I already tried to add migrate : 'drop' in that particular model but it won't work.
I am currently using sails 1.0 version

I found a way.
Sails Bootstrap
This runs before sails lifted
I drop the table here.
config/bootstrap.js
module.exports.bootstrap = async function() {
await Users.destroy({});
};

Manually Delete
if you want to delete collection manually, you can use this command in shell.
db.collection.drop()
check this link for more detail click here
Delete via Sails.js
to use low-level native code for mongodb
// Get access to the native MongoDB client via the default Sails datastore.
var db = sails.getDatastore().manager;
db.collection('users').drop()
find link here link

In sails.js structure there is one file called bootstrap which located in config folder.
bootstrap file execute before sails server is lift.
so whatever you wan't to do before server lifting you can write code in this bootstrap file.
module.exports.bootstrap = function(callback) {
Users.destroy({},(error,response)=>
{
if(error) { console.log("error while deleting table"); }
else { callback(); }
});
};

Related

EntityFrameworkCore: How to initialize a Database and seed it the first time user uses an application

I have build a project using Microsoft Visual Studio 2015 and EntityFrameworkCore.
I have seed manually a couple of dummy data and I was developing my solution. Now, I want to deploy the in the server, but I get the problem that by starting the application the first time, it crash since it does not find a data base and data.
I have googled and I find the solution for Visual Studio 2013 and previous using the CreateDatabaseIfNotExists class that need the package: System.Data.Entity
(http://www.entityframeworktutorial.net/code-first/database-initialization-strategy-in-code-first.aspx), however, such classes and packages do not exist in EntityFrameworkCore.
How does I create and populate a database with at least one row if user is using my application by the first time in EntityFrameworkCore?
or which is the equivalent to System.Data.Entity in Entity Framework Core?
Rowan Miller says that ApplyMigrations is enough to create database (if not exist) and apply all (nesessary) migrations.
Create method like this:
public void CreateDbAndSampleData(IServiceProvider applicationServices)
{
using (var serviceScope = applicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
using (var db = serviceProvider.GetService<ApplicationDbContext>())
{
// This will [try to] create database
// and apply all necessary migrations
db.Database.AsRelational().ApplyMigrations();
// then you can check for existing data and modify something
var admin = db.Users.Where(x => x.Name == "Superadmin").FirstOrDefault();
if (admin == null)
{
db.Users.Add(new User {...});
db.SaveChanges();
}
}
}
}
And call it from your Startup.cs, at end of Configure method:
CreateDbAndSampleData(app.ApplicationServices);
This code will run on every app startup, so you need to be accurate and do not overwrite any non-critical data changes (like changing Users's comment etc)
You can use MusicStore app as a sample: Startup.cs and SampleData.cs

Meteor.subscribe on server side

I want to create a backend service which monitors a mongodb collection for new entries. As those are being created, I wish to run processing and update them.
I thought doing so with a Meteor service/app would be a wise idea because Meteor uses 'oplog tailing' which seems ideal for this purpose (I'd rather avoid polling if possible).
As such, I figured creating a minimal server-side-only app should solve it.
So basically, I need something along these lines:
if (Meteor.isServer) {
MyCollection = new Mongo.Collection('myCollection');
Meteor.publish('myCollectionPub', function () {
return MyCollection.find({ some: criteria... });
}
// is there such a thing?
Meteor.serverSideSubscribe('MyCollectionPub',
function (newDocs) {
// process/update newDocs
});
}
According to the Meteor docs, I cannot use Meteor.subscribe() on the server (and indeed it crashes if I try).
Question is:
Are there ways of 'subscribing' to collection updates on the server?
The PeerLibrary server-autorun package (along with it's dependant, reactive-mongo) will provide you with easy server-side observation of collections.
An alternative to #tarmes suggestion is the collection-hooks package, however as pointed out by David Weldon, it will only trigger in instance it is run in:
https://github.com/matb33/meteor-collection-hooks
MyCollection.after.insert(function (userId, doc) {
// ...
});
If you need it to run even when another instance makes a change in the mongo database, you can observe a cursor that is returned from your collection:
MyCollection.find({created_at : {$gt: some_current_time}}).observe({
added: function(item) {
// Alert code
}
});

Meteor / Iron Router: Data from Mongo db collection don't show up in dynamic template page

I'm quite new in javascript programing, so I really appreciate any help.
I'm using:
Meteor (official Windows port) latest version 1.1.0.2 with Iron Router and Autopublish package
What I'm trying to do, shouldn't be hard to do, but something is missing to me.
I just want to load data from Mongo DB collection
Movies = new Mongo.Collection('movies');
into my template in /client folder
<template name="movie_template">
<p>Dynamic page test with movieID {{id}}</p>
<h1>{{name}}</h1>
<p>{{year}}</p>
</template>
my router.js file based in /lib folder in root of my Meteor project
Router.route('/movies/:movieId', function() {
this.render('movie_template', {
waitOn: function() {
return Meteor.subscribe('moviesDetails', this.params.movieId);
},
data: function() {
return Movies.findOne({id: this.params.movieId});
}
});
});
publications.js in /server folder
Meteor.publish('movieDetails', function(movieID) {
check(movieID, Number);
return Movies.find({id: movieID});
});
Only one thing what I get is paragraph text without ID. What I'm doing wrong?
Side question:
Do I have to use publish() function while I'm using Autopublish package?
In that case just use Movies.find() instead subscribe()?
NOTE: this is my Movie object field keys.
{_id, id, name, year}
#below9k is correct and you should be using _id rather than id.
To answer your side question, with the autopublish package it is not necessary to do either Meteor.publish and Meteor.subscribe

sails.js setup: How to make a node module available across the sails project (controller / model, etc)?

I just getting started with SailsJS as my first web framework on Node. Let's say I wanna add MomentJS in and use across the app. How to set it up?
you can use the bootstrap.js (in config/)
like:
module.exports.bootstrap = function (cb) {
sails.moment = require('moment');
cb();
};
in all Sails-Files you can use
sails.moment()
now.
If you're trying to include your node_modules into the client side, such as jQuery, AngularJS or one of the various many font libraries, then you can npm install them as normal, but just to be sure in sails you edit your tasks/config/copy.js and add a new block, example:
grunt.config.set('copy', {
dev: {
files: [{
expand:true,
cwd: './node_modules/font-awesome/fonts',
src: ['**/*'],
dest: '.tmp/public/fonts'
}
}
});
LESS can be #imported like normal without being copied around. Other assets will need to be copied as above. If you're using the sails linker then don't forget to add your JS paths to tasks/pipeline.js too (if necessary).
You can read more here:
http://ash.zi.vc/sails/2016/02/02/including-client-side-node-modules-in-my-sails-application/
It's not directly obvious how to sync npm modules to the web accessible directories.
SailsJS is no different to any other NodeJS app. So on top of your (say) Controller.js file, you do
var m = require("moment");
And you're good to go. #mdunisch's method will obviously let you use the moment package throughout your app, without having to do "require" in each file.

Displaying data from mongoskin through HTML

I am using mongodb to store data and i wrote a simple js script using mongoskin to query and retrieve data from a collection and it works fine...
var db = require('mongoskin').db('winter.ceit.uq.edu.au/openbeacon');
var time = 0;
var tagid = 1101;
db.collection('set1').find({tag : {'$elemMatch': {id: tagid,name :"reader07"}}},function(err, result) {
if (err) throw err;
result.each(function(err, band) {
console.log(band.tag);
time += band.time;
});
});
However i need a way to integrate this functionality into a webpage...so say i press a button on the page, the js script runs and the queried data is displayed on the webpage. When i try using this javascript in a HTML file, it erros saying "module not found" since im referencing the index.js for mongoskin and mongodb as the source in my html file.....
Please lemme know what are the ways (preferably the simplest ways) to do this.
thank you.
Try looking for example applications
here is one
https://github.com/christkv/tic-tac-toe-steps