DB
Query and manipulate database without Odin
models interfering
JavaScript:
const { DB } = require("@foxify/odin");
TypeScript:
import { DB } from "@foxify/odin";
Table of Contents
- DB.connection(connection: string)
- DB.collection(collection: string)
- db.collection(collection: string)
-
[db.aggregate(…objects: object[] object[][])](#dbaggregateobjects-object–object) - db.join(collection: string, query?: DB.JoinQuery, as?: string)
- db.map(mapper: (item: object, index: number, items: object[]) => void)
-
[db.orderBy(field: string { [field: string]: “asc” “desc” }, order?: “asc” “desc”)](#dborderbyfield-string—field-string-%22asc%22–%22desc%22–order-%22asc%22–%22desc%22)
DB.connection(connection: string)
Specify the database connection.
const db = DB.connection("default");
DB.collection(collection: string)
Specify the database collection.
It sets
default
as the connection.
const db = DB.collection("users");
db.collection(collection: string)
Specify the database collection.
const db = db.collection("users");
db.aggregate(…objects: object[] | object[][])
Pushes the deep flattened array of the given objects to query aggregation.
const db = db.aggregate(
{
$match: {
foo: "bar",
},
},
{
$sort: {
foo: 1,
},
},
);
db.join(collection: string, query?: DB.JoinQuery, as?: string)
Joins the given collection
by the given query (which has a default value) as as
which is the collection
by default
const db = db.join("user_bills", q => q.where("user_id", "users.id"), "bills");
db.map(mapper: (item: object, index: number, items: object[]) => void)
Gives the given mapper
to mongodb
aggregation cursor map
method.
const db = db.map((item, index, items) => item);
db.orderBy(field: string | { [field: string]: “asc” | “desc” }, order?: “asc” | “desc”)
Sorts results by the given values.
const db = db.orderBy("foo");
const db = db.orderBy({
foo: "asc",
bar: "desc",
});