2
0
mirror of https://github.com/frappe/books.git synced 2024-12-22 19:09:01 +00:00

chore: switch to better-sqlite3

This commit is contained in:
18alantom 2022-05-20 16:42:32 +05:30
parent ca5514417e
commit 905829ca20
12 changed files with 696 additions and 119 deletions

View File

@ -65,7 +65,7 @@ export class BespokeQueries {
.select('name') .select('name')
.where('accountType', 'in', ['Cash', 'Bank']) .where('accountType', 'in', ['Cash', 'Bank'])
.andWhere('isGroup', false); .andWhere('isGroup', false);
const dateAsMonthYear = db.knex!.raw('strftime("%Y-%m", ??)', 'date'); const dateAsMonthYear = db.knex!.raw(`strftime('%Y-%m', ??)`, 'date');
return await db.knex!('AccountingLedgerEntry') return await db.knex!('AccountingLedgerEntry')
.where('reverted', false) .where('reverted', false)
.sum({ .sum({

View File

@ -59,16 +59,10 @@ export default class DatabaseCore extends DatabaseBase {
super(); super();
this.dbPath = dbPath ?? ':memory:'; this.dbPath = dbPath ?? ':memory:';
this.connectionParams = { this.connectionParams = {
client: 'sqlite3', client: 'better-sqlite3',
connection: { connection: {
filename: this.dbPath, filename: this.dbPath,
}, },
pool: {
afterCreate(conn: { run: (query: string) => void }, done: () => void) {
conn.run('PRAGMA foreign_keys=ON');
done();
},
},
useNullAsDefault: true, useNullAsDefault: true,
asyncStackTraces: process.env.NODE_ENV === 'development', asyncStackTraces: process.env.NODE_ENV === 'development',
}; };
@ -77,7 +71,7 @@ export default class DatabaseCore extends DatabaseBase {
static async getCountryCode(dbPath: string): Promise<string> { static async getCountryCode(dbPath: string): Promise<string> {
let countryCode = 'in'; let countryCode = 'in';
const db = new DatabaseCore(dbPath); const db = new DatabaseCore(dbPath);
db.connect(); await db.connect();
let query: { value: string }[] = []; let query: { value: string }[] = [];
try { try {
@ -101,11 +95,12 @@ export default class DatabaseCore extends DatabaseBase {
this.schemaMap = schemaMap; this.schemaMap = schemaMap;
} }
connect() { async connect() {
this.knex = knex(this.connectionParams); this.knex = knex(this.connectionParams);
this.knex.on('query-error', (error) => { this.knex.on('query-error', (error) => {
error.type = this.#getError(error); error.type = this.#getError(error);
}); });
await this.knex.raw('PRAGMA foreign_keys=ON');
} }
async close() { async close() {
@ -113,8 +108,13 @@ export default class DatabaseCore extends DatabaseBase {
} }
async commit() { async commit() {
/**
* this auto commits, commit is not required
* will later wrap the outermost functions in
* transactions.
*/
try { try {
await this.knex!.raw('commit'); // await this.knex!.raw('commit');
} catch (err) { } catch (err) {
const type = this.#getError(err as Error); const type = this.#getError(err as Error);
if (type !== CannotCommitError) { if (type !== CannotCommitError) {

View File

@ -29,7 +29,7 @@ export class DatabaseManager extends DatabaseDemuxBase {
countryCode ??= await DatabaseCore.getCountryCode(dbPath); countryCode ??= await DatabaseCore.getCountryCode(dbPath);
this.db = new DatabaseCore(dbPath); this.db = new DatabaseCore(dbPath);
this.db.connect(); await this.db.connect();
const schemaMap = getSchemas(countryCode); const schemaMap = getSchemas(countryCode);
this.db.setSchemaMap(schemaMap); this.db.setSchemaMap(schemaMap);

View File

@ -35,8 +35,8 @@ describe('DatabaseCore: Connect Migrate Close', function () {
assert.strictEqual(schemaMap, db.schemaMap); assert.strictEqual(schemaMap, db.schemaMap);
}); });
specify('connect', function () { specify('connect', async function () {
assert.doesNotThrow(() => db.connect()); await assertDoesNotThrow(async () => await db.connect());
assert.notStrictEqual(db.knex, undefined); assert.notStrictEqual(db.knex, undefined);
}); });
@ -54,7 +54,7 @@ describe('DatabaseCore: Migrate and Check Db', function () {
this.beforeEach(async function () { this.beforeEach(async function () {
db = new DatabaseCore(); db = new DatabaseCore();
db.connect(); await db.connect();
db.setSchemaMap(schemaMap); db.setSchemaMap(schemaMap);
}); });
@ -147,7 +147,7 @@ describe('DatabaseCore: CRUD', function () {
this.beforeEach(async function () { this.beforeEach(async function () {
db = new DatabaseCore(); db = new DatabaseCore();
db.connect(); await db.connect();
db.setSchemaMap(schemaMap); db.setSchemaMap(schemaMap);
await db.migrate(); await db.migrate();
}); });

View File

@ -261,7 +261,7 @@ function toDocCheck(value: RawValue, field: Field): boolean {
} }
if (typeof value === 'string') { if (typeof value === 'string') {
return value === '1'; return !!parseFloat(value);
} }
if (typeof value === 'number') { if (typeof value === 'number') {

View File

@ -17,8 +17,7 @@ export class DatabaseDemux extends DatabaseDemuxBase {
if (response.error?.name) { if (response.error?.name) {
const { name, message, stack } = response.error; const { name, message, stack } = response.error;
const dberror = new DatabaseError(message); const dberror = new DatabaseError(`${name}\n${message}`);
dberror.name = name;
dberror.stack = stack; dberror.stack = stack;
throw dberror; throw dberror;

View File

@ -1,13 +1,9 @@
import { t } from './translation';
export class BaseError extends Error { export class BaseError extends Error {
label: string;
message: string; message: string;
statusCode: number; statusCode: number;
constructor(statusCode: number, message: string) { constructor(statusCode: number, message: string) {
super(message); super(message);
this.label = t`Base Error`;
this.name = 'BaseError'; this.name = 'BaseError';
this.statusCode = statusCode; this.statusCode = statusCode;
this.message = message; this.message = message;
@ -17,7 +13,6 @@ export class BaseError extends Error {
export class ValidationError extends BaseError { export class ValidationError extends BaseError {
constructor(message: string) { constructor(message: string) {
super(417, message); super(417, message);
this.label = t`Validation Error`;
this.name = 'ValidationError'; this.name = 'ValidationError';
} }
} }
@ -25,7 +20,6 @@ export class ValidationError extends BaseError {
export class NotFoundError extends BaseError { export class NotFoundError extends BaseError {
constructor(message: string) { constructor(message: string) {
super(404, message); super(404, message);
this.label = t`Not Found Error`;
this.name = 'NotFoundError'; this.name = 'NotFoundError';
} }
} }
@ -33,7 +27,6 @@ export class NotFoundError extends BaseError {
export class ForbiddenError extends BaseError { export class ForbiddenError extends BaseError {
constructor(message: string) { constructor(message: string) {
super(403, message); super(403, message);
this.label = t`Forbidden Error`;
this.name = 'ForbiddenError'; this.name = 'ForbiddenError';
} }
} }
@ -41,7 +34,6 @@ export class ForbiddenError extends BaseError {
export class DuplicateEntryError extends ValidationError { export class DuplicateEntryError extends ValidationError {
constructor(message: string) { constructor(message: string) {
super(message); super(message);
this.label = t`Duplicate Entry Error`;
this.name = 'DuplicateEntryError'; this.name = 'DuplicateEntryError';
} }
} }
@ -49,7 +41,6 @@ export class DuplicateEntryError extends ValidationError {
export class LinkValidationError extends ValidationError { export class LinkValidationError extends ValidationError {
constructor(message: string) { constructor(message: string) {
super(message); super(message);
this.label = t`Link Validation Error`;
this.name = 'LinkValidationError'; this.name = 'LinkValidationError';
} }
} }
@ -57,7 +48,6 @@ export class LinkValidationError extends ValidationError {
export class MandatoryError extends ValidationError { export class MandatoryError extends ValidationError {
constructor(message: string) { constructor(message: string) {
super(message); super(message);
this.label = t`Mandatory Error`;
this.name = 'MandatoryError'; this.name = 'MandatoryError';
} }
} }
@ -65,7 +55,6 @@ export class MandatoryError extends ValidationError {
export class DatabaseError extends BaseError { export class DatabaseError extends BaseError {
constructor(message: string) { constructor(message: string) {
super(500, message); super(500, message);
this.label = t`Database Error`;
this.name = 'DatabaseError'; this.name = 'DatabaseError';
} }
} }
@ -73,7 +62,6 @@ export class DatabaseError extends BaseError {
export class CannotCommitError extends DatabaseError { export class CannotCommitError extends DatabaseError {
constructor(message: string) { constructor(message: string) {
super(message); super(message);
this.label = t`Cannot Commit Error`;
this.name = 'CannotCommitError'; this.name = 'CannotCommitError';
} }
} }
@ -81,7 +69,6 @@ export class CannotCommitError extends DatabaseError {
export class NotImplemented extends BaseError { export class NotImplemented extends BaseError {
constructor() { constructor() {
super(501, ''); super(501, '');
this.label = t`Not Implemented`;
this.name = 'NotImplemented'; this.name = 'NotImplemented';
} }
} }

View File

@ -11,25 +11,26 @@
"serve": "vue-cli-service serve", "serve": "vue-cli-service serve",
"build": "vue-cli-service build", "build": "vue-cli-service build",
"lint": "vue-cli-service lint", "lint": "vue-cli-service lint",
"postinstall": "electron-rebuild",
"postuninstall": "electron-rebuild",
"electron:build": "vue-cli-service electron:build", "electron:build": "vue-cli-service electron:build",
"electron:serve": "vue-cli-service electron:serve", "electron:serve": "vue-cli-service electron:serve",
"postinstall": "electron-builder install-app-deps", "script:translate": "scripts/runner.sh scripts/generateTranslations.ts",
"postuninstall": "electron-builder install-app-deps", "script:profile": "scripts/profile.sh",
"script:translate": "ts-node -O '{\"module\":\"commonjs\"}' scripts/generateTranslations.ts", "test": "scripts/runner.sh ./node_modules/.bin/mocha ./**/tests/**/*.spec.ts --exit"
"test": "TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' mocha --reporter nyan --require ts-node/register --require tsconfig-paths/register ./**/tests/**/*.spec.ts --exit"
}, },
"dependencies": { "dependencies": {
"@popperjs/core": "^2.10.2", "@popperjs/core": "^2.10.2",
"better-sqlite3": "^7.5.3",
"core-js": "^3.19.0", "core-js": "^3.19.0",
"csvjson-csv2json": "^5.0.6", "csvjson-csv2json": "^5.0.6",
"electron-store": "^8.0.1", "electron-store": "^8.0.1",
"feather-icons": "^4.28.0", "feather-icons": "^4.28.0",
"knex": "^0.95.12", "knex": "^2.0.0",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"luxon": "^2.0.2", "luxon": "^2.0.2",
"node-fetch": "2", "node-fetch": "2",
"pesa": "^1.1.11", "pesa": "^1.1.11",
"sqlite3": "npm:@vscode/sqlite3@^5.0.7",
"vue": "^3.2.30", "vue": "^3.2.30",
"vue-router": "^4.0.12" "vue-router": "^4.0.12"
}, },
@ -57,6 +58,7 @@
"electron-builder": "^23.0.3", "electron-builder": "^23.0.3",
"electron-devtools-installer": "^3.2.0", "electron-devtools-installer": "^3.2.0",
"electron-notarize": "^1.1.1", "electron-notarize": "^1.1.1",
"electron-rebuild": "^3.2.7",
"electron-updater": "^4.3.9", "electron-updater": "^4.3.9",
"eslint": "^7.32.0", "eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0", "eslint-config-prettier": "^8.3.0",

View File

@ -1,4 +1,4 @@
#! /bin/sh #! /usr/bin/env zsh
# https://nodejs.org/en/docs/guides/simple-profiling/ # https://nodejs.org/en/docs/guides/simple-profiling/
@ -7,9 +7,12 @@ export TS_NODE_COMPILER_OPTIONS='{"module":"commonjs"}'
rm ./isolate-*-v8.log 2> /dev/null rm ./isolate-*-v8.log 2> /dev/null
rm ./profiler-output.log 2> /dev/null rm ./profiler-output.log 2> /dev/null
export ELECTRON_RUN_AS_NODE=true
alias electron_node=./node_modules/.bin/electron
echo "running profile.ts" echo "running profile.ts"
node --require ts-node/register --require tsconfig-paths/register --prof ./scripts/profile.ts electron_node --require ts-node/register --require tsconfig-paths/register --prof ./scripts/profile.ts
echo "processing tick file" echo "processing tick file"
node --prof-process ./isolate-*-v8.log > ./profiler-output.log && echo "generated profiler-output.log" electron_node --prof-process ./isolate-*-v8.log > ./profiler-output.log && echo "generated profiler-output.log"
rm ./isolate-*-v8.log rm ./isolate-*-v8.log

10
scripts/runner.sh Executable file
View File

@ -0,0 +1,10 @@
#! /usr/bin/env zsh
# basically uses electron's node to prevent
# mismatch in NODE_MODULE_VERSION when running
# better-sqlite3
export TS_NODE_COMPILER_OPTIONS='{"module":"commonjs"}'
export ELECTRON_RUN_AS_NODE=true
alias electron_node="./node_modules/.bin/electron --require ts-node/register --require tsconfig-paths/register"
electron_node $@

View File

@ -3,7 +3,7 @@ import { t } from 'fyo';
import { ConfigKeys } from 'fyo/core/types'; import { ConfigKeys } from 'fyo/core/types';
import { Doc } from 'fyo/model/doc'; import { Doc } from 'fyo/model/doc';
import { TelemetrySetting } from 'fyo/telemetry/types'; import { TelemetrySetting } from 'fyo/telemetry/types';
import { BaseError, MandatoryError, ValidationError } from 'fyo/utils/errors'; import { MandatoryError, ValidationError } from 'fyo/utils/errors';
import { ErrorLog } from 'fyo/utils/types'; import { ErrorLog } from 'fyo/utils/types';
import { IPC_ACTIONS, IPC_MESSAGES } from 'utils/messages'; import { IPC_ACTIONS, IPC_MESSAGES } from 'utils/messages';
import { fyo } from './initFyo'; import { fyo } from './initFyo';
@ -45,7 +45,7 @@ async function reportError(errorLogObj: ErrorLog, cb?: Function) {
function getToastProps(errorLogObj: ErrorLog, canLog: boolean, cb?: Function) { function getToastProps(errorLogObj: ErrorLog, canLog: boolean, cb?: Function) {
const props: ToastOptions = { const props: ToastOptions = {
message: t`Error: ` + errorLogObj.name, message: errorLogObj.name ?? t`Error`,
type: 'error', type: 'error',
}; };
@ -93,7 +93,6 @@ export function handleError(
const errorLogObj = getErrorLogObject(error, more ?? {}); const errorLogObj = getErrorLogObject(error, more ?? {});
// @ts-ignore
const canLog = getCanLog(); const canLog = getCanLog();
if (canLog) { if (canLog) {
reportError(errorLogObj, cb); reportError(errorLogObj, cb);
@ -106,7 +105,7 @@ export async function handleErrorWithDialog(error: Error, doc?: Doc) {
const errorMessage = getErrorMessage(error, doc); const errorMessage = getErrorMessage(error, doc);
handleError(false, error, { errorMessage, doc }); handleError(false, error, { errorMessage, doc });
const name = (error as BaseError).label ?? error.name; const name = error.name ?? t`Error`;
await showMessageDialog({ message: name, detail: errorMessage }); await showMessageDialog({ message: name, detail: errorMessage });
throw error; throw error;
} }

725
yarn.lock

File diff suppressed because it is too large Load Diff