2
0
mirror of https://github.com/frappe/books.git synced 2024-11-14 01:14:03 +00:00
books/backend/helpers.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

88 lines
1.8 KiB
TypeScript
Raw Normal View History

import { constants } from 'fs';
import fs from 'fs/promises';
2022-03-31 07:18:32 +00:00
import { DatabaseMethod } from 'utils/db/types';
import { CUSTOM_EVENTS } from 'utils/messages';
2022-03-25 10:12:39 +00:00
import { KnexColumnType } from './database/types';
export const sqliteTypeMap: Record<string, KnexColumnType> = {
2022-03-23 06:16:13 +00:00
AutoComplete: 'text',
Currency: 'text',
Int: 'integer',
Float: 'float',
Percent: 'float',
2022-03-25 10:12:39 +00:00
Check: 'boolean',
2022-03-23 06:16:13 +00:00
Code: 'text',
2022-03-25 10:12:39 +00:00
Date: 'date',
Datetime: 'datetime',
Time: 'time',
2022-03-23 06:16:13 +00:00
Text: 'text',
Data: 'text',
Link: 'text',
DynamicLink: 'text',
Password: 'text',
Select: 'text',
2022-10-13 11:25:34 +00:00
Attachment: 'text',
2022-03-23 06:16:13 +00:00
AttachImage: 'text',
Color: 'text',
};
export const SYSTEM = '__SYSTEM__';
2022-03-23 06:16:13 +00:00
export const validTypes = Object.keys(sqliteTypeMap);
export function getDefaultMetaFieldValueMap() {
const now = new Date().toISOString();
return {
createdBy: SYSTEM,
modifiedBy: SYSTEM,
created: now,
modified: now,
};
}
2022-03-31 07:18:32 +00:00
export const databaseMethodSet: Set<DatabaseMethod> = new Set([
'insert',
'get',
'getAll',
'getSingleValues',
'rename',
'update',
'delete',
'deleteAll',
2022-03-31 07:18:32 +00:00
'close',
'exists',
]);
export function emitMainProcessError(
error: unknown,
more?: Record<string, unknown>
) {
(
process.emit as (
event: string,
error: unknown,
more?: Record<string, unknown>
) => void
)(CUSTOM_EVENTS.MAIN_PROCESS_ERROR, error, more);
}
export async function checkFileAccess(filePath: string, mode?: number) {
mode ??= constants.W_OK;
return await fs
.access(filePath, mode)
.then(() => true)
.catch(() => false);
}
export async function unlinkIfExists(filePath: unknown) {
if (!filePath || typeof filePath !== 'string') {
return false;
}
const exists = await checkFileAccess(filePath);
if (exists) {
await fs.unlink(filePath);
return true;
}
return false;
}