I created an ObjectStore named "components" with the code below.
var objectStore = db.createObjectStore("components", { keyPath: "id", autoIncrement: true });
let index = objectStore.createIndex('componentFloorId, componentRoomId', ['componentFloorId', 'componentRoomId']);
The view of the data in the browser
Using the componentFloorId and componentRoomId Keys, I can query with the code below.
const txn = db.transaction('components', 'readonly');
const store = txn.objectStore('components');
// get the index from the Object Store
const index = store.index('componentFloorId, componentRoomId');
// query by indexes
let query = index.getAll([floorIdFromCard, roomIdFromCard]);
However, what I need is to be able to query with the id key as well as these two keys.
SQL Query Example
SELECT * FROM components WHERE componentFloorId="1" AND componentRoomId="1" AND id=2;
How do I do this sql query with indexeddb?
Related
I have a script that compares 2 JSON files, It takes a name from the first JSON and comparing to another JSON file, and if they don't have an equal "name", the object will be deleted.
Here is a code:
const fs = require('fs');
// Read the goods JSON file
const goods = JSON.parse(fs.readFileSync('good.json', 'utf-8'));
// Read the products JSON file
const products = JSON.parse(fs.readFileSync('products.json', 'utf-8'));
// Create an array of the names of objects in goods
const goodsNames = goods.map(obj => obj.name);
// Filter objects from products that don't have a matching name in goods
const filteredProducts = products.filter(obj => goodsNames.includes(obj.name));
// Write the filtered and sorted products back to the products file with formatting for readability
fs.writeFileSync('products.json', JSON.stringify(filteredProducts, null, 4), 'utf-8');
I need to export a field "unit" from good.json, another JSON, and put the field in objects with equals "name".
Try to add this above the last line in your code:
filteredProducts.forEach(product => {
const matchingGood = goods.find(good => good.name === product.name);
product.unit = matchingGood.unit;
});
I have a database based on MySQL where I have UUIDs stored as Binary(16) and a Bigquery SQL database where the same UUIDs are stored as BYTES. I tried setting up this way to save space on both databases, but it turned out to be a little nightmare.
I'm using Prisma ORM to retrieve the UUID, which returns a Buffer type with the UUID string, then I need to query for that UUID on Bigquery. The only way I'm managing to do so is by converting both the BYTES field on Bigquery and the Buffer returned from MySQL to a hexadecimal string and comparing them together. But if I'm comparing UUIDs in hexadecimal strings, why bother to keep them on binary fields? Is there any way to compare the binaries without conversions?
const { BigQuery } = require('#google-cloud/bigquery')
const { PrismaClient } = require('#prisma/client')
const prisma = new PrismaClient()
const dataset = process.env.DB_DATASET
const tableClients = `(
SELECT *, EXTRACT(DATE FROM insertion_time) ptt, ROW_NUMBER() OVER (PARTITION BY uuid ORDER BY insertion_time desc) rn
FROM \`${dataset}.clients\` WHERE insertion_time <= CURRENT_DATE() AND TO_HEX(parent_company_uuid) = #parent_company
) clients`
const projectId = 'my-project'
const keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS
const bigqueryClient = new BigQuery({
projectId,
keyFilename,
})
const TestService = {
async test() {
const company = await prisma.company.findFirst()
const query = `SELECT * FROM ${tableClients} WHERE rn = 1 AND DATE(insertion_time) <= CURRENT_DATE()`
const params = { parent_company: company.uuid.toString('hex') }
const options = {
query: `${query};`,
params,
}
const [result] = await bigqueryClient.query(options)
return result
},
}
module.exports = TestService
The tableClients thing is because instead of updating the database (which Bigquery hates to) I'm inserting the same record with a new insertion_time, creating a partition based on this field, and getting the most recent record (row_number = 1). Also, I filter the company uuid there.
This is the code that I have: I am trying to get data stored in the Firebase Database sorted by wage. The order of the database goes: "Posts -> PostID(.push when saving) -> Wage." I have the data retrieval working just not in order.
var PostsRef = firebase.database().ref().child("Posts");
PostsRef.on('child_added', function(data) {
var title = data.val().title;
var description = data.val().description;
var completionDate = data.val().completionDate;
var complexity = data.val().complexity;
var wage = data.val().wage;
var uid = data.val().userID;
});
You're storing the wages as string, which means that they will be sorted in lexicographical order:
1
13
2
20
3
If you want to get the results in numerical order, you should store the wages as numbers, so without the quotes.
If you store them from your code, this might require that you convert it with parseInt(wagesAsString).
After you store the wages as numbers, you can get them in order with:
var PostsRef = firebase.database().ref().child("Posts");
var query = PostsRef.orderByChild("wage");
query.on('child_added', function(data) {
I am using parse.com as my back end.
I am using containedIn query to retrieve multiple user rows from parse "User" table.
My problem is that I am not able to get user's object Id in the success block
following is my code:
r_userIdList= ['PzpdRdKTVZ','PoJdRdKTVU','fvdRdKTVX'];
var query = new Parse.Query("_User");
query.containedIn("objectId", r_userIdList); // returns multiple rows
query.find({
success: function(r_uobject) {
var userObjId ,fName,lName,full_Name="Unknown User";
for(var i =0; i<r_uobject.length;i++){
var r_obj = r_uobject[i];
uID = r_obj.get('objectId');//not working
var u = uID.id;//not working
fName = r_obj.get('firstName');
lName = r_obj.get('lastName');
full_Name = firstName+' '+lastName;
r_userIdList[u].fullName = full_Name; //not working
}
}
});
Once I enter in the success block, I am unable to identify that which user's data has been retrieved.
Actually I need user's ObjectId because I have array called r_userIdList in which I need to store the firstName and LastName of user as Object.
IDs can usually be accessed with .id - in your case r_obj.id. Your uID.id won't work as uID is itself going to be undefined.
You also need to check that you are getting any results back at all: does r_userIdList have a length?
Firebase - Data structure issue for extracting an object from nested structure.
I want to find the uid and then check if the key is a jobId.
I've labelled accordingly below.
I'm using typescript and angular2 with firebase.
This is my current attempt that returns "null":
var jobId = "-K5fIAiuHM-4xeEQJiIS";
var uid = "3f61ae7a-99a1-4cbf-9c8e-00b2249956a7";
var userRef = this.refApp.child('key').child(uid);
var query = userRef.child('jobId').child(jobId);
query.on('value', (snap) => {
//This returns null
var response = snap.val();
});
This is my database structure:
Your structure is /applications/$userId/$jobId. Use those keys to get to your data.
JSBin Demo
var jobId = "-K5fIAiuHM-4xeEQJiIS";
var uid = "3f61ae7a-99a1-4cbf-9c8e-00b2249956a7";
var refApp = new Firebase('<my-firebase-app>/applications');
var jobRef = refApp.child(uid).child(jobId);
jobRef.on('value', (snap) => console.log(snap.val()));
Right now you're using "key", which I believe is from my previous demo. That's just for show, not for your actual solution. Keep your data structure in mind when reading the sample code, because it can vary.