Querying, updating, and deleting records for @neabyte/jsonary.
// Import Jsonary
import Jsonary from '@neabyte/jsonary'
// Create database instance
const db = new Jsonary({ path: './data.json' })
// Insert records
db.insert({ name: 'John', age: 30, profile: { role: 'user', active: false } })
db.insert({ name: 'Jane', age: 25, profile: { role: 'admin', active: true } })
// Query with dot-path nested field
const admins = db.where('profile.role = admin').get()
// Get first match
const firstAdult = db.where('age >= 18').first()
// Count matching records
const countActive = db.where('profile.active = true').count()Supported operators in string conditions:
=- Equal!=- Not equal>- Greater than (numbers only)<- Less than (numbers only)>=- Greater than or equal (numbers only)<=- Less than or equal (numbers only)contains- String containsstartsWith- String starts withendsWith- String ends with
Examples:
// Comparison operators
db.where('age >= 18').get()
db.where('name != John').get()
// String matching operators
db.where('name contains "oh"').get()
db.where('name startsWith J').get()
db.where('name endsWith "e"').get()Note
String values can be written without quotes (name = John) or with quotes (name = "John" or name = 'John').
You can query nested properties using dot paths:
// Query nested object properties
db.where('profile.role = admin').get()
db.where('profile.settings.theme = dark').get()You can also update nested properties using dot paths:
// Update nested properties via dot-path
db.updateWhere('name = John', { 'profile.active': true })
// Works with query builder too
db.where('profile.role = admin').update({ 'profile.verified': true })Creates a database instance and loads data from the file (if it exists).
const db = new Jsonary({ path: './data.json' })Insert one record.
// Insert single record
db.insert({ name: 'John', age: 30 })Insert multiple records.
// Insert multiple records at once
db.insertMany([{ name: 'Jane' }, { name: 'Bob' }])Return all records (copy).
// Get all records (returns a copy)
const all = db.get()Update records matching condition. Returns number of updated records.
// Update records where age is greater than 30
const updated = db.updateWhere('age > 30', { status: 'senior' })
// Update records where age is greater than 30 using a function predicate
const updatedFn = db.updateWhere(item => (item.age as number) > 30, { status: 'senior' })Delete records matching condition. Returns number of deleted records.
// Delete by string condition
const deleted = db.deleteWhere('age < 18')
// Delete by function predicate
const deletedFn = db.deleteWhere(item => (item.age as number) < 18)Clear all records (writes empty array to file).
// Remove all records
// Note: Also persists empty array to file
db.clear()Reload internal data from file.
// Reload data from file
// Useful when file is modified externally
db.reload()Create a query builder. condition can be a string condition or a predicate function.
// Query with string condition
const qb = db.where('age >= 18')
// Query with function predicate
const qbFn = db.where(item => (item.age as number) >= 18)You can chain multiple where() calls to refine your query:
// Chain multiple where conditions (AND logic)
db.where('age >= 18').where('profile.active = true').get()
// Mix string conditions with function predicates
db.where('age >= 18')
.where(item => (item.name as string).startsWith('J'))
.first()
// Chaining works with all query builder methods
db.where('age >= 18').where('profile.role = admin').count()Get all matching records (copy).
// Get all matching records
const adults = db.where('age >= 18').get()Get the first matching record or null.
// Get first match or null if none found
const first = db.where('profile.role = admin').first()Count matching records.
// Count matching records
const count = db.where('profile.active = true').count()Update all matching records (writes through to the database file when called from db.where(...)).
// Update all matching records
db.where('profile.role = admin').update({ level: 'staff' })
// Update nested properties
db.where('profile.role = admin').update({ 'profile.verified': true })Delete all matching records (writes through to the database file when called from db.where(...)).
// Delete all matching records
const deleted = db.where('age < 18').delete()- Synchronous I/O: Reads and writes use synchronous filesystem calls, this is simple but can block on large files.
- No file locking: Concurrent writes (multiple processes) can race, avoid shared writers or add your own locking.
- Type safety: Records are stored as
Record<string, unknown>, you may want to validate shape at the edges.