Readme

Blob Array

Create an array as a Database, instantly!

Just create a new val for your array:

Create valimport { BlobArray } from "https://esm.town/v/joey/BlobArray"; type User = { email: string }; // specify the type inside the TS generic <> to define the type of your array export let myUserArray = BlobArray<User>("my-user-array");

Now import it and start using it in your vals!

Create valimport { myUserArray } from "https://esm.town/v/joey/myUserArray"; // overwrite the entire array await myUserArray.set([{ email: "user1@gmail.com" }, { email: "user2@yahoo.com" }]); // get all the entries in your array const allUsers = await myUserArray.get(); // call any array method you want, just use "await" await myUserArray.push({ email: "johndoe@gmail.com" }); const gmailUsers = await myUserArray.filter(u => u.email.includes("@gmail.com"));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { blob } from "https://esm.town/v/std/blob";
export const BlobArray = <T extends any>(id: string) => {
const getValue = (): Promise<T[]> => blob.getJSON(id).then(json => JSON.parse(json || "[]"));
const setValue = (arr: T[]) => blob.setJSON(id, JSON.stringify(arr)).then(res => arr);
return {
set: setValue,
get: getValue,
// mutational
push: async (...args: Parameters<Array<T>["push"]>) =>
getValue().then(async (arr) => {
const res = arr.push(...args);
await setValue(arr);
return res;
}),
pop: () =>
getValue().then(async (arr) => {
const res = arr.pop();
await setValue(arr);
return res;
}),
splice: async (...args: Parameters<Array<T>["splice"]>) => {
const current = await getValue();
const res = current.splice(...args);
await setValue(current);
return res;
},
unshift: async (...args: Parameters<Array<T>["unshift"]>) => {
const current = await getValue();
const res = current.unshift(...args);
await setValue(current);
return res;
},
reverse: async (...args: Parameters<Array<T>["reverse"]>) => {
const current = await getValue();
const res = current.reverse(...args);
await setValue(current);
return res;
},
sort: async (...args: Parameters<Array<T>["sort"]>) => {
const current = await getValue();
const res = current.sort(...args);
await setValue(current);
return res;
},
// non-mutation
concat: (...args: Parameters<Array<T>["concat"]>) => getValue().then(arr => arr.concat(...args)),
every: (...args: Parameters<Array<T>["every"]>) => getValue().then(arr => arr.every(...args)),
filter: (...args: Parameters<Array<T>["filter"]>) => getValue().then(arr => arr.filter(...args)),
find: (...args: Parameters<Array<T>["find"]>) => getValue().then(arr => arr.find(...args)),
findIndex: (...args: Parameters<Array<T>["findIndex"]>) => getValue().then(arr => arr.findIndex(...args)),
flat: (...args: Parameters<Array<T>["flat"]>) => getValue().then(arr => arr.flat(...args)),
flatMap: (...args: Parameters<Array<T>["flatMap"]>) => getValue().then(arr => arr.flatMap(...args)),
forEach: (...args: Parameters<Array<T>["forEach"]>) => getValue().then(arr => arr.forEach(...args)),
includes: (...args: Parameters<Array<T>["includes"]>) => getValue().then(arr => arr.includes(...args)),
indexOf: (...args: Parameters<Array<T>["indexOf"]>) => getValue().then(arr => arr.indexOf(...args)),
lastIndexOf: (...args: Parameters<Array<T>["lastIndexOf"]>) => getValue().then(arr => arr.lastIndexOf(...args)),
map: (...args: Parameters<Array<T>["map"]>) => getValue().then(arr => arr.map(...args)),
reduce: (...args: Parameters<Array<T>["reduce"]>) => getValue().then(arr => arr.reduce(...args)),
slice: (...args: Parameters<Array<T>["slice"]>) => getValue().then(arr => arr.slice(...args)),
some: (...args: Parameters<Array<T>["some"]>) => getValue().then(arr => arr.some(...args)),
};
};
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.