Relations
A link field points at records in another collection, like a reference holding an
ObjectId in MongoDB. In the app it shows the linked records as chips; in the API its value is
a record id, or an array of ids when the field allows several records.
import { DriveDB } from '@faeizfurqan/drivedb';
const db = new DriveDB(process.env.DRIVEDB_URL!).db();
// A "deals" collection whose `client` field links to a record in "clients".const deals = await db.createCollection('deals', { fields: [ { name: 'title', type: 'text' }, { name: 'amount', type: 'number' }, { name: 'client', type: 'link', options: { linkedCollection: 'clients', multiple: false } }, ],});
const mei = await db.collection('clients').findOne({ name: 'Mei Lin' });await deals.insertMany([ { title: 'Website redesign', amount: 1200, client: mei!._id }, { title: 'Logo', amount: 300, client: mei!._id },]);
// populate: replace the link with the linked documentconst [deal] = await deals.find({ title: 'Website redesign' }).populate('client').toArray();console.log(deal?.title, 'for', (deal?.client as { name: string }).name);
// $lookup: join in an aggregation pipelineconst perClient = await db .collection('clients') .aggregate([ { $lookup: { from: 'deals', localField: '_id', foreignField: 'client', as: 'deals' } }, { $project: { name: 1, dealCount: { $size: '$deals' }, total: { $sum: '$deals.amount' } } }, { $match: { dealCount: { $gt: 0 } } }, ]) .toArray();console.log(perClient);Create collections with typed fields
Section titled “Create collections with typed fields”createCollection takes field definitions, so you can set types the API can’t guess, such
as links, single selects or emails:
await db.createCollection('tasks', { displayName: 'Tasks', // name shown in the app fields: [ { name: 'title', type: 'text' }, { name: 'priority', type: 'singleSelect', options: { choices: [ { name: 'High', color: 'red' }, { name: 'Low', color: 'gray' }, ], }, }, { name: 'assignee', type: 'link', options: { linkedCollection: 'people', multiple: false } }, { name: 'watchers', type: 'link', options: { linkedCollection: 'people' } }, // several ],});You can also add link fields in the app: add a field, choose Link to records, and pick the table.
Writing links
Section titled “Writing links”- One record:
{ client: '<id>' }. Several:{ watchers: ['<id>', '<id>'] }. - Every id must exist in the linked collection, or the write fails with
ValidationError. - Add or remove one link with array operators:
{ $addToSet: { watchers: id } },{ $pull: { watchers: id } }.
Reading links
Section titled “Reading links”populatereplaces ids with the linked documents infindandfindOneresults:find(filter, { populate: ['client'] })or.populate('client'). Up to 10 fields.$lookupjoins in an aggregation pipeline. Use the collection’s API name infrom.- Reverse lookups: find what points at a record by filtering on the link field:
deals.find({ client: meiId }).
Deleting a record doesn’t delete the records that link to it. Their link fields keep the id;
populate returns null for it (or leaves it out of arrays).
Made by Faeiz Furqan