Files
twenty/packages/twenty-website/src/database/database.ts
T
Abdullah. f7a80d250b Update dizzle-kit and drizzle-orm to avoid the dependency on Hono. (#15343)
Updated both drizzle-kit and drizzle-orm to the latest versions.
Process:
- Ran migrations on the database.
- Updated the versions.
- Made the required changes to `drizzle-posgres.config.ts`.
- Ensured migrations are not out of sync be running them again - no
issues, no updates applied.
- Updated the snapshots.
- Deleted the database named `website`.
- Re-ran the migrations to confirm.

There are no breaking changes because the surface drizzle-orm covers is
limited. However, we had to update drizzle-orm in order to update
drizzle-kit to a version greater than 0.27.0 in order to avoid the use
of hono. Therefore, I went ahead and updated both to the latest.

Resolves [Dependabot Alert
#274](https://github.com/twentyhq/twenty/security/dependabot/274) and
three others.
2025-10-24 16:54:15 +02:00

70 lines
1.8 KiB
TypeScript

import { global } from '@apollo/client/utilities/globals';
import { drizzle, type PostgresJsDatabase } from 'drizzle-orm/postgres-js';
import { migrate as postgresMigrate } from 'drizzle-orm/postgres-js/migrator';
import postgres from 'postgres';
import * as schema from './schema-postgres';
import 'dotenv/config';
let pgDb: PostgresJsDatabase<typeof schema>;
if (global.process.env.DATABASE_PG_URL) {
const pgClient = postgres(`${global.process.env.DATABASE_PG_URL}`);
pgDb = drizzle(pgClient, { schema, logger: false });
}
const migrate = async () => {
await postgresMigrate(pgDb, {
migrationsFolder: './src/database/migrations',
});
};
const findOne = (model: any, orderBy: any) => {
return pgDb.select().from(model).orderBy(orderBy).limit(1).execute();
};
const findAll = (model: any, orderBy?: any) => {
if (orderBy) {
return pgDb.select().from(model).orderBy(orderBy).execute();
}
return pgDb.select().from(model).execute();
};
const insertMany = async (
model: any,
data: any,
options?: {
onConflictKey?: string;
onConflictUpdateObject?: any;
onConflictDoNothing?: boolean;
},
) => {
const query = pgDb.insert(model).values(data);
if (options?.onConflictUpdateObject) {
if (options?.onConflictKey) {
return query
.onConflictDoUpdate({
target: [model[options.onConflictKey]],
set: options.onConflictUpdateObject,
})
.execute();
}
}
if (options?.onConflictDoNothing && !options?.onConflictKey) {
return query.onConflictDoNothing().execute();
}
if (options?.onConflictKey) {
return query
.onConflictDoNothing({
target: [model[options.onConflictKey]],
})
.execute();
}
return query.execute();
};
export { findAll, findOne, insertMany, migrate };