2
0
mirror of https://github.com/frappe/books.git synced 2024-11-15 09:54:04 +00:00
books/backend/database/core.ts

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

993 lines
27 KiB
TypeScript
Raw Normal View History

import { getDbError, NotFoundError, ValueError } from 'fyo/utils/errors';
import { knex, Knex } from 'knex';
import {
Field,
FieldTypeEnum,
RawValue,
Schema,
SchemaMap,
2022-07-30 11:03:09 +00:00
TargetField,
} from '../../schemas/types';
import {
getIsNullOrUndef,
getRandomString,
2022-07-30 11:03:09 +00:00
getValueMapFromList,
} from '../../utils';
2022-03-31 07:33:58 +00:00
import { DatabaseBase, GetAllOptions, QueryFilter } from '../../utils/db/types';
2022-03-31 07:18:32 +00:00
import { getDefaultMetaFieldValueMap, sqliteTypeMap, SYSTEM } from '../helpers';
import {
2023-07-11 07:11:22 +00:00
AlterConfig,
ColumnDiff,
FieldValueMap,
GetQueryBuilderOptions,
2023-07-11 07:11:22 +00:00
MigrationConfig,
NonExtantConfig,
2022-07-30 11:03:09 +00:00
SingleValue,
2023-07-11 07:11:22 +00:00
UpdateSinglesConfig,
} from './types';
2022-03-28 10:01:29 +00:00
/**
* # DatabaseCore
* This is the ORM, the DatabaseCore interface (function signatures) should be
* replicated by the frontend demuxes and all the backend muxes.
*
* ## Db Core Call Sequence
2022-03-28 10:01:29 +00:00
*
* 1. Init core: `const db = new DatabaseCore(dbPath)`.
* 2. Connect db: `db.connect()`. This will allow for raw queries to be executed.
* 3. Set schemas: `db.setSchemaMap(schemaMap)`. This will allow for ORM functions to be executed.
2022-03-28 10:01:29 +00:00
* 4. Migrate: `await db.migrate()`. This will create absent tables and update the tables' shape.
* 5. ORM function execution: `db.get(...)`, `db.insert(...)`, etc.
* 6. Close connection: `await db.close()`.
*
* Note: Meta values: created, modified, createdBy, modifiedBy are set by DatabaseCore
* only for schemas that are SingleValue. Else they have to be passed by the caller in
* the `fieldValueMap`.
2022-03-28 10:01:29 +00:00
*/
2022-03-31 07:18:32 +00:00
export default class DatabaseCore extends DatabaseBase {
2022-03-25 10:12:39 +00:00
knex?: Knex;
2022-03-23 06:16:13 +00:00
typeMap = sqliteTypeMap;
dbPath: string;
schemaMap: SchemaMap = {};
connectionParams: Knex.Config;
2022-03-23 06:16:13 +00:00
constructor(dbPath?: string) {
2022-03-31 07:18:32 +00:00
super();
this.dbPath = dbPath ?? ':memory:';
2022-03-23 06:16:13 +00:00
this.connectionParams = {
2022-05-20 11:12:32 +00:00
client: 'better-sqlite3',
2022-03-23 06:16:13 +00:00
connection: {
filename: this.dbPath,
},
useNullAsDefault: true,
asyncStackTraces: process.env.NODE_ENV === 'development',
};
}
static async getCountryCode(dbPath: string): Promise<string> {
let countryCode = 'in';
const db = new DatabaseCore(dbPath);
2022-05-20 11:12:32 +00:00
await db.connect();
let query: { value: string }[] = [];
try {
query = (await db.knex!('SingleValue').where({
fieldname: 'countryCode',
parent: 'SystemSettings',
})) as { value: string }[];
} catch {
// Database not inialized and no countryCode passed
}
if (query.length > 0) {
countryCode = query[0].value;
}
await db.close();
return countryCode;
}
setSchemaMap(schemaMap: SchemaMap) {
this.schemaMap = schemaMap;
}
2022-05-20 11:12:32 +00:00
async connect() {
2022-03-23 06:16:13 +00:00
this.knex = knex(this.connectionParams);
2022-05-20 11:12:32 +00:00
await this.knex.raw('PRAGMA foreign_keys=ON');
2022-03-23 06:16:13 +00:00
}
2022-03-28 10:01:29 +00:00
async close() {
await this.knex!.destroy();
2022-03-23 06:16:13 +00:00
}
2023-07-11 07:11:22 +00:00
async migrate(config: MigrationConfig = {}) {
const { create, alter } = await this.#getCreateAlterList();
const hasSingleValueTable = !create.includes('SingleValue');
let singlesConfig: UpdateSinglesConfig = {
update: [],
updateNonExtant: [],
};
if (hasSingleValueTable) {
singlesConfig = await this.#getSinglesUpdateList();
}
const shouldMigrate = !!(
create.length ||
alter.length ||
singlesConfig.update.length ||
singlesConfig.updateNonExtant.length
);
if (!shouldMigrate) {
return;
}
await config.pre?.();
for (const schemaName of create) {
await this.#createTable(schemaName);
}
for (const config of alter) {
await this.#alterTable(config);
}
if (!hasSingleValueTable) {
singlesConfig = await this.#getSinglesUpdateList();
}
await this.#initializeSingles(singlesConfig);
await config.post?.();
}
async #getCreateAlterList() {
const create: string[] = [];
const alter: AlterConfig[] = [];
for (const [schemaName, schema] of Object.entries(this.schemaMap)) {
if (!schema || schema.isSingle) {
continue;
}
2023-07-11 07:11:22 +00:00
const exists = await this.#tableExists(schemaName);
if (!exists) {
create.push(schemaName);
continue;
}
const diff: ColumnDiff = await this.#getColumnDiff(schemaName);
const newForeignKeys: Field[] = await this.#getNewForeignKeys(schemaName);
if (diff.added.length || diff.removed.length || newForeignKeys.length) {
alter.push({
schemaName,
diff,
newForeignKeys,
});
}
}
2023-07-11 07:11:22 +00:00
return { create, alter };
}
async exists(schemaName: string, name?: string): Promise<boolean> {
const schema = this.schemaMap[schemaName] as Schema;
if (schema.isSingle) {
return this.#singleExists(schemaName);
}
let row = [];
try {
2022-03-25 10:12:39 +00:00
const qb = this.knex!(schemaName);
if (name !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
qb.where({ name });
}
row = await qb.limit(1);
} catch (err) {
if (getDbError(err as Error) !== NotFoundError) {
throw err;
}
}
return row.length > 0;
}
2022-03-31 07:18:32 +00:00
async insert(
schemaName: string,
fieldValueMap: FieldValueMap
): Promise<FieldValueMap> {
// insert parent
if (this.schemaMap[schemaName]!.isSingle) {
await this.#updateSingleValues(schemaName, fieldValueMap);
} else {
await this.#insertOne(schemaName, fieldValueMap);
}
// insert children
await this.#insertOrUpdateChildren(schemaName, fieldValueMap, false);
return fieldValueMap;
}
async get(
schemaName: string,
name = '',
fields?: string | string[]
): Promise<FieldValueMap> {
const schema = this.schemaMap[schemaName] as Schema;
if (!schema.isSingle && !name) {
throw new ValueError('name is mandatory');
}
/**
* If schema is single return all the values
* of the single type schema, in this case field
* is ignored.
*/
let fieldValueMap: FieldValueMap = {};
if (schema.isSingle) {
return await this.#getSingle(schemaName);
}
if (typeof fields === 'string') {
fields = [fields];
}
if (fields === undefined) {
fields = schema.fields.filter((f) => !f.computed).map((f) => f.fieldname);
}
/**
* Separate table fields and non table fields
*/
const allTableFields: TargetField[] = this.#getTableFields(schemaName);
const allTableFieldNames: string[] = allTableFields.map((f) => f.fieldname);
const tableFields: TargetField[] = allTableFields.filter((f) =>
fields!.includes(f.fieldname)
);
const nonTableFieldNames: string[] = fields.filter(
(f) => !allTableFieldNames.includes(f)
);
/**
* If schema is not single then return specific fields
* if child fields are selected, all child fields are returned.
*/
if (nonTableFieldNames.length) {
fieldValueMap =
(await this.#getOne(schemaName, name, nonTableFieldNames)) ?? {};
}
if (tableFields.length) {
2022-05-01 04:45:47 +00:00
await this.#loadChildren(name, fieldValueMap, tableFields);
}
return fieldValueMap;
}
async getAll(
schemaName: string,
options: GetAllOptions = {}
): Promise<FieldValueMap[]> {
const schema = this.schemaMap[schemaName] as Schema;
if (schema === undefined) {
throw new NotFoundError(`schema ${schemaName} not found`);
}
const hasCreated = !!schema.fields.find((f) => f.fieldname === 'created');
const {
fields = ['name'],
filters,
offset,
limit,
groupBy,
orderBy = hasCreated ? 'created' : undefined,
order = 'desc',
} = options;
return (await this.#getQueryBuilder(
schemaName,
typeof fields === 'string' ? [fields] : fields,
filters ?? {},
{
offset,
limit,
groupBy,
orderBy,
order,
}
)) as FieldValueMap[];
}
async deleteAll(schemaName: string, filters: QueryFilter): Promise<number> {
const builder = this.knex!(schemaName);
this.#applyFiltersToBuilder(builder, filters);
return await builder.delete();
}
async getSingleValues(
...fieldnames: ({ fieldname: string; parent?: string } | string)[]
): Promise<SingleValue<RawValue>> {
const fieldnameList = fieldnames.map((fieldname) => {
if (typeof fieldname === 'string') {
return { fieldname };
}
return fieldname;
});
2022-03-25 10:12:39 +00:00
let builder = this.knex!('SingleValue');
builder = builder.where(fieldnameList[0]);
fieldnameList.slice(1).forEach(({ fieldname, parent }) => {
if (typeof parent === 'undefined') {
builder = builder.orWhere({ fieldname });
} else {
builder = builder.orWhere({ fieldname, parent });
}
});
let values: { fieldname: string; parent: string; value: RawValue }[] = [];
try {
values = await builder.select('fieldname', 'value', 'parent');
} catch (err) {
if (getDbError(err as Error) === NotFoundError) {
return [];
}
throw err;
}
return values;
}
async rename(schemaName: string, oldName: string, newName: string) {
/**
* Rename is expensive mostly won't allow it.
* TODO: rename all links
* TODO: rename in childtables
*/
2022-03-25 10:12:39 +00:00
await this.knex!(schemaName)
.update({ name: newName })
.where('name', oldName);
}
async update(schemaName: string, fieldValueMap: FieldValueMap) {
// update parent
if (this.schemaMap[schemaName]!.isSingle) {
await this.#updateSingleValues(schemaName, fieldValueMap);
} else {
await this.#updateOne(schemaName, fieldValueMap);
}
// insert or update children
await this.#insertOrUpdateChildren(schemaName, fieldValueMap, true);
}
async delete(schemaName: string, name: string) {
const schema = this.schemaMap[schemaName] as Schema;
if (schema.isSingle) {
await this.#deleteSingle(schemaName, name);
return;
}
await this.#deleteOne(schemaName, name);
// delete children
const tableFields = this.#getTableFields(schemaName);
for (const field of tableFields) {
await this.#deleteChildren(field.target, name);
}
}
2023-05-04 10:45:12 +00:00
async #tableExists(schemaName: string): Promise<boolean> {
2022-03-25 10:12:39 +00:00
return await this.knex!.schema.hasTable(schemaName);
2022-03-23 06:16:13 +00:00
}
2023-05-04 10:45:12 +00:00
async #singleExists(singleSchemaName: string): Promise<boolean> {
2022-03-25 10:12:39 +00:00
const res = await this.knex!('SingleValue')
.count('parent as count')
.where('parent', singleSchemaName)
.first();
2023-05-04 10:45:12 +00:00
if (typeof res?.count === 'number') {
return res.count > 0;
}
return false;
2022-03-23 06:16:13 +00:00
}
2022-10-16 11:37:06 +00:00
async #dropColumns(schemaName: string, targetColumns: string[]) {
await this.knex!.schema.table(schemaName, (table) => {
table.dropColumns(...targetColumns);
});
}
2022-03-23 06:16:13 +00:00
async prestigeTheTable(schemaName: string, tableRows: FieldValueMap[]) {
// Alter table hacx for sqlite in case of schema change.
const tempName = `__${schemaName}`;
2022-10-16 11:37:06 +00:00
// Create replacement table
await this.knex!.schema.dropTableIfExists(tempName);
2022-03-25 10:12:39 +00:00
await this.knex!.raw('PRAGMA foreign_keys=OFF');
await this.#createTable(schemaName, tempName);
2022-10-16 11:37:06 +00:00
// Insert rows from source table into the replacement table
await this.knex!.batchInsert(tempName, tableRows, 200);
2022-10-16 11:37:06 +00:00
// Replace with the replacement table
2022-03-25 10:12:39 +00:00
await this.knex!.schema.dropTable(schemaName);
await this.knex!.schema.renameTable(tempName, schemaName);
await this.knex!.raw('PRAGMA foreign_keys=ON');
2022-03-23 06:16:13 +00:00
}
async #getTableColumns(schemaName: string): Promise<string[]> {
2022-03-28 10:01:29 +00:00
const info: FieldValueMap[] = await this.knex!.raw(
2022-03-25 10:12:39 +00:00
`PRAGMA table_info(${schemaName})`
);
return info.map((d) => d.name as string);
}
2022-03-23 06:16:13 +00:00
async truncate(tableNames?: string[]) {
if (tableNames === undefined) {
const q = (await this.knex!.raw(`
select name from sqlite_schema
where type='table'
and name not like 'sqlite_%'`)) as { name: string }[];
tableNames = q.map((i) => i.name);
}
for (const name of tableNames) {
await this.knex!(name).del();
}
}
async #getForeignKeys(schemaName: string): Promise<string[]> {
2022-03-28 10:01:29 +00:00
const foreignKeyList: FieldValueMap[] = await this.knex!.raw(
`PRAGMA foreign_key_list(${schemaName})`
);
2022-03-25 10:12:39 +00:00
return foreignKeyList.map((d) => d.from as string);
}
2022-03-23 06:16:13 +00:00
#getQueryBuilder(
schemaName: string,
fields: string[],
filters: QueryFilter,
options: GetQueryBuilderOptions
): Knex.QueryBuilder {
/* eslint-disable @typescript-eslint/no-floating-promises */
2022-03-25 10:12:39 +00:00
const builder = this.knex!.select(fields).from(schemaName);
this.#applyFiltersToBuilder(builder, filters);
2023-01-05 05:44:05 +00:00
const { orderBy, groupBy, order } = options;
if (Array.isArray(orderBy)) {
builder.orderBy(orderBy.map((column) => ({ column, order })));
2022-03-23 06:16:13 +00:00
}
2023-01-05 05:44:05 +00:00
if (typeof orderBy === 'string') {
builder.orderBy(orderBy, order);
}
if (Array.isArray(groupBy)) {
builder.groupBy(...groupBy);
}
if (typeof groupBy === 'string') {
builder.groupBy(groupBy);
2022-03-23 06:16:13 +00:00
}
if (options.offset) {
builder.offset(options.offset);
2022-03-23 06:16:13 +00:00
}
if (options.limit) {
builder.limit(options.limit);
}
return builder;
}
#applyFiltersToBuilder(builder: Knex.QueryBuilder, filters: QueryFilter) {
// {"status": "Open"} => `status = "Open"`
// {"status": "Open", "name": ["like", "apple%"]}
// => `status="Open" and name like "apple%"
// {"date": [">=", "2017-09-09", "<=", "2017-11-01"]}
// => `date >= 2017-09-09 and date <= 2017-11-01`
const filtersArray = this.#getFiltersArray(filters);
for (let i = 0; i < filtersArray.length; i++) {
const filter = filtersArray[i];
const field = filter[0] as string;
const operator = filter[1];
const comparisonValue = filter[2];
const type = i === 0 ? 'where' : 'andWhere';
if (operator === '=') {
builder[type](field, comparisonValue);
} else if (
operator === 'in' &&
(comparisonValue as (string | null)[]).includes(null)
) {
const nonNulls = (comparisonValue as (string | null)[]).filter(
Boolean
) as string[];
builder[type](field, operator, nonNulls).orWhere(field, null);
} else {
builder[type](field, operator as string, comparisonValue as string);
}
}
}
#getFiltersArray(filters: QueryFilter) {
const filtersArray = [];
for (const field in filters) {
const value = filters[field];
2022-04-14 06:43:58 +00:00
let operator: string | number = '=';
let comparisonValue = value as string | number | (string | number)[];
if (Array.isArray(value)) {
operator = (value[0] as string).toLowerCase();
2022-04-14 06:43:58 +00:00
comparisonValue = value[1] as string | number | (string | number)[];
if (operator === 'includes') {
operator = 'like';
}
2022-04-14 06:43:58 +00:00
if (
operator === 'like' &&
typeof comparisonValue === 'string' &&
!comparisonValue.includes('%')
2022-04-14 06:43:58 +00:00
) {
comparisonValue = `%${comparisonValue}%`;
}
}
filtersArray.push([field, operator, comparisonValue]);
if (Array.isArray(value) && value.length > 2) {
// multiple conditions
const operator = value[2];
const comparisonValue = value[3];
filtersArray.push([field, operator, comparisonValue]);
}
2022-03-23 06:16:13 +00:00
}
return filtersArray;
2022-03-23 06:16:13 +00:00
}
async #getColumnDiff(schemaName: string): Promise<ColumnDiff> {
const tableColumns = await this.#getTableColumns(schemaName);
2022-07-14 18:00:24 +00:00
const validFields = this.schemaMap[schemaName]!.fields.filter(
(f) => !f.computed
);
const diff: ColumnDiff = { added: [], removed: [] };
2022-03-23 06:16:13 +00:00
for (const field of validFields) {
2022-03-25 10:12:39 +00:00
const hasDbType = this.typeMap.hasOwnProperty(field.fieldtype);
if (!tableColumns.includes(field.fieldname) && hasDbType) {
2022-03-23 06:16:13 +00:00
diff.added.push(field);
}
}
const validFieldNames = validFields.map((field) => field.fieldname);
for (const column of tableColumns) {
if (!validFieldNames.includes(column)) {
diff.removed.push(column);
}
}
return diff;
}
2022-03-25 10:12:39 +00:00
async #getNewForeignKeys(schemaName: string): Promise<Field[]> {
const foreignKeys = await this.#getForeignKeys(schemaName);
const newForeignKeys: Field[] = [];
const schema = this.schemaMap[schemaName] as Schema;
for (const field of schema.fields) {
2022-03-23 06:16:13 +00:00
if (
field.fieldtype === 'Link' &&
!foreignKeys.includes(field.fieldname)
) {
newForeignKeys.push(field);
}
}
return newForeignKeys;
}
#buildColumnForTable(table: Knex.AlterTableBuilder, field: Field) {
if (field.fieldtype === FieldTypeEnum.Table) {
// In case columnType is "Table"
// childTable links are handled using the childTable's "parent" field
return;
2022-03-23 06:16:13 +00:00
}
const columnType = this.typeMap[field.fieldtype];
if (!columnType) {
2022-03-23 06:16:13 +00:00
return;
}
2022-03-25 10:12:39 +00:00
const column = table[columnType](
field.fieldname
) as Knex.SqlLiteColumnBuilder;
// primary key
if (field.fieldname === 'name') {
column.primary();
}
// iefault value
if (field.default !== undefined) {
column.defaultTo(field.default);
}
// required
2022-03-28 10:01:29 +00:00
if (field.required) {
column.notNullable();
}
// link
if (field.fieldtype === FieldTypeEnum.Link && field.target) {
const targetSchemaName = field.target;
const schema = this.schemaMap[targetSchemaName] as Schema;
table
.foreign(field.fieldname)
.references('name')
.inTable(schema.name)
.onUpdate('CASCADE')
.onDelete('RESTRICT');
}
2022-03-23 06:16:13 +00:00
}
2023-07-11 07:11:22 +00:00
async #alterTable({ schemaName, diff, newForeignKeys }: AlterConfig) {
2022-10-16 11:37:06 +00:00
await this.knex!.schema.table(schemaName, (table) => {
if (!diff.added.length) {
return;
2022-03-25 10:12:39 +00:00
}
2022-10-16 11:37:06 +00:00
for (const field of diff.added) {
this.#buildColumnForTable(table, field);
2022-03-25 10:12:39 +00:00
}
});
2022-10-16 11:37:06 +00:00
if (diff.removed.length) {
await this.#dropColumns(schemaName, diff.removed);
}
if (newForeignKeys.length) {
await this.#addForeignKeys(schemaName);
2022-10-16 11:37:06 +00:00
}
2022-03-23 06:16:13 +00:00
}
async #createTable(schemaName: string, tableName?: string) {
tableName ??= schemaName;
const fields = this.schemaMap[schemaName]!.fields.filter(
(f) => !f.computed
);
return await this.#runCreateTableQuery(tableName, fields);
}
#runCreateTableQuery(schemaName: string, fields: Field[]) {
2022-03-25 10:12:39 +00:00
return this.knex!.schema.createTable(schemaName, (table) => {
for (const field of fields) {
this.#buildColumnForTable(table, field);
}
2022-03-23 06:16:13 +00:00
});
}
async #getNonExtantSingleValues(singleSchemaName: string) {
const existingFields = (
(await this.knex!('SingleValue')
.where({ parent: singleSchemaName })
.select('fieldname')) as { fieldname: string }[]
).map(({ fieldname }) => fieldname);
2022-03-23 06:16:13 +00:00
2023-07-11 07:11:22 +00:00
const nonExtant: NonExtantConfig['nonExtant'] = [];
const fields = this.schemaMap[singleSchemaName]?.fields ?? [];
for (const { fieldname, default: value } of fields) {
if (existingFields.includes(fieldname) || value === undefined) {
continue;
}
nonExtant.push({ fieldname, value });
}
return nonExtant;
2022-03-23 06:16:13 +00:00
}
async #deleteOne(schemaName: string, name: string) {
return await this.knex!(schemaName).where('name', name).delete();
}
async #deleteSingle(schemaName: string, fieldname: string) {
return await this.knex!('SingleValue')
.where({ parent: schemaName, fieldname })
.delete();
2022-03-23 06:16:13 +00:00
}
#deleteChildren(schemaName: string, parentName: string) {
2022-03-25 10:12:39 +00:00
return this.knex!(schemaName).where('parent', parentName).delete();
}
#runDeleteOtherChildren(
field: TargetField,
parentName: string,
added: string[]
) {
2022-03-23 06:16:13 +00:00
// delete other children
2022-03-25 10:12:39 +00:00
return this.knex!(field.target)
.where('parent', parentName)
2022-03-23 06:16:13 +00:00
.andWhere('name', 'not in', added)
.delete();
}
#prepareChild(
parentSchemaName: string,
parentName: string,
child: FieldValueMap,
field: Field,
idx: number
) {
2022-03-23 06:16:13 +00:00
if (!child.name) {
child.name ??= getRandomString();
2022-03-23 06:16:13 +00:00
}
child.parent = parentName;
child.parentSchemaName = parentSchemaName;
child.parentFieldname = field.fieldname;
child.idx ??= idx;
2022-03-23 06:16:13 +00:00
}
async #addForeignKeys(schemaName: string) {
2022-10-16 11:37:06 +00:00
const tableRows = await this.knex!.select().from(schemaName);
await this.prestigeTheTable(schemaName, tableRows);
2022-03-23 06:16:13 +00:00
}
async #loadChildren(
2022-05-01 04:45:47 +00:00
parentName: string,
fieldValueMap: FieldValueMap,
tableFields: TargetField[]
) {
for (const field of tableFields) {
fieldValueMap[field.fieldname] = await this.getAll(field.target, {
fields: ['*'],
2022-05-01 04:45:47 +00:00
filters: { parent: parentName },
orderBy: 'idx',
order: 'asc',
});
}
2022-03-23 06:16:13 +00:00
}
async #getOne(schemaName: string, name: string, fields: string[]) {
const fieldValueMap = (await this.knex!.select(fields)
.from(schemaName)
.where('name', name)
.first()) as FieldValueMap;
return fieldValueMap;
}
2022-03-23 06:16:13 +00:00
async #getSingle(schemaName: string): Promise<FieldValueMap> {
const values = await this.getAll('SingleValue', {
fields: ['fieldname', 'value'],
filters: { parent: schemaName },
orderBy: 'fieldname',
order: 'asc',
});
2022-03-23 06:16:13 +00:00
2023-07-13 07:18:27 +00:00
const fieldValueMap = getValueMapFromList(
values,
'fieldname',
'value'
) as FieldValueMap;
const tableFields: TargetField[] = this.#getTableFields(schemaName);
if (tableFields.length) {
await this.#loadChildren(schemaName, fieldValueMap, tableFields);
}
return fieldValueMap;
}
2022-03-23 06:16:13 +00:00
#insertOne(schemaName: string, fieldValueMap: FieldValueMap) {
if (!fieldValueMap.name) {
fieldValueMap.name = getRandomString();
}
// Column fields
const fields = this.schemaMap[schemaName]!.fields.filter(
(f) => f.fieldtype !== FieldTypeEnum.Table && !f.computed
);
const validMap: FieldValueMap = {};
for (const { fieldname } of fields) {
validMap[fieldname] = fieldValueMap[fieldname];
}
return this.knex!(schemaName).insert(validMap);
2022-03-23 06:16:13 +00:00
}
async #updateSingleValues(
singleSchemaName: string,
fieldValueMap: FieldValueMap
) {
const fields = this.schemaMap[singleSchemaName]!.fields.filter(
2023-07-13 07:18:27 +00:00
(f) => !f.computed && f.fieldtype !== 'Table'
);
for (const field of fields) {
const value = fieldValueMap[field.fieldname] as RawValue | undefined;
if (value === undefined) {
continue;
}
await this.#updateSingleValue(singleSchemaName, field.fieldname, value);
}
2022-03-23 06:16:13 +00:00
}
async #updateSingleValue(
singleSchemaName: string,
fieldname: string,
value: RawValue
) {
const updateKey = {
parent: singleSchemaName,
fieldname,
};
const names = (await this.knex!('SingleValue')
.select('name')
.where(updateKey)) as { name: string }[];
if (!names?.length) {
this.#insertSingleValue(singleSchemaName, fieldname, value);
} else {
return await this.knex!('SingleValue').where(updateKey).update({
value,
modifiedBy: SYSTEM,
modified: new Date().toISOString(),
});
}
}
async #insertSingleValue(
singleSchemaName: string,
fieldname: string,
value: RawValue
) {
const updateMap = getDefaultMetaFieldValueMap();
const fieldValueMap: FieldValueMap = Object.assign({}, updateMap, {
parent: singleSchemaName,
fieldname,
value,
name: getRandomString(),
});
return await this.knex!('SingleValue').insert(fieldValueMap);
2022-03-23 06:16:13 +00:00
}
2023-07-11 07:11:22 +00:00
async #getSinglesUpdateList() {
const update: string[] = [];
const updateNonExtant: NonExtantConfig[] = [];
for (const [schemaName, schema] of Object.entries(this.schemaMap)) {
if (!schema || !schema.isSingle) {
continue;
}
2022-03-23 06:16:13 +00:00
2023-07-11 07:11:22 +00:00
const exists = await this.#singleExists(schemaName);
if (!exists && schema.fields.some((f) => f.default !== undefined)) {
update.push(schemaName);
}
if (!exists) {
continue;
}
2022-03-23 06:16:13 +00:00
2023-07-11 07:11:22 +00:00
const nonExtant = await this.#getNonExtantSingleValues(schemaName);
if (nonExtant.length) {
updateNonExtant.push({
schemaName,
nonExtant,
});
}
}
return { update, updateNonExtant };
}
async #initializeSingles({ update, updateNonExtant }: UpdateSinglesConfig) {
for (const config of updateNonExtant) {
await this.#updateNonExtantSingleValues(config);
}
for (const schemaName of update) {
const fields = this.schemaMap[schemaName]!.fields;
const defaultValues: FieldValueMap = fields.reduce((acc, f) => {
if (f.default !== undefined) {
acc[f.fieldname] = f.default;
2022-03-23 06:16:13 +00:00
}
return acc;
2022-03-25 10:12:39 +00:00
}, {} as FieldValueMap);
await this.#updateSingleValues(schemaName, defaultValues);
2022-03-23 06:16:13 +00:00
}
}
2022-03-23 06:16:13 +00:00
2023-07-11 07:11:22 +00:00
async #updateNonExtantSingleValues({
schemaName,
nonExtant,
}: NonExtantConfig) {
for (const { fieldname, value } of nonExtant) {
await this.#updateSingleValue(schemaName, fieldname, value);
}
2022-03-23 06:16:13 +00:00
}
async #updateOne(schemaName: string, fieldValueMap: FieldValueMap) {
const updateMap = { ...fieldValueMap };
delete updateMap.name;
const schema = this.schemaMap[schemaName] as Schema;
for (const { fieldname, fieldtype, computed } of schema.fields) {
if (fieldtype !== FieldTypeEnum.Table && !computed) {
continue;
}
delete updateMap[fieldname];
}
if (Object.keys(updateMap).length === 0) {
return;
}
2022-03-25 10:12:39 +00:00
return await this.knex!(schemaName)
.where('name', fieldValueMap.name as string)
.update(updateMap);
2022-03-23 06:16:13 +00:00
}
async #insertOrUpdateChildren(
schemaName: string,
fieldValueMap: FieldValueMap,
isUpdate: boolean
2022-03-23 06:16:13 +00:00
) {
2023-07-13 07:18:27 +00:00
let parentName = fieldValueMap.name as string;
if (this.schemaMap[schemaName]?.isSingle) {
parentName = schemaName;
}
const tableFields = this.#getTableFields(schemaName);
2022-03-23 06:16:13 +00:00
for (const field of tableFields) {
const added: string[] = [];
const tableFieldValue = fieldValueMap[field.fieldname] as
| FieldValueMap[]
| undefined
| null;
if (getIsNullOrUndef(tableFieldValue)) {
continue;
}
for (const child of tableFieldValue) {
this.#prepareChild(schemaName, parentName, child, field, added.length);
if (
isUpdate &&
(await this.exists(field.target, child.name as string))
) {
await this.#updateOne(field.target, child);
} else {
await this.#insertOne(field.target, child);
}
2022-03-23 06:16:13 +00:00
added.push(child.name as string);
}
2022-03-23 06:16:13 +00:00
if (isUpdate) {
await this.#runDeleteOtherChildren(field, parentName, added);
}
2022-03-23 06:16:13 +00:00
}
}
2022-03-23 06:16:13 +00:00
#getTableFields(schemaName: string): TargetField[] {
return this.schemaMap[schemaName]!.fields.filter(
(f) => f.fieldtype === FieldTypeEnum.Table
) as TargetField[];
}
2022-03-23 06:16:13 +00:00
}