@andrejs1979/document
Version:
MongoDB-compatible document database for NoSQL
833 lines • 29.5 kB
JavaScript
/**
* NoSQL - Document Module Examples
* Comprehensive examples demonstrating MongoDB-compatible operations
*/
import { EdgeDocumentDB } from '../index';
/**
* Basic CRUD Operations Example
*/
export async function basicCrudExample(env) {
// Initialize the document database
const db = await EdgeDocumentDB.create({
name: 'my_app_db',
d1Database: env.DB,
kvStore: env.KV,
r2Bucket: env.BUCKET,
options: {
enableAutoIndexing: true,
enableRelationships: true,
vectorConfig: {
enabled: true,
autoEmbedding: true
}
}
});
try {
// Insert a single document
const user = {
name: 'John Doe',
email: 'john@example.com',
age: 30,
tags: ['developer', 'javascript'],
profile: {
bio: 'Full-stack developer with 5 years of experience',
skills: ['React', 'Node.js', 'TypeScript']
}
};
const insertResult = await db.insertOne('users', user);
console.log('Inserted user:', insertResult.insertedId);
// Insert multiple documents
const posts = [
{
title: 'Getting Started with React',
content: 'React is a popular JavaScript library for building user interfaces...',
authorId: insertResult.insertedId,
tags: ['react', 'javascript', 'tutorial'],
publishedAt: new Date(),
likes: 42
},
{
title: 'Advanced TypeScript Patterns',
content: 'TypeScript provides powerful type system features that can help...',
authorId: insertResult.insertedId,
tags: ['typescript', 'javascript', 'advanced'],
publishedAt: new Date(),
likes: 28
}
];
const insertManyResult = await db.insertMany('posts', posts);
console.log('Inserted posts:', insertManyResult.insertedIds);
// Find documents
const allPosts = await db.find('posts', {});
console.log('All posts:', allPosts.length);
// Find with filters
const reactPosts = await db.find('posts', {
tags: { $in: ['react'] }
});
console.log('React posts:', reactPosts.length);
// Find with complex query
const popularRecentPosts = await db.find('posts', {
$and: [
{ likes: { $gte: 25 } },
{ publishedAt: { $gte: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000) } }
]
}, {
sort: { likes: -1 },
limit: 10
});
console.log('Popular recent posts:', popularRecentPosts.length);
// Update document
const updateResult = await db.updateOne('posts', { title: 'Getting Started with React' }, {
$inc: { likes: 1 },
$set: { lastUpdated: new Date() }
});
console.log('Updated posts:', updateResult.modifiedCount);
// Delete document
const deleteResult = await db.deleteOne('posts', {
title: 'Getting Started with React'
});
console.log('Deleted posts:', deleteResult.deletedCount);
}
finally {
await db.close();
}
}
/**
* Advanced Querying Example
*/
export async function advancedQueryingExample(env) {
const db = await EdgeDocumentDB.create({
name: 'ecommerce_db',
d1Database: env.DB,
kvStore: env.KV
});
try {
// Sample e-commerce data
const products = [
{
name: 'MacBook Pro',
category: 'Electronics',
price: 1999.99,
tags: ['laptop', 'apple', 'professional'],
specs: {
cpu: 'M2 Pro',
ram: '16GB',
storage: '512GB SSD'
},
inStock: true,
reviews: [
{ rating: 5, comment: 'Excellent performance!' },
{ rating: 4, comment: 'Great build quality' }
]
},
{
name: 'Dell XPS 13',
category: 'Electronics',
price: 1299.99,
tags: ['laptop', 'dell', 'ultrabook'],
specs: {
cpu: 'Intel i7',
ram: '16GB',
storage: '256GB SSD'
},
inStock: false,
reviews: [
{ rating: 4, comment: 'Good value for money' },
{ rating: 5, comment: 'Love the display' }
]
}
];
await db.insertMany('products', products);
// Complex aggregation pipeline
const priceStatsByCategory = await db.aggregate('products', [
{
$match: { inStock: true }
},
{
$group: {
_id: '$category',
averagePrice: { $avg: '$price' },
minPrice: { $min: '$price' },
maxPrice: { $max: '$price' },
productCount: { $sum: 1 }
}
},
{
$sort: { averagePrice: -1 }
}
]);
console.log('Price stats by category:', priceStatsByCategory);
// Text search
const searchResults = await db.textSearch('products', 'macbook pro', {
filters: { inStock: true },
limit: 5
});
console.log('Search results:', searchResults.length);
// Find by tags with hierarchy
const taggedProducts = await db.findByTags('products', ['laptop'], {
operator: 'and',
includeHierarchy: true
});
console.log('Tagged products:', taggedProducts.length);
// Query explanation
const explanation = await db.explain('products', {
category: 'Electronics',
price: { $gte: 1000 }
});
console.log('Query explanation:', explanation);
}
finally {
await db.close();
}
}
/**
* Hybrid Search Example
*/
export async function hybridSearchExample(env) {
const db = await EdgeDocumentDB.create({
name: 'content_db',
d1Database: env.DB,
kvStore: env.KV,
options: {
vectorConfig: {
enabled: true,
autoEmbedding: true,
embeddingFields: ['content', 'title']
}
}
});
try {
// Sample content with text and potential vector embeddings
const articles = [
{
title: 'Introduction to Machine Learning',
content: 'Machine learning is a subset of artificial intelligence that enables computers to learn and improve from experience without being explicitly programmed...',
category: 'Technology',
tags: ['AI', 'ML', 'technology'],
publishedAt: new Date()
},
{
title: 'Deep Learning with Neural Networks',
content: 'Deep learning uses neural networks with multiple layers to analyze and learn from data. It has revolutionized fields like computer vision and natural language processing...',
category: 'Technology',
tags: ['AI', 'deep-learning', 'neural-networks'],
publishedAt: new Date()
},
{
title: 'Cooking the Perfect Pasta',
content: 'Cooking pasta might seem simple, but there are many techniques to achieve the perfect texture and flavor...',
category: 'Food',
tags: ['cooking', 'pasta', 'italian'],
publishedAt: new Date()
}
];
await db.insertMany('articles', articles);
// Hybrid search combining text and vector similarity
const hybridQuery = {
text: 'artificial intelligence neural networks',
filter: { category: 'Technology' },
weights: {
text: 0.6,
vector: 0.4,
metadata: 0.0
},
options: {
limit: 10,
threshold: 0.1
}
};
const hybridResults = await db.hybridSearch('articles', hybridQuery);
console.log('Hybrid search results:', hybridResults.documents.length);
console.log('Search metadata:', hybridResults.metadata);
// Semantic search using text embedding
const semanticResults = await db.semanticSearch('articles', 'machine learning algorithms', {
filters: { category: 'Technology' },
limit: 5,
textWeight: 0.3,
vectorWeight: 0.7
});
console.log('Semantic search results:', semanticResults.length);
// Find similar documents
if (hybridResults.documents.length > 0) {
const similarArticles = await db.findSimilarDocuments('articles', hybridResults.documents[0]._id, {
useText: true,
useVector: true,
useMetadata: true,
limit: 3
});
console.log('Similar articles:', similarArticles.length);
}
// Get personalized recommendations
const recommendations = await db.getRecommendations('articles', {
viewedDocuments: hybridResults.documents.map(doc => doc._id),
searchQueries: ['machine learning', 'AI'],
likedDocuments: []
}, {
limit: 5,
diversityFactor: 0.7
});
console.log('Recommendations:', recommendations.length);
}
finally {
await db.close();
}
}
/**
* Relationship Management Example
*/
export async function relationshipExample(env) {
const db = await EdgeDocumentDB.create({
name: 'blog_db',
d1Database: env.DB,
kvStore: env.KV,
options: {
enableRelationships: true
}
});
try {
// Define relationships
await db.defineRelationship('posts', 'users', {
type: 'manyToOne',
localField: 'authorId',
foreignField: '_id',
foreignCollection: 'users'
});
await db.defineRelationship('posts', 'comments', {
type: 'oneToMany',
localField: '_id',
foreignField: 'postId',
foreignCollection: 'comments'
});
// Insert related data
const user = await db.insertOne('users', {
name: 'Jane Smith',
email: 'jane@example.com',
bio: 'Tech blogger and developer'
});
const post = await db.insertOne('posts', {
title: 'Building Scalable APIs',
content: 'When building APIs for production use...',
authorId: user.insertedId,
publishedAt: new Date()
});
const comments = await db.insertMany('comments', [
{
postId: post.insertedId,
author: 'Reader1',
content: 'Great article! Very helpful.',
createdAt: new Date()
},
{
postId: post.insertedId,
author: 'Reader2',
content: 'I implemented this pattern and it works well.',
createdAt: new Date()
}
]);
// Find with population
const populatedPosts = await db.findWithPopulate('posts', {}, [
{
path: 'authorId',
select: 'name email bio'
},
{
path: 'comments',
match: { createdAt: { $gte: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000) } }
}
]);
console.log('Populated posts:', populatedPosts.length);
if (populatedPosts.length > 0) {
console.log('Author:', populatedPosts[0].populated.authorId);
console.log('Comments:', populatedPosts[0].populated.comments);
}
}
finally {
await db.close();
}
}
/**
* Bulk Operations Example
*/
export async function bulkOperationsExample(env) {
const db = await EdgeDocumentDB.create({
name: 'analytics_db',
d1Database: env.DB,
kvStore: env.KV
});
try {
// Bulk write operations
const bulkOps = [
{
insertOne: {
document: {
event: 'page_view',
userId: 'user1',
page: '/home',
timestamp: new Date()
}
}
},
{
updateOne: {
filter: { userId: 'user1' },
update: { $inc: { totalViews: 1 } },
upsert: true
}
},
{
deleteOne: {
filter: { event: 'old_event' }
}
}
];
const bulkResult = await db.bulkWrite('analytics', bulkOps);
console.log('Bulk operation result:', {
inserted: bulkResult.insertedCount,
modified: bulkResult.modifiedCount,
deleted: bulkResult.deletedCount
});
// Streaming insert for large datasets
async function* generateAnalyticsData() {
for (let i = 0; i < 10000; i++) {
yield {
event: 'user_action',
userId: `user${Math.floor(Math.random() * 1000)}`,
action: ['click', 'scroll', 'hover'][Math.floor(Math.random() * 3)],
timestamp: new Date(),
metadata: {
sessionId: `session_${Math.random()}`,
userAgent: 'Mozilla/5.0...'
}
};
}
}
const streamResult = await db.streamInsert('analytics', generateAnalyticsData(), {
batchSize: 500,
ordered: false,
onProgress: (inserted, total) => {
if (inserted % 1000 === 0) {
console.log(`Streamed ${inserted} documents`);
}
}
});
console.log('Stream insert result:', {
inserted: streamResult.insertedCount,
failed: streamResult.failedCount,
errors: streamResult.errors.length
});
// Create a real-time document stream
const stream = db.createDocumentStream('analytics', {
batchSize: 100,
flushInterval: 5000, // 5 seconds
compression: true,
errorHandler: (error, batch) => {
console.error('Stream error:', error.message);
console.log('Failed batch size:', batch.length);
}
});
// Write to stream
for (let i = 0; i < 50; i++) {
await stream.write({
event: 'real_time_event',
data: { value: Math.random() },
timestamp: new Date()
});
}
// Stop the stream
await stream.stop();
}
finally {
await db.close();
}
}
/**
* Indexing and Performance Example
*/
export async function indexingExample(env) {
const db = await EdgeDocumentDB.create({
name: 'performance_db',
d1Database: env.DB,
kvStore: env.KV,
options: {
enableAutoIndexing: true,
autoIndexThreshold: 100
}
});
try {
// Create custom indexes
await db.createIndex('products', {
key: { category: 1, price: -1 },
options: { name: 'category_price_idx' }
});
await db.createIndex('products', {
key: { name: 'text', description: 'text' },
options: {
name: 'text_search_idx',
weights: { name: 10, description: 5 }
}
});
// Vector index for similarity search
await db.createIndex('products', {
key: { _vector: 'vector' },
options: {
name: 'vector_similarity_idx',
vectorOptions: {
dimensions: 1536,
similarity: 'cosine',
type: 'hnsw'
}
}
});
// Insert test data to trigger auto-indexing
const testProducts = Array.from({ length: 500 }, (_, i) => ({
name: `Product ${i}`,
category: ['Electronics', 'Clothing', 'Books'][i % 3],
price: Math.random() * 1000,
description: `This is product ${i} with various features...`,
tags: [`tag${i % 10}`, `category${i % 5}`]
}));
await db.insertMany('products', testProducts);
// Perform queries to generate patterns
for (let i = 0; i < 150; i++) {
await db.find('products', {
category: 'Electronics',
price: { $gte: 100 }
});
}
// Get index recommendations
const recommendations = await db.getIndexRecommendations('products');
console.log('Index recommendations:', recommendations);
// Auto-create recommended indexes
await db.autoCreateIndexes('products');
// List all indexes
const indexes = await db.listIndexes('products');
console.log('Current indexes:', indexes.map(idx => ({
name: idx.name,
key: idx.key,
size: idx.size
})));
// Get index statistics
const indexStats = await db.indexManager.getIndexStats('performance_db', 'products');
console.log('Index statistics:', indexStats);
}
finally {
await db.close();
}
}
/**
* Tagging and Metadata Example
*/
export async function taggingExample(env) {
const db = await EdgeDocumentDB.create({
name: 'content_management_db',
d1Database: env.DB,
kvStore: env.KV
});
try {
// Sample documents
const documents = [
{
title: 'React Best Practices',
content: 'React is a powerful JavaScript library for building user interfaces...',
type: 'article',
category: 'development'
},
{
title: 'Introduction to TypeScript',
content: 'TypeScript adds static typing to JavaScript...',
type: 'tutorial',
category: 'development'
}
];
const insertResult = await db.insertMany('content', documents);
// Auto-tag documents
for (const [index, docId] of insertResult.insertedIds.entries()) {
const autoTags = await db.autoTag('content', documents[index], {
tagSources: ['content', 'metadata', 'filename'],
customTagger: (doc) => {
// Custom tagging logic
const tags = [];
if (doc.content?.includes('JavaScript'))
tags.push('javascript');
if (doc.content?.includes('React'))
tags.push('react');
if (doc.content?.includes('TypeScript'))
tags.push('typescript');
return tags;
}
});
await db.tagDocument('content', docId, autoTags);
console.log(`Auto-tagged document ${docId}:`, autoTags);
}
// Get tag suggestions
const newDocument = {
title: 'Advanced React Patterns',
content: 'This article covers advanced React patterns like render props and higher-order components...',
type: 'article',
category: 'development'
};
const suggestions = await db.getTagSuggestions('content', newDocument, {
includePopular: true,
includeSimilar: true,
limit: 10
});
console.log('Tag suggestions:', suggestions);
// Find documents by tags
const reactDocs = await db.findByTags('content', ['react'], {
operator: 'or',
includeHierarchy: true
});
console.log('React documents:', reactDocs.length);
// Get tag statistics
const tagStats = await db.getTagStats('content', {
sortBy: 'count',
limit: 20
});
console.log('Tag statistics:', tagStats);
// Define tag hierarchy
await db.taggingSystem.defineTagHierarchy('programming', [
'javascript', 'typescript', 'react', 'node'
]);
// Bulk tag documents
const bulkTagResult = await db.taggingSystem.bulkTag('content', {
category: 'development'
}, ['programming', 'web-development'], {
merge: true
});
console.log('Bulk tag result:', bulkTagResult);
}
finally {
await db.close();
}
}
/**
* Real-world Application Example
*/
export async function realWorldExample(env) {
const db = await EdgeDocumentDB.create({
name: 'social_media_db',
d1Database: env.DB,
kvStore: env.KV,
r2Bucket: env.BUCKET,
options: {
enableAutoIndexing: true,
enableRelationships: true,
vectorConfig: {
enabled: true,
autoEmbedding: true,
embeddingFields: ['content', 'bio']
}
}
});
try {
// Define relationships
await db.defineRelationship('posts', 'users', {
type: 'manyToOne',
localField: 'authorId',
foreignField: '_id',
foreignCollection: 'users'
});
await db.defineRelationship('posts', 'likes', {
type: 'oneToMany',
localField: '_id',
foreignField: 'postId',
foreignCollection: 'likes'
});
// Create users
const users = await db.insertMany('users', [
{
username: 'john_dev',
email: 'john@example.com',
bio: 'Full-stack developer passionate about React and Node.js',
followers: 150,
following: 75,
tags: ['developer', 'react', 'nodejs']
},
{
username: 'sarah_designer',
email: 'sarah@example.com',
bio: 'UI/UX designer with expertise in modern web design',
followers: 200,
following: 120,
tags: ['designer', 'ui', 'ux']
}
]);
// Create posts with auto-tagging
const posts = [
{
content: 'Just finished building a React dashboard with TypeScript. The type safety really helps with large projects! #react #typescript #webdev',
authorId: users.insertedIds[0],
createdAt: new Date(),
likes: 25,
reposts: 5
},
{
content: 'Working on a new design system using Figma. Consistency is key in good UI design. #design #figma #ui',
authorId: users.insertedIds[1],
createdAt: new Date(),
likes: 30,
reposts: 8
}
];
const insertedPosts = await db.insertMany('posts', posts);
// Create engagement data
const likes = [
{ postId: insertedPosts.insertedIds[0], userId: users.insertedIds[1], createdAt: new Date() },
{ postId: insertedPosts.insertedIds[1], userId: users.insertedIds[0], createdAt: new Date() }
];
await db.insertMany('likes', likes);
// Complex analytics query
const popularPosts = await db.aggregate('posts', [
{
$match: {
createdAt: { $gte: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000) }
}
},
{
$lookup: {
from: 'likes',
localField: '_id',
foreignField: 'postId',
as: 'engagements'
}
},
{
$addFields: {
engagementScore: {
$add: ['$likes', { $multiply: ['$reposts', 2] }]
}
}
},
{
$sort: { engagementScore: -1 }
},
{
$limit: 10
}
]);
console.log('Popular posts:', popularPosts.length);
// Content discovery using hybrid search
const contentDiscovery = await db.hybridSearch('posts', {
text: 'react typescript development',
weights: {
text: 0.7,
vector: 0.3,
metadata: 0.0
},
options: {
limit: 5
}
});
console.log('Content discovery results:', contentDiscovery.documents.length);
// User recommendations based on interests
const userRecommendations = await db.getRecommendations('posts', {
viewedDocuments: contentDiscovery.documents.map(doc => doc._id),
searchQueries: ['react', 'typescript', 'web development'],
likedDocuments: [insertedPosts.insertedIds[0]]
}, {
limit: 10,
diversityFactor: 0.6,
filters: {
createdAt: { $gte: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) }
}
});
console.log('User recommendations:', userRecommendations.length);
// Real-time activity stream
const activityStream = db.createDocumentStream('activities', {
batchSize: 50,
flushInterval: 2000,
transform: (activity) => ({
...activity,
processedAt: new Date(),
source: 'web_app'
})
});
// Simulate real-time activities
const activities = [
{ type: 'like', userId: users.insertedIds[0], postId: insertedPosts.insertedIds[1] },
{ type: 'follow', userId: users.insertedIds[1], targetUserId: users.insertedIds[0] },
{ type: 'post_view', userId: users.insertedIds[0], postId: insertedPosts.insertedIds[1] }
];
for (const activity of activities) {
await activityStream.write({
...activity,
timestamp: new Date()
});
}
await activityStream.stop();
// Performance monitoring
const dbStats = await db.stats();
console.log('Database statistics:', dbStats);
// Tag analysis
const tagStats = await db.getTagStats('posts', {
sortBy: 'count',
limit: 15
});
console.log('Trending tags:', tagStats);
}
finally {
await db.close();
}
}
// Export all examples
export const examples = {
basicCrudExample,
advancedQueryingExample,
hybridSearchExample,
relationshipExample,
bulkOperationsExample,
indexingExample,
taggingExample,
realWorldExample
};
// Usage in a Cloudflare Worker
export default {
async fetch(request, env) {
try {
const url = new URL(request.url);
const example = url.searchParams.get('example');
switch (example) {
case 'basic':
await basicCrudExample(env);
break;
case 'advanced':
await advancedQueryingExample(env);
break;
case 'hybrid':
await hybridSearchExample(env);
break;
case 'relationships':
await relationshipExample(env);
break;
case 'bulk':
await bulkOperationsExample(env);
break;
case 'indexing':
await indexingExample(env);
break;
case 'tagging':
await taggingExample(env);
break;
case 'real-world':
await realWorldExample(env);
break;
default:
return new Response('Available examples: basic, advanced, hybrid, relationships, bulk, indexing, tagging, real-world', {
status: 400
});
}
return new Response('Example completed successfully!', {
status: 200,
headers: { 'Content-Type': 'text/plain' }
});
}
catch (error) {
console.error('Example error:', error);
return new Response(`Error: ${error.message}`, {
status: 500,
headers: { 'Content-Type': 'text/plain' }
});
}
}
};
//# sourceMappingURL=basic-usage.js.map