@joystick.js/db-canary 0.0.0-canary.2256 → 0.0.0-canary.2257
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
import{get_database as
|
|
1
|
+
import{get_database as v,build_collection_key as O,generate_document_id as x}from"../query_engine.js";import{update_indexes_on_update as $,update_indexes_on_insert as S}from"../index_manager.js";import{get_write_queue as q}from"../write_queue.js";import E from"../logger.js";const{create_context_logger:J}=E("update_one"),w=(t,n,e)=>{const r=n.split("."),o={...t};let s=o;for(let c=0;c<r.length-1;c++){const u=r[c];!(u in s)||typeof s[u]!="object"||s[u]===null?s[u]={}:s[u]={...s[u]},s=s[u]}return s[r[r.length-1]]=e,o},N=(t,n)=>{let e={...t};for(const[r,o]of Object.entries(n))r.includes(".")?e=w(e,r,o):e[r]=o;return e},A=(t,n)=>{const e=n.split(".");let r=t;for(const o of e){if(r==null||typeof r!="object")return;r=r[o]}return r},F=(t,n)=>{const e=n.split("."),r={...t};let o=r;for(let s=0;s<e.length-1;s++){const c=e[s];if(!(c in o)||typeof o[c]!="object"||o[c]===null)return r;o[c]={...o[c]},o=o[c]}return delete o[e[e.length-1]],r},U=(t,n)=>{let e={...t};for(const r of Object.keys(n))r.includes(".")?e=F(e,r):delete e[r];return e},D=(t,n)=>{let e={...t};for(const[r,o]of Object.entries(n))if(r.includes(".")){const s=A(e,r)||0;e=w(e,r,s+o)}else e[r]=(e[r]||0)+o;return e},C=(t,n)=>{const e={...t};for(const[r,o]of Object.entries(n))Array.isArray(e[r])||(e[r]=[]),e[r]=[...e[r],o];return e},I=(t,n)=>{const e={...t};for(const[r,o]of Object.entries(n))Array.isArray(e[r])&&(e[r]=e[r].filter(s=>s!==o));return e},b=(t,n)=>{let e={...t};for(const[r,o]of Object.entries(n))switch(r){case"$set":e=N(e,o);break;case"$unset":e=U(e,o);break;case"$inc":e=D(e,o);break;case"$push":e=C(e,o);break;case"$pull":e=I(e,o);break;default:throw new Error(`Unsupported update operator: ${r}`)}return e},R=(t,n,e)=>t[n]===e,z=(t,n)=>{if(!n||Object.keys(n).length===0)return!0;for(const[e,r]of Object.entries(n))if(!R(t,e,r))return!1;return!0},B=t=>{if(!t)throw new Error("Database name is required")},G=t=>{if(!t)throw new Error("Collection name is required")},H=t=>{if(!t||typeof t!="object")throw new Error("Filter must be a valid object")},K=t=>{if(!t||typeof t!="object")throw new Error("Update must be a valid object")},L=(t,n,e,r)=>{B(t),G(n),H(e),K(r)},M=t=>{try{return JSON.parse(t)}catch{return null}},g=()=>new Date().toISOString(),P=t=>({...t,_updated_at:g()}),Q=(t,n)=>JSON.stringify(t)!==JSON.stringify(n),T=(t,n)=>{const e=x(),r=g(),o={...t,_id:e,_created_at:r,_updated_at:r};return b(o,n)},V=(t,n,e,r,o,s)=>{let c=0,u=0,i=null,d=null,l=null,a=null;const p=`${n}:${e}:`;let f=!1;const k=t.getRange({start:p,end:p+"\xFF"});for(const{key:m,value:h}of k){const _=M(h);if(_&&z(_,r)){f=!0,c=1;const j=b(_,o),y=P(j);Q(_,y)&&(t.put(m,JSON.stringify(y)),d=_,l=y,u=1);break}}if(!f&&s.upsert){a=T(r,o);const m=O(n,e,a._id);t.put(m,JSON.stringify(a)),i=a._id,c=0,u=0}return{matched_count:c,modified_count:u,upserted_id:i,old_document:d,new_document:l,upserted_document:a}},W=async(t,n,e,r)=>{e&&r&&await $(t,n,e,r)},X=async(t,n,e)=>{e&&await S(t,n,e)},Y=(t,n,e,r,o,s)=>{t.info("Update operation completed",{database:n,collection:e,matched_count:r,modified_count:o,upserted_id:s})},Z=(t,n,e)=>{const r={acknowledged:!0,matched_count:t,modified_count:n};return e&&(r.upserted_id=e),r},ee=(t,n,e)=>({operation:"update_one",database:t,collection:n,filter_keys:Object.keys(e||{})}),te=async(t,n,e,r,o={})=>{const s=J();L(t,n,e,r);const c=v(),u=await c.transaction(()=>V(c,t,n,e,r,o)),{matched_count:i,modified_count:d,upserted_id:l,old_document:a,new_document:p,upserted_document:f}=u;return await W(t,n,a,p),await X(t,n,f),Y(s,t,n,i,d,l),Z(i,d,l)},re=async(t,n,e,r,o={})=>{const s=q(),c=ee(t,n,e);return await s.enqueue_write_operation(()=>te(t,n,e,r,o),c)};var ue=re;export{ue as default};
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@joystick.js/db-canary",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.0-canary.
|
|
5
|
-
"canary_version": "0.0.0-canary.
|
|
4
|
+
"version": "0.0.0-canary.2257",
|
|
5
|
+
"canary_version": "0.0.0-canary.2256",
|
|
6
6
|
"description": "JoystickDB - A minimalist database server for the Joystick framework",
|
|
7
7
|
"main": "./dist/server/index.js",
|
|
8
8
|
"scripts": {
|
|
@@ -5,6 +5,32 @@ import create_logger from '../logger.js';
|
|
|
5
5
|
|
|
6
6
|
const { create_context_logger } = create_logger('update_one');
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Sets a nested field value using dot notation.
|
|
10
|
+
* @param {Object} obj - Object to update
|
|
11
|
+
* @param {string} path - Dot notation path (e.g., 'user.profile.name')
|
|
12
|
+
* @param {any} value - Value to set
|
|
13
|
+
* @returns {Object} Updated object
|
|
14
|
+
*/
|
|
15
|
+
const set_nested_field = (obj, path, value) => {
|
|
16
|
+
const keys = path.split('.');
|
|
17
|
+
const result = { ...obj };
|
|
18
|
+
let current = result;
|
|
19
|
+
|
|
20
|
+
for (let i = 0; i < keys.length - 1; i++) {
|
|
21
|
+
const key = keys[i];
|
|
22
|
+
if (!(key in current) || typeof current[key] !== 'object' || current[key] === null) {
|
|
23
|
+
current[key] = {};
|
|
24
|
+
} else {
|
|
25
|
+
current[key] = { ...current[key] };
|
|
26
|
+
}
|
|
27
|
+
current = current[key];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
current[keys[keys.length - 1]] = value;
|
|
31
|
+
return result;
|
|
32
|
+
};
|
|
33
|
+
|
|
8
34
|
/**
|
|
9
35
|
* Applies $set operator to document.
|
|
10
36
|
* @param {Object} document - Document to update
|
|
@@ -12,7 +38,63 @@ const { create_context_logger } = create_logger('update_one');
|
|
|
12
38
|
* @returns {Object} Updated document
|
|
13
39
|
*/
|
|
14
40
|
const apply_set_operator = (document, operations) => {
|
|
15
|
-
|
|
41
|
+
let updated_document = { ...document };
|
|
42
|
+
|
|
43
|
+
for (const [field, value] of Object.entries(operations)) {
|
|
44
|
+
if (field.includes('.')) {
|
|
45
|
+
// Handle nested field updates with dot notation
|
|
46
|
+
updated_document = set_nested_field(updated_document, field, value);
|
|
47
|
+
} else {
|
|
48
|
+
// Handle simple field updates
|
|
49
|
+
updated_document[field] = value;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return updated_document;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Gets a nested field value using dot notation.
|
|
58
|
+
* @param {Object} obj - Object to get value from
|
|
59
|
+
* @param {string} path - Dot notation path (e.g., 'user.profile.name')
|
|
60
|
+
* @returns {any} Field value or undefined
|
|
61
|
+
*/
|
|
62
|
+
const get_nested_field = (obj, path) => {
|
|
63
|
+
const keys = path.split('.');
|
|
64
|
+
let current = obj;
|
|
65
|
+
|
|
66
|
+
for (const key of keys) {
|
|
67
|
+
if (current === null || current === undefined || typeof current !== 'object') {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
current = current[key];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return current;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Unsets a nested field using dot notation.
|
|
78
|
+
* @param {Object} obj - Object to update
|
|
79
|
+
* @param {string} path - Dot notation path (e.g., 'user.profile.name')
|
|
80
|
+
* @returns {Object} Updated object
|
|
81
|
+
*/
|
|
82
|
+
const unset_nested_field = (obj, path) => {
|
|
83
|
+
const keys = path.split('.');
|
|
84
|
+
const result = { ...obj };
|
|
85
|
+
let current = result;
|
|
86
|
+
|
|
87
|
+
for (let i = 0; i < keys.length - 1; i++) {
|
|
88
|
+
const key = keys[i];
|
|
89
|
+
if (!(key in current) || typeof current[key] !== 'object' || current[key] === null) {
|
|
90
|
+
return result; // Path doesn't exist, nothing to unset
|
|
91
|
+
}
|
|
92
|
+
current[key] = { ...current[key] };
|
|
93
|
+
current = current[key];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
delete current[keys[keys.length - 1]];
|
|
97
|
+
return result;
|
|
16
98
|
};
|
|
17
99
|
|
|
18
100
|
/**
|
|
@@ -22,10 +104,18 @@ const apply_set_operator = (document, operations) => {
|
|
|
22
104
|
* @returns {Object} Updated document
|
|
23
105
|
*/
|
|
24
106
|
const apply_unset_operator = (document, operations) => {
|
|
25
|
-
|
|
107
|
+
let updated_document = { ...document };
|
|
108
|
+
|
|
26
109
|
for (const field of Object.keys(operations)) {
|
|
27
|
-
|
|
110
|
+
if (field.includes('.')) {
|
|
111
|
+
// Handle nested field unset with dot notation
|
|
112
|
+
updated_document = unset_nested_field(updated_document, field);
|
|
113
|
+
} else {
|
|
114
|
+
// Handle simple field unset
|
|
115
|
+
delete updated_document[field];
|
|
116
|
+
}
|
|
28
117
|
}
|
|
118
|
+
|
|
29
119
|
return updated_document;
|
|
30
120
|
};
|
|
31
121
|
|
|
@@ -36,10 +126,19 @@ const apply_unset_operator = (document, operations) => {
|
|
|
36
126
|
* @returns {Object} Updated document
|
|
37
127
|
*/
|
|
38
128
|
const apply_inc_operator = (document, operations) => {
|
|
39
|
-
|
|
129
|
+
let updated_document = { ...document };
|
|
130
|
+
|
|
40
131
|
for (const [field, value] of Object.entries(operations)) {
|
|
41
|
-
|
|
132
|
+
if (field.includes('.')) {
|
|
133
|
+
// Handle nested field increment with dot notation
|
|
134
|
+
const current_value = get_nested_field(updated_document, field) || 0;
|
|
135
|
+
updated_document = set_nested_field(updated_document, field, current_value + value);
|
|
136
|
+
} else {
|
|
137
|
+
// Handle simple field increment
|
|
138
|
+
updated_document[field] = (updated_document[field] || 0) + value;
|
|
139
|
+
}
|
|
42
140
|
}
|
|
141
|
+
|
|
43
142
|
return updated_document;
|
|
44
143
|
};
|
|
45
144
|
|