christrading
christrading
Explore posts from servers
DTDrizzle Team
Created by christrading on 8/24/2024 in #help
Drizzle Error - Drizzle Kit - Integer
Received integer which cannot be safely represented as a JavaScript number
Received integer which cannot be safely represented as a JavaScript number
Bug report is attached
1 replies
DTDrizzle Team
Created by christrading on 8/20/2024 in #help
drizzle-kit migrate error
When I drizzle-kit generate a schema change of
ALTER TABLE `item` ADD `count` integer DEFAULT 0 NOT NULL;
ALTER TABLE `item` ADD `count` integer DEFAULT 0 NOT NULL;
and then run drizzle-kit migrate how come I get the below error that is about another unrelated credential table?
[⣷] applying migrations...LibsqlError: SQLITE_ERROR: table `credential` already exists
at mapSqliteError (.../node_modules/.pnpm/@[email protected]/node_modules/@libsql/client/lib-esm/sqlite3.js:350:16)
at executeStmt (.../node_modules/.pnpm/@[email protected]/node_modules/@libsql/client/lib-esm/sqlite3.js:255:15)
at .../node_modules/.pnpm/@[email protected]/node_modules/@libsql/client/lib-esm/sqlite3.js:90:24
... 3 lines matching cause stack trace ...
at migrate (.../node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]/node_modules/src/libsql/migrator.ts:50:19) {
code: 'SQLITE_ERROR',
rawCode: 1,
[cause]: SqliteError: table `credential` already exists
at convertError (.../node_modules/.pnpm/[email protected]/node_modules/libsql/index.js:49:12)
at Database.prepare (.../node_modules/.pnpm/[email protected]/node_modules/libsql/index.js:113:13)
at executeStmt (.../node_modules/.pnpm/@[email protected]/node_modules/@libsql/client/lib-esm/sqlite3.js:226:28)
at .../node_modules/.pnpm/@[email protected]/node_modules/@libsql/client/lib-esm/sqlite3.js:90:24
at Array.map (<anonymous>)
at Sqlite3Client.batch (.../node_modules/.pnpm/@[email protected]/node_modules/@libsql/client/lib-esm/sqlite3.js:86:38)
at LibSQLSession.batch (.../node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]/node_modules/src/libsql/session.ts:75:42)
at migrate (.../node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]/node_modules/src/libsql/migrator.ts:50:19) {
code: 'SQLITE_ERROR',
rawCode: 1
}
}
 ELIFECYCLE  Command failed with exit code 1.
[⣷] applying migrations...LibsqlError: SQLITE_ERROR: table `credential` already exists
at mapSqliteError (.../node_modules/.pnpm/@[email protected]/node_modules/@libsql/client/lib-esm/sqlite3.js:350:16)
at executeStmt (.../node_modules/.pnpm/@[email protected]/node_modules/@libsql/client/lib-esm/sqlite3.js:255:15)
at .../node_modules/.pnpm/@[email protected]/node_modules/@libsql/client/lib-esm/sqlite3.js:90:24
... 3 lines matching cause stack trace ...
at migrate (.../node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]/node_modules/src/libsql/migrator.ts:50:19) {
code: 'SQLITE_ERROR',
rawCode: 1,
[cause]: SqliteError: table `credential` already exists
at convertError (.../node_modules/.pnpm/[email protected]/node_modules/libsql/index.js:49:12)
at Database.prepare (.../node_modules/.pnpm/[email protected]/node_modules/libsql/index.js:113:13)
at executeStmt (.../node_modules/.pnpm/@[email protected]/node_modules/@libsql/client/lib-esm/sqlite3.js:226:28)
at .../node_modules/.pnpm/@[email protected]/node_modules/@libsql/client/lib-esm/sqlite3.js:90:24
at Array.map (<anonymous>)
at Sqlite3Client.batch (.../node_modules/.pnpm/@[email protected]/node_modules/@libsql/client/lib-esm/sqlite3.js:86:38)
at LibSQLSession.batch (.../node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]/node_modules/src/libsql/session.ts:75:42)
at migrate (.../node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]/node_modules/src/libsql/migrator.ts:50:19) {
code: 'SQLITE_ERROR',
rawCode: 1
}
}
 ELIFECYCLE  Command failed with exit code 1.
1 replies
DTDrizzle Team
Created by christrading on 6/11/2024 in #help
LibsqlError: SQLITE_BUSY: database is locked
I am getting a LibsqlError: SQLITE_BUSY: database is locked error on my insert function here with LibSQL/Turso as my SQLite driver
export async function putClientGroup(
clientGroup: ClientGroupRecord,
): Promise<void> {
const db = await getDB()
const { id, cvrVersion, clientGroupVersion } = clientGroup
const insertClientGroupStatementQuery = db
.insert(replicacheClientGroup)
.values({
id,
cvrVersion,
clientGroupVersion,
lastModified: new Date(),
})
.onConflictDoUpdate({
target: replicacheClientGroup.id,
set: {
cvrVersion,
clientGroupVersion,
lastModified: new Date(),
},
})
.prepare()

await insertClientGroupStatementQuery.run()
}
export async function putClientGroup(
clientGroup: ClientGroupRecord,
): Promise<void> {
const db = await getDB()
const { id, cvrVersion, clientGroupVersion } = clientGroup
const insertClientGroupStatementQuery = db
.insert(replicacheClientGroup)
.values({
id,
cvrVersion,
clientGroupVersion,
lastModified: new Date(),
})
.onConflictDoUpdate({
target: replicacheClientGroup.id,
set: {
cvrVersion,
clientGroupVersion,
lastModified: new Date(),
},
})
.prepare()

await insertClientGroupStatementQuery.run()
}
This error occurs after refactoring my driver from better-sqlite3 to LibSQL / Turso. My application ran without the error with the relatively same code here. The only difference two differences with the LibSQL / Turso driver is that we await to get the db and .run() the query statement. In both the working better-sqlite3 driver and non-working turso driver, we have this function called from within a db.transaction(). Then pullForChanges calls the function with await putClientGroup(nextClientGroupRecord)
const {
nextCVRVersion,
nextCVR,
clientChanges,
lists,
shares,
todos,
} = await db.transaction(async () => pullForChanges(
clientGroupID,
baseCVR,
userID,
replicacheCookie,
))
const {
nextCVRVersion,
nextCVR,
clientChanges,
lists,
shares,
todos,
} = await db.transaction(async () => pullForChanges(
clientGroupID,
baseCVR,
userID,
replicacheCookie,
))
5 replies
DTDrizzle Team
Created by christrading on 5/6/2024 in #help
Drizzle Migrate with Turso
On the migrate command drizzle-kit generate:sqlite && npx tsx ./drizzle/migrate.ts
//./drizzle/migrate.ts
import { migrate } from 'drizzle-orm/libsql/migrator'
import db from './db'

(async () => {
await migrate(db, { migrationsFolder: './drizzle/migrations' })
})()
//./drizzle/migrate.ts
import { migrate } from 'drizzle-orm/libsql/migrator'
import db from './db'

(async () => {
await migrate(db, { migrationsFolder: './drizzle/migrations' })
})()
I get error
return new api_1.LibsqlError(e.message, code,
LibsqlError: SERVER_ERROR: Server returned HTTP status 500`
return new api_1.LibsqlError(e.message, code,
LibsqlError: SERVER_ERROR: Server returned HTTP status 500`
51 replies
DTDrizzle Team
Created by christrading on 1/29/2024 in #help
Drizzle Schema Defaults
This is one of my table schemas in Prisma
model User {
id Int @id @default(autoincrement())
email String @unique
username String @unique
credentials Credential[]

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model User {
id Int @id @default(autoincrement())
email String @unique
username String @unique
credentials Credential[]

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
Are identifiers such as @id and default with values such as autoincrement() and now() available in Drizzle? I am using better-sqlite-3 as the driver
17 replies
DTDrizzle Team
Created by christrading on 1/1/2024 in #help
SQLite Query
export function updateTodo(
userID: string,
todoToUpdate: TodoUpdate,
): Affected {
const { listID } = mustGetTodo(todoToUpdate.id)
requireAccessToList(listID, userID)
const {
text, complete, sort, id,
} = todoToUpdate
const updateItemStatementQuery = db
.update(item)
.set({
title: sql<string>`'coalesce(${text}, title)'`,
complete: sql<boolean>`'coalesce(${complete}, complete)'`,
ord: sql<number>`'coalesce(${sort}, ord)'`,
rowVersion: sql<number>`'row_version + 1)'`,
lastModified: new Date(),
})
.where(eq(item.id, id))
.prepare()

updateItemStatementQuery.run()

return {
listIDs: [listID],
userIDs: [],
}
}
export function updateTodo(
userID: string,
todoToUpdate: TodoUpdate,
): Affected {
const { listID } = mustGetTodo(todoToUpdate.id)
requireAccessToList(listID, userID)
const {
text, complete, sort, id,
} = todoToUpdate
const updateItemStatementQuery = db
.update(item)
.set({
title: sql<string>`'coalesce(${text}, title)'`,
complete: sql<boolean>`'coalesce(${complete}, complete)'`,
ord: sql<number>`'coalesce(${sort}, ord)'`,
rowVersion: sql<number>`'row_version + 1)'`,
lastModified: new Date(),
})
.where(eq(item.id, id))
.prepare()

updateItemStatementQuery.run()

return {
listIDs: [listID],
userIDs: [],
}
}
My Drizzle + TypeScript + SQLite (better-sqlite-3) fails on updateItemStatementQuery.run(). Why is that? I have updated my fields to contain double quotes with '' as suggested before from a previous help thread.
15 replies
DTDrizzle Team
Created by christrading on 12/28/2023 in #help
As Column Alias
Hello, I have this drizzle + sqlite (better-sqlite-3) + typescript function, however, it's failing on the part where I use a subquery in the main query.
function requireAccessToList(
listID: string,
accessingUserID: string,
) {
const shareListIdSubquery = db
.select({ listID: share.listID })
.from(share)
.where(eq(share.userID, accessingUserID))
.as('shareListIdSubquery')

const listRowStatementQuery = db
.select({ count: sql<number>`count(*)` })
.from(list)
.where(
and(
eq(list.id, listID),
or(
eq(list.ownerID, accessingUserID),
inArray(list.id, shareListIdSubquery),
),
),
)
.prepare()

const numberOfRows = listRowStatementQuery.all()

if (numberOfRows.length === 0) {
throw new Error("Authorization error, can't access list")
}
}
function requireAccessToList(
listID: string,
accessingUserID: string,
) {
const shareListIdSubquery = db
.select({ listID: share.listID })
.from(share)
.where(eq(share.userID, accessingUserID))
.as('shareListIdSubquery')

const listRowStatementQuery = db
.select({ count: sql<number>`count(*)` })
.from(list)
.where(
and(
eq(list.id, listID),
or(
eq(list.ownerID, accessingUserID),
inArray(list.id, shareListIdSubquery),
),
),
)
.prepare()

const numberOfRows = listRowStatementQuery.all()

if (numberOfRows.length === 0) {
throw new Error("Authorization error, can't access list")
}
}
This is the part that makes the query fail, and works otherwise if I comment it out
inArray(list.id, shareListIdSubquery),
inArray(list.id, shareListIdSubquery),
My editor intellisense shows that the shareListIdSubquery type is SubqueryWithSelection What may be the reason that the subquery is breaking?
4 replies
DTDrizzle Team
Created by christrading on 12/21/2023 in #help
"No Such Column" Error
I'm using TypeScript, DrizzleORM, and SQLite (better-sqlite-3)
export function searchTodosAndShares(listIDs: string[]) {
if (listIDs.length === 0) return { shareMeta: [], todoMeta: [] }

const shareStatementQuery = db
.select({
id: share.id,
rowVersion: share.rowVersion,
type: sql<string>`share`,
})
.from(share)
.innerJoin(list, eq(share.listID, list.id))
.where(inArray(list.id, listIDs))

const todoStatementQuery = db
.select({
id: item.id,
rowVersion: item.rowVersion,
type: sql<string>`todo`,
})
.from(item)
.where(inArray(list.id, listIDs))

const sharesAndTodos = unionAll(
shareStatementQuery,
todoStatementQuery,
)
.prepare()
.all()

const result: {
shareMeta: { id: string; rowVersion: number; }[],
todoMeta: { id: string; rowVersion: number; }[] } = {
shareMeta: [],
todoMeta: [],
}

sharesAndTodos.forEach((row) => {
const { id, rowVersion, type } = row
result[type as 'shareMeta' | 'todoMeta'].push({ id, rowVersion })
})

return result
}
export function searchTodosAndShares(listIDs: string[]) {
if (listIDs.length === 0) return { shareMeta: [], todoMeta: [] }

const shareStatementQuery = db
.select({
id: share.id,
rowVersion: share.rowVersion,
type: sql<string>`share`,
})
.from(share)
.innerJoin(list, eq(share.listID, list.id))
.where(inArray(list.id, listIDs))

const todoStatementQuery = db
.select({
id: item.id,
rowVersion: item.rowVersion,
type: sql<string>`todo`,
})
.from(item)
.where(inArray(list.id, listIDs))

const sharesAndTodos = unionAll(
shareStatementQuery,
todoStatementQuery,
)
.prepare()
.all()

const result: {
shareMeta: { id: string; rowVersion: number; }[],
todoMeta: { id: string; rowVersion: number; }[] } = {
shareMeta: [],
todoMeta: [],
}

sharesAndTodos.forEach((row) => {
const { id, rowVersion, type } = row
result[type as 'shareMeta' | 'todoMeta'].push({ id, rowVersion })
})

return result
}
How come I get error of no such column: share or no such column: todo? Thanks in advance for the help 🙂 🙏
21 replies
TTCTheo's Typesafe Cult
Created by christrading on 2/14/2023 in #questions
Create T3 App Lighthouse Performances
10 replies