Files
twenty/packages/twenty-website/src/database/database.ts
Ady Beraud b82519301c Website UI design (#4829)
**Fixed different issues** :
 
- Multiple CSS fixes: font-size, colors, margins, z-index ...
- Fixed hover on contributor avatars
- Added link to contributors in footer
- Made the year in the footer dynamic (2023 --> 2024)
- Added name of contributor in "Thank you" section of Contributor page
- Added footer in small screens
- Made Activity Log Responsive 
- Fixed bug in "saving issues to DB", title was null everywhere. I
needed to implement an "upsert" behaviour to update the existing
database on init

**To be noted :** 

There is the following bug on production happening on mobile when you
refresh a second time :

<img width="1440" alt="Screenshot 2024-04-05 at 01 30 58"
src="https://github.com/twentyhq/twenty/assets/102751374/b935b07a-63dc-463d-8dcb-070ad4ef6db0">


It seems to be related to the following issue on mdx :
[https://github.com/hashicorp/next-mdx-remote/issues/350](https://github.com/hashicorp/next-mdx-remote/issues/350)

I added the following code that fixed this bug for me in development
(this needs to be tested in production) :

```
const serialized = await serialize(content, {
    mdxOptions: {
      development: process.env.NODE_ENV === 'development',
    }
  })
```

---------

Co-authored-by: Ady Beraud <a.beraud96@gmail.com>
2024-04-05 08:41:08 +02:00

136 lines
3.5 KiB
TypeScript

import { global } from '@apollo/client/utilities/globals';
import { createClient } from '@libsql/client';
import { drizzle as sqliteDrizzle, LibSQLDatabase } from 'drizzle-orm/libsql';
import { migrate as sqliteMigrate } from 'drizzle-orm/libsql/migrator';
import {
drizzle as pgDrizzle,
PostgresJsDatabase,
} from 'drizzle-orm/postgres-js';
import { migrate as postgresMigrate } from 'drizzle-orm/postgres-js/migrator';
import { SQLiteTableWithColumns } from 'drizzle-orm/sqlite-core';
import postgres from 'postgres';
import 'dotenv/config';
// Todo: Deprecate SQLite once prototyping is complete, this is making things impossible to type properly
const databaseDriver = global.process.env.DATABASE_DRIVER || 'sqlite';
const isSqliteDriver = databaseDriver === 'sqlite';
const isPgDriver = databaseDriver === 'pg';
let sqliteDb: LibSQLDatabase;
let pgDb: PostgresJsDatabase;
if (isSqliteDriver) {
const sqliteClient = createClient({
url: 'file:twenty-website.sqlite',
});
sqliteDb = sqliteDrizzle(sqliteClient, { logger: false });
}
if (isPgDriver) {
const pgClient = postgres(`${global.process.env.DATABASE_PG_URL}`);
pgDb = pgDrizzle(pgClient, { logger: false });
}
const migrate = async () => {
if (isSqliteDriver) {
await sqliteMigrate(sqliteDb, {
migrationsFolder: './src/database/sqlite/migrations',
});
return;
}
if (isPgDriver) {
await postgresMigrate(pgDb, {
migrationsFolder: './src/database/postgres/migrations',
});
return;
}
throw new Error('Unsupported database driver');
};
const findOne = (model: SQLiteTableWithColumns<any>, orderBy: any) => {
if (isSqliteDriver) {
return sqliteDb.select().from(model).orderBy(orderBy).limit(1).execute();
}
if (isPgDriver) {
return pgDb.select().from(model).orderBy(orderBy).limit(1).execute();
}
throw new Error('Unsupported database driver');
};
const findAll = (model: SQLiteTableWithColumns<any>) => {
if (isSqliteDriver) {
return sqliteDb.select().from(model).all();
}
if (isPgDriver) {
return pgDb.select().from(model).execute();
}
throw new Error('Unsupported database driver');
};
const insertMany = async (
model: SQLiteTableWithColumns<any>,
data: any,
options?: {
onConflictKey?: string;
onConflictUpdateObject?: any;
},
) => {
if (isSqliteDriver) {
const query = sqliteDb.insert(model).values(data);
if (options?.onConflictUpdateObject) {
if (options?.onConflictKey) {
return query
.onConflictDoUpdate({
target: [model[options.onConflictKey]],
set: options.onConflictUpdateObject,
})
.execute();
}
}
if (options?.onConflictKey) {
return query
.onConflictDoNothing({
target: [model[options.onConflictKey]],
})
.execute();
}
return query.execute();
}
if (isPgDriver) {
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?.onConflictKey) {
return query
.onConflictDoNothing({
target: [model[options.onConflictKey]],
})
.execute();
}
return query.execute();
}
throw new Error('Unsupported database driver');
};
export { findAll, findOne, insertMany, migrate };