drizzle-team / drizzle-team/drizzle-orm
[BUG]: OP SQLite timestamp integer column returns a string initially then a Date
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### What version of `drizzle-orm` are you using?
0.32.0
### What version of `drizzle-kit` are you using?
0.22.7
### Describe the Bug
this is what a direct console log of the query shows up:
```
[{"expiration": "2024-07-17T23:33:36.000Z"}]
[{"expiration": "2024-07-17T23:33:36.000Z"}]
[{"expiration": "2024-07-17T23:33:36.000Z"}]
[{"expiration": "2024-07-17T23:33:36.000Z"}]
[{"expiration": "2024-07-17T23:33:36.000Z"}]
[{"expiration": 2024-07-17T23:33:36.000Z}]
```
it is a string initially then gets converted on its own to Date.
when running `typeof session.expiration` it's start with `string` then changes to `object`
when running `session.expiration instanceof Date` it's start with `false` then changes to `true`
### Expected behavior
Return a `Date` object rather than a string.
### Environment & setup
Expo 51.0.20
OP Sqlite: 6.2.11
```
import { drizzle } from "drizzle-orm/op-sqlite";
import { open, } from '@op-engineering/op-sqlite';
import { notes } from "./tables/notes";
import { history } from "./tables/history";
import { session } from "./tables/session";
// const expo = openDatabaseSync("db.db", { enableChangeListener: true, });
export const opsqlite = open({
name: 'myDb',
});
const db = drizzle(opsqlite, {
schema: { notes, history, session }
});
export default db
```
```
import sources from '@/constants/sources'
import { integer, sqliteTable, text, } from 'drizzle-orm/sqlite-core'
export const session = sqliteTable('session', {
source: text('id').primaryKey().$type(),
expiration: integer("expiration", { mode: "timestamp" }).notNull(),
token: text("token").notNull(),
agent: text("agent").notNull()
})
export type Session = typeof session.$inferSelect
export type InsertSession = typeof session.$inferInsert
```
This is where I tested it:
```
import { SourceKey } from "@/constants/sources";
import db, { opsqlite } from "@/database/db";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { useEffect } from "react";
const useSession = (source: SourceKey) => {
const queryClient = useQueryClient();
useEffect(() => {
let unsubscribe = opsqlite.reactiveExecute({
query: 'SELECT * FROM session',
fireOn: [{ table: 'session' }],
arguments: [],
callback: () => {
queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === "session" })
console.log("REFETCHING SESSIONS")
}
})
return () => {
unsubscribe()
}
}, [queryClient, source])
return useQuery({
queryKey: ["session", source],
// This is where the problem occurs
queryFn: () => db.query.session.findMany().prepare().execute(),
select(data) {
console.log(data)
return data.find(session => session.source === source);
},
gcTime: 0,
})
}
export default useSession
```
Contributor guide
Assessment
This issue has not been assessed yet.