Urgent.News

What's breaking now, across thousands of outlets.

Tech

LioranDB TypeScript Series #4: Type-Safe CRUD with Databases and Collections

LioranDB TypeScript Series #4: Type-Safe CRUD with Databases and Collections LioranDB TypeScript Series: Build with a developer-first document database powered by Rust and designed for TypeScript. We have a connection. Now let's store actual application data. Define your model type Product = { _id ?: string ; sku : string ; title : string ; price : number ; inStock : boolean ; category : string ;…

LioranDB TypeScript Series #4 focuses on type-safe CRUD operations with databases and collections. LioranDB is a developer-first document database built with Rust and designed for TypeScript.

First, define your model type, such as the Product type with properties like sku, title, price, inStock, and category.

Next, create a typed collection using the client's db and the desired collection name. For example:

const db = client.db('default');

const products = db.collection<Product>('products');

With this setup, your application gains TypeScript assistance when working with documents. Insert one document using insertOne:

const result = await products.insertOne({ sku: 'bk-001', title: 'Blue Notebook', price: 149, inStock: true, category: 'stationery' });

console.log(result.insertedId);

To insert multiple documents, use insertMany:

await products.insertMany([

{ sku: 'bk-002', title: 'Black Notebook', price: 199, inStock: true, category: 'stationery' },

{ sku: 'pn-001', title: 'Marker', price: 49, inStock: true, category: 'writing' },

]);

Find documents by querying the collection. Example includes finding all available products, sorting by price, and limiting results:

const available = await products.find({ inStock: true }).sort({ price: 1 }).limit(10).toArray();

Retrieve a single document by its sku:

const product = await products.findOne({ sku: 'bk-001' });

Count documents in the collection:

const total = await products.countDocuments();

const availableCount = await products.countDocuments({ inStock: true });

Update documents with updateOne or updateMany. For example, update the inStock status of a product:

await products.updateOne({ sku: 'bk-001' }, { $set: { inStock: false } });

Or update multiple products:

await products.updateMany({ category: 'stationery' }, { $set: { inStock: true } });

Delete documents using deleteOne or deleteMany:

await products.deleteOne({ sku: 'bk-001' });

Or delete all discontinued products:

await products.deleteMany({ category: 'discontinued' });

Filters can go beyond equality, such as finding products with a price greater than or equal to 100:

const productsOver100 = await products.find({ price: { $gte: 100 } }).toArray();

Text queries are available when a text index exists, allowing searches based on the title field:

const matches = await products.find({ $text: { $search: 'Notebook', $field: 'title' } }).toArray();

Idempotency keys are provided for write operations where retry safety is important.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Saturday 15 August →