How do I get the date of the last time a folder on google drive has been updated.
For an example, when the last file was copied into it.
Currently I'm using the following code to get back the date when the folder was created:
const drive = google.drive({ version: 'v3', auth});
console.log("Calling listing")
await drive.files.list({
pageSize: 1000,
fields: 'nextPageToken, files(id, name, modifiedTime)',
q: "mimeType='application/vnd.google-apps.folder'",
}, (err, dataResponse) => {
if(err) {
res.json("Error " + err);
}
const folders = dataResponse.data.files;
console.log(folders);
res.json(folders);
})
In agreement with the UI behavior, the Last Modified time the API will give you for the folder itself will be different from the Last Modified time of the latest modified file within this folder
This is due to Google's Drive hierarchy where a change of the Last Modified time of a file does not necessary change the Last Modified time of the parent folder
In any case, if you would like to know the Last Modified time of the folder itself, you need to perform a Files: get request specifying the id of the folder as fileId and setting fields to modifiedTime.
Sample:
...
await drive.files.get({
fileId: "XXX"
fields: "modifiedTime"
},
...
If you would like to access the modifiedTime property of the files in the folder, you can do it with Files: list , setting q to 'XXX' in parents, whereby XXX is the id of the parent folder, and setting fields to files/modifiedTime.
Sample:
...
await drive.files.list({
fields: "files(modifiedTime)",
q: "'XXX' in parents"
}
...
This will return you a response like
{
"files": [
{
"modifiedTime": "2021-10-09T14:22:58.306Z"
},
{
"modifiedTime": "2021-10-07T17:38:56.515Z"
},
{
"modifiedTime": "2021-09-28T16:28:12.476Z"
},
{
"modifiedTime": "2021-09-27T16:13:58.201Z"
}
]
}
You can then programmatically determine the newest timestamp from the result list.
Related
I have been trying to create a pull request using the createPullRequest plugin from octokit and so far for simple text files it works well, but when I am trying to commit some changes into a JSON file in the repository, I get something like this:
ss from the pull request
I want this data to present itself in a json-structure, I am using the following code:
const updatedContent = JSON.stringify(json_for_package);
const pr=await octokit_pull.createPullRequest({
owner: ownername,
repo: repoName,
title: "pull request "+dependency,
body: "change "+dependency+" v"+curr_version+" to v"+version,
base: "main" ,
head: "pulls-requests-branches-names-main-"+dependency+"-v"+version,
forceFork: true,
changes: [
{
files: {
"package.json": updatedContent,
},
commit:
"updated the contents of package.json",
},
],
});
Here json_for_package is the JSON content that I want to write into my package.json file, I just need to know how I can pass the updatedContent in the files so that it recognizes that it a JSON file and not any simple text.
Anyone facing the same problem can do the following- first writing the json data into a .json file and then read it to use it, seems to have worked for me!
Beside writing the JSON to a file first, and use the file as parameter, you can also use gr2m/octokit-plugin-create-pull-request:
Example
const MyOctokit = Octokit.plugin(createPullRequest);
const TOKEN = "secret123"; // create token at https://github.com/settings/tokens/new?scopes=repo
const octokit = new MyOctokit({
auth: TOKEN,
});
And then:
// Returns a normal Octokit PR response
// See https://octokit.github.io/rest.js/#octokit-routes-pulls-create
octokit
.createPullRequest({
owner: "user-or-org-login",
repo: "repo-name",
title: "pull request title",
body: "pull request description",
base: "main" /* optional: defaults to default branch */,
head: "pull-request-branch-name",
forceFork: false /* optional: force creating fork even when user has write rights */,
changes: [
{
/* optional: if `files` is not passed, an empty commit is created instead */
files: {
"path/to/file1.txt": "Content for file1",
"path/to/file2.png": {
content: "_base64_encoded_content_",
encoding: "base64",
},
// deletes file if it exists,
"path/to/file3.txt": null,
// updates file based on current content
"path/to/file4.txt": ({ exists, encoding, content }) => {
// do not create the file if it does not exist
if (!exists) return null;
return Buffer.from(content, encoding)
.toString("utf-8")
.toUpperCase();
},
"path/to/file5.sh": {
content: "echo Hello World",
encoding: "utf-8",
// one of the modes supported by the git tree object
// https://developer.github.com/v3/git/trees/#tree-object
mode: "100755",
},
},
commit:
"creating file1.txt, file2.png, deleting file3.txt, updating file4.txt (if it exists), file5.sh",
},
],
})
.then((pr) => console.log(pr.data.number));
I'm trying to figure out how to clear a bunch of keys in redis from the cache in the most efficient manner possible.
In this example I'm getting a string array of userIds. Each user has a watchlist of auctionIds in their user document inside mongodb
"watchlist": [
"5eb81dbaecb87a4060c3d5a7",
"5eb81e24ecb87a4060c3d5a8",
"5eb8302ebae779466c97c1d9"
],
It's an auction site so if a user places a bid, then I need the watchlists that have the listing inside that user's [string] watchlist to clear the cache for that key const watchlistKey = `watchlist-${req.query.userIdBidder}`;
but in production that could be 100s of ids. Who knows.
I've been using this library for other pattern delete examples: https://www.npmjs.com/package/redis-delete-wildcard?activeTab=readme but I don't think I can apply it to this use case. What will be the most cost effective way to clear the cache using string array as input data?
User.find({
watchlist: { $in: [req.query.listingId] },
}).select("_id").then((res) => {
console.log(res);
const watchlistKey = `watchlist-${req.query.userIdBidder}`;
client.delwild('main-listings-no-filter-page=*', function (error, numberDeletedKeys) {
console.log("Page Didn't Match During Bid. All Listings Pages have been removed from cache.");
console.log(numberDeletedKeys);
});
})
Console Output
[ { _id: 5eb8196eecb87a4060c3d5a5 },
{ _id: 5eb82d0c71b0a230b853bd6f } ]
I have one-to-many relation:
class User {
#PrimaryGeneratedColumn()
id: number;
#Column()
name: string;
#OneToMany(() => Phone, phone => phone.user, {cascade: true})
phones?: Phone[];
}
Assume current data in database is following: user has two phone numbers:
{
"id": 1,
"name": "Jhon",
"phones": [
{"id": 1, "phone": "123456"},
{"id": 2, "phone": "567809"}
]
}
I build UI (web-interface) and from web-interface I want to have possibility to delete a phone number from user profile. So I do it and following POST request comes from UI: second phone number deleted
{
"id": 1,
"name": "Jhon",
"phones": [
{"id": 1, "phone": "123456"}
]
}
How to delete phone number id=2 from database?
I use Nest CRUD module, as I understand from source code it just merges a new data from request with current data from database, so merging two phone numbers from database with the only phone number from request gives us array of tho phone numbers again and nothing deleted :frowning:
I tried to do it manually and there are a lot of code!
#Override()
async updateOne(
#ParsedRequest() req: CrudRequest,
#ParsedBody() dto: UpdateUserDto,
#Param('id') id: number,
): Promise<Shipment> {
// use standart Nest.js way to save new user data into database
await this.service.updateOne(req, dto);
// load fresh user data from database
const user = await this.service.findOne(id);
const phonesFromUi = dto.phones;
const phonesToBeDeleted = [];
// loop phone numbers from database - detect which to be deleted
user.phones.forEach((phoneFromDb) => {
const hasThisPhoneOnUi = phonesFromUi.find((phoneFromUi) => phoneFromUi.id === phoneFromDb.id);
if (!hasThisPhoneOnUi) {
// looks like this phone number was deleted on ui, so delete it from database too
phonesToBeDeleted.push(phoneFromDb);
}
});
// actually delete phone numbers from database
await this.connection.getRepositoryFor(Phone).remove(phonesToBeDeleted);
// reload fresh user data from database to get fresh list of phone numbers after delition some of them
const user = await this.service.findOne(id);
return user;
}
Is there any way to do it via TypeORM build in functions or methods? How to delete some elements from nested array?
There is no very good support in typeorm for doing a join update, what I advise you to do is receiving the phones parameter and get a select of the phones that are related to the UserId and then delete the ids that are not in the new array:
const phones = await this.phoneRepository.find({userId:1});
const toDeletePhones = phones.filter((element)) => {
return updatePhones.indexOf(element) === -1;
}
this.phoneRepository.remove(toDeletePhones);
then procede to update the user or yo can even do it before this
In my nodejs elastic writer I want to replace one document with another.
Currently, I run-
var data = { doc: doc, "doc_as_upsert": true };
var metadata =
{ update: { _id: idToUpdate, _index:indexName,_type: INDEX_TYPE_PREFIX } };
body.push(metadata);
body.push(payment);
}
elasticsearchClient.bulk({
body: body,
}, function (err, resp) {
But in case the document in elastic contained field X and the updated document didn't, field X stays in elastic- I want it to be removed.
According to
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html
using "doc:" is for partial update, so what's the alternative for full update?
Don't use the update api, use the index api instead.
I have a meteor app prototype that works well, but is very insecure as of now: I needed to display a list of matching users to the currently logged-in user. For starters, I decided to publish all users, limiting the fields to what I would need to filter the user list on the client side.
Meteor.publish('users', function () {
return Meteor.users.find({}, {
fields: {
'profile.picture': 1,
'profile.likes': 1,
'profile.friends': 1,
'profile.type': 1
}
});
});
Then in my router, I would do a request to only show what I wanted on the client side:
Router.map(function() {
this.route('usersList', {
path: '/users',
waitOn: function () {
return Meteor.subscribe('users');
},
data: function () {
var user = Meteor.user();
return {
users: Meteor.users.find({ $and: [
{_id: {$ne : user._id}},
{'profile.type': user.profile.interest}
]})
};
}
});
});
In the code above, I query all users who are not the current user and whose type correspond the current user's interest. I also display a certain border on the photos of users who have my user in their "profile.friends" array, using this client helper:
Template.userItem.helpers({
getClass: function(userId) {
var user = Meteor.user();
var lookedup = Meteor.users.findOne({_id: userId});
if ($.inArray(user._id, lookedup.profile.friends) !== -1)
return "yes";
return "no";
}
});
Now this all worked great, but with this setup, every client can query every users and get their type, picture, list of friends and number of likes. If I was in an MVC, this info would only be accessible on server side. So I decided my next iteration to be a security one. I would move my query from the router file to the publications file. That's where trouble began...
Meteor.publish('users', function () {
var user = Meteor.users.findOne({_id: this.userId});
var interest = user.profile.interest;
// retrieve all users, with their friends for now
allUsers = Meteor.users.find({ $and: [
{'_id': {$ne: user._id}},
{'profile.type':interest}
]},
{ fields: {'profile.picture': 1, 'profile.friends': 1}}
);
return allUsers;
});
And in the router:
Router.map(function() {
this.route('usersList', {
path: '/users',
waitOn: function () {
return Meteor.subscribe('users');
},
data: function () {
var user = Meteor.user();
return {users: Meteor.users.find({_id: {$ne : user._id}})};
}
});
});
(note that I still need to exclude the current user from the router query since the current user is always fully published)
This works, but:
the user list does not get updated when I change the user interest and then do a Router.go('usersList'). Only when I refresh the browser, my list is updated according to the user's new interest. No idea why.
this solution still publishes the users' friends in order to display my matching borders. I wish to add a temporary field in my publish query, setting it to "yes" if the user is in the user's friends and "no" otherwise, but... no success so far. I read I could use aggregate to maybe achieve that but haven't managed to so far. Also, aggregate doesn't return a cursor which is what is expected from a publication.
This problem makes me doubt about the praises about meteor being suitable for secure apps... This would be so easy to achieve in Rails or others!
EDIT: As requested, here is the code I have so far for the transition of my "matching" check to the server:
Meteor.publish('users', function () {
var user = Meteor.users.findOne({_id: this.userId});
var interest = user.profile.interest;
// retrieve all users, with their friends for now
allUsers = Meteor.users.find({ $and: [
{'_id': {$ne: user._id}},
{'profile.type':interest}
]},
{ fields: {'profile.picture': 1, 'profile.friends': 1}}
);
// ------------- ADDED ---------------
allUsers.forEach(function (lookedup) {
if (_.contains(lookedup.profile.friends, user._id))
lookedup.profile.relation = "yes";
else
lookedup.profile.relation = "no";
lookedup.profile.friends = undefined;
return lookedup;
});
// ------------- END ---------------
return allUsers;
});
Obviously this code doesn't work at all, since I cannot modify cursor values in a foreach loop. But it gives an idea of what I want to achieve: give a way to the client to know if a friend is matched or not, without giving access to the friend lists of all users to the client. (and also, avoiding having to do one request per each user during display to ask the server if this specific user matches this specific one)
You can add a transform function and modify a cursor docs on the fly
meteor Collection.find