@bitcobblers/wod-wiki-storage 0.59.2236 → 0.59.2240

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -817,11 +817,211 @@ function isUserOwnedRow(store, row, ctx) {
817
817
  }
818
818
  }
819
819
 
820
+ // src/memory.ts
821
+ var InMemoryStore = class {
822
+ constructor(name, getUserId, rows = /* @__PURE__ */ new Map(), mutable = true, commit) {
823
+ this.name = name;
824
+ this.getUserId = getUserId;
825
+ this.rows = rows;
826
+ this.mutable = mutable;
827
+ this.commit = commit;
828
+ this.def = STORE_DEFS.find((def) => def.name === name);
829
+ if (!this.def) throw new Error(`unknown store ${name}`);
830
+ }
831
+ name;
832
+ getUserId;
833
+ rows;
834
+ mutable;
835
+ commit;
836
+ def;
837
+ async get(key) {
838
+ return structuredClone(this.rows.get(JSON.stringify(key)));
839
+ }
840
+ async getMany(keys) {
841
+ return Promise.all(keys.map((key) => this.get(key)));
842
+ }
843
+ async getAll(query, count) {
844
+ const range = rangeFromIDB(query);
845
+ const records = [...this.rows.values()].filter((row) => !range || keyInRange(keyFromValue(this.def, row), range));
846
+ records.sort((a, b) => compareKeys(keyFromValue(this.def, a), keyFromValue(this.def, b)));
847
+ return structuredClone(count === void 0 ? records : records.slice(0, count));
848
+ }
849
+ async getAllFromIndex(indexName, query, count) {
850
+ const index = this.def.indexes.find((index2) => index2.name === indexName);
851
+ if (!index) throw new Error(`unknown index ${this.name}.${indexName}`);
852
+ const range = rangeFromIDB(query);
853
+ const entries = [];
854
+ for (const value of this.rows.values()) {
855
+ if (index.multiEntry) {
856
+ const items = typeof value === "object" && value !== null ? Reflect.get(value, index.keyPath[0]) : void 0;
857
+ if (Array.isArray(items)) for (const item of new Set(items)) {
858
+ if (typeof item !== "string" && typeof item !== "number") continue;
859
+ if (!range || keyInRange(item, range)) entries.push({ key: item, value });
860
+ }
861
+ } else {
862
+ const parts = index.keyPath.map((path) => extractKeyPart(value, path));
863
+ if (parts.some((part) => part === void 0)) continue;
864
+ const key = parts.length === 1 ? parts[0] : parts;
865
+ if (!range || keyInRange(key, range)) entries.push({ key, value });
866
+ }
867
+ }
868
+ entries.sort((a, b) => compareKeys(a.key, b.key) || compareKeys(keyFromValue(this.def, a.value), keyFromValue(this.def, b.value)));
869
+ const selected = count === void 0 ? entries : entries.slice(0, count);
870
+ return structuredClone(selected.map((entry) => entry.value));
871
+ }
872
+ async getAllFromIndexMany(index, keys) {
873
+ return Promise.all(keys.map((key) => this.getAllFromIndex(index, key)));
874
+ }
875
+ async count(query) {
876
+ return (await this.getAll(query)).length;
877
+ }
878
+ async countFromIndex(index, query) {
879
+ return (await this.getAllFromIndex(index, query)).length;
880
+ }
881
+ async put(value, _key) {
882
+ if (!this.mutable) throw new Error("read-only store");
883
+ if (this.commit) return this.commit((store) => store.put(value));
884
+ const userId = this.getUserId?.();
885
+ const stored = userId ? stampUserOwned(this.name, value, userId) : value;
886
+ validateStorageRow(this.name, stored);
887
+ const key = keyFromValue(this.def, stored), slot = JSON.stringify(key);
888
+ const previous = this.rows.get(slot);
889
+ if (this.name === "block_revisions" && previous !== void 0 && !rowsEqual(previous, stored)) throw new Error("immutable block revision collision");
890
+ if (this.name === "notes") validateNoteWrite(previous, stored);
891
+ if (this.name === "sessions") validateSessionWrite(previous, stored);
892
+ if (rowsEqual(previous, stored)) return key;
893
+ for (const index of this.def.indexes) {
894
+ if (!index.unique) continue;
895
+ const parts = index.keyPath.map((path) => extractKeyPart(stored, path));
896
+ if (parts.some((part) => part === void 0)) continue;
897
+ for (const [otherKey, row] of this.rows) {
898
+ if (otherKey !== slot && rowsEqual(parts, index.keyPath.map((path) => extractKeyPart(row, path)))) throw new Error(`unique index collision ${index.name}`);
899
+ }
900
+ }
901
+ this.rows.set(slot, structuredClone(stored));
902
+ return key;
903
+ }
904
+ async putMany(values, keys) {
905
+ if (this.commit) return this.commit((store) => store.putMany(values, keys));
906
+ if (keys && keys.length !== values.length) throw new Error("putMany key count mismatch");
907
+ const out = [];
908
+ for (let i = 0; i < values.length; i++) out.push(await this.put(values[i], keys?.[i]));
909
+ return out;
910
+ }
911
+ async delete(key) {
912
+ if (!this.mutable) throw new Error("read-only store");
913
+ if (this.name === "block_revisions") throw new Error("block history requires explicit wipe");
914
+ if (this.commit) {
915
+ await this.commit((store) => store.delete(key));
916
+ return;
917
+ }
918
+ const range = rangeFromIDB(key);
919
+ for (const [slot, value] of this.rows) if (keyInRange(keyFromValue(this.def, value), range)) this.rows.delete(slot);
920
+ }
921
+ async clear() {
922
+ if (!this.mutable) throw new Error("read-only store");
923
+ if (this.name === "block_revisions") throw new Error("block history requires explicit wipe");
924
+ if (this.commit) {
925
+ await this.commit((store) => store.clear());
926
+ return;
927
+ }
928
+ this.rows.clear();
929
+ }
930
+ snapshotRows() {
931
+ return structuredClone(this.rows);
932
+ }
933
+ restoreRows(rows) {
934
+ this.rows.clear();
935
+ for (const [key, value] of rows ?? []) this.rows.set(key, structuredClone(value));
936
+ }
937
+ };
938
+ var InMemoryStorage = class {
939
+ constructor(options = {}) {
940
+ this.options = options;
941
+ }
942
+ options;
943
+ stores = /* @__PURE__ */ new Map();
944
+ chain = Promise.resolve();
945
+ readonly(name) {
946
+ let rows = this.stores.get(name);
947
+ if (!rows) {
948
+ rows = /* @__PURE__ */ new Map();
949
+ this.stores.set(name, rows);
950
+ }
951
+ return new InMemoryStore(name, this.options.getUserId, rows, false);
952
+ }
953
+ readwrite(name) {
954
+ let rows = this.stores.get(name);
955
+ if (!rows) {
956
+ rows = /* @__PURE__ */ new Map();
957
+ this.stores.set(name, rows);
958
+ }
959
+ return new InMemoryStore(
960
+ name,
961
+ this.options.getUserId,
962
+ rows,
963
+ true,
964
+ (operation) => this.transaction([name, "block_revisions"], "readwrite", (tx) => operation(tx.readwrite(name)))
965
+ );
966
+ }
967
+ transaction(stores, mode, fn) {
968
+ const run = async () => {
969
+ const staged = /* @__PURE__ */ new Map();
970
+ const scope = [...stores, ...mode === "readwrite" && stores.includes("notes") ? ["block_index", "block_efforts"] : []];
971
+ for (const name of scope) staged.set(name, structuredClone(this.stores.get(name) ?? /* @__PURE__ */ new Map()));
972
+ const view = (name, mutable) => {
973
+ if (!stores.includes(name)) throw new Error("store outside transaction scope");
974
+ const rows = staged.get(name);
975
+ if (!rows) throw new Error("store outside transaction scope");
976
+ if (mutable && mode !== "readwrite") throw new Error("read-only transaction");
977
+ return new InMemoryStore(name, this.options.getUserId, rows, mutable);
978
+ };
979
+ const result = await fn({ readonly: (name) => view(name, false), readwrite: (name) => view(name, true) });
980
+ if (mode === "readwrite") {
981
+ if (staged.has("notes")) {
982
+ const notes2 = staged.get("notes");
983
+ const removed = new Set([...this.stores.get("notes")?.keys() ?? []].filter((key) => !notes2.has(key)));
984
+ for (const name of ["block_index", "block_efforts"]) {
985
+ for (const [key, row] of staged.get(name)) {
986
+ if (row && typeof row === "object" && "noteId" in row && removed.has(JSON.stringify(row.noteId))) staged.get(name).delete(key);
987
+ }
988
+ }
989
+ }
990
+ const notes = [...staged.get("notes")?.values() ?? []];
991
+ const sessions = [...staged.get("sessions")?.values() ?? []];
992
+ const revisions = staged.get("block_revisions") ?? this.stores.get("block_revisions");
993
+ await validateStoredPins(notes, sessions, async (keys) => keys.map((key) => revisions?.get(JSON.stringify(key))));
994
+ for (const [name, rows] of staged) {
995
+ const live = this.stores.get(name);
996
+ if (!live) this.stores.set(name, rows);
997
+ else {
998
+ live.clear();
999
+ for (const [key, value] of rows) live.set(key, value);
1000
+ }
1001
+ }
1002
+ }
1003
+ return result;
1004
+ };
1005
+ const next = this.chain.then(run, run);
1006
+ this.chain = next.then(() => void 0, () => void 0);
1007
+ return next;
1008
+ }
1009
+ async wipe() {
1010
+ await this.chain;
1011
+ for (const def of STORE_DEFS) if (!def.system) this.stores.get(def.name)?.clear();
1012
+ }
1013
+ async close() {
1014
+ await this.chain;
1015
+ }
1016
+ };
1017
+
820
1018
  exports.ApiStorage = ApiStorage;
821
1019
  exports.COLUMN_TYPES = COLUMN_TYPES;
822
1020
  exports.DB_VERSION = DB_VERSION;
823
1021
  exports.DEFAULT_MEMBERSHIP_ID = DEFAULT_MEMBERSHIP_ID;
824
1022
  exports.DOMAIN_PROJECTION_VERSION = DOMAIN_PROJECTION_VERSION;
1023
+ exports.InMemoryStorage = InMemoryStorage;
1024
+ exports.InMemoryStore = InMemoryStore;
825
1025
  exports.STORE_DEFS = STORE_DEFS;
826
1026
  exports.USER_DELETED_SEED_PREFIX = USER_DELETED_SEED_PREFIX;
827
1027
  exports.USER_OWNED_STORES = USER_OWNED_STORES;