Skip to content

Commit

Permalink
Merge pull request #6936 from ever-co/feat/#4717-better-sqlite-3
Browse files Browse the repository at this point in the history
Feat/#4717 better sqlite 3
  • Loading branch information
evereq authored Oct 9, 2023
2 parents 38e8054 + 82ad369 commit 934c8ba
Show file tree
Hide file tree
Showing 177 changed files with 1,076 additions and 848 deletions.
4 changes: 3 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@
"Desterrro",
"Chetan",
"chetan",
"Yostorono"
"Yostorono",
"sarif",
"SARIF"
],
"useGitignore": true,
"ignorePaths": [
Expand Down
4 changes: 2 additions & 2 deletions .env.docker
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ CLIENT_BASE_URL=http://localhost:4200
#set to Website Platform
PLATFORM_WEBSITE_URL=https://gauzy.co

# DB_TYPE: sqlite | postgres
DB_TYPE=sqlite
# DB_TYPE: sqlite | postgres | better-sqlite3
DB_TYPE=better-sqlite3
DB_SYNCHRONIZE=false

# Below are PostgreSQL Connection Parameters
Expand Down
4 changes: 2 additions & 2 deletions .env.local
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ CLIENT_BASE_URL=http://localhost:4200
#set to Website Platform
PLATFORM_WEBSITE_URL=https://gauzy.co

# DB_TYPE: sqlite | postgres
DB_TYPE=sqlite
# DB_TYPE: sqlite | postgres | better-sqlite3
DB_TYPE=better-sqlite3
DB_SYNCHRONIZE=false

# Below are PostgreSQL Connection Parameters
Expand Down
4 changes: 2 additions & 2 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ CLIENT_BASE_URL=http://localhost:4200
#set to Website Platform
PLATFORM_WEBSITE_URL=https://gauzy.co

# DB_TYPE: sqlite | postgres
DB_TYPE=sqlite
# DB_TYPE: sqlite | postgres | better-sqlite3
DB_TYPE=better-sqlite3

# PostgreSQL Connection Parameters
# DB_HOST=localhost
Expand Down
21 changes: 21 additions & 0 deletions apps/api/src/plugin-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,26 @@ function getDbConfig(): DataSourceOptions {
logger: 'file', // Removes console logging, instead logs all queries in a file ormlogs.log
synchronize: process.env.DB_SYNCHRONIZE === 'true' ? true : false, // We are using migrations, synchronize should be set to false.
};
case 'better-sqlite3':
const betterSqlitePath =
process.env.DB_PATH ||
path.join(
path.resolve('.', ...['apps', 'api', 'data']),
'gauzy.sqlite3'
);

return {
type: dbType,
database: betterSqlitePath,
logging: 'all',
logger: 'file', // Removes console logging, instead logs all queries in a file ormlogs.log
synchronize: process.env.DB_SYNCHRONIZE === 'true', // We are using migrations, synchronize should be set to false.
prepareDatabase: (db) => {
if (!process.env.IS_ELECTRON) {
// Enhance performance
db.pragma('journal_mode = WAL');
}
}
};
}
}
45 changes: 6 additions & 39 deletions apps/desktop-timer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ app.on('ready', async () => {
if (!settings) {
launchAtStartup(true, false);
}
if (provider.dialect === 'sqlite') {
if (['sqlite', 'better-sqlite'].includes(provider.dialect)) {
try {
const res = await knex.raw(`pragma journal_mode = WAL;`);
console.log(res);
Expand Down Expand Up @@ -600,46 +600,13 @@ ipcMain.on('restart_and_update', () => {

ipcMain.on('check_database_connection', async (event, arg) => {
try {
const provider = arg.db;
let databaseOptions;
if (provider === 'postgres' || provider === 'mysql') {
databaseOptions = {
client: provider === 'postgres' ? 'pg' : 'mysql',
connection: {
host: arg[provider].dbHost,
user: arg[provider].dbUsername,
password: arg[provider].dbPassword,
database: arg[provider].dbName,
port: arg[provider].dbPort,
},
};
} else {
databaseOptions = {
client: 'sqlite3',
connection: {
filename: sqlite3filename,
},
};
}
const dbConn = require('knex')(databaseOptions);
await dbConn.raw('select 1+1 as result');
const driver = await provider.check(arg);
event.sender.send('database_status', {
status: true,
message:
provider === 'postgres'
? TranslateService.instant(
'TIMER_TRACKER.DIALOG.CONNECTION_DRIVER',
{ driver: 'PostgresSQL' }
)
: provider === 'mysql'
? TranslateService.instant(
'TIMER_TRACKER.DIALOG.CONNECTION_DRIVER',
{ driver: 'MySQL' }
)
: TranslateService.instant(
'TIMER_TRACKER.DIALOG.CONNECTION_DRIVER',
{ driver: 'SQLite' }
),
message: TranslateService.instant(
'TIMER_TRACKER.DIALOG.CONNECTION_DRIVER',
{ driver }
),
});
} catch (error) {
event.sender.send('database_status', {
Expand Down
33 changes: 7 additions & 26 deletions apps/desktop/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,10 @@ async function startServer(value, restart = false) {
if (value.db === 'sqlite') {
process.env.DB_PATH = sqlite3filename;
process.env.DB_TYPE = 'sqlite';
} else {
}else if(value.db === 'better-sqlite') {
process.env.DB_PATH = sqlite3filename;
process.env.DB_TYPE = 'better-sqlite3';
}else {
process.env.DB_TYPE = 'postgres';
process.env.DB_HOST = value['postgres']?.dbHost;
process.env.DB_PORT = value['postgres']?.dbPort;
Expand Down Expand Up @@ -462,7 +465,7 @@ app.on('ready', async () => {
splashScreen = new SplashScreen(pathWindow.timeTrackerUi);
await splashScreen.loadURL();
splashScreen.show();
if (provider.dialect === 'sqlite') {
if (['sqlite', 'better-sqlite'].includes(provider.dialect)) {
try {
const res = await knex.raw(`pragma journal_mode = WAL;`)
console.log(res);
Expand Down Expand Up @@ -698,34 +701,12 @@ ipcMain.on('restart_and_update', () => {

ipcMain.on('check_database_connection', async (event, arg) => {
try {
const provider = arg.db;
let databaseOptions;
if (provider === 'postgres') {
databaseOptions = {
client: 'pg',
connection: {
host: arg[provider].dbHost,
user: arg[provider].dbUsername,
password: arg[provider].dbPassword,
database: arg[provider].dbName,
port: arg[provider].dbPort
}
};
} else {
databaseOptions = {
client: 'sqlite',
connection: {
filename: sqlite3filename,
},
};
}
const dbConn = require('knex')(databaseOptions);
await dbConn.raw('select 1+1 as result');
const driver = await provider.check(arg);
event.sender.send('database_status', {
status: true,
message: TranslateService.instant(
'TIMER_TRACKER.DIALOG.CONNECTION_DRIVER',
{ driver: provider === 'postgres' ? 'PostgresSQL' : 'SQLite' }
{ driver }
)
});
} catch (error) {
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ const getEnvApi = () => {
const config = serverConfig.setting;
serverConfig.update();
const addsConfig = LocalStore.getAdditionalConfig();
const provider = config.db;
const provider =
config.db === 'better-sqlite' ? 'better-sqlite3' : config.db;
return {
IS_ELECTRON: 'true',
DB_PATH: sqlite3filename,
Expand Down
29 changes: 29 additions & 0 deletions packages/config/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,35 @@ switch (dbType) {

connectionConfig = sqliteConfig;

break;

case 'better-sqlite3':
const betterSqlitePath =
process.env.DB_PATH ||
path.join(
process.cwd(),
...['apps', 'api', 'data'],
'gauzy.sqlite3'
);

console.log('Better Sqlite DB Path: ' + betterSqlitePath);

const betterSqliteConfig: DataSourceOptions = {
type: dbType,
database: betterSqlitePath,
logging: 'all',
logger: 'file', // Removes console logging, instead logs all queries in a file ormlogs.log
synchronize: process.env.DB_SYNCHRONIZE === 'true', // We are using migrations, synchronize should be set to false.
prepareDatabase: (db) => {
if (!process.env.IS_ELECTRON) {
// Enhance performance
db.pragma('journal_mode = WAL');
}
}
};

connectionConfig = betterSqliteConfig;

break;
}

Expand Down
5 changes: 3 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
"@gauzy/config": "^0.1.0",
"@gauzy/contracts": "^0.1.0",
"@gauzy/integration-ai": "^0.1.0",
"@gauzy/integration-hubstaff": "^0.1.0",
"@gauzy/integration-upwork": "^0.1.0",
"@gauzy/integration-github": "^0.1.0",
"@gauzy/integration-hubstaff": "^0.1.0",
"@gauzy/integration-jira": "^0.1.0",
"@gauzy/integration-upwork": "^0.1.0",
"@gauzy/plugin": "^0.1.0",
"@godaddy/terminus": "^4.5.0",
"@grpc/grpc-js": "^1.6.7",
Expand Down Expand Up @@ -86,6 +86,7 @@
"aws-sdk": "^2.1082.0",
"axios": "^1.5.0",
"bcrypt": "^5.1.0",
"better-sqlite3": "^8.7.0",
"cache-manager": "^3.4.0",
"camelcase": "^6.2.1",
"chalk": "4.1.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class GetConflictAvailabilitySlotsHandler
public async execute(
command: GetConflictAvailabilitySlotsCommand
): Promise<IAvailabilitySlot[]> {

const { input } = command;
const { startTime, endTime, employeeId, organizationId } = input;
const tenantId = RequestContext.currentTenantId() || input.tenantId;
Expand All @@ -38,7 +38,7 @@ export class GetConflictAvailabilitySlotsHandler
});

query.andWhere(
this.configService.dbConnectionOptions.type === 'sqlite'
['sqlite', 'better-sqlite3'].includes(this.configService.dbConnectionOptions.type)
? `'${startedAt}' >= "${query.alias}"."startTime" AND '${startedAt}' <= "${query.alias}"."endTime"`
: `("${query.alias}"."startTime", "${query.alias}"."endTime") OVERLAPS (timestamptz '${startedAt}', timestamptz '${stoppedAt}')`
);
Expand All @@ -52,7 +52,7 @@ export class GetConflictAvailabilitySlotsHandler

if (input.type) {
query.andWhere(`${query.alias}.type = :type`, {
type: input.type
type: input.type
});
}

Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ export function unixTimestampToDate(
export function convertToDatetime(datetime) {
if (moment(new Date(datetime)).isValid()) {
const dbType = getConfig().dbConnectionOptions.type || 'sqlite';
if (dbType === 'sqlite') {
const allowedDbTypes = ['sqlite', 'better-sqlite3'];
if (allowedDbTypes.includes(dbType)) {
return moment(new Date(datetime)).format('YYYY-MM-DD HH:mm:ss');
} else {
return moment(new Date(datetime)).toDate();
Expand Down Expand Up @@ -132,7 +133,7 @@ export function getDateRange(
}

const dbType = getConfig().dbConnectionOptions.type || 'sqlite';
if (dbType === 'sqlite') {
if (['sqlite', 'better-sqlite3'].includes(dbType)) {
start = start.format('YYYY-MM-DD HH:mm:ss');
end = end.format('YYYY-MM-DD HH:mm:ss');
} else {
Expand Down Expand Up @@ -224,7 +225,8 @@ export function getDateRangeFormat(
}

const dbType = getConfig().dbConnectionOptions.type || 'sqlite';
if (dbType === 'sqlite') {
const allowedDbTypes = ['sqlite', 'better-sqlite3'];
if (allowedDbTypes.includes(dbType)) {
return {
start: start.format('YYYY-MM-DD HH:mm:ss'),
end: end.format('YYYY-MM-DD HH:mm:ss'),
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/database/migration-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export class ${camelCase(name, true)}${timestamp} implements MigrationInterface
* @param queryRunner
*/
public async up(queryRunner: QueryRunner): Promise<any> {
if (queryRunner.connection.options.type === 'sqlite') {
if (['sqlite', 'better-sqlite3'].includes(queryRunner.connection.options.type)) {
await this.sqliteUpQueryRunner(queryRunner);
} else {
await this.postgresUpQueryRunner(queryRunner);
Expand All @@ -288,7 +288,7 @@ export class ${camelCase(name, true)}${timestamp} implements MigrationInterface
* @param queryRunner
*/
public async down(queryRunner: QueryRunner): Promise<any> {
if (queryRunner.connection.options.type === 'sqlite') {
if (['sqlite', 'better-sqlite3'].includes(queryRunner.connection.options.type)) {
await this.sqliteDownQueryRunner(queryRunner);
} else {
await this.postgresDownQueryRunner(queryRunner);
Expand Down Expand Up @@ -318,23 +318,23 @@ export class ${camelCase(name, true)}${timestamp} implements MigrationInterface
}
/**
* SqliteDB Up Migration
* SqliteDB and BetterSQlite3DB Up Migration
*
* @param queryRunner
*/
public async sqliteUpQueryRunner(queryRunner: QueryRunner): Promise<any> {
${(connection.options.type === 'sqlite') ? upSqls.join(`
${(['sqlite', 'better-sqlite3'].includes(connection.options.type)) ? upSqls.join(`
`) : [].join(`
`)}
}
/**
* SqliteDB Down Migration
* SqliteDB and BetterSQlite3DB Down Migration
*
* @param queryRunner
*/
public async sqliteDownQueryRunner(queryRunner: QueryRunner): Promise<any> {
${(connection.options.type === 'sqlite') ? downSqls.join(`
${(['sqlite', 'better-sqlite3'].includes(connection.options.type)) ? downSqls.join(`
`) : [].join(`
`)}
}
Expand Down
Loading

0 comments on commit 934c8ba

Please sign in to comment.