Skip to content

Commit

Permalink
Added datetime cast option, updated version for release
Browse files Browse the repository at this point in the history
  • Loading branch information
theianjohnson committed Nov 26, 2023
1 parent f13168e commit b124931
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 13 deletions.
22 changes: 19 additions & 3 deletions __tests__/castAttribute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,25 @@ describe('castAttribute', () => {
it('should handle invalid date strings when casting', () => {
const invalidDateString = 'not a valid date';
const result = Model.castAttribute('date', invalidDateString);
// Depending on how you want to handle invalid dates,
// you can expect either a null, an invalid Date object, or the original string
// For example, expecting an invalid Date object:
expect(result).toBeInstanceOf(Date);
expect(isNaN(result.getTime())).toBe(true); // Invalid Date object check
});

it('should cast datetime strings to Date objects', () => {
const isoDateTimeString = '2020-01-01T12:30:45.000Z';
const dateTimeObject = Model.castAttribute('datetime', isoDateTimeString);
expect(dateTimeObject).toBeInstanceOf(Date);
expect(dateTimeObject.toISOString()).toBe(isoDateTimeString);
});

it('should not cast null fields to datetime objects', () => {
const dateTimeObject = Model.castAttribute('datetime', null);
expect(dateTimeObject).toBeNull();
});

it('should handle invalid datetime strings when casting', () => {
const invalidDateTimeString = 'not a valid datetime';
const result = Model.castAttribute('datetime', invalidDateTimeString);
expect(result).toBeInstanceOf(Date);
expect(isNaN(result.getTime())).toBe(true); // Invalid Date object check
});
Expand Down
2 changes: 1 addition & 1 deletion dist/Model.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type Casts = {
[key: string]: 'number' | 'boolean' | 'string' | 'date' | 'json';
[key: string]: 'number' | 'boolean' | 'string' | 'date' | 'datetime' | 'json';
};
type ModelAttributes = Record<string, any>;
type SQLResult = {
Expand Down
10 changes: 7 additions & 3 deletions dist/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ class Model {
return String(value);
case 'date':
return !!value ? new Date(value) : null;
case 'datetime':
return !!value ? new Date(value) : null;
case 'json':
try {
return JSON.parse(value);
Expand All @@ -237,7 +239,9 @@ class Model {
case 'string':
return String(value);
case 'date':
return value instanceof Date ? value.toISOString() : value;
return value instanceof Date ? value.toISOString().split('T')[0] : new Date(value).toISOString().split('T')[0];
case 'datetime':
return value instanceof Date ? value.toISOString() : new Date(value).toISOString();
case 'json':
try {
return JSON.stringify(value);
Expand Down Expand Up @@ -539,8 +543,8 @@ exports.Model = Model;
Model.db = SQLite.openDatabase('app.db');
Model.tableName = '';
Model.casts = {
createdAt: 'date',
updatedAt: 'date'
createdAt: 'datetime',
updatedAt: 'datetime'
};
Model.withTimestamps = true;
Model.createdAtColumn = 'createdAt';
Expand Down
2 changes: 1 addition & 1 deletion dist/Model.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "expo-sqlite-eloquent-orm",
"version": "0.9.18",
"version": "0.9.19",
"description": "An Expo SQLite ORM",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
12 changes: 8 additions & 4 deletions src/Model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as SQLite from 'expo-sqlite'

type Casts = {[key: string]: 'number' | 'boolean' | 'string' | 'date' | 'json'}
type Casts = {[key: string]: 'number' | 'boolean' | 'string' | 'date' | 'datetime' | 'json'}

type Clauses = {
select: string
Expand Down Expand Up @@ -34,8 +34,8 @@ export class Model {
static tableName = ''

static casts: Casts = {
createdAt: 'date',
updatedAt: 'date',
createdAt: 'datetime',
updatedAt: 'datetime',
}

static withTimestamps: boolean = true;
Expand Down Expand Up @@ -218,6 +218,8 @@ export class Model {
return String(value);
case 'date':
return !!value ? new Date(value) : null;
case 'datetime':
return !!value ? new Date(value) : null;
case 'json':
try {
return JSON.parse(value);
Expand All @@ -241,7 +243,9 @@ export class Model {
case 'string':
return String(value);
case 'date':
return value instanceof Date ? value.toISOString() : value;
return value instanceof Date ? value.toISOString().split('T')[0] : (new Date(value)).toISOString().split('T')[0];
case 'datetime':
return value instanceof Date ? value.toISOString() : (new Date(value)).toISOString();
case 'json':
try {
return JSON.stringify(value);
Expand Down

0 comments on commit b124931

Please sign in to comment.