Skip to content

Commit

Permalink
style: use LF instead of CRLF in BetterSqlite3 modules (#6433)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnotjames committed Jul 21, 2020
1 parent 2241451 commit c6ae601
Show file tree
Hide file tree
Showing 2 changed files with 246 additions and 246 deletions.
282 changes: 141 additions & 141 deletions src/driver/better-sqlite3/BetterSqlite3Driver.ts
@@ -1,141 +1,141 @@
import { DriverPackageNotInstalledError } from "../../error/DriverPackageNotInstalledError";
import { DriverOptionNotSetError } from "../../error/DriverOptionNotSetError";
import { PlatformTools } from "../../platform/PlatformTools";
import { Connection } from "../../connection/Connection";
import { ColumnType } from "../types/ColumnTypes";
import { QueryRunner } from "../../query-runner/QueryRunner";
import { AbstractSqliteDriver } from "../sqlite-abstract/AbstractSqliteDriver";
import { BetterSqlite3ConnectionOptions } from "./BetterSqlite3ConnectionOptions";
import { BetterSqlite3QueryRunner } from "./BetterSqlite3QueryRunner";

/**
* Organizes communication with sqlite DBMS.
*/
export class BetterSqlite3Driver extends AbstractSqliteDriver {

// -------------------------------------------------------------------------
// Public Implemented Properties
// -------------------------------------------------------------------------

/**
* Connection options.
*/
options: BetterSqlite3ConnectionOptions;

/**
* SQLite underlying library.
*/
sqlite: any;

// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------

constructor(connection: Connection) {
super(connection);

this.connection = connection;
this.options = connection.options as BetterSqlite3ConnectionOptions;
this.database = this.options.database;

// validate options to make sure everything is set
if (!this.options.database)
throw new DriverOptionNotSetError("database");

// load sqlite package
this.loadDependencies();
}

// -------------------------------------------------------------------------
// Public Methods
// -------------------------------------------------------------------------

/**
* Closes connection with database.
*/
async disconnect(): Promise<void> {
this.queryRunner = undefined;
this.databaseConnection.close();
}

/**
* Creates a query runner used to execute database queries.
*/
createQueryRunner(mode: "master" | "slave" = "master"): QueryRunner {
if (!this.queryRunner)
this.queryRunner = new BetterSqlite3QueryRunner(this);

return this.queryRunner;
}

normalizeType(column: { type?: ColumnType, length?: number | string, precision?: number | null, scale?: number }): string {
if ((column.type as any) === Buffer) {
return "blob";
}

return super.normalizeType(column);
}

// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------

/**
* Creates connection with the database.
*/
protected async createDatabaseConnection() {
// not to create database directory if is in memory
if (this.options.database !== ":memory:")
await this.createDatabaseDirectory(this.options.database);

const {
database,
readonly = false,
fileMustExist = false,
timeout = 5000,
verbose = null,
prepareDatabase
} = this.options;
const databaseConnection = this.sqlite(database, { readonly, fileMustExist, timeout, verbose });

// we need to enable foreign keys in sqlite to make sure all foreign key related features
// working properly. this also makes onDelete to work with sqlite.
databaseConnection.exec(`PRAGMA foreign_keys = ON`);

// turn on WAL mode to enhance performance
databaseConnection.exec(`PRAGMA journal_mode = WAL`);

// in the options, if encryption key for SQLCipher is setted.
if (this.options.key) {
databaseConnection.exec(`PRAGMA key = ${JSON.stringify(this.options.key)}`);
}

if (typeof prepareDatabase === "function") {
prepareDatabase(databaseConnection);
}

return databaseConnection;
}

/**
* If driver dependency is not given explicitly, then try to load it via "require".
*/
protected loadDependencies(): void {
try {
this.sqlite = PlatformTools.load("better-sqlite3");

} catch (e) {
throw new DriverPackageNotInstalledError("SQLite", "better-sqlite3");
}
}

/**
* Auto creates database directory if it does not exist.
*/
protected createDatabaseDirectory(fullPath: string): Promise<void> {
const mkdirp = PlatformTools.load("mkdirp");
const path = PlatformTools.load("path");
return mkdirp(path.dirname(fullPath));
}

}
import { DriverPackageNotInstalledError } from "../../error/DriverPackageNotInstalledError";
import { DriverOptionNotSetError } from "../../error/DriverOptionNotSetError";
import { PlatformTools } from "../../platform/PlatformTools";
import { Connection } from "../../connection/Connection";
import { ColumnType } from "../types/ColumnTypes";
import { QueryRunner } from "../../query-runner/QueryRunner";
import { AbstractSqliteDriver } from "../sqlite-abstract/AbstractSqliteDriver";
import { BetterSqlite3ConnectionOptions } from "./BetterSqlite3ConnectionOptions";
import { BetterSqlite3QueryRunner } from "./BetterSqlite3QueryRunner";

/**
* Organizes communication with sqlite DBMS.
*/
export class BetterSqlite3Driver extends AbstractSqliteDriver {

// -------------------------------------------------------------------------
// Public Implemented Properties
// -------------------------------------------------------------------------

/**
* Connection options.
*/
options: BetterSqlite3ConnectionOptions;

/**
* SQLite underlying library.
*/
sqlite: any;

// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------

constructor(connection: Connection) {
super(connection);

this.connection = connection;
this.options = connection.options as BetterSqlite3ConnectionOptions;
this.database = this.options.database;

// validate options to make sure everything is set
if (!this.options.database)
throw new DriverOptionNotSetError("database");

// load sqlite package
this.loadDependencies();
}

// -------------------------------------------------------------------------
// Public Methods
// -------------------------------------------------------------------------

/**
* Closes connection with database.
*/
async disconnect(): Promise<void> {
this.queryRunner = undefined;
this.databaseConnection.close();
}

/**
* Creates a query runner used to execute database queries.
*/
createQueryRunner(mode: "master" | "slave" = "master"): QueryRunner {
if (!this.queryRunner)
this.queryRunner = new BetterSqlite3QueryRunner(this);

return this.queryRunner;
}

normalizeType(column: { type?: ColumnType, length?: number | string, precision?: number | null, scale?: number }): string {
if ((column.type as any) === Buffer) {
return "blob";
}

return super.normalizeType(column);
}

// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------

/**
* Creates connection with the database.
*/
protected async createDatabaseConnection() {
// not to create database directory if is in memory
if (this.options.database !== ":memory:")
await this.createDatabaseDirectory(this.options.database);

const {
database,
readonly = false,
fileMustExist = false,
timeout = 5000,
verbose = null,
prepareDatabase
} = this.options;
const databaseConnection = this.sqlite(database, { readonly, fileMustExist, timeout, verbose });

// we need to enable foreign keys in sqlite to make sure all foreign key related features
// working properly. this also makes onDelete to work with sqlite.
databaseConnection.exec(`PRAGMA foreign_keys = ON`);

// turn on WAL mode to enhance performance
databaseConnection.exec(`PRAGMA journal_mode = WAL`);

// in the options, if encryption key for SQLCipher is setted.
if (this.options.key) {
databaseConnection.exec(`PRAGMA key = ${JSON.stringify(this.options.key)}`);
}

if (typeof prepareDatabase === "function") {
prepareDatabase(databaseConnection);
}

return databaseConnection;
}

/**
* If driver dependency is not given explicitly, then try to load it via "require".
*/
protected loadDependencies(): void {
try {
this.sqlite = PlatformTools.load("better-sqlite3");

} catch (e) {
throw new DriverPackageNotInstalledError("SQLite", "better-sqlite3");
}
}

/**
* Auto creates database directory if it does not exist.
*/
protected createDatabaseDirectory(fullPath: string): Promise<void> {
const mkdirp = PlatformTools.load("mkdirp");
const path = PlatformTools.load("path");
return mkdirp(path.dirname(fullPath));
}

}

0 comments on commit c6ae601

Please sign in to comment.