Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isoDateTimeMillis and isoTimeMillis #7

Merged
merged 3 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion docs/Data-Types.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The column data type is automatically decided by RedBeanNode.

You can use **R.isoDate()** and **R.isoDateTime()** to generate the current date(time) or parse your date.
You can use **R.isoDate()**, **R.isoDateTime()**, or **R.isoDateTimeMillis()** to generate the current date(time) or parse your date.


## DateTime
Expand All @@ -21,6 +21,21 @@ book.created_at = R.isoDateTime(new Date())
book.created_at = R.isoDateTime(dayjs())
```

## DateTimeMillis

```javascript
// Current Date Time
book.created_at = R.isoDateTimeMillis()

// Datetime string format with YYYY-MM-DD HH:mm:ss.SSS
book.created_at = "2020-10-20 17:43:00.000"

// JS Date object
book.created_at = R.isoDateTimeMillis(new Date())

// Dayjs
book.created_at = R.isoDateTimeMillis(dayjs())

## Date

```javascript
Expand Down Expand Up @@ -53,6 +68,21 @@ book.time = R.isoTime(new Date())
book.time = R.isoTime(dayjs())
```

## TimeMillis

```javascript
// Current Time
book.time = R.isoTimeMillis()

// Time string format with HH:mm:ss.SSS
book.time = "17:50:00.000"

// JS Date object
book.time = R.isoTimeMillis(new Date())

// Dayjs
book.time = R.isoTimeMillis(dayjs())
```

## varchar

Expand Down
70 changes: 63 additions & 7 deletions lib/redbean-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ export class RedBeanNode {
this.debugLog("Create field (Datetime): " + fieldName);
col = table.dateTime(fieldName);

} else if (valueType == "datetimemillis") {
this.debugLog("Create field (Datetimemillis): " + fieldName);
col = table.dateTime(fieldName);

} else if (valueType == "date") {
this.debugLog("Create field (Date): " + fieldName);
col = table.date(fieldName);
Expand All @@ -333,6 +337,10 @@ export class RedBeanNode {
this.debugLog("Create field (Time): " + fieldName);
col = table.time(fieldName);

} else if (valueType == "timemillis") {
this.debugLog("Create field (Timemillis): " + fieldName);
col = table.time(fieldName);

} else {
this.debugLog("Create field (String): " + fieldName);
col = table.string(fieldName);
Expand Down Expand Up @@ -393,10 +401,14 @@ export class RedBeanNode {

if (this.isDateTime(value)) {
return "datetime";
} else if (this.isDateTimeMillis(value)) {
return "datetimemillis";
} else if (this.isDate(value)) {
return "date";
} else if (this.isTime(value)) {
return "time";
} else if (this.isTimeMillis(value)) {
return "timemillis";
}

return "varchar";
Expand All @@ -414,7 +426,8 @@ export class RedBeanNode {
if (
valueType == "integer" || valueType == "float" || valueType == "varchar" ||
valueType == "text" || valueType == "bigInteger" ||
valueType == "datetime" || valueType == "date" || valueType =="time"
valueType == "datetime" || valueType == "datetimemillis" || valueType == "date" ||
valueType =="time" || valueType == "timemillis"
) {
return false;
}
Expand All @@ -424,7 +437,8 @@ export class RedBeanNode {
if (columnType == "integer" || columnType == "int") {
if (
valueType == "float" || valueType == "varchar" || valueType == "text" || valueType == "bigInteger" ||
valueType == "datetime" || valueType == "date" || valueType =="time"
valueType == "datetime" || valueType == "datetimemillis" || valueType == "date" ||
valueType =="time" || valueType == "timemillis"
) {
return false;
}
Expand All @@ -434,7 +448,8 @@ export class RedBeanNode {
if (columnType == "bigInteger" || columnType == "bigint") {
if (
valueType == "float" || valueType == "varchar" || valueType == "text" ||
valueType == "datetime" || valueType == "date" || valueType =="time"
valueType == "datetime" || valueType == "datetimemillis" || valueType == "date" ||
valueType =="time" || valueType == "timemillis"
) {
return false;
}
Expand All @@ -443,8 +458,9 @@ export class RedBeanNode {
// Float
if (columnType == "float") {
if (
valueType == "varchar" || valueType == "text" ||
valueType == "datetime" || valueType == "date" || valueType =="time"
valueType == "varchar" || valueType == "text" || valueType == "datetime" ||
valueType == "datetimemillis" || valueType == "date" || valueType =="time" ||
valueType == "timemillis"
) {
return false;
}
Expand All @@ -454,7 +470,7 @@ export class RedBeanNode {
if (columnType == "time") {
if (
valueType == "varchar" || valueType == "text" ||
valueType == "datetime" || valueType == "date"
valueType == "datetime" || valueType == "datetimemillis" || valueType == "date"
) {
return false;
}
Expand All @@ -464,7 +480,7 @@ export class RedBeanNode {
if (columnType == "date") {
if (
valueType == "varchar" || valueType == "text" ||
valueType == "datetime"
valueType == "datetime" || valueType == "datetimemillis"
) {
return false;
}
Expand Down Expand Up @@ -978,6 +994,18 @@ export class RedBeanNode {
return dayjsObject.format('YYYY-MM-DD HH:mm:ss');
}

isoDateTimeMillis(dateTime : dayjs.Dayjs | Date | undefined = undefined) : string {
let dayjsObject;

if (dateTime instanceof dayjs) {
dayjsObject = dateTime;
} else {
dayjsObject = dayjs(dateTime);
}

return dayjsObject.format('YYYY-MM-DD HH:mm:ss.SSS');
}

isoDate(date : dayjs.Dayjs | Date | undefined = undefined) : string {
let dayjsObject;

Expand Down Expand Up @@ -1006,6 +1034,22 @@ export class RedBeanNode {
return dayjsObject.format('HH:mm:ss');
}

/**
* HH:mm:ss.SSS
* @param date
*/
isoTimeMillis(date : dayjs.Dayjs | Date | undefined = undefined) {
let dayjsObject;

if (date instanceof dayjs) {
dayjsObject = date;
} else {
dayjsObject = dayjs(date);
}

return dayjsObject.format('HH:mm:ss.SSS');
}

isDate(value : string) {
let format = "YYYY-MM-DD";
return dayjs(value, format).format(format) === value;
Expand All @@ -1016,13 +1060,25 @@ export class RedBeanNode {
return dayjs(value, format).format(format) === value;
}

isDateTimeMillis(value : string) {
let format = "YYYY-MM-DD HH:mm:ss.SSS";
return dayjs(value, format).format(format) === value;
}

isTime(value : string) {
// Since dayjs is not supporting time only format, so prefix a fake date to parse
value = "2020-10-20 " + value;
let format = "YYYY-MM-DD HH:mm:ss";
return dayjs(value, format).format(format) === value;
}

isTimeMillis(value : string) {
// Since dayjs is not supporting time only format, so prefix a fake date to parse
value = "2020-10-20 " + value;
let format = "YYYY-MM-DD HH:mm:ss.SSS";
return dayjs(value, format).format(format) === value;
}

autoloadModels(dir: string) {
let tsFileList, jsFileList;

Expand Down
21 changes: 19 additions & 2 deletions test/common/test-redbean-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,17 +332,21 @@ module.exports = () => {

describe('#isoDateTime', () => {
it('get the correct datetime with new Date()', () => {
let d = new Date(2018, 11, 24, 10, 33, 30, 0);
let d = new Date(2018, 11, 24, 10, 33, 30, 765);
expect(R.isoDateTime(d)).to.equal("2018-12-24 10:33:30");
expect(R.isoDateTimeMillis(d)).to.equal("2018-12-24 10:33:30.765");
expect(R.isoDate(d)).to.equal("2018-12-24");
expect(R.isoTime(d)).to.equal("10:33:30");
expect(R.isoTimeMillis(d)).to.equal("10:33:30.765");
});

it('get the correct datetime with new Dayjs', () => {
let d = dayjs(new Date(2018, 11, 24, 10, 33, 30, 0));
let d = dayjs(new Date(2018, 11, 24, 10, 33, 30, 765));
expect(R.isoDateTime(d)).to.equal("2018-12-24 10:33:30");
expect(R.isoDateTimeMillis(d)).to.equal("2018-12-24 10:33:30.765");
expect(R.isoDate(d)).to.equal("2018-12-24");
expect(R.isoTime(d)).to.equal("10:33:30");
expect(R.isoTimeMillis(d)).to.equal("10:33:30.765");
});
});

Expand Down Expand Up @@ -384,8 +388,10 @@ module.exports = () => {
expect(R.getDataType("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901")).to.equal("text");

expect(R.getDataType(R.isoDateTime())).to.equal("datetime");
expect(R.getDataType(R.isoDateTimeMillis())).to.equal("datetimemillis");
expect(R.getDataType(R.isoDate())).to.equal("date");
expect(R.getDataType(R.isoTime())).to.equal("time");
expect(R.getDataType(R.isoTimeMillis())).to.equal("timemillis");


expect(R.getDataType(0, "shop_id")).to.equal("integer");
Expand All @@ -400,6 +406,7 @@ module.exports = () => {
expect(R.isValidType("bit", "integer")).to.be.false;

expect(R.isValidType("datetime2", "datetime")).to.be.true;
expect(R.isValidType("datetime2", "datetimemillis")).to.be.true;
expect(R.isValidType("bit", "boolean")).to.be.true;
expect(R.isValidType("nvarchar", "boolean")).to.be.true;
});
Expand Down Expand Up @@ -453,6 +460,11 @@ module.exports = () => {
info = await R.inspect("test_field");
expect(["datetime"]).to.include(info["date_time"].type);

bean.dateTimeMillis = R.isoDateTimeMillis();
await R.store(bean);
info = await R.inspect("test_field");
expect(["datetime"]).to.include(info["date_time"].type);

bean.date = R.isoDate();
await R.store(bean);
info = await R.inspect("test_field");
Expand All @@ -463,6 +475,11 @@ module.exports = () => {
info = await R.inspect("test_field");
expect(["time"]).to.include(info["time"].type);

bean.timeMillis = R.isoTimeMillis();
await R.store(bean);
info = await R.inspect("test_field");
expect(["time"]).to.include(info["time"].type);

bean.bool = 2

try {
Expand Down