Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndexedDB extension (omg this is like my 3rd try lol) #1341

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
940820f
Create indexedDB.js
ffghdfh Feb 29, 2024
0b3bd7a
Update indexedDB.js license
ffghdfh Feb 29, 2024
72d4f1e
fixed dumb issues
ffghdfh Feb 29, 2024
23d44ce
Create indexedDB.svg
ffghdfh Feb 29, 2024
67e03c8
Update extensions.json
ffghdfh Feb 29, 2024
f5943a7
made indexedDB.svg's background non transparent
ffghdfh Feb 29, 2024
969ac55
oops, lowercase i in extensions.json
ffghdfh Feb 29, 2024
18f89a2
Update README.md, added indexedDB.svg attribution
ffghdfh Feb 29, 2024
3ab7aa7
Update README.md, forgot to add my name and a /, i am so sorry
ffghdfh Feb 29, 2024
a8b4a7d
Update indexedDB.js, removed all the reject()-s
ffghdfh Feb 29, 2024
6c89433
made indexedDB.js prettier :)
ffghdfh Mar 2, 2024
a1627df
Update indexedDB.js with discord username
ffghdfh Mar 2, 2024
75616b8
Update indexedDB.js oops forgot punctuation
ffghdfh Mar 2, 2024
9454702
Update indexedDB.js, even MORE fixes
ffghdfh Mar 2, 2024
c0cad27
Update indexedDB.js, added // By: and set undefined key resolve to ju…
ffghdfh Mar 3, 2024
fe89cb8
oh my god i missed the part about scratch profile links only. changed…
ffghdfh Mar 3, 2024
9a13d72
Merge branch 'TurboWarp:master' into IndexedDB-Extension
ffghdfh Mar 24, 2024
596c35e
Merge branch 'TurboWarp:master' into IndexedDB-Extension
ffghdfh Mar 31, 2024
ca36a42
Merge branch 'TurboWarp:master' into IndexedDB-Extension
ffghdfh Aug 1, 2024
fc5ef27
add new features and improve the extension
ffghdfh Aug 1, 2024
9b1b2f0
Removed monitor checkbox
ffghdfh Aug 1, 2024
c6dd8d9
Made get key return nothing
ffghdfh Aug 1, 2024
54eadf7
Merge branch 'TurboWarp:master' into IndexedDB-Extension
ffghdfh Oct 2, 2024
8c0d73f
Merge branch 'TurboWarp:master' into IndexedDB-Extension
ffghdfh Oct 16, 2024
3f65999
Merge branch 'TurboWarp:master' into IndexedDB-Extension
ffghdfh Dec 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions extensions/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"fetch",
"text",
"local-storage",
"ffghdfh/indexedDB",
"true-fantom/base",
"bitwise",
"Skyhigh173/bigint",
Expand Down
316 changes: 316 additions & 0 deletions extensions/ffghdfh/indexedDB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,316 @@
(function (Scratch) {
ffghdfh marked this conversation as resolved.
Show resolved Hide resolved
"use strict";
if (!Scratch.extensions.unsandboxed) {
throw new Error("Indexed Database must be run unsandboxed");
}

class IndexedDBExtension {
constructor() {
this.dbName = "MyScratchDB"; // Default database name
this.db = null;
}

// Set the database name
_setDBName(name) {
this.dbName = name;
}

// Initialize the IndexedDB
_initDB() {
return new Promise((resolve, reject) => {
ffghdfh marked this conversation as resolved.
Show resolved Hide resolved
const request = indexedDB.open(this.dbName, 1);

request.onerror = (event) => {
console.error("Database error:", event.target.error);
resolve();
};

request.onsuccess = (event) => {
this.db = event.target.result;
resolve(this.db);
};

request.onupgradeneeded = (event) => {
const db = event.target.result;
db.createObjectStore("data",  

Check failure on line 35 in extensions/ffghdfh/indexedDB.js

View workflow job for this annotation

GitHub Actions / lint

Irregular whitespace not allowed
{ keyPath: "key" });
};
});
}

// Set value in the IndexedDB
_setValue(key, value) {
return this._initDB().then((db) => {
const transaction = db.transaction(["data"], "readwrite");
const store = transaction.objectStore("data");
const request = store.put({ key, value });

return new Promise((resolve) => {
request.onsuccess = () => resolve();
request.onerror = () => resolve();
});
});
}

// Get value from the IndexedDB
_getValue(key) {
return this._initDB().then((db) => {
const transaction = db.transaction(["data"], "readonly");
const store = transaction.objectStore("data");
const request = store.get(key);

return new Promise((resolve) => {
request.onsuccess = () => {
const value = request.result ? request.result.value : undefined;
resolve(value !== undefined ? value : "");
};

request.onerror = () => resolve("");
});
});
}

// Delete all keys in the IndexedDB
_deleteAllKeys() {
return this._initDB().then((db) => {
const transaction = db.transaction(["data"], "readwrite");
const store = transaction.objectStore("data");
const request = store.clear();

return new Promise((resolve) => {
request.onsuccess = () => resolve();
request.onerror = () => resolve();
});
});
}

// Delete a specific key in the IndexedDB
_deleteKey(key) {
return this._initDB().then((db) => {
const transaction = db.transaction(["data"], "readwrite");
const store = transaction.objectStore("data");
const request = store.delete(key);

return new Promise((resolve) => {
request.onsuccess = () => resolve();
request.onerror = () => resolve();
});
});
}

// Check if a key exists in the IndexedDB
_keyExists(key) {
return this._initDB().then((db) => {
const transaction = db.transaction(["data"], "readonly");
const store = transaction.objectStore("data");
const request = store.get(key);

return new Promise((resolve) => {
request.onsuccess = () => resolve(request.result !== undefined);
});
});
}

// Export the entire database
_exportDB() {
return this._initDB().then((db) => {
const transaction = db.transaction(["data"], "readonly");
const store = transaction.objectStore("data");
const request = store.getAll();

return new Promise((resolve) => {
request.onsuccess = () => {
const result = {};
request.result.forEach((item) => {
result[item.key] = item.value;
});
resolve(JSON.stringify(result));
};

request.onerror = () => {
resolve("{}");
};

request.onerror = () => {
resolve("{}");
};
});
});
}

// Import data into the database, replacing existing data
_importDB(data) {
return this._deleteAllKeys().then(() => {
const parsedData = JSON.parse(data);
const promises = Object.keys(parsedData).map(key => {
return this._setValue(key, parsedData[key]);
});
return Promise.all(promises);
});
}

// Merge data into the database, without replacing existing keys
_mergeDB(data) {
const parsedData = JSON.parse(data);
const promises = Object.keys(parsedData).map(key => {
return this._keyExists(key).then(exists => {
if (!exists) {
return this._setValue(key, parsedData[key]);
}
});
});
return Promise.all(promises);
}

// Scratch blocks implementation
getInfo() {
return {
id: "ffghdfhIndexedDB",
name: "IndexedDB",
color1: "#fd7f54", // Main block color
color2: "#c26140",
color3: "#b35b3d", // Slightly darker outline color
blocks: [
{
opcode: "setDBName",
blockType: Scratch.BlockType.COMMAND,
text: "set database name [NAME]",
arguments: {
NAME: {
type: Scratch.ArgumentType.STRING,
defaultValue: "",
},
},
},
{
opcode: "setValue",
blockType: Scratch.BlockType.COMMAND,
text: "set key [KEY] to [VALUE]",
arguments: {
KEY: {
type: Scratch.ArgumentType.STRING,
defaultValue:  

Check failure on line 192 in extensions/ffghdfh/indexedDB.js

View workflow job for this annotation

GitHub Actions / lint

Irregular whitespace not allowed
"",
},
VALUE: {
type: Scratch.ArgumentType.STRING,
defaultValue: "",
},
},
},
{
opcode: "getValue",
blockType: Scratch.BlockType.REPORTER,
text: "get key [KEY]",
arguments: {
KEY: {
type: Scratch.ArgumentType.STRING,
defaultValue: "",
},
},
},
{
opcode: "deleteAllKeys",
blockType: Scratch.BlockType.COMMAND,
text: "delete all keys",
},
{
opcode: "deleteKey",
blockType: Scratch.BlockType.COMMAND,
text: "delete key [KEY]",
arguments: {
KEY: {
type: Scratch.ArgumentType.STRING,
defaultValue: "",
},
},
},
{
opcode: "keyExists",
blockType: Scratch.BlockType.BOOLEAN,
text: "key [KEY] exists?",
arguments: {
KEY: {
type: Scratch.ArgumentType.STRING,
defaultValue: "",
},
},
},
{
opcode: "exportDB",
blockType: Scratch.BlockType.REPORTER,
text: "export db",
disableMonitor: true
},
{
opcode: "importDB",
blockType: Scratch.BlockType.COMMAND,
text: "import db from [DATA]",
arguments: {
DATA: {
type: Scratch.ArgumentType.STRING,
defaultValue: "",
},
},
},
{
opcode: "mergeDB",
blockType: Scratch.BlockType.COMMAND,
text: "merge db from [DATA]",
arguments: {
DATA: {
type: Scratch.ArgumentType.STRING,
defaultValue: "",
},
},
},
],
};
}

setDBName(args) {
const { NAME } = args;
ffghdfh marked this conversation as resolved.
Show resolved Hide resolved
this._setDBName(NAME);
}

setValue(args) {
const { KEY, VALUE } = args;
return this._setValue(KEY, VALUE);
}

getValue(args) {
const { KEY } = args;
return this._getValue(KEY);
yuri-kiss marked this conversation as resolved.
Show resolved Hide resolved
}

deleteAllKeys() {
return this._deleteAllKeys();
}

deleteKey(args) {
const { KEY } = args;
return this._deleteKey(KEY);
}

keyExists(args) {
const { KEY } = args;
return this._keyExists(KEY);
}

exportDB() {
return this._exportDB();
}

importDB(args) {
const { DATA } = args;
return this._importDB(DATA);
}

mergeDB(args) {
const { DATA } = args;
return this._mergeDB(DATA);
}
}

Scratch.extensions.register(new IndexedDBExtension());
})(Scratch);
4 changes: 4 additions & 0 deletions images/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ All images in this folder are licensed under the [GNU General Public License ver
- File icons based on https://icon-icons.com/icon/file-pdf/153412 under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/).
- Background based on https://bgjar.com/contour-line under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/).

## ffghdfh/indexedDB.svg
- Created by [ffghdfh](https://github.com/ffghdfh)
- Database icon by [@fontawesome](https://fontawesome.com), [CC BY 4.0](https://creativecommons.org/licenses/by/4.0), via Wikimedia Commons

## battery.svg
- Created by [@BlueDome77](https://github.com/BlueDome77) in https://github.com/TurboWarp/extensions/issues/90#issuecomment-1586143227
- Based on image created by [@Martinelplayz](https://scratch.mit.edu/users/MARTINELPLAYZ/) in https://github.com/TurboWarp/extensions/pull/504#issuecomment-1574243161
Expand Down
Loading
Loading