ujjwalguptaofficial / JsStore

Simplifying IndexedDB with SQL like syntax and promises
http://jsstore.net/
MIT License
858 stars 110 forks source link

Question: Add extra data to add metadata to column definition for middleware #379

Closed RohitM-IN closed 1 week ago

RohitM-IN commented 1 week ago

Discussed in https://github.com/ujjwalguptaofficial/JsStore/discussions/376

Originally posted by **RohitM-IN** October 25, 2024 I wanted to add some extra configuration to my columns so that i can use that in my middleware to know this is the column that it needs to process like ``` export const tblApiCalls = { name: 'api', columns: { key: { notNull: true, primaryKey: true, }, response: { notNull: false, enableSearch: false, isCompressed: true // my field }, } }; ``` then on the middleware ``` else if (request.name === 'select') { request.onResult((results: any[]): Promise => { return new Promise((resolve) => { if (!results || !Array.isArray(results)) { resolve(results); return; } results.forEach(result => { Object.keys(result).forEach(key => { if (compressionColumns.includes(key)) // some condition to identify this is the column i need to work on result[key] = decode(result[key]); } }); }); resolve(results); }); }); } ``` or is there a way to provide extra configuration to middleware it self like providing its own metadata what i am doing above is adding compression and decompression to the jsstore so that we can save some space since we need to cache api calls directly for whatever reason
ujjwalguptaofficial commented 1 week ago

A straightforward solution is to introduce a meta column with an object data type, where we can store specific metadata about each column’s functionality.

For instance, if we want a column to support compression, we could store the following in the meta column:

{
   "name": "compression"
}

Then, in the middleware selection logic, we can simply iterate over the meta column to identify columns and their associated functionalities, allowing for flexible configuration of column behaviors.

ujjwalguptaofficial commented 1 week ago

Since you mentioned about your approach, that is also possible. have a look at jsttore encrypt-decrypt plugin.

https://github.com/ujjwalguptaofficial/jsstore-encrypt/blob/main/src/index.ts

in this i encrypt data when inserting and decrypt when selecting with column definition ofcourse.

Let me know if need any help. Both approaches are great - The first approach is thinking more about your db , the second is more generic where you can create a seperate plugin and even launch it on npm.