@@ -9,20 +9,29 @@ Querying, updating, and deleting records for `@neabyte/jsonary`.
99- [ Nested Fields (Dot Paths)] ( #nested-fields-dot-paths )
1010- [ CRUD API] ( #crud-api )
1111- [ Query Builder API] ( #query-builder-api )
12+ - [ Chaining ` where() ` ] ( #chaining-where )
1213- [ Notes and Limits] ( #notes-and-limits )
1314
1415## Quick Start
1516
1617``` typescript
18+ // Import Jsonary
1719import Jsonary from ' @neabyte/jsonary'
1820
21+ // Create database instance
1922const db = new Jsonary ({ path: ' ./data.json' })
2023
24+ // Insert records
2125db .insert ({ name: ' John' , age: 30 , profile: { role: ' user' , active: false } })
2226db .insert ({ name: ' Jane' , age: 25 , profile: { role: ' admin' , active: true } })
2327
28+ // Query with dot-path nested field
2429const admins = db .where (' profile.role = admin' ).get ()
30+
31+ // Get first match
2532const firstAdult = db .where (' age >= 18' ).first ()
33+
34+ // Count matching records
2635const countActive = db .where (' profile.active = true' ).count ()
2736```
2837
@@ -43,8 +52,11 @@ Supported operators in string conditions:
4352Examples:
4453
4554``` typescript
55+ // Comparison operators
4656db .where (' age >= 18' ).get ()
4757db .where (' name != John' ).get ()
58+
59+ // String matching operators
4860db .where (' name contains "oh"' ).get ()
4961db .where (' name startsWith J' ).get ()
5062db .where (' name endsWith "e"' ).get ()
@@ -58,14 +70,18 @@ db.where('name endsWith "e"').get()
5870You can query nested properties using dot paths:
5971
6072``` typescript
73+ // Query nested object properties
6174db .where (' profile.role = admin' ).get ()
6275db .where (' profile.settings.theme = dark' ).get ()
6376```
6477
6578You can also update nested properties using dot paths:
6679
6780``` typescript
81+ // Update nested properties via dot-path
6882db .updateWhere (' name = John' , { ' profile.active' : true })
83+
84+ // Works with query builder too
6985db .where (' profile.role = admin' ).update ({ ' profile.verified' : true })
7086```
7187
@@ -84,6 +100,7 @@ const db = new Jsonary({ path: './data.json' })
84100Insert one record.
85101
86102``` typescript
103+ // Insert single record
87104db .insert ({ name: ' John' , age: 30 })
88105```
89106
@@ -92,6 +109,7 @@ db.insert({ name: 'John', age: 30 })
92109Insert multiple records.
93110
94111``` typescript
112+ // Insert multiple records at once
95113db .insertMany ([{ name: ' Jane' }, { name: ' Bob' }])
96114```
97115
@@ -100,6 +118,7 @@ db.insertMany([{ name: 'Jane' }, { name: 'Bob' }])
100118Return all records (copy).
101119
102120``` typescript
121+ // Get all records (returns a copy)
103122const all = db .get ()
104123```
105124
@@ -108,22 +127,32 @@ const all = db.get()
108127Update records matching condition. Returns number of updated records.
109128
110129``` typescript
130+ // Update records where age is greater than 30
111131const updated = db .updateWhere (' age > 30' , { status: ' senior' })
132+
133+ // Update records where age is greater than 30 using a function predicate
134+ const updatedFn = db .updateWhere (item => (item .age as number ) > 30 , { status: ' senior' })
112135```
113136
114137### ` db.deleteWhere(condition) `
115138
116139Delete records matching condition. Returns number of deleted records.
117140
118141``` typescript
142+ // Delete by string condition
119143const deleted = db .deleteWhere (' age < 18' )
144+
145+ // Delete by function predicate
146+ const deletedFn = db .deleteWhere (item => (item .age as number ) < 18 )
120147```
121148
122149### ` db.clear() `
123150
124151Clear all records (writes empty array to file).
125152
126153``` typescript
154+ // Remove all records
155+ // Note: Also persists empty array to file
127156db .clear ()
128157```
129158
@@ -132,6 +161,8 @@ db.clear()
132161Reload internal data from file.
133162
134163``` typescript
164+ // Reload data from file
165+ // Useful when file is modified externally
135166db .reload ()
136167```
137168
@@ -142,15 +173,36 @@ db.reload()
142173Create a query builder. ` condition ` can be a string condition or a predicate function.
143174
144175``` typescript
176+ // Query with string condition
145177const qb = db .where (' age >= 18' )
178+
179+ // Query with function predicate
146180const qbFn = db .where (item => (item .age as number ) >= 18 )
147181```
148182
183+ ### Chaining ` where() `
184+
185+ You can chain multiple ` where() ` calls to refine your query:
186+
187+ ``` typescript
188+ // Chain multiple where conditions (AND logic)
189+ db .where (' age >= 18' ).where (' profile.active = true' ).get ()
190+
191+ // Mix string conditions with function predicates
192+ db .where (' age >= 18' )
193+ .where (item => (item .name as string ).startsWith (' J' ))
194+ .first ()
195+
196+ // Chaining works with all query builder methods
197+ db .where (' age >= 18' ).where (' profile.role = admin' ).count ()
198+ ```
199+
149200### ` where(...).get() `
150201
151202Get all matching records (copy).
152203
153204``` typescript
205+ // Get all matching records
154206const adults = db .where (' age >= 18' ).get ()
155207```
156208
@@ -159,6 +211,7 @@ const adults = db.where('age >= 18').get()
159211Get the first matching record or ` null ` .
160212
161213``` typescript
214+ // Get first match or null if none found
162215const first = db .where (' profile.role = admin' ).first ()
163216```
164217
@@ -167,6 +220,7 @@ const first = db.where('profile.role = admin').first()
167220Count matching records.
168221
169222``` typescript
223+ // Count matching records
170224const count = db .where (' profile.active = true' ).count ()
171225```
172226
@@ -175,7 +229,10 @@ const count = db.where('profile.active = true').count()
175229Update all matching records (writes through to the database file when called from ` db.where(...) ` ).
176230
177231``` typescript
232+ // Update all matching records
178233db .where (' profile.role = admin' ).update ({ level: ' staff' })
234+
235+ // Update nested properties
179236db .where (' profile.role = admin' ).update ({ ' profile.verified' : true })
180237```
181238
@@ -184,6 +241,7 @@ db.where('profile.role = admin').update({ 'profile.verified': true })
184241Delete all matching records (writes through to the database file when called from ` db.where(...) ` ).
185242
186243``` typescript
244+ // Delete all matching records
187245const deleted = db .where (' age < 18' ).delete ()
188246```
189247
0 commit comments