2022-03-23 06:16:13 +00:00
|
|
|
import {
|
|
|
|
CannotCommitError,
|
|
|
|
DatabaseError,
|
|
|
|
DuplicateEntryError,
|
|
|
|
LinkValidationError,
|
2022-03-24 09:50:40 +00:00
|
|
|
NotFoundError,
|
2022-03-30 07:16:01 +00:00
|
|
|
ValueError,
|
2022-04-19 05:59:36 +00:00
|
|
|
} from 'fyo/utils/errors';
|
|
|
|
import { knex, Knex } from 'knex';
|
2022-03-24 09:50:40 +00:00
|
|
|
import {
|
|
|
|
Field,
|
|
|
|
FieldTypeEnum,
|
|
|
|
RawValue,
|
2022-04-07 06:20:14 +00:00
|
|
|
Schema,
|
2022-03-24 09:50:40 +00:00
|
|
|
SchemaMap,
|
2022-03-30 07:16:01 +00:00
|
|
|
TargetField,
|
2022-03-24 09:50:40 +00:00
|
|
|
} from '../../schemas/types';
|
2022-03-31 07:33:58 +00:00
|
|
|
import { getRandomString, getValueMapFromList } from '../../utils';
|
|
|
|
import { DatabaseBase, GetAllOptions, QueryFilter } from '../../utils/db/types';
|
2022-03-31 07:18:32 +00:00
|
|
|
import { getDefaultMetaFieldValueMap, sqliteTypeMap, SYSTEM } from '../helpers';
|
2022-04-18 11:29:20 +00:00
|
|
|
import {
|
|
|
|
ColumnDiff,
|
|
|
|
FieldValueMap,
|
|
|
|
GetQueryBuilderOptions,
|
|
|
|
SingleValue,
|
|
|
|
} from './types';
|
2022-03-24 09:50:40 +00:00
|
|
|
|
2022-03-28 10:01:29 +00:00
|
|
|
/**
|
2022-03-30 07:16:01 +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.
|
2022-04-16 07:25:36 +00:00
|
|
|
* 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()`.
|
2022-03-30 07:16:01 +00:00
|
|
|
*
|
|
|
|
* 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;
|
2022-03-24 09:50:40 +00:00
|
|
|
dbPath: string;
|
2022-03-24 13:13:59 +00:00
|
|
|
schemaMap: SchemaMap = {};
|
2022-03-24 09:50:40 +00:00
|
|
|
connectionParams: Knex.Config;
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
constructor(dbPath?: string) {
|
2022-03-31 07:18:32 +00:00
|
|
|
super();
|
2022-03-24 13:13:59 +00:00
|
|
|
this.dbPath = dbPath ?? ':memory:';
|
2022-03-23 06:16:13 +00:00
|
|
|
this.connectionParams = {
|
|
|
|
client: 'sqlite3',
|
|
|
|
connection: {
|
|
|
|
filename: this.dbPath,
|
|
|
|
},
|
|
|
|
pool: {
|
2022-03-25 10:12:39 +00:00
|
|
|
afterCreate(conn: { run: (query: string) => void }, done: () => void) {
|
2022-03-23 06:16:13 +00:00
|
|
|
conn.run('PRAGMA foreign_keys=ON');
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
useNullAsDefault: true,
|
|
|
|
asyncStackTraces: process.env.NODE_ENV === 'development',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-16 07:25:36 +00:00
|
|
|
static async getCountryCode(dbPath: string): Promise<string> {
|
|
|
|
let countryCode = 'in';
|
|
|
|
const db = new DatabaseCore(dbPath);
|
|
|
|
db.connect();
|
|
|
|
|
2022-04-26 10:12:33 +00:00
|
|
|
let query: { value: string }[] = [];
|
2022-04-16 07:25:36 +00:00
|
|
|
try {
|
|
|
|
query = await db.knex!('SingleValue').where({
|
|
|
|
fieldname: 'countryCode',
|
|
|
|
parent: 'SystemSettings',
|
|
|
|
});
|
|
|
|
} catch {
|
|
|
|
// Database not inialized and no countryCode passed
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query.length > 0) {
|
2022-04-26 10:12:33 +00:00
|
|
|
countryCode = query[0].value as string;
|
2022-04-16 07:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await db.close();
|
|
|
|
return countryCode;
|
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
setSchemaMap(schemaMap: SchemaMap) {
|
|
|
|
this.schemaMap = schemaMap;
|
|
|
|
}
|
|
|
|
|
2022-03-23 06:16:13 +00:00
|
|
|
connect() {
|
|
|
|
this.knex = knex(this.connectionParams);
|
|
|
|
this.knex.on('query-error', (error) => {
|
2022-03-24 13:13:59 +00:00
|
|
|
error.type = this.#getError(error);
|
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
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
async commit() {
|
|
|
|
try {
|
2022-03-28 10:01:29 +00:00
|
|
|
await this.knex!.raw('commit');
|
2022-03-24 13:13:59 +00:00
|
|
|
} catch (err) {
|
2022-03-25 10:12:39 +00:00
|
|
|
const type = this.#getError(err as Error);
|
|
|
|
if (type !== CannotCommitError) {
|
2022-03-24 13:13:59 +00:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async migrate() {
|
|
|
|
for (const schemaName in this.schemaMap) {
|
2022-04-07 06:20:14 +00:00
|
|
|
const schema = this.schemaMap[schemaName] as Schema;
|
2022-03-24 13:13:59 +00:00
|
|
|
if (schema.isSingle) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (await this.#tableExists(schemaName)) {
|
|
|
|
await this.#alterTable(schemaName);
|
|
|
|
} else {
|
|
|
|
await this.#createTable(schemaName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.commit();
|
|
|
|
await this.#initializeSingles();
|
|
|
|
}
|
|
|
|
|
|
|
|
async exists(schemaName: string, name?: string): Promise<boolean> {
|
2022-04-07 06:20:14 +00:00
|
|
|
const schema = this.schemaMap[schemaName] as Schema;
|
2022-03-24 13:13:59 +00:00
|
|
|
if (schema.isSingle) {
|
|
|
|
return this.#singleExists(schemaName);
|
|
|
|
}
|
|
|
|
|
|
|
|
let row = [];
|
|
|
|
try {
|
2022-03-25 10:12:39 +00:00
|
|
|
const qb = this.knex!(schemaName);
|
2022-03-24 13:13:59 +00:00
|
|
|
if (name !== undefined) {
|
|
|
|
qb.where({ name });
|
|
|
|
}
|
|
|
|
row = await qb.limit(1);
|
|
|
|
} catch (err) {
|
|
|
|
if (this.#getError(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> {
|
2022-03-24 13:13:59 +00:00
|
|
|
// insert parent
|
2022-04-07 06:20:14 +00:00
|
|
|
if (this.schemaMap[schemaName]!.isSingle) {
|
2022-03-24 13:13:59 +00:00
|
|
|
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: string = '',
|
2022-03-30 07:16:01 +00:00
|
|
|
fields?: string | string[]
|
2022-03-24 13:13:59 +00:00
|
|
|
): Promise<FieldValueMap> {
|
2022-04-07 06:20:14 +00:00
|
|
|
const schema = this.schemaMap[schemaName] as Schema;
|
2022-03-30 07:16:01 +00:00
|
|
|
if (!schema.isSingle && !name) {
|
2022-03-24 13:13:59 +00:00
|
|
|
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 = {};
|
2022-03-30 07:16:01 +00:00
|
|
|
if (schema.isSingle) {
|
2022-03-29 08:18:39 +00:00
|
|
|
return await this.#getSingle(schemaName);
|
2022-03-24 13:13:59 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 07:16:01 +00:00
|
|
|
if (typeof fields === 'string') {
|
2022-03-24 13:13:59 +00:00
|
|
|
fields = [fields];
|
|
|
|
}
|
|
|
|
|
2022-03-30 07:16:01 +00:00
|
|
|
if (fields === undefined) {
|
|
|
|
fields = schema.fields.map((f) => f.fieldname);
|
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
/**
|
|
|
|
* Separate table fields and non table fields
|
|
|
|
*/
|
2022-03-30 07:16:01 +00:00
|
|
|
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)
|
|
|
|
);
|
2022-03-24 13:13:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If schema is not single then return specific fields
|
|
|
|
* if child fields are selected, all child fields are returned.
|
|
|
|
*/
|
|
|
|
if (nonTableFieldNames.length) {
|
2022-03-30 07:16:01 +00:00
|
|
|
fieldValueMap =
|
|
|
|
(await this.#getOne(schemaName, name, nonTableFieldNames)) ?? {};
|
2022-03-24 13:13:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tableFields.length) {
|
|
|
|
await this.#loadChildren(fieldValueMap, tableFields);
|
|
|
|
}
|
|
|
|
return fieldValueMap;
|
|
|
|
}
|
|
|
|
|
2022-03-30 07:16:01 +00:00
|
|
|
async getAll(
|
|
|
|
schemaName: string,
|
|
|
|
options: GetAllOptions = {}
|
|
|
|
): Promise<FieldValueMap[]> {
|
2022-04-07 06:20:14 +00:00
|
|
|
const schema = this.schemaMap[schemaName] as Schema;
|
2022-03-30 07:16:01 +00:00
|
|
|
const hasCreated = !!schema.fields.find((f) => f.fieldname === 'created');
|
|
|
|
const {
|
|
|
|
fields = ['name', ...(schema.keywordFields ?? [])],
|
|
|
|
filters,
|
|
|
|
offset,
|
2022-03-24 13:13:59 +00:00
|
|
|
limit,
|
|
|
|
groupBy,
|
2022-03-30 07:16:01 +00:00
|
|
|
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[];
|
2022-03-24 13:13:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async getSingleValues(
|
2022-03-29 08:18:39 +00:00
|
|
|
...fieldnames: ({ fieldname: string; parent?: string } | string)[]
|
2022-04-18 11:29:20 +00:00
|
|
|
): Promise<SingleValue<RawValue>> {
|
2022-03-29 08:18:39 +00:00
|
|
|
const fieldnameList = fieldnames.map((fieldname) => {
|
2022-03-24 13:13:59 +00:00
|
|
|
if (typeof fieldname === 'string') {
|
|
|
|
return { fieldname };
|
|
|
|
}
|
|
|
|
return fieldname;
|
|
|
|
});
|
|
|
|
|
2022-03-25 10:12:39 +00:00
|
|
|
let builder = this.knex!('SingleValue');
|
2022-03-29 08:18:39 +00:00
|
|
|
builder = builder.where(fieldnameList[0]);
|
2022-03-24 13:13:59 +00:00
|
|
|
|
2022-03-29 08:18:39 +00:00
|
|
|
fieldnameList.slice(1).forEach(({ fieldname, parent }) => {
|
2022-03-24 13:13:59 +00:00
|
|
|
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 (this.#getError(err as Error) === NotFoundError) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
async rename(schemaName: string, oldName: string, newName: string) {
|
2022-03-30 07:16:01 +00:00
|
|
|
/**
|
|
|
|
* 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)
|
2022-03-24 13:13:59 +00:00
|
|
|
.update({ name: newName })
|
|
|
|
.where('name', oldName);
|
|
|
|
await this.commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
async update(schemaName: string, fieldValueMap: FieldValueMap) {
|
|
|
|
// update parent
|
2022-04-07 06:20:14 +00:00
|
|
|
if (this.schemaMap[schemaName]!.isSingle) {
|
2022-03-24 13:13:59 +00:00
|
|
|
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) {
|
2022-04-07 06:20:14 +00:00
|
|
|
const schema = this.schemaMap[schemaName] as Schema;
|
2022-03-29 08:18:39 +00:00
|
|
|
if (schema.isSingle) {
|
|
|
|
await this.#deleteSingle(schemaName, name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
await this.#deleteOne(schemaName, name);
|
|
|
|
|
|
|
|
// delete children
|
|
|
|
const tableFields = this.#getTableFields(schemaName);
|
|
|
|
|
|
|
|
for (const field of tableFields) {
|
|
|
|
await this.#deleteChildren(field.target, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async #tableExists(schemaName: string) {
|
2022-03-25 10:12:39 +00:00
|
|
|
return await this.knex!.schema.hasTable(schemaName);
|
2022-03-23 06:16:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
async #singleExists(singleSchemaName: string) {
|
2022-03-25 10:12:39 +00:00
|
|
|
const res = await this.knex!('SingleValue')
|
2022-03-24 09:50:40 +00:00
|
|
|
.count('parent as count')
|
|
|
|
.where('parent', singleSchemaName)
|
|
|
|
.first();
|
2022-03-25 10:12:39 +00:00
|
|
|
return (res?.count ?? 0) > 0;
|
2022-03-23 06:16:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
async #removeColumns(schemaName: string, targetColumns: string[]) {
|
2022-03-24 09:50:40 +00:00
|
|
|
// TODO: Implement this for sqlite
|
|
|
|
}
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
#getError(err: Error) {
|
2022-03-24 09:50:40 +00:00
|
|
|
let errorType = DatabaseError;
|
|
|
|
if (err.message.includes('SQLITE_ERROR: no such table')) {
|
|
|
|
errorType = NotFoundError;
|
|
|
|
}
|
|
|
|
if (err.message.includes('FOREIGN KEY')) {
|
|
|
|
errorType = LinkValidationError;
|
|
|
|
}
|
|
|
|
if (err.message.includes('SQLITE_ERROR: cannot commit')) {
|
|
|
|
errorType = CannotCommitError;
|
|
|
|
}
|
|
|
|
if (err.message.includes('SQLITE_CONSTRAINT: UNIQUE constraint failed:')) {
|
|
|
|
errorType = DuplicateEntryError;
|
|
|
|
}
|
|
|
|
return errorType;
|
|
|
|
}
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-03-24 09:50:40 +00:00
|
|
|
async prestigeTheTable(schemaName: string, tableRows: FieldValueMap[]) {
|
|
|
|
const max = 200;
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-03-24 09:50:40 +00:00
|
|
|
// Alter table hacx for sqlite in case of schema change.
|
|
|
|
const tempName = `__${schemaName}`;
|
2022-03-25 10:12:39 +00:00
|
|
|
await this.knex!.schema.dropTableIfExists(tempName);
|
2022-03-24 09:50:40 +00:00
|
|
|
|
2022-03-25 10:12:39 +00:00
|
|
|
await this.knex!.raw('PRAGMA foreign_keys=OFF');
|
2022-03-24 13:13:59 +00:00
|
|
|
await this.#createTable(schemaName, tempName);
|
2022-03-24 09:50:40 +00:00
|
|
|
|
|
|
|
if (tableRows.length > 200) {
|
|
|
|
const fi = Math.floor(tableRows.length / max);
|
|
|
|
for (let i = 0; i <= fi; i++) {
|
|
|
|
const rowSlice = tableRows.slice(i * max, i + 1 * max);
|
|
|
|
if (rowSlice.length === 0) {
|
|
|
|
break;
|
2022-03-23 06:16:13 +00:00
|
|
|
}
|
2022-03-25 10:12:39 +00:00
|
|
|
await this.knex!.batchInsert(tempName, rowSlice);
|
2022-03-24 09:50:40 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-03-25 10:12:39 +00:00
|
|
|
await this.knex!.batchInsert(tempName, tableRows);
|
2022-03-24 09:50:40 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +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-24 09:50:40 +00:00
|
|
|
}
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
async #getForeignKeys(schemaName: string): Promise<string[]> {
|
2022-03-28 10:01:29 +00:00
|
|
|
const foreignKeyList: FieldValueMap[] = await this.knex!.raw(
|
2022-03-24 09:50:40 +00:00
|
|
|
`PRAGMA foreign_key_list(${schemaName})`
|
|
|
|
);
|
2022-03-25 10:12:39 +00:00
|
|
|
return foreignKeyList.map((d) => d.from as string);
|
2022-03-24 09:50:40 +00:00
|
|
|
}
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
#getQueryBuilder(
|
2022-03-24 09:50:40 +00:00
|
|
|
schemaName: string,
|
|
|
|
fields: string[],
|
|
|
|
filters: QueryFilter,
|
|
|
|
options: GetQueryBuilderOptions
|
|
|
|
): Knex.QueryBuilder {
|
2022-03-25 10:12:39 +00:00
|
|
|
const builder = this.knex!.select(fields).from(schemaName);
|
2022-03-24 09:50:40 +00:00
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
this.#applyFiltersToBuilder(builder, filters);
|
2022-03-24 09:50:40 +00:00
|
|
|
|
|
|
|
if (options.orderBy) {
|
|
|
|
builder.orderBy(options.orderBy, options.order);
|
2022-03-23 06:16:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 09:50:40 +00:00
|
|
|
if (options.groupBy) {
|
|
|
|
builder.groupBy(options.groupBy);
|
2022-03-23 06:16:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 09:50:40 +00:00
|
|
|
if (options.offset) {
|
|
|
|
builder.offset(options.offset);
|
2022-03-23 06:16:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 09:50:40 +00:00
|
|
|
if (options.limit) {
|
|
|
|
builder.limit(options.limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
#applyFiltersToBuilder(builder: Knex.QueryBuilder, filters: QueryFilter) {
|
2022-03-24 09:50:40 +00:00
|
|
|
// {"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 = [];
|
|
|
|
|
|
|
|
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)[];
|
2022-03-24 09:50:40 +00:00
|
|
|
|
|
|
|
if (Array.isArray(value)) {
|
2022-04-14 06:43:58 +00:00
|
|
|
operator = value[0] as string;
|
|
|
|
comparisonValue = value[1] as string | number | (string | number)[];
|
2022-03-24 09:50:40 +00:00
|
|
|
operator = operator.toLowerCase();
|
|
|
|
|
|
|
|
if (operator === 'includes') {
|
|
|
|
operator = 'like';
|
|
|
|
}
|
|
|
|
|
2022-04-14 06:43:58 +00:00
|
|
|
if (
|
|
|
|
operator === 'like' &&
|
|
|
|
!(comparisonValue as (string | number)[]).includes('%')
|
|
|
|
) {
|
2022-03-24 09:50:40 +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
|
|
|
}
|
2022-03-24 09:50:40 +00:00
|
|
|
|
|
|
|
filtersArray.map((filter) => {
|
2022-04-14 06:43:58 +00:00
|
|
|
const field = filter[0] as string;
|
|
|
|
const operator = filter[1];
|
|
|
|
const comparisonValue = filter[2];
|
|
|
|
|
2022-03-24 09:50:40 +00:00
|
|
|
if (operator === '=') {
|
2022-04-14 06:43:58 +00:00
|
|
|
builder.where(field, comparisonValue);
|
2022-03-24 09:50:40 +00:00
|
|
|
} else {
|
2022-04-14 06:43:58 +00:00
|
|
|
builder.where(field, operator as string, comparisonValue as string);
|
2022-03-24 09:50:40 +00:00
|
|
|
}
|
|
|
|
});
|
2022-03-23 06:16:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
async #getColumnDiff(schemaName: string): Promise<ColumnDiff> {
|
|
|
|
const tableColumns = await this.#getTableColumns(schemaName);
|
2022-04-07 06:20:14 +00:00
|
|
|
const validFields = this.schemaMap[schemaName]!.fields;
|
2022-03-24 09:50:40 +00:00
|
|
|
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[]> {
|
2022-03-24 13:13:59 +00:00
|
|
|
const foreignKeys = await this.#getForeignKeys(schemaName);
|
2022-03-24 09:50:40 +00:00
|
|
|
const newForeignKeys: Field[] = [];
|
2022-04-07 06:20:14 +00:00
|
|
|
const schema = this.schemaMap[schemaName] as Schema;
|
2022-03-24 09:50:40 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
#buildColumnForTable(table: Knex.AlterTableBuilder, field: Field) {
|
2022-03-24 09:50:40 +00:00
|
|
|
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
|
|
|
}
|
2022-03-24 09:50:40 +00:00
|
|
|
|
|
|
|
const columnType = this.typeMap[field.fieldtype];
|
|
|
|
if (!columnType) {
|
2022-03-23 06:16:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-03-24 09:50:40 +00:00
|
|
|
|
2022-03-25 10:12:39 +00:00
|
|
|
const column = table[columnType](
|
|
|
|
field.fieldname
|
|
|
|
) as Knex.SqlLiteColumnBuilder;
|
2022-03-24 09:50:40 +00:00
|
|
|
|
|
|
|
// 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) {
|
2022-03-24 09:50:40 +00:00
|
|
|
column.notNullable();
|
|
|
|
}
|
|
|
|
|
|
|
|
// link
|
2022-03-28 10:01:29 +00:00
|
|
|
if (
|
|
|
|
field.fieldtype === FieldTypeEnum.Link &&
|
|
|
|
(field as TargetField).target
|
|
|
|
) {
|
|
|
|
const targetSchemaName = (field as TargetField).target as string;
|
2022-04-07 06:20:14 +00:00
|
|
|
const schema = this.schemaMap[targetSchemaName] as Schema;
|
2022-03-24 09:50:40 +00:00
|
|
|
table
|
|
|
|
.foreign(field.fieldname)
|
|
|
|
.references('name')
|
|
|
|
.inTable(schema.name)
|
|
|
|
.onUpdate('CASCADE')
|
|
|
|
.onDelete('RESTRICT');
|
|
|
|
}
|
2022-03-23 06:16:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
async #alterTable(schemaName: string) {
|
2022-03-24 09:50:40 +00:00
|
|
|
// get columns
|
2022-03-24 13:13:59 +00:00
|
|
|
const diff: ColumnDiff = await this.#getColumnDiff(schemaName);
|
|
|
|
const newForeignKeys: Field[] = await this.#getNewForeignKeys(schemaName);
|
2022-03-24 09:50:40 +00:00
|
|
|
|
2022-03-25 10:12:39 +00:00
|
|
|
return this.knex!.schema.table(schemaName, (table) => {
|
|
|
|
if (diff.added.length) {
|
|
|
|
for (const field of diff.added) {
|
|
|
|
this.#buildColumnForTable(table, field);
|
2022-03-24 09:50:40 +00:00
|
|
|
}
|
2022-03-25 10:12:39 +00:00
|
|
|
}
|
2022-03-24 09:50:40 +00:00
|
|
|
|
2022-03-25 10:12:39 +00:00
|
|
|
if (diff.removed.length) {
|
|
|
|
this.#removeColumns(schemaName, diff.removed);
|
|
|
|
}
|
|
|
|
}).then(() => {
|
|
|
|
if (newForeignKeys.length) {
|
|
|
|
return this.#addForeignKeys(schemaName, newForeignKeys);
|
|
|
|
}
|
|
|
|
});
|
2022-03-23 06:16:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
async #createTable(schemaName: string, tableName?: string) {
|
2022-03-24 09:50:40 +00:00
|
|
|
tableName ??= schemaName;
|
2022-04-07 06:20:14 +00:00
|
|
|
const fields = this.schemaMap[schemaName]!.fields;
|
2022-03-24 13:13:59 +00:00
|
|
|
return await this.#runCreateTableQuery(tableName, fields);
|
2022-03-24 09:50:40 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
#runCreateTableQuery(schemaName: string, fields: Field[]) {
|
2022-03-25 10:12:39 +00:00
|
|
|
return this.knex!.schema.createTable(schemaName, (table) => {
|
2022-03-24 09:50:40 +00:00
|
|
|
for (const field of fields) {
|
2022-03-24 13:13:59 +00:00
|
|
|
this.#buildColumnForTable(table, field);
|
2022-03-24 09:50:40 +00:00
|
|
|
}
|
2022-03-23 06:16:13 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
async #getNonExtantSingleValues(singleSchemaName: string) {
|
2022-03-24 09:50:40 +00:00
|
|
|
const existingFields = (
|
2022-03-25 10:12:39 +00:00
|
|
|
await this.knex!('SingleValue')
|
2022-03-24 09:50:40 +00:00
|
|
|
.where({ parent: singleSchemaName })
|
|
|
|
.select('fieldname')
|
|
|
|
).map(({ fieldname }) => fieldname);
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-04-07 06:20:14 +00:00
|
|
|
return this.schemaMap[singleSchemaName]!.fields.map(
|
|
|
|
({ fieldname, default: value }) => ({
|
2022-03-24 09:50:40 +00:00
|
|
|
fieldname,
|
|
|
|
value: value as RawValue | undefined,
|
2022-04-07 06:20:14 +00:00
|
|
|
})
|
|
|
|
).filter(
|
|
|
|
({ fieldname, value }) =>
|
|
|
|
!existingFields.includes(fieldname) && value !== undefined
|
|
|
|
);
|
2022-03-23 06:16:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
async #deleteOne(schemaName: string, name: string) {
|
2022-03-29 08:18:39 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
#deleteChildren(schemaName: string, parentName: string) {
|
2022-03-25 10:12:39 +00:00
|
|
|
return this.knex!(schemaName).where('parent', parentName).delete();
|
2022-03-24 09:50:40 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
#runDeleteOtherChildren(
|
2022-03-24 09:50:40 +00:00
|
|
|
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)
|
2022-03-24 09:50:40 +00:00
|
|
|
.where('parent', parentName)
|
2022-03-23 06:16:13 +00:00
|
|
|
.andWhere('name', 'not in', added)
|
|
|
|
.delete();
|
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
#prepareChild(
|
2022-03-24 09:50:40 +00:00
|
|
|
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-30 07:16:01 +00:00
|
|
|
child.parent = parentName;
|
2022-03-24 09:50:40 +00:00
|
|
|
child.parentSchemaName = parentSchemaName;
|
|
|
|
child.parentFieldname = field.fieldname;
|
2022-03-23 06:16:13 +00:00
|
|
|
child.idx = idx;
|
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
async #addForeignKeys(schemaName: string, newForeignKeys: Field[]) {
|
2022-03-28 10:01:29 +00:00
|
|
|
await this.knex!.raw('PRAGMA foreign_keys=OFF');
|
|
|
|
await this.knex!.raw('BEGIN TRANSACTION');
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-03-24 09:50:40 +00:00
|
|
|
const tempName = 'TEMP' + schemaName;
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-03-24 09:50:40 +00:00
|
|
|
// create temp table
|
2022-03-24 13:13:59 +00:00
|
|
|
await this.#createTable(schemaName, tempName);
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-03-24 09:50:40 +00:00
|
|
|
try {
|
|
|
|
// copy from old to new table
|
2022-03-25 10:12:39 +00:00
|
|
|
await this.knex!(tempName).insert(this.knex!.select().from(schemaName));
|
2022-03-24 09:50:40 +00:00
|
|
|
} catch (err) {
|
2022-03-28 10:01:29 +00:00
|
|
|
await this.knex!.raw('ROLLBACK');
|
|
|
|
await this.knex!.raw('PRAGMA foreign_keys=ON');
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-03-25 10:12:39 +00:00
|
|
|
const rows = await this.knex!.select().from(schemaName);
|
2022-03-24 09:50:40 +00:00
|
|
|
await this.prestigeTheTable(schemaName, rows);
|
|
|
|
return;
|
2022-03-23 06:16:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 09:50:40 +00:00
|
|
|
// drop old table
|
2022-03-25 10:12:39 +00:00
|
|
|
await this.knex!.schema.dropTable(schemaName);
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-03-24 09:50:40 +00:00
|
|
|
// rename new table
|
2022-03-25 10:12:39 +00:00
|
|
|
await this.knex!.schema.renameTable(tempName, schemaName);
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-03-28 10:01:29 +00:00
|
|
|
await this.knex!.raw('COMMIT');
|
|
|
|
await this.knex!.raw('PRAGMA foreign_keys=ON');
|
2022-03-23 06:16:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
async #loadChildren(
|
|
|
|
fieldValueMap: FieldValueMap,
|
|
|
|
tableFields: TargetField[]
|
2022-03-24 09:50:40 +00:00
|
|
|
) {
|
2022-03-24 13:13:59 +00:00
|
|
|
for (const field of tableFields) {
|
2022-03-30 07:16:01 +00:00
|
|
|
fieldValueMap[field.fieldname] = await this.getAll(field.target, {
|
2022-03-24 13:13:59 +00:00
|
|
|
fields: ['*'],
|
|
|
|
filters: { parent: fieldValueMap.name as string },
|
|
|
|
orderBy: 'idx',
|
|
|
|
order: 'asc',
|
|
|
|
});
|
2022-03-24 09:50:40 +00:00
|
|
|
}
|
2022-03-23 06:16:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 07:16:01 +00:00
|
|
|
async #getOne(schemaName: string, name: string, fields: string[]) {
|
2022-03-25 10:12:39 +00:00
|
|
|
const fieldValueMap: FieldValueMap = await this.knex!.select(fields)
|
2022-03-24 09:50:40 +00:00
|
|
|
.from(schemaName)
|
|
|
|
.where('name', name)
|
|
|
|
.first();
|
|
|
|
return fieldValueMap;
|
|
|
|
}
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
async #getSingle(schemaName: string): Promise<FieldValueMap> {
|
2022-03-30 07:16:01 +00:00
|
|
|
const values = await this.getAll('SingleValue', {
|
2022-03-24 09:50:40 +00:00
|
|
|
fields: ['fieldname', 'value'],
|
|
|
|
filters: { parent: schemaName },
|
|
|
|
orderBy: 'fieldname',
|
|
|
|
order: 'asc',
|
|
|
|
});
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-03-29 08:18:39 +00:00
|
|
|
return getValueMapFromList(values, 'fieldname', 'value') as FieldValueMap;
|
2022-03-24 09:50:40 +00:00
|
|
|
}
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
#insertOne(schemaName: string, fieldValueMap: FieldValueMap) {
|
2022-03-24 09:50:40 +00:00
|
|
|
if (!fieldValueMap.name) {
|
|
|
|
fieldValueMap.name = getRandomString();
|
|
|
|
}
|
|
|
|
|
2022-03-30 07:16:01 +00:00
|
|
|
// Non Table Fields
|
2022-04-07 06:20:14 +00:00
|
|
|
const fields = this.schemaMap[schemaName]!.fields.filter(
|
2022-03-30 07:16:01 +00:00
|
|
|
(f) => f.fieldtype !== FieldTypeEnum.Table
|
|
|
|
);
|
2022-03-24 09:50:40 +00:00
|
|
|
|
2022-03-30 07:16:01 +00:00
|
|
|
const validMap: FieldValueMap = {};
|
|
|
|
for (const { fieldname } of fields) {
|
2022-03-24 09:50:40 +00:00
|
|
|
validMap[fieldname] = fieldValueMap[fieldname];
|
|
|
|
}
|
|
|
|
|
2022-03-30 07:16:01 +00:00
|
|
|
return this.knex!(schemaName).insert(validMap);
|
2022-03-23 06:16:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
async #updateSingleValues(
|
2022-03-24 09:50:40 +00:00
|
|
|
singleSchemaName: string,
|
|
|
|
fieldValueMap: FieldValueMap
|
|
|
|
) {
|
2022-04-07 06:20:14 +00:00
|
|
|
const fields = this.schemaMap[singleSchemaName]!.fields;
|
2022-03-24 09:50:40 +00:00
|
|
|
|
|
|
|
for (const field of fields) {
|
|
|
|
const value = fieldValueMap[field.fieldname] as RawValue | undefined;
|
|
|
|
if (value === undefined) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
await this.#updateSingleValue(singleSchemaName, field.fieldname, value);
|
2022-03-24 09:50:40 +00:00
|
|
|
}
|
2022-03-23 06:16:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
async #updateSingleValue(
|
2022-03-24 09:50:40 +00:00
|
|
|
singleSchemaName: string,
|
|
|
|
fieldname: string,
|
|
|
|
value: RawValue
|
|
|
|
) {
|
2022-03-29 08:18:39 +00:00
|
|
|
const names: { name: string }[] = await this.knex!('SingleValue')
|
|
|
|
.select('name')
|
2022-03-24 09:50:40 +00:00
|
|
|
.where({
|
|
|
|
parent: singleSchemaName,
|
|
|
|
fieldname,
|
2022-03-29 08:18:39 +00:00
|
|
|
});
|
|
|
|
const name = names?.[0]?.name as string | undefined;
|
|
|
|
|
|
|
|
if (name === undefined) {
|
|
|
|
this.#insertSingleValue(singleSchemaName, fieldname, value);
|
|
|
|
} else {
|
|
|
|
return await this.knex!('SingleValue').where({ name }).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
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
async #initializeSingles() {
|
2022-03-24 09:50:40 +00:00
|
|
|
const singleSchemaNames = Object.keys(this.schemaMap).filter(
|
2022-04-07 06:20:14 +00:00
|
|
|
(n) => this.schemaMap[n]!.isSingle
|
2022-03-24 09:50:40 +00:00
|
|
|
);
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-03-24 09:50:40 +00:00
|
|
|
for (const schemaName of singleSchemaNames) {
|
2022-03-24 13:13:59 +00:00
|
|
|
if (await this.#singleExists(schemaName)) {
|
|
|
|
await this.#updateNonExtantSingleValues(schemaName);
|
2022-03-24 09:50:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-04-07 06:20:14 +00:00
|
|
|
const fields = this.schemaMap[schemaName]!.fields;
|
2022-03-24 09:50:40 +00:00
|
|
|
if (fields.every((f) => f.default === undefined)) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-03-24 09:50:40 +00:00
|
|
|
const defaultValues: FieldValueMap = fields.reduce((acc, f) => {
|
|
|
|
if (f.default !== undefined) {
|
|
|
|
acc[f.fieldname] = f.default;
|
2022-03-23 06:16:13 +00:00
|
|
|
}
|
2022-03-24 09:50:40 +00:00
|
|
|
|
|
|
|
return acc;
|
2022-03-25 10:12:39 +00:00
|
|
|
}, {} as FieldValueMap);
|
2022-03-24 09:50:40 +00:00
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
await this.#updateSingleValues(schemaName, defaultValues);
|
2022-03-23 06:16:13 +00:00
|
|
|
}
|
2022-03-24 09:50:40 +00:00
|
|
|
}
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
async #updateNonExtantSingleValues(schemaName: string) {
|
|
|
|
const singleValues = await this.#getNonExtantSingleValues(schemaName);
|
2022-03-24 09:50:40 +00:00
|
|
|
for (const sv of singleValues) {
|
2022-03-25 10:12:39 +00:00
|
|
|
await this.#updateSingleValue(schemaName, sv.fieldname, sv.value!);
|
2022-03-24 09:50:40 +00:00
|
|
|
}
|
2022-03-23 06:16:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
async #updateOne(schemaName: string, fieldValueMap: FieldValueMap) {
|
2022-03-24 09:50:40 +00:00
|
|
|
const updateMap = { ...fieldValueMap };
|
|
|
|
delete updateMap.name;
|
2022-04-07 06:20:14 +00:00
|
|
|
const schema = this.schemaMap[schemaName] as Schema;
|
2022-03-30 07:16:01 +00:00
|
|
|
for (const { fieldname, fieldtype } of schema.fields) {
|
|
|
|
if (fieldtype !== FieldTypeEnum.Table) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete updateMap[fieldname];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(updateMap).length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-24 09:50:40 +00:00
|
|
|
|
2022-03-25 10:12:39 +00:00
|
|
|
return await this.knex!(schemaName)
|
2022-03-24 09:50:40 +00:00
|
|
|
.where('name', fieldValueMap.name as string)
|
2022-03-24 13:13:59 +00:00
|
|
|
.update(updateMap);
|
2022-03-23 06:16:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
async #insertOrUpdateChildren(
|
2022-03-24 09:50:40 +00:00
|
|
|
schemaName: string,
|
|
|
|
fieldValueMap: FieldValueMap,
|
|
|
|
isUpdate: boolean
|
2022-03-23 06:16:13 +00:00
|
|
|
) {
|
2022-03-24 13:13:59 +00:00
|
|
|
const tableFields = this.#getTableFields(schemaName);
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-03-24 09:50:40 +00:00
|
|
|
const parentName = fieldValueMap.name as string;
|
|
|
|
for (const field of tableFields) {
|
|
|
|
const added: string[] = [];
|
|
|
|
|
|
|
|
const tableFieldValue = (fieldValueMap[field.fieldname] ??
|
|
|
|
[]) as FieldValueMap[];
|
|
|
|
for (const child of tableFieldValue) {
|
2022-03-24 13:13:59 +00:00
|
|
|
this.#prepareChild(schemaName, parentName, child, field, added.length);
|
2022-03-24 09:50:40 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
isUpdate &&
|
|
|
|
(await this.exists(field.target, child.name as string))
|
|
|
|
) {
|
2022-03-24 13:13:59 +00:00
|
|
|
await this.#updateOne(field.target, child);
|
2022-03-24 09:50:40 +00:00
|
|
|
} else {
|
2022-03-24 13:13:59 +00:00
|
|
|
await this.#insertOne(field.target, child);
|
2022-03-24 09:50:40 +00:00
|
|
|
}
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-03-24 09:50:40 +00:00
|
|
|
added.push(child.name as string);
|
|
|
|
}
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-03-24 09:50:40 +00:00
|
|
|
if (isUpdate) {
|
2022-03-24 13:13:59 +00:00
|
|
|
await this.#runDeleteOtherChildren(field, parentName, added);
|
2022-03-24 09:50:40 +00:00
|
|
|
}
|
2022-03-23 06:16:13 +00:00
|
|
|
}
|
2022-03-24 09:50:40 +00:00
|
|
|
}
|
2022-03-23 06:16:13 +00:00
|
|
|
|
2022-03-24 13:13:59 +00:00
|
|
|
#getTableFields(schemaName: string): TargetField[] {
|
2022-04-07 06:20:14 +00:00
|
|
|
return this.schemaMap[schemaName]!.fields.filter(
|
2022-03-24 13:13:59 +00:00
|
|
|
(f) => f.fieldtype === FieldTypeEnum.Table
|
|
|
|
) as TargetField[];
|
2022-03-24 09:50:40 +00:00
|
|
|
}
|
2022-03-23 06:16:13 +00:00
|
|
|
}
|