请问下:
Dexie(本质:IndexedDB)是否是没有关联查询?
我问AI,得到的结果是:
import Dexie from 'dexie';
const db = new Dexie('MyDatabase');
db.version(1).stores({
users: '++id,name,email',
orders: '++id,userId,product,quantity'
});
查询:
async function getUserWithOrders(userId) {
const user = await db.users.get(userId); // 获取用户信息
const orders = await db.orders.where('userId').equals(userId).toArray(); // 获取该用户的订单
return { user, orders }; // 返回用户和订单的组合
}
或者:
async function getUsersWithOrders() {
const users = await db.users.toArray(); // 获取所有用户
const usersWithOrders = await Promise.all(users.map(async user => {
const orders = await db.orders.where('userId').equals(user.id).toArray(); // 获取每个用户的订单
return { ...user, orders }; // 返回用户和订单的组合
}));
return usersWithOrders; // 返回所有用户及其订单的数组
}
===
如果是这样,那么它是没有类似MySQL的表之间的关联查询,或者MongoDB的$lookup的关联查询的是吗?
IndexedDB 是非关系型的,没有 JOIN 一说。
而 MongoDB 所谓的
$lookup
也只是一种 Aggregation 罢了,只是说 MongoDB 跟客户端通常是网络通信,如果是客户端发起查询多次会有些不必要的性能损耗,所以它内置了这么个语法,客户端只发起一次就行了。但 IndexedDB 反正是本机的,无所谓这点儿 I/O 开销,你自己代码里包一层 Aggregation 操作就行了。