Recreate localDiskDb.db in Sails.js - javascript

So I deleted the file .tmp/localDiskDb.db manually and now when Sails generates it on start its empty, Is there a way to make Sails to recreate it based on my models?

As far as I understand, that file contains only your models' instances, i.e. your actual data. To make it have some data, just create some instances of your models and save them into the file database.

Deleting ./tmp/localDisk.db removes your data. I wouldn't actually use the default sails-disk as my adapter, if I were you. You should use a better DB (e.g mysql, sqlite, mongodb etc) that prevents issues like these. localDisk.db is literally a text file not isolated from your dev environment. you can see how this would be a problem.

use fixtures in your config/bootstrap to import "dummy" Data on Startup

Related

How to programatically create pull request with changed file using GitHub API

How can I create PR on Github using their API? Let's imagine I have package.json file as a string and I want to make changes to this file. So I parse it, make changes to it and then what exactly I need to do to make it look like I made those changes locally after checking out the new branch, making changes, commiting them to new branch and the pushing them to remote? I see that they have POST API endpoint to create a commit and POST API endpoint to create a PR, but I don't see where I put the file I changed.
Using something like GitJS could be a solution.
This library is a simple wrapper around the git command line.
Instead of using the Github API you could just work the git commands programatically with a library like this.
That way you don't have to work with the much more complicated API and have the benefit of supporting other source control sites too.
Looking at their example we can see it's incredibly easy to commit and push a file using javascript:
require('simple-git')()
.add('./*')
.commit("first commit!")
.addRemote('origin', 'some-repo-url')
.push(['-u', 'origin', 'master'], () => console.log('done'));
Make sure you refer to the usage documentation before trying that example to ensure you configure everything correctly.
Alternatively, you can use the package Octokit to more easily interface with the Github API.
When adding, committing and pushing a file via the API you must first start by creating a tree. Then you use that tree as part of the commit request, update the references and finally push the commit.
You can find a working example on this Github issue.

Is there a best practice for storing and retrieving static list in node

I am building a application for the first time in node.
My website will include a static list of countries, music genres and so on...
Should I store the data in my database, or should I use a static json file with a list(countries, genre)?
My folder structure looks something like src\lib..scsss..server and so on.
My question ultimately is - Is there a best practice for storing static lists in node - if a josn file is preferred where should this exist in my folder structure?
If your data is not gonna change and static, then you should use file system which will have high R/W operation rate compared to communication with DB Server overhead.
Moreover you can use filecache to cache all your static files. Which will load the files even faster.
The answer is really that "it depends" upon some things you have not specified.
First off, if it is a list of data that does not change while your app is running (or changes very infrequently), then you don't want to load it from some remote source every time you need it. You will want to load it once and then keep that list in memory for subsequent use. This will be a lot more efficient for your server.
As to where to store the list in the first place, you have several choices that depend upon who is going to maintain that list and what level of programming skill they might have.
If the list of countries will not change often and will be maintained by a Javascript developer, then you can either put the list right into a Javascript literal in your code or in a JSON file in your file system. If choosing the latter option as a JSON file, it can be in the same directory as your Javascript source files and just loaded directly with require() upon startup.
If the list of countries will be maintained by someone who is not a Javascript developer, but can be trusted to follow JSON syntax rules, then you can put the list in a JSON file. Whether you put this file in the same directory as your JS files or in a separate data directory really depends more upon how your application is deployed, who has permission to do what, etc...
If the list of countries will be maintained by someone who has no idea about programming or syntax rules and should be modifiable completely independently from your code, then you may want to either put it in the database and build some sort of admin interface for modifying it or put it in a plain text file (one country per line) and then parse that file upon app startup.

Watch FTP folder via node.js

I need to watch ftp folder for files create/update/remove events.
But I can't find any suitable solution to do this using node.js.
Can you help me, please?
You need get recursive listing of remote ftp directory and save it (look at https://github.com/mscdex/node-ftp and https://github.com/evanplaice/node-ftpsync)
Set timeout to get new recursive listing
Compare new and old listing (look at https://github.com/andreyvit/json-diff) and call handlers to the corresponding events
Overwrite old listing with new
Return to step two
You can use sftp-watcher modules. which will reduce your time.
https://www.npmjs.com/package/sftp-watcher

Reference to ejs view files does not change after the build, although it combines it inside of production.js - Javascript MVC

In the code we use something like this:
$('#wrapper').html('//app/views/content.ejs', {foo:"bar"});
And when we build the app, this still stays the same, although the content.ejs file is built into production.js.
So my question is, what should we do so that when we build the app, these references point to ejs files inside of production.js?
We are using JMVC 3.2.2
We've also tried using this way:
$('#wrapper').html( $.View('//app/views/content.ejs', {foo:"bar"}) );
Your views are not getting added to production.js; you need to steal each one of them:
steal('//app/views/content.ejs');
JMVC 3.1:
steal.views('//app/views/content.ejs');
Got the answer in JMVC forum: https://forum.javascriptmvc.com/topic/#Topic/32525000000958049
Credit to: Curtis Cummings
Answer:
The paths to the views do not need to change.
When the production.js file is created, your views are included and
get preloaded when the script runs. When you reference:
'//app/views/content.ejs', view first checks if the view file you are
requesting has been preloaded and if it has, will use that instead of
making a request for the .ejs file.

MongoDB remove GridFS objects from shell

I have files stored in a MongoDB using GridFS. I need to remove some of those files by ID, from the JavaScript shell. I need to remove a single file using it's ID. I figured I could just do this:
db.fs.files.remove({_id: my_id});
This works to some extent; it removes the file from the fs.files collection but does not remove the chunks itself from the fs.chunks collection. The reason I know that is because I check the length of both collections before and after in RockMongo.
I could go through the chunks and remove those that are referring to that file, but is there a better, built-in way of doing that?
You can delete gridFS file by deleting both chunks and files from shell. for example
db['fs.chunks'].remove({files_id:my_id});
db['fs.files'].remove({_id:my_id});
Those commands will do such trick.
You want to use db.fs.delete(_id); instead.
Update
Sorry, that apparently doesn't work from the shell, only through the driver. GridFS is a specification for storage implemented by the drivers. Looks like it doesn't have much built-in functionality from the shell, as such.
Update 2 There is also a command line tool, mongofiles (http://www.mongodb.org/display/DOCS/GridFS+Tools), which allows you to delete files by name. mongofiles delete <filename>. It comes with a warning that it will delete all of the files by that name, so it's not as granular as by id.
mongofiles --host localhost:30000 --db logo delete logo_susan1225.png
refer to this page:
http://docs.mongodb.org/manual/reference/program/mongofiles/#bin.mongofiles

Categories