defuss-db 1.1.4 → 2.0.1

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.
@@ -0,0 +1,242 @@
1
+ const INTERNAL_INDEX_PREFIX = "__defuss_idx_";
2
+ const normalizedTableCache = /* @__PURE__ */ new WeakMap();
3
+ function sanitizeIndexName(name) {
4
+ return name.replace(/[^a-zA-Z0-9_]+/g, "_");
5
+ }
6
+ function getValueAtPath(value, path) {
7
+ const segments = path.split(".").filter(Boolean);
8
+ let current = value;
9
+ for (const segment of segments) {
10
+ if (current === null || current === void 0 || typeof current !== "object") {
11
+ return void 0;
12
+ }
13
+ current = current[segment];
14
+ }
15
+ return current;
16
+ }
17
+ function setValueAtPath(value, path, nextValue) {
18
+ const segments = path.split(".").filter(Boolean);
19
+ if (segments.length === 0) {
20
+ return;
21
+ }
22
+ let current = value;
23
+ for (let index = 0; index < segments.length - 1; index += 1) {
24
+ const segment = segments[index];
25
+ const existing = current[segment];
26
+ if (existing === null || existing === void 0 || typeof existing !== "object") {
27
+ current[segment] = {};
28
+ }
29
+ current = current[segment];
30
+ }
31
+ current[segments[segments.length - 1]] = nextValue;
32
+ }
33
+ function isEqualRecordValue(left, right) {
34
+ if (left instanceof Date && right instanceof Date) {
35
+ return left.getTime() === right.getTime();
36
+ }
37
+ if (left instanceof ArrayBuffer && right instanceof ArrayBuffer) {
38
+ if (left.byteLength !== right.byteLength) {
39
+ return false;
40
+ }
41
+ const leftView = new Uint8Array(left);
42
+ const rightView = new Uint8Array(right);
43
+ for (let index = 0; index < leftView.length; index += 1) {
44
+ if (leftView[index] !== rightView[index]) {
45
+ return false;
46
+ }
47
+ }
48
+ return true;
49
+ }
50
+ return left === right;
51
+ }
52
+ function normalizeIndexDefinition(index) {
53
+ const selectorKey = typeof index.source === "string" ? index.source : index.name;
54
+ return {
55
+ name: index.name,
56
+ selectorKey,
57
+ storageKey: `${INTERNAL_INDEX_PREFIX}${sanitizeIndexName(index.name)}`,
58
+ unique: index.unique === true,
59
+ source: index.source,
60
+ resolveValue: (value) => {
61
+ if (typeof index.source === "function") {
62
+ return index.source(value);
63
+ }
64
+ if (typeof index.source === "string") {
65
+ return getValueAtPath(value, index.source);
66
+ }
67
+ return getValueAtPath(value, index.name);
68
+ }
69
+ };
70
+ }
71
+ function normalizeTableDefinition(definition) {
72
+ const existing = normalizedTableCache.get(definition);
73
+ if (existing) {
74
+ return existing;
75
+ }
76
+ const indexes = [
77
+ {
78
+ name: "id",
79
+ selectorKey: "id",
80
+ storageKey: "id",
81
+ unique: true,
82
+ resolveValue: (value) => value.id
83
+ }
84
+ ];
85
+ for (const index of definition.indexes ?? []) {
86
+ if (index.name === "id") {
87
+ continue;
88
+ }
89
+ indexes.push(normalizeIndexDefinition(index));
90
+ }
91
+ const selectorIndexMap = /* @__PURE__ */ new Map();
92
+ for (const index of indexes) {
93
+ selectorIndexMap.set(index.selectorKey, index);
94
+ }
95
+ const normalized = {
96
+ name: definition.name,
97
+ indexes,
98
+ selectorIndexMap
99
+ };
100
+ normalizedTableCache.set(definition, normalized);
101
+ return normalized;
102
+ }
103
+ function assertValidId(id) {
104
+ if (id === void 0) {
105
+ return;
106
+ }
107
+ if (typeof id === "string" && id.trim().length === 0) {
108
+ throw new Error("defuss-db: id cannot be empty.");
109
+ }
110
+ if (typeof id === "number" && !Number.isFinite(id)) {
111
+ throw new Error("defuss-db: id must be a finite number.");
112
+ }
113
+ }
114
+ function toStoredRecord(definition, value) {
115
+ assertValidId(value.id);
116
+ const storedRecord = { ...value };
117
+ for (const index of definition.indexes) {
118
+ if (index.selectorKey === "id") {
119
+ continue;
120
+ }
121
+ const indexValue = index.resolveValue(value);
122
+ if (indexValue === void 0) {
123
+ delete storedRecord[index.storageKey];
124
+ continue;
125
+ }
126
+ storedRecord[index.storageKey] = indexValue;
127
+ }
128
+ return storedRecord;
129
+ }
130
+ function fromStoredRecord(storedRecord) {
131
+ const result = {};
132
+ for (const [key, value] of Object.entries(storedRecord)) {
133
+ if (key.startsWith(INTERNAL_INDEX_PREFIX)) {
134
+ continue;
135
+ }
136
+ result[key] = value;
137
+ }
138
+ return result;
139
+ }
140
+ function matchesSelector(value, selector, definition) {
141
+ for (const [key, expected] of Object.entries(selector)) {
142
+ if (expected === void 0) {
143
+ continue;
144
+ }
145
+ const actual = key === "id" ? value.id : definition?.selectorIndexMap.get(key)?.resolveValue(value) ?? getValueAtPath(value, key);
146
+ if (!isEqualRecordValue(actual, expected)) {
147
+ return false;
148
+ }
149
+ }
150
+ return true;
151
+ }
152
+ function mergeSelectorIntoValue(definition, selector, value) {
153
+ const merged = cloneRuntimeValue(value);
154
+ for (const [key, selectorValue] of Object.entries(selector)) {
155
+ if (selectorValue === void 0) {
156
+ continue;
157
+ }
158
+ if (key === "id") {
159
+ if (merged.id !== void 0 && !isEqualRecordValue(merged.id, selectorValue)) {
160
+ throw new Error("defuss-db: upsert selector id does not match value.id.");
161
+ }
162
+ merged.id = selectorValue;
163
+ continue;
164
+ }
165
+ const index = definition.selectorIndexMap.get(key);
166
+ if (!index) {
167
+ setValueAtPath(merged, key, selectorValue);
168
+ continue;
169
+ }
170
+ if (typeof index.source === "string") {
171
+ const existing = getValueAtPath(merged, index.source);
172
+ if (existing !== void 0 && !isEqualRecordValue(existing, selectorValue)) {
173
+ throw new Error(
174
+ `defuss-db: upsert selector '${key}' does not match the provided value.`
175
+ );
176
+ }
177
+ setValueAtPath(merged, index.source, selectorValue);
178
+ }
179
+ }
180
+ return merged;
181
+ }
182
+ function cloneRuntimeValue(value) {
183
+ if (value === null || value === void 0 || typeof value === "string" || typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
184
+ return value;
185
+ }
186
+ if (value instanceof Date) {
187
+ return new Date(value.getTime());
188
+ }
189
+ if (value instanceof ArrayBuffer) {
190
+ return value.slice(0);
191
+ }
192
+ if (Array.isArray(value)) {
193
+ return value.map((item) => cloneRuntimeValue(item));
194
+ }
195
+ if (isBlobLike(value)) {
196
+ return value.slice(0, value.size, value.type);
197
+ }
198
+ if (typeof value === "object") {
199
+ const result = {};
200
+ for (const [key, nestedValue] of Object.entries(value)) {
201
+ result[key] = cloneRuntimeValue(nestedValue);
202
+ }
203
+ return result;
204
+ }
205
+ return value;
206
+ }
207
+ function isBlobLike(value) {
208
+ return typeof value === "object" && value !== null && typeof value.arrayBuffer === "function" && typeof value.slice === "function" && typeof value.type === "string";
209
+ }
210
+ function resolveSelectorIndex(definition, key) {
211
+ return definition.selectorIndexMap.get(key);
212
+ }
213
+ function resolveIndexedSelectorValue(definition, key, value) {
214
+ const index = resolveSelectorIndex(definition, key);
215
+ if (!index) {
216
+ return null;
217
+ }
218
+ return {
219
+ storageKey: index.storageKey,
220
+ value
221
+ };
222
+ }
223
+ function resolveUniqueSelector(definition, selector) {
224
+ const keys = Object.keys(selector).filter((key) => selector[key] !== void 0);
225
+ if (keys.length === 0) {
226
+ throw new Error("defuss-db: upsert requires a non-empty selector.");
227
+ }
228
+ if (keys.length !== 1) {
229
+ throw new Error(
230
+ "defuss-db: upsert selectors must target exactly one unique field."
231
+ );
232
+ }
233
+ const index = resolveSelectorIndex(definition, keys[0]);
234
+ if (!index || !index.unique) {
235
+ throw new Error(
236
+ `defuss-db: upsert selector '${keys[0]}' must resolve to 'id' or a declared unique index.`
237
+ );
238
+ }
239
+ return index;
240
+ }
241
+
242
+ export { resolveUniqueSelector as a, mergeSelectorIntoValue as b, resolveSelectorIndex as c, fromStoredRecord as f, matchesSelector as m, normalizeTableDefinition as n, resolveIndexedSelectorValue as r, toStoredRecord as t };
@@ -0,0 +1,17 @@
1
+ import { DefussRecord, RecordValue } from './index.js';
2
+
3
+ interface NormalizedDefussIndexDefinition<T extends DefussRecord = DefussRecord> {
4
+ name: string;
5
+ selectorKey: string;
6
+ storageKey: string;
7
+ unique: boolean;
8
+ source?: string | ((value: T) => RecordValue | undefined);
9
+ resolveValue: (value: T) => RecordValue | undefined;
10
+ }
11
+ interface NormalizedDefussTableDefinition<T extends DefussRecord = DefussRecord> {
12
+ name: string;
13
+ indexes: Array<NormalizedDefussIndexDefinition<T>>;
14
+ selectorIndexMap: Map<string, NormalizedDefussIndexDefinition<T>>;
15
+ }
16
+
17
+ export type { NormalizedDefussTableDefinition as N };
@@ -0,0 +1,251 @@
1
+ 'use strict';
2
+
3
+ const INTERNAL_INDEX_PREFIX = "__defuss_idx_";
4
+ const normalizedTableCache = /* @__PURE__ */ new WeakMap();
5
+ function sanitizeIndexName(name) {
6
+ return name.replace(/[^a-zA-Z0-9_]+/g, "_");
7
+ }
8
+ function getValueAtPath(value, path) {
9
+ const segments = path.split(".").filter(Boolean);
10
+ let current = value;
11
+ for (const segment of segments) {
12
+ if (current === null || current === void 0 || typeof current !== "object") {
13
+ return void 0;
14
+ }
15
+ current = current[segment];
16
+ }
17
+ return current;
18
+ }
19
+ function setValueAtPath(value, path, nextValue) {
20
+ const segments = path.split(".").filter(Boolean);
21
+ if (segments.length === 0) {
22
+ return;
23
+ }
24
+ let current = value;
25
+ for (let index = 0; index < segments.length - 1; index += 1) {
26
+ const segment = segments[index];
27
+ const existing = current[segment];
28
+ if (existing === null || existing === void 0 || typeof existing !== "object") {
29
+ current[segment] = {};
30
+ }
31
+ current = current[segment];
32
+ }
33
+ current[segments[segments.length - 1]] = nextValue;
34
+ }
35
+ function isEqualRecordValue(left, right) {
36
+ if (left instanceof Date && right instanceof Date) {
37
+ return left.getTime() === right.getTime();
38
+ }
39
+ if (left instanceof ArrayBuffer && right instanceof ArrayBuffer) {
40
+ if (left.byteLength !== right.byteLength) {
41
+ return false;
42
+ }
43
+ const leftView = new Uint8Array(left);
44
+ const rightView = new Uint8Array(right);
45
+ for (let index = 0; index < leftView.length; index += 1) {
46
+ if (leftView[index] !== rightView[index]) {
47
+ return false;
48
+ }
49
+ }
50
+ return true;
51
+ }
52
+ return left === right;
53
+ }
54
+ function normalizeIndexDefinition(index) {
55
+ const selectorKey = typeof index.source === "string" ? index.source : index.name;
56
+ return {
57
+ name: index.name,
58
+ selectorKey,
59
+ storageKey: `${INTERNAL_INDEX_PREFIX}${sanitizeIndexName(index.name)}`,
60
+ unique: index.unique === true,
61
+ source: index.source,
62
+ resolveValue: (value) => {
63
+ if (typeof index.source === "function") {
64
+ return index.source(value);
65
+ }
66
+ if (typeof index.source === "string") {
67
+ return getValueAtPath(value, index.source);
68
+ }
69
+ return getValueAtPath(value, index.name);
70
+ }
71
+ };
72
+ }
73
+ function normalizeTableDefinition(definition) {
74
+ const existing = normalizedTableCache.get(definition);
75
+ if (existing) {
76
+ return existing;
77
+ }
78
+ const indexes = [
79
+ {
80
+ name: "id",
81
+ selectorKey: "id",
82
+ storageKey: "id",
83
+ unique: true,
84
+ resolveValue: (value) => value.id
85
+ }
86
+ ];
87
+ for (const index of definition.indexes ?? []) {
88
+ if (index.name === "id") {
89
+ continue;
90
+ }
91
+ indexes.push(normalizeIndexDefinition(index));
92
+ }
93
+ const selectorIndexMap = /* @__PURE__ */ new Map();
94
+ for (const index of indexes) {
95
+ selectorIndexMap.set(index.selectorKey, index);
96
+ }
97
+ const normalized = {
98
+ name: definition.name,
99
+ indexes,
100
+ selectorIndexMap
101
+ };
102
+ normalizedTableCache.set(definition, normalized);
103
+ return normalized;
104
+ }
105
+ function assertValidId(id) {
106
+ if (id === void 0) {
107
+ return;
108
+ }
109
+ if (typeof id === "string" && id.trim().length === 0) {
110
+ throw new Error("defuss-db: id cannot be empty.");
111
+ }
112
+ if (typeof id === "number" && !Number.isFinite(id)) {
113
+ throw new Error("defuss-db: id must be a finite number.");
114
+ }
115
+ }
116
+ function toStoredRecord(definition, value) {
117
+ assertValidId(value.id);
118
+ const storedRecord = { ...value };
119
+ for (const index of definition.indexes) {
120
+ if (index.selectorKey === "id") {
121
+ continue;
122
+ }
123
+ const indexValue = index.resolveValue(value);
124
+ if (indexValue === void 0) {
125
+ delete storedRecord[index.storageKey];
126
+ continue;
127
+ }
128
+ storedRecord[index.storageKey] = indexValue;
129
+ }
130
+ return storedRecord;
131
+ }
132
+ function fromStoredRecord(storedRecord) {
133
+ const result = {};
134
+ for (const [key, value] of Object.entries(storedRecord)) {
135
+ if (key.startsWith(INTERNAL_INDEX_PREFIX)) {
136
+ continue;
137
+ }
138
+ result[key] = value;
139
+ }
140
+ return result;
141
+ }
142
+ function matchesSelector(value, selector, definition) {
143
+ for (const [key, expected] of Object.entries(selector)) {
144
+ if (expected === void 0) {
145
+ continue;
146
+ }
147
+ const actual = key === "id" ? value.id : definition?.selectorIndexMap.get(key)?.resolveValue(value) ?? getValueAtPath(value, key);
148
+ if (!isEqualRecordValue(actual, expected)) {
149
+ return false;
150
+ }
151
+ }
152
+ return true;
153
+ }
154
+ function mergeSelectorIntoValue(definition, selector, value) {
155
+ const merged = cloneRuntimeValue(value);
156
+ for (const [key, selectorValue] of Object.entries(selector)) {
157
+ if (selectorValue === void 0) {
158
+ continue;
159
+ }
160
+ if (key === "id") {
161
+ if (merged.id !== void 0 && !isEqualRecordValue(merged.id, selectorValue)) {
162
+ throw new Error("defuss-db: upsert selector id does not match value.id.");
163
+ }
164
+ merged.id = selectorValue;
165
+ continue;
166
+ }
167
+ const index = definition.selectorIndexMap.get(key);
168
+ if (!index) {
169
+ setValueAtPath(merged, key, selectorValue);
170
+ continue;
171
+ }
172
+ if (typeof index.source === "string") {
173
+ const existing = getValueAtPath(merged, index.source);
174
+ if (existing !== void 0 && !isEqualRecordValue(existing, selectorValue)) {
175
+ throw new Error(
176
+ `defuss-db: upsert selector '${key}' does not match the provided value.`
177
+ );
178
+ }
179
+ setValueAtPath(merged, index.source, selectorValue);
180
+ }
181
+ }
182
+ return merged;
183
+ }
184
+ function cloneRuntimeValue(value) {
185
+ if (value === null || value === void 0 || typeof value === "string" || typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
186
+ return value;
187
+ }
188
+ if (value instanceof Date) {
189
+ return new Date(value.getTime());
190
+ }
191
+ if (value instanceof ArrayBuffer) {
192
+ return value.slice(0);
193
+ }
194
+ if (Array.isArray(value)) {
195
+ return value.map((item) => cloneRuntimeValue(item));
196
+ }
197
+ if (isBlobLike(value)) {
198
+ return value.slice(0, value.size, value.type);
199
+ }
200
+ if (typeof value === "object") {
201
+ const result = {};
202
+ for (const [key, nestedValue] of Object.entries(value)) {
203
+ result[key] = cloneRuntimeValue(nestedValue);
204
+ }
205
+ return result;
206
+ }
207
+ return value;
208
+ }
209
+ function isBlobLike(value) {
210
+ return typeof value === "object" && value !== null && typeof value.arrayBuffer === "function" && typeof value.slice === "function" && typeof value.type === "string";
211
+ }
212
+ function resolveSelectorIndex(definition, key) {
213
+ return definition.selectorIndexMap.get(key);
214
+ }
215
+ function resolveIndexedSelectorValue(definition, key, value) {
216
+ const index = resolveSelectorIndex(definition, key);
217
+ if (!index) {
218
+ return null;
219
+ }
220
+ return {
221
+ storageKey: index.storageKey,
222
+ value
223
+ };
224
+ }
225
+ function resolveUniqueSelector(definition, selector) {
226
+ const keys = Object.keys(selector).filter((key) => selector[key] !== void 0);
227
+ if (keys.length === 0) {
228
+ throw new Error("defuss-db: upsert requires a non-empty selector.");
229
+ }
230
+ if (keys.length !== 1) {
231
+ throw new Error(
232
+ "defuss-db: upsert selectors must target exactly one unique field."
233
+ );
234
+ }
235
+ const index = resolveSelectorIndex(definition, keys[0]);
236
+ if (!index || !index.unique) {
237
+ throw new Error(
238
+ `defuss-db: upsert selector '${keys[0]}' must resolve to 'id' or a declared unique index.`
239
+ );
240
+ }
241
+ return index;
242
+ }
243
+
244
+ exports.fromStoredRecord = fromStoredRecord;
245
+ exports.matchesSelector = matchesSelector;
246
+ exports.mergeSelectorIntoValue = mergeSelectorIntoValue;
247
+ exports.normalizeTableDefinition = normalizeTableDefinition;
248
+ exports.resolveIndexedSelectorValue = resolveIndexedSelectorValue;
249
+ exports.resolveSelectorIndex = resolveSelectorIndex;
250
+ exports.resolveUniqueSelector = resolveUniqueSelector;
251
+ exports.toStoredRecord = toStoredRecord;