LunaQL - hasMany, belongsTo and many more

Back

Over the past month I've been occasionally working on LunaQL. I made a few bug fixes and also implemented a few new features such as hasMany and belongsTo.

Lets take a look at the new features and bug fixes.

Relationships

hasMany

A hasMany relationship is used to define relationships where a single document is the parent to one or more child documents. For example, a blog post may have an infinite number of comments:

query({
from({
posts: {
hasMany: {
/**
* Get the comments for the blog post.
*/
comments: {
where: ["post_id", "===", "$._id"]
};
};
do: "fetch";
};
});
})

hasOne

A hasOne relationship is used to define relationships where a single document is associated to another document. For example, a user may be associated to a profile:

query({
from({
users: {
hasOne: {
/**
* Get the profile associated with the user.
*/
profile: {
where: ["_id", "===", "$.profile_id"]
};
};
do: "fetch";
};
});
})

belongsTo

A belongsTo relationship is used to define relationships where a single document is the child to one parent document. For example, a post may belong to a user:

query({
from({
posts: {
belongsTo: {
/**
* Get the user that owns the post.
*/
user: {
where: ["_id", "===", "$.user_id"]
};
};
do: "fetch";
};
});
})

Query Builder Actions

fetchFirst

LunaQL allows you to use the fetch action to get the results of your query. This normally returns the results as an array. With the addition of the new fetchFirst action, you can fetch the first matched document:

query({
from({
posts: {
belongsTo: {
/**
* Get the user that owns the post.
*/
user: {
where: ["_id", "===", "$.user_id"]
};
};
where: ["_id", "===", 1];
do: "fetchFirst";
};
});
})

Instead of MoanaDB returning a list of documents, a single document will be returned.

Bug fixes

© Donald Pakkies.
github