Obtain Dexie values without using toArray() - javascript

I have the following code:
(function () {
var gameDataLocalStorageName = "myTest3";
var defaultUserSettings = {
volumes: {
musicVolume: 0.3,
sfxVolume: 0.5,
voicesVolume: 1
}
};
var savedGames = [
{
screenshot: "data uri here",
day: "1",
month: "1",
year: "1",
time: "1",
gameData: {
fonts: [{
id: 123,
name: "Arial"
}],
globalSpeeches: {
anotherVal: "something"
}
}
}
];
console.log(gameDataLocalStorageName);
console.log(defaultUserSettings);
console.log(savedGames);
/* Create db START */
var db = new Dexie(gameDataLocalStorageName);
db.version(1).stores({
usersData: ""
});
db.usersData.put(defaultUserSettings, 'userSettings');
db.usersData.put(savedGames, 'savedGames');
}());
/* Create db END */
/* Recall db START */
setTimeout(function(){
var db2 = new Dexie("myTest3");
db2.version(1).stores({
usersData: "userSettings,savedGames"
});
db2.usersData.toArray().then(function (results) {
console.log("User settings is: ", results[1]);
console.log("Saved games is: ", results[0]);
});
}, 3000);
Which runs great. However how can I obtain the data again without having to render out as an array toArray(). Currently to obtain them I have to hardcode results[0] and results[1] which is also not in the same order as I entered them into the db.
Ideally I want to do something like:
db2.get('usersData.userSettings');
db2.get('usersData.savedGames');

The sample show you are changing primary key which is not supported:
The first declaration specifies a table "usersData" with outbound primary keys:
db.version(1).stores({
usersData: ""
});
Then in the setTimout callback, you redeclare it with:
db2.version(1).stores({
usersData: "userSettings,savedGames"
});
...which means you want an inbound primary key from the property "userSettings" and and index on property "savedGames".
There are three errors here:
You cannot change declaration without incrementing version number which is not done here.
You cannot change primary key on an existing database.
Promises are not catched so you do not see the errors.
It seems what you really intend is so use Dexie as a key/value store, which is perfectly ok but much simpler to do than the sample shows.
If you put() (or add()) a value using a certain key, you retrieve the same using get().
If so, try the following:
db.version(1).stores({
usersData: "",
});
And don't forget to catch promises or await and do try/catch.
(async ()=>{
await db.usersData.put(defaultUserSettings, 'userSettings')
await db.usersData.put(savedGames, 'savedGames');
// Get using key:
const userSettings = await db.usersData.get('userSettings');
console.log("User settings is: ", userSettings);
const savedGames = await db.usersData.get('savedGames');
console.log("User settings is: ", savedGames);
})().catch(console.error);
However, putting entire arrays as values in a key/value store is not very optimal.
Maybe only have two tables "userSettings" and "savedGames" where each saved game would be its own row? Will you support multiple users or just one single user? If multiple, you could add an index "userId" to your tables.
If so, try the following:
db.version(2).stores({
userSettings: "userId" // userId is primary key
savedGames: "++gameId, userId" // incremented id and userId is foreign key
});
(async ()=>{
await db.userSettings.put({...defaultUserSettings, userId: "fooUser"});
await db.savedGames.bulkPut(savedGames.map(game =>
({...game, userId: "fooUser"}));
// Get user settings:
const userSettings = await db.usersData.get('fooUser');
console.log("User settings is: ", userSettings);
const savedGames = await db.usersData.where({userId: "fooUser"}).toArray();
console.log("Saved games for fooUser are: ", savedGames);
})().catch(console.error);

Related

Realtime database update or create functionality

I have a user object in firebases realtime database. I am querying realtime database and when successful I am wanting to write some new data to the user object under the users node.
My desired outcome: When a user has not fetched the information before, a new field called 'lastViewed' under the user object is created and if the field has already been created then we update the timeViewed keys value. A user can have multiple objects in the array corresponding to the uuid of the fetched data.
Please see the user object below
This may not need to be an array if using .push()
-N0X8VLHTw3xgvD2vJs- : { // this is the users unique key
name: 'myName',
lastViewed: {
[
{
timeViewed: 1651558791, // this is the field to update if exists
datasUniqueKey: 'N17ZmwIsbqaVSGh93Q0' // if this value exists update timeViewed else we create the entry.
},
{
timeViewed: 1651558952,
datasUniqueKey: 'N17ZmwIsbqaVSad3gad'
},
]
}
}
Please see my attempt below.
const getData = database()
.ref(`data/${uniqueKeyFromData}`)
.on('value', snapshot => {
if (snapshot.exists()) {
database()
.ref(`users/${currentFirebaseUserKey}/lastViewed`) // currentFirebaseUserKey = N0X8VLHTw3xgvD2vJs
.once('value', childSnapshot => {
if (childSnapshot.exists()) {
// update
database()
.ref(
`users/${currentFirebaseUserKey}/lastViewed`,
)
.update({
timeViewed: new Date(), // new data will not give us the corresponding date format in the user object above but don't worry about that
fetchedDatasUniqueKey: uniqueKeyFromData,
});
} else {
// create
database()
.ref(
`users/${currentFirebaseUserKey}/lastViewed`,
)
// Push creates a unique key which might not be required so maybe set?
.push({
timeViewed: new Date(),
fetchedDatasUniqueKey: uniqueKeyFromData,
});
}
});
}
});
Where I think I am going wrong
Above I am not creating an array, if I use push I would get a unique key generated from firebase but then would have to use that key when updating, something like
`users/${currentFirebaseUserKey}/lastViewed/${lastViewedUniqueKey}`
So the user object would look like so
-N0X8VLHTw3xgvD2vJs- : { // this is the users unique key
name: 'myName',
lastViewed: {
-N17i2X2-rKYXywbJGmQ: { // this is lastViewedUniqueKey
timeViewed: 1651558791,
datasUniqueKey: 'N17ZmwIsbqaVSGh93Q0'
},
}
}
then check for snapshot.key in the if?, any help would be appreciated.
Since you don't want a list of data, but a single set of properties for ``, you should just call set instead of push:
database()
.ref(`users/${currentFirebaseUserKey}/lastViewed`)
.set({ // 👈
timeViewed: new Date(),
fetchedDatasUniqueKey: uniqueKeyFromData,
});
I also don't think you need the two different cases here for create vs update, as both seem to do the exact same thing. If you do need both cases though, consider using a transaction.

ValidationException: The provided key element does not match the schema with primary key and sort key

Using "aws-sdk": "^2.1063.0" and nodejs 12
Inside my lambda I am doing an update to a dynamodb table.
My table has a Primary key: JobUID type string and a Sort key type string.
My parameters look like this:
var params = {
TableName: tableName,
Key: {
"JobUID": payload.JobUID,
"TimeStamp": payload.TimeStamp
},
UpdateExpression:
"set #HasResponse = :v_HasResponse, #ResponseTimeStamp = :v_ResponseTimeStamp, #Recommendation = :v_Recommendation, #ThreadRepComment = :v_ThreadRepComment",
ExpressionAttributeNames: {
"#HasResponse": payload.HasResponse,
"#ResponseTimeStamp": payload.ResponseTimeStamp,
"#Recommendation": payload.Recommendation,
"#ThreadRepComment": payload.ThreadRepComment,
},
ExpressionAttributeValues: {
":v_HasResponse": payload.HasResponse,
":v_ResponseTimeStamp": payload.ResponseTimeStamp,
":v_Recommendation": payload.Recommendation,
":v_ThreadRepComment": payload.ThreadRepComment,
},
// returns only the affected attributes, as they appeared after the update
ReturnValues: "UPDATED_NEW"
};
I have printed out the payload.JobUID and payload.TimeStamp in the log so I know they are what expect.
The latest row in the table has JobUID and TimeStamp exactly as I printed them out.
I want to update the 4 properties in the expression attribute names.
I am getting the error "ValidationException: The provided key element does not match the schema"
I have looked on the web and in SOF at examples of updates and I cannot seem to get this to work.
what is wrong with my key values.
The update call looks like this. Super simple
var returnValue = await dynamo.update(params).promise();
I also tried
Key: {
JobUID: {"S": payload.JobUID},
TimeStamp: {"S":payload.TimeStamp}
},
So this is what I found works:
var params = {
TableName: tableName,
Key: {
JobUID: payload.JobUID,
TimeStamp: payload.TimeStamp
},
UpdateExpression:
"set HasResponse = :v_HasResponse, ResponseTimeStamp = :v_ResponseTimeStamp, Recommendation = :v_Recommendation, ThreadRepComment = :v_ThreadRepComment",
ExpressionAttributeValues: {
":v_HasResponse": payload.HasResponse,
":v_ResponseTimeStamp": payload.ResponseTimeStamp,
":v_Recommendation": payload.Recommendation,
":v_ThreadRepComment": payload.ThreadRepComment || "",
},
// returns only the affected attributes, as they appeared after the update
ReturnValues: "UPDATED_NEW"
};
var returnValue = await dynamo.update(params).promise();
If there is a property is is null or empty, in my case the ThreadRepComment could be null or empty so you need to handle that.

mongoose.save() neither saving data into database nor throwing any err or log messages

I have logged the dbSong and results are satisfying, but they are not being saved into database.
The schema and all is like this.
const baseURL = "https://songspk.mobi";
mongoose.connect("mongodb://localhost:27017/songsDB", {useNewUrlParser: true, useUnifiedTopology: true});
//New Song Schema
const songSchema = new mongoose.Schema({
name: String,
parent_url: String,
child_url: String,
album: String,
duration: String,
singers: String,
lyricist: String,
music_director: String,
download_128: String,
download_320: String,
image_url: String
});
const Song = mongoose.model("Song", songSchema);
This is distribution of work.
if(cluster.isMaster) {
console.log(`I'm Master ${process.pid}`);
for(let i=0;i<numCPUs;i++) { //Forking all 8 CPUs
cluster.fork();
}
} else {
const wokerID = cluster.worker.id;
fetchResults(links[wokerID-1]);
fetchResults(links[wokerID + 7]);
fetchResults(links[wokerID + 15]);
if(wokerID === 1) {
fetchResults(links[24]);
}
if(wokerID === 2) {
fetchResults(links[25]);
}
//console.log(`I'm worker ${process.pid}`);
process.exit();
}
I'm using cluster for higher performance. So, I have started calling functions on each CPU, and it's working. The work is now very fast, almost 8 times and it's logging what it is doing. Each CPU is working parallelly and calling a function names fetchResults(), which is fecthing some results from a website.
Now inside fetchResults() function, a link has been passed and it goes to a web page and fetches all the song details, let say 25 songs on a link.
function fetchResults(link) {
const result = makeGetRequest(link);
const dom = new jsdom.JSDOM(result);
const numberOfSongs = dom.window.document.querySelectorAll(".thumb-image a[href]").length;
for(var i=0;i<numberOfSongs;i++) { //Particular Song
const song = {
name: "",
parent_url: link,
child_url: "",
album: "",
duration: "",
singers: "",
lyricist: "",
music_director: "",
download_320: "",
download_128: "",
image_url: ""
};
// I have put all songs details in this song object. Now I want to save this into my
//database
const dbSong = new Song({
name: song.name,
parent_url: song.parent_url,
child_url: song.child_url,
album: song.album,
duration: song.duration,
singers: song.singers,
lyricist: song.lyricist,
music_director: song.music_director,
download_128: song.download_128,
download_320: song.download_320,
image_url: song.image_url
});
//console.log(dbSong);
dbSong.save(function(err){
if(err) {
console.log(err);
} else {
console.log("*********I did that********");
}
});
}
}
Now if I log dbSong or song it shows everything on console, but nothing error or Success message in console. And also, it is not saving any data into database. Please help me. If I save data outside the function it saves that correctly, but not inside the function which contains a loop. I'm not getting why it is so, may me something like sync or async, please help me!
When I tried to use
const promise = dbSong.save();
console.log(promise);
I got result on log as:
Promise { <pending> }

Meteor MongoDB Setting field of array per document

I have a Documents in a Collection that have a field that is an Array (foo). This is an Array of other subdocuments. I want to set the same field (bar) for each subdocument in each document to the same value. This value comes from a checkbox.
So..my client-side code is something like
'click #checkAll'(e, template) {
const target = e.target;
const checked = $(target).prop('checked');
//Call Server Method to update list of Docs
const docIds = getIds();
Meteor.call('updateAllSubDocs', docIds, checked);
}
I tried using https://docs.mongodb.com/manual/reference/operator/update/positional-all/#positional-update-all
And came up with the following for my Server helper method.
'updateAllSubDocs'(ids, checked) {
Items.update({ _id: { $in: ids } }, { $set: { "foo.$[].bar": bar } },
{ multi: true }, function (err, result) {
if (err) {
throw new Meteor.Error('error updating');
}
});
}
But that throws an error 'foo.$[].bar is not allowed by the Schema'. Any ideas?
I'm using SimpleSchema for both the parent and subdocument
Thanks!
Try passing an option to bypass Simple Schema. It might be lacking support for this (somewhat) newer Mongo feature.
bypassCollection2
Example:
Items.update({ _id: { $in: ids } }, { $set: { "foo.$[].bar": bar } },
{ multi: true, bypassCollection2: true }, function (err, result) {
if (err) {
throw new Meteor.Error('error updating');
}
});
Old answer:
Since you say you need to make a unique update for each document it sounds like bulk updating is the way to go in this case. Here's an example of how to do this in Meteor.
if (docsToUpdate.length < 1) return
const bulk = MyCollection.rawCollection().initializeUnorderedBulkOp()
for (const myDoc of docsToUpdate) {
bulk.find({ _id: myDoc._id }).updateOne({ $set: update })
}
Promise.await(bulk.execute()) // or use regular await if you want...
Note we exit the function early if there's no docs because bulk.execute() throws an exception if there's no operations to process.
If your data have different data in the $set for each entry on array, I think you need a loop in server side.
Mongo has Bulk operations, but I don't know if you can call them using Collection.rawCollection().XXXXX
I've used rawCollection() to access aggregate and it works fine to me. Maybe work with bulk operations.

Mongo update removing other keys in document

I am trying mongo update where one document key from a different collection is inserted into another collection.
CODE
// update user document with remove of otp and new state set.
updateOne = await db.collection(_collection).updateOne(
// search basis.
__docUpdateSearchBasis,
// updates.
__docUpdateBasis
)
RESULT
You need to make query like this:
updateOne = await db.collection(_collection).findOneAndUpdate(
//Condition
{
_id: req.user.id
},
//Update what you want
{
$set: {
key: value
}
});

Categories