Skip to content

Commit

Permalink
Support more datatypes in type generation (#5)
Browse files Browse the repository at this point in the history
* implement support for more complex types

* changelog

* fix tests
  • Loading branch information
zth authored Oct 28, 2023
1 parent 7e5c82d commit 0861172
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# master

- Don't pick up %edgeql in single line comments.
- Support more complex types in type generation.

# 0.2.0

- Move to use `rescript-embed-lang` utils for building the CLI.
Expand Down
18 changes: 17 additions & 1 deletion cli/EdgeDbGenerator.res
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ module Codec = {
@module("edgedb/dist/codecs/numbers.js")
external int32Codec: t = "Int32Codec"

@module("edgedb/dist/codecs/json.js")
external jsonCodec: t = "JSONCodec"

@module("edgedb/dist/codecs/numerics.js")
external bigintCodec: t = "BigIntCodec"

Expand Down Expand Up @@ -226,11 +229,24 @@ module AnalyzeQuery = {
} else if codec->is(int16Codec) || codec->is(int32Codec) {
"int"
} else if codec->is(bigintCodec) {
"bigint"
"BigInt.t"
} else if codec->is(jsonCodec) {
"JSON.t"
} else {
switch codec->tsType {
| "number" => "float"
| "boolean" => "bool"
| "Date" => "Date.t"
| ("LocalDateTime"
| "LocalDate"
| "RelativeDuration"
| "ConfigMemory"
| "Duration"
| "DateDuration"
| "LocalTime") as dataType =>
`EdgeDB.DataTypes.${dataType}.t`
| tsType if tsType->String.charAt(0)->String.toUpperCase === tsType->String.charAt(0) =>
`${tsType}.t`
| tsType => tsType
}
}
Expand Down
13 changes: 13 additions & 0 deletions dbTestProject/dbschema/default.esdl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ module default {
required property name -> str;
property age -> int32;
multi link pets -> Pet;
link typesDump -> TypesDump;
}

type TypesDump {
property date -> datetime;
property localDateTime -> cal::local_datetime;
property localDate -> cal::local_date;
property relativeDuration -> cal::relative_duration;
property duration -> duration;
property dateDuration -> cal::date_duration;
property localTime -> cal::local_time;
property json -> json;

}

type Movie {
Expand Down
17 changes: 17 additions & 0 deletions dbTestProject/dbschema/migrations/00003.edgeql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CREATE MIGRATION m1x7evrp4v4ervukqqmdu4rjwiakf45iw2q7nazyumw77ceb24g6wq
ONTO m1i6q7vybrrde2mfclrnfjrvvgsuxpkm4yptsr6suwqh2i6iii4zia
{
CREATE TYPE default::TypesDump {
CREATE PROPERTY date: std::datetime;
CREATE PROPERTY dateDuration: cal::date_duration;
CREATE PROPERTY duration: std::duration;
CREATE PROPERTY json: std::json;
CREATE PROPERTY localDate: cal::local_date;
CREATE PROPERTY localDateTime: cal::local_datetime;
CREATE PROPERTY localTime: cal::local_time;
CREATE PROPERTY relativeDuration: cal::relative_duration;
};
ALTER TYPE default::Person {
CREATE LINK typesDump: default::TypesDump;
};
};
30 changes: 29 additions & 1 deletion dbTestProject/src/Main.res
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,37 @@ let _ = movies->Array.forEach(movie => {
let singleMovie = await client->Movies.movieByTitle(~title="The Great Adventure")

let _ = switch singleMovie {
| Some({title, actors: [{id, numberOfPets}]}) =>
| Some({
title,
actors: [
{
id,
numberOfPets,
typesDump: Value({
date,
localDateTime,
localDate,
relativeDuration,
duration,
dateDuration,
localTime,
json,
}),
},
],
}) =>
let _id = id
let _title = title
let _numberOfPets = numberOfPets
Console.log((
date,
localDateTime,
localDate,
relativeDuration,
duration,
dateDuration,
localTime,
json,
))
| _ => ()
}
12 changes: 11 additions & 1 deletion dbTestProject/src/Movies.res
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,17 @@ let movieByTitle = (client, ~title) => {
actors: {
id,
name,
numberOfPets := count(.pets)
numberOfPets := count(.pets),
typesDump: {
date,
localDateTime,
localDate,
relativeDuration,
duration,
dateDuration,
localTime,
json
}
}
}
filter .title = <str>$title
Expand Down
26 changes: 24 additions & 2 deletions dbTestProject/src/__generated__/Movies__edgeql.res

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 2 additions & 15 deletions dbTestProject/test/__snapshots__/TestProject.test.mjs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ exports[`fetching data fetching single movie 1`] = `
{
"id": "<id>",
"name": "John Doe",
"numberOfPets": 2
"numberOfPets": 2,
"typesDump": null
}
]
}"
Expand All @@ -78,17 +79,3 @@ File "Movies":
- id
"
`;

exports[`fetching data fetching single movie 2`] = `
"{
"id": "<id>",
"title": "The Great Adventure",
"actors": [
{
"id": "<id>",
"name": "John Doe",
"numberOfPets": 2
}
]
}"
`;
142 changes: 142 additions & 0 deletions src/EdgeDB.res
Original file line number Diff line number Diff line change
Expand Up @@ -524,3 +524,145 @@ module TransactionHelpers = {
| exception Exn.Error(err) => Error(err->Error.fromExn)
}
}

module DataTypes = {
module LocalDate = {
type months =
| @as(1) January
| @as(2) February
| @as(3) March
| @as(4) April
| @as(5) May
| @as(6) June
| @as(7) July
| @as(8) August
| @as(9) September
| @as(10) October
| @as(11) November
| @as(12) December

type dayOfWeek =
| @as(1) Monday
| @as(2) Tuesday
| @as(3) Wednesday
| @as(4) Thursday
| @as(5) Friday
| @as(6) Saturday
| @as(7) Sunday

type t

@get external year: t => int = "year"
@get external months: t => months = "month"
@get external day: t => int = "day"
@get external dayOfWeek: t => dayOfWeek = "dayOfWeek"
@get external dayOfYear: t => int = "dayOfYear"
@get external daysInWeek: t => int = "daysInWeek"
@get external daysInMonth: t => int = "daysInMonth"
@get external daysInYear: t => int = "daysInYear"
@get external monthsInYear: t => int = "monthsInYear"
@get external inLeapYear: t => bool = "inLeapYear"
@send external toString: t => string = "toString"
@send external toJSON: t => string = "toJSON"
}

module LocalDateTime = {
type t

@get external year: t => int = "year"
@get external months: t => LocalDate.months = "month"
@get external day: t => int = "day"
@get external dayOfWeek: t => LocalDate.dayOfWeek = "dayOfWeek"
@get external dayOfYear: t => int = "dayOfYear"
@get external daysInWeek: t => int = "daysInWeek"
@get external daysInMonth: t => int = "daysInMonth"
@get external daysInYear: t => int = "daysInYear"
@get external monthsInYear: t => int = "monthsInYear"
@get external inLeapYear: t => bool = "inLeapYear"

@get external hour: t => int = "hour"
@get external minute: t => int = "minute"
@get external second: t => int = "second"
@get external millisecond: t => int = "millisecond"
@get external microsecond: t => int = "microsecond"
@get external nanosecond: t => int = "nanosecond"

@send external toString: t => string = "toString"
@send external toJSON: t => string = "toJSON"
}

module LocalTime = {
type t

@get external hour: t => int = "hour"
@get external minute: t => int = "minute"
@get external second: t => int = "second"
@get external millisecond: t => int = "millisecond"
@get external microsecond: t => int = "microsecond"
@get external nanosecond: t => int = "nanosecond"

@send external toString: t => string = "toString"
@send external toJSON: t => string = "toJSON"
}

module Duration = {
type t

@get external years: t => float = "years"
@get external months: t => float = "months"
@get external weeks: t => float = "weeks"
@get external days: t => float = "days"
@get external hours: t => float = "hours"
@get external minutes: t => float = "minutes"
@get external seconds: t => float = "seconds"
@get external milliseconds: t => float = "milliseconds"
@get external microseconds: t => float = "microseconds"
@get external sign: t => float = "sign"
@get external blank: t => bool = "blank"

@send external toString: t => string = "toString"
@send external toJSON: t => string = "toJSON"
}

module DateDuration = {
type t

@get external years: t => float = "years"
@get external months: t => float = "months"
@get external weeks: t => float = "weeks"
@get external days: t => float = "days"

@send external toString: t => string = "toString"
@send external toJSON: t => string = "toJSON"
}

module RelativeDuration = {
type t

@get external years: t => float = "years"
@get external months: t => float = "months"
@get external weeks: t => float = "weeks"
@get external days: t => float = "days"
@get external hours: t => float = "hours"
@get external minutes: t => float = "minutes"
@get external seconds: t => float = "seconds"
@get external milliseconds: t => float = "milliseconds"
@get external microseconds: t => float = "microseconds"

@send external toString: t => string = "toString"
@send external toJSON: t => string = "toJSON"
}

module ConfigMemory = {
type t

@get external bytes: t => float = "bytes"
@get external bytesBigInt: t => BigInt.t = "bytesBigInt"
@get external kibibytes: t => float = "kibibytes"
@get external mebibytes: t => float = "mebibytes"
@get external gibibytes: t => float = "gibibytes"
@get external tebibytes: t => float = "tebibytes"
@get external pebibytes: t => float = "pebibytes"
@send external toString: t => string = "toString"
}
}

0 comments on commit 0861172

Please sign in to comment.