edgedb-js automatically converts EdgeDB types to the corresponding JavaScript types and vice versa.
The table below shows the correspondence between EdgeDB and JavaScript types.
EdgeDB Type |
JavaScript Type |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
| |
| |
|
|
| |
|
|
| |
|
n/a |
|
|
|
|
Inexact single-precision float
values may have a different
representation when decoded into a JavaScript number. This is inherent
to the implementation of limited-precision floating point types.
If you need the decimal representation to match, cast the expression
to float64
in your query.
Due to precision limitations the decimal
type cannot be decoded to a
JavaScript number. Use an explicit cast to float64
if the precision
degradation is acceptable or a cast to str
for an exact decimal
representation.
Set
represents the set of values returned by a query. If a query
contained an explicit ORDER BY
clause, the values will be ordered,
otherwise no specific ordering is guaranteed.
This type also allows to differentiate between a set of values and an explicit array.
EdgeDB array
maps onto the JavaScript Array
.
// Use the Node.js assert library to test results.
const assert = require("assert");
const edgedb = require("edgedb");
async function main() {
const conn = await edgedb.connect({
dsn: "edgedb://edgedb@localhost/"
});
try {
let data = await conn.queryOne("SELECT [1, 2, 3]");
// The result is an Array.
assert(data instanceof Array);
assert(typeof data[0] === "number");
assert(data.length === 3);
assert(data[2] === 3);
} finally {
conn.close();
}
}
main();
Object
represents an object instance returned from a query. The value of an
object property or a link can be accessed through a corresponding object key:
// Use the Node.js assert library to test results.
const assert = require("assert");
const edgedb = require("edgedb");
async function main() {
const conn = await edgedb.connect({
dsn: "edgedb://edgedb@localhost/"
});
try {
let data = await conn.queryOne(`
SELECT schema::Property {
name,
annotations: {name, @value}
}
FILTER .name = 'listen_port'
AND .source.name = 'cfg::Config'
LIMIT 1
`);
// The property 'name' is accessible.
assert(typeof data.name === "string");
// The link 'annotaions' is accessible and is a Set.
assert(typeof data.annotations === "object");
assert(data.annotations instanceof edgedb.Set);
// The Set of 'annotations' is array-like.
assert(data.annotations.length > 0);
assert(data.annotations[0].name === "cfg::system");
assert(data.annotations[0]["@value"] === "true");
} finally {
conn.close();
}
}
main();
A regular EdgeDB tuple
becomes an Array
in JavaScript.
// Use the Node.js assert library to test results.
const assert = require("assert");
const edgedb = require("edgedb");
async function main() {
const conn = await edgedb.connect({
dsn: "edgedb://edgedb@localhost/"
});
try {
let data = await conn.queryOne(`
SELECT (1, 'a', [3])
`);
// The resulting tuple is an Array.
assert(data instanceof Array);
assert(data.length === 3);
assert(typeof data[0] === "number");
assert(typeof data[1] === "string");
assert(data[2] instanceof Array);
} finally {
conn.close();
}
}
main();
A named EdgeDB tuple
becomes an Array
-like object
in JavaScript,
where the elements are accessible either by their names or indexes.
// Use the Node.js assert library to test results.
const assert = require("assert");
const edgedb = require("edgedb");
async function main() {
const conn = await edgedb.connect({
dsn: "edgedb://edgedb@localhost/"
});
try {
let data = await conn.queryOne(`
SELECT (a := 1, b := 'a', c := [3])
`);
// The resulting tuple is an Array.
assert(data instanceof Array);
assert(data.length === 3);
assert(typeof data[0] === "number");
assert(typeof data[1] === "string");
assert(data[2] instanceof Array);
// Elements can be accessed by their names.
assert(typeof data.a === "number");
assert(typeof data["b"] === "string");
assert(data.c instanceof Array);
} finally {
conn.close();
}
}
main();
A JavaScript representation of an EdgeDB local_date
value. Implements
a subset of the TC39 Temporal Proposal
PlainDate
type.
Assumes the calendar is always ISO 8601.
The year value of the local date.
The numerical month value of the local date.
Unlike the JS Date
object, months in LocalDate
start at 1.
ie. Jan = 1, Feb = 2, etc.
The day of the month value of the local date (starting with 1).
The weekday number of the local date. Returns a value between 1 and 7 inclusive, where 1 = Monday and 7 = Sunday.
The ordinal day of the year of the local date. Returns a value between 1 and 365 (or 366 in a leap year).
The ISO week number of the local date. Returns a value between 1 and 53, where ISO week 1 is defined as the week containing the first Thursday of the year.
The number of days in the week of the local date. Always returns 7.
The number of days in the month of the local date. Returns a value between 28 and 31 inclusive.
The number of days in the year of the local date. Returns either 365 or 366 if the year is a leap year.
The number of months in the year of the local date. Always returns 12.
Return whether the year of the local date is a leap year.
Get the string representation of the LocalDate
in the
YYYY-MM-DD
format.
Always throws an Error. LocalDate
objects are not comparable.
A JavaScript representation of an EdgeDB local_time
value. Implements
a subset of the TC39 Temporal Proposal
PlainTime
type.
The EdgeDB local_time
type only has microsecond precision, any
nanoseconds specified in the LocalTime
will be ignored when
encoding to an EdgeDB local_time
.
The hours component of the local time in 0-23 range.
The minutes component of the local time in 0-59 range.
The seconds component of the local time in 0-59 range.
The millisecond component of the local time in 0-999 range.
The microsecond component of the local time in 0-999 range.
The nanosecond component of the local time in 0-999 range.
Get the string representation of the local_time
in the HH:MM:SS
24-hour format.
Always throws an Error. LocalTime
objects are not comparable.
A JavaScript representation of an EdgeDB local_datetime
value.
Implements a subset of the TC39 Temporal Proposal
PlainDateTime
type.
Inherits all properties from the LocalDate()
and
LocalTime()
types.
Get the string representation of the local_datetime
in the
YYYY-MM-DDTHH:MM:SS
24-hour format.
Always throws an Error. LocalDateTime
objects are not comparable.
A JavaScript representation of an EdgeDB duration
value. Implements
a subset of the TC39 Temporal Proposal
Duration
type.
No arguments may be infinite and all must have the same sign. Any non-integer arguments will be rounded towards zero.
The Temporal Duration
type can contain both absolute duration
components, such as hours, minutes, seconds, etc. and relative
duration components, such as years, months, weeks, and days, where
their absolute duration changes depending on the exact date they are
relative to (eg. different months have a different number of days).
The EdgeDB duration
type only supports absolute durations, so any
Duration
with non-zero years, months, weeks, or days will throw
an error when trying to encode them.
The EdgeDB duration
type only has microsecond precision, any
nanoseconds specified in the Duration
will be ignored when
encoding to an EdgeDB duration
.
Temporal Duration
objects can be unbalanced, (ie. have a greater
value in any property than it would naturally have, eg. have a seconds
property greater than 59), but EdgeDB duration
objects are always
balanced.
Therefore in a round-trip of a Duration
object to EdgeDB and back,
the returned object, while being an equivalent duration, may not
have exactly the same property values as the sent object.
The number of years in the duration.
The number of months in the duration.
The number of weeks in the duration.
The number of days in the duration.
The number of hours in the duration.
The number of minutes in the duration.
The number of seconds in the duration.
The number of milliseconds in the duration.
The number of microseconds in the duration.
The number of nanoseconds in the duration.
Returns -1, 0, or 1 depending on whether the duration is negative, zero or positive.
Returns true
if the duration is zero.
Get the string representation of the duration in ISO 8601 duration format.
Always throws an Error. Duration
objects are not comparable.