Mock Adapter
Mock Adapter is a simple in-memory adapter for URPC that provides temporary data storage during application runtime. It's perfect for testing, prototyping, and development scenarios where you don't need persistent data storage.
Features
- In-memory storage: Data is stored in memory and will be lost when the application restarts
- No setup required: Works out of the box without any configuration
- Development friendly: Perfect for testing and rapid prototyping
- Full CRUD support: Supports all basic repository operations
Installation
The Mock Adapter is included in the @unilab/urpc-adapters
package:
npm install @unilab/urpc-adapters
Basic Usage
Import and Setup
import { UserEntity } from "./entities/user";
import { repo, URPC } from "@unilab/urpc";
import { Plugin } from "@unilab/urpc-core";
import { Logging } from "@unilab/urpc-core/middleware";
import { MockAdapter } from "@unilab/urpc-adapters";
const MyPlugin: Plugin = {
entities: [UserEntity],
};
URPC.init({
plugins: [MyPlugin],
middlewares: [Logging()],
entityConfigs: {
user: {
defaultSource: "mock",
},
},
globalAdapters: [MockAdapter],
});
Creating Data
async function createUser() {
await repo<UserEntity>({
entity: "user",
}).create({
data: {
id: "1",
name: "John Doe",
email: "[email protected]",
avatar: "https://example.com/avatar.png",
},
});
}
Querying Data
async function getUsers() {
const users = await repo({
entity: "user",
}).findMany();
console.log("All users:", users);
}
Complete Example
Here's a complete working example:
import { UserEntity } from "./entities/user";
import { repo, URPC } from "@unilab/urpc";
import { Plugin } from "@unilab/urpc-core";
import { Logging } from "@unilab/urpc-core/middleware";
import { MockAdapter } from "@unilab/urpc-adapters";
const MyPlugin: Plugin = {
entities: [UserEntity],
};
URPC.init({
plugins: [MyPlugin],
middlewares: [Logging()],
entityConfigs: {
user: {
defaultSource: "mock",
},
},
globalAdapters: [MockAdapter],
});
async function demo() {
// Create first user
await repo<UserEntity>({
entity: "user",
}).create({
data: {
id: "1",
name: "John Doe",
email: "[email protected]",
avatar: "https://example.com/avatar.png",
},
});
// Create second user
await repo<UserEntity>({
entity: "user",
}).create({
data: {
id: "2",
name: "Jane Doe",
email: "[email protected]",
avatar: "https://example.com/avatar.png",
},
});
// Query all users
const users = await repo({
entity: "user",
}).findMany();
console.log("Users:", users);
}
demo();
Configuration Options
Entity Configuration
You can configure the Mock Adapter as the default source for specific entities:
URPC.init({
entityConfigs: {
user: {
defaultSource: "mock",
},
post: {
defaultSource: "mock",
},
},
globalAdapters: [MockAdapter],
});
Global Configuration
To use Mock Adapter globally for all entities, add it to the globalAdapters
array:
URPC.init({
globalAdapters: [MockAdapter],
});
Supported Operations
The Mock Adapter supports all standard repository operations:
create()
- Create new recordsfindMany()
- Query multiple recordsfindOne()
- Query a single recordupdate()
- Update existing recordsdelete()
- Delete recordsupsert()
- Update or create records
Best Practices
- Development Only: Use Mock Adapter only for development and testing
- Data Persistence: Remember that data will be lost on application restart
- Testing: Ideal for unit tests and integration tests
- Prototyping: Perfect for rapid prototyping and demos
Limitations
- No persistence: Data is lost when the application restarts
- Memory usage: All data is stored in memory, which may not be suitable for large datasets
- No concurrent access: Not suitable for production environments with multiple instances
Memory Adapter
Memory Adapter is a lightweight in-memory storage adapter for URPC that stores data directly in application memory. It's perfect for development, testing, and scenarios where you need temporary data storage without persistence.
Relations
Relations in URPC allow you to define connections between different entities using flexible callback functions. This approach enables you to query related data in a single request with complete control over the relation logic.