diff --git a/docs/Data-Types.md b/docs/Data-Types.md index 371782a..02bbebd 100644 --- a/docs/Data-Types.md +++ b/docs/Data-Types.md @@ -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 @@ -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 @@ -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 diff --git a/lib/redbean-node.ts b/lib/redbean-node.ts index 0993af9..f86ced5 100644 --- a/lib/redbean-node.ts +++ b/lib/redbean-node.ts @@ -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); @@ -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); @@ -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"; @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -464,7 +480,7 @@ export class RedBeanNode { if (columnType == "date") { if ( valueType == "varchar" || valueType == "text" || - valueType == "datetime" + valueType == "datetime" || valueType == "datetimemillis" ) { return false; } @@ -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; @@ -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; @@ -1016,6 +1060,11 @@ 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; @@ -1023,6 +1072,13 @@ export class RedBeanNode { 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; diff --git a/test/common/test-redbean-node.js b/test/common/test-redbean-node.js index 2c5a20d..ae02385 100644 --- a/test/common/test-redbean-node.js +++ b/test/common/test-redbean-node.js @@ -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"); }); }); @@ -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"); @@ -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; }); @@ -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"); @@ -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 {