nocobase/packages/core/database/src/utils.ts

85 lines
2.1 KiB
TypeScript
Raw Normal View History

import crypto from 'crypto';
2022-10-14 06:51:19 +00:00
import { IdentifierError } from './errors/identifier-error';
import { Model } from './model';
feat: provide the underscored option for the database (#1366) * feat: underscored options * feat: underscored using hook * feat: database underscored options * feat: underscored env * fix: collectionExistsInDb * fix: test * fix: nocobase install * fix: test * fix: belongsTo association * fix: test of underscored * chore: console.log * fix: list action test * fix: dump test * chore: snakeCase algo * fix: underscored field create * fix: underscored env * fix(acl): custom appends merge strategy (#1416) * Update index.md * fix(plugin-workflow): use promise to request (#1426) * Update index.md * Update collection.md * Update index.md * Update index.md * Update collection.md * Update field.md * Update repository.md * Update has-one-repository.md * Update has-many-repository.md * Update belongs-to-many-repository.md * Update index.md * chore: translate 'Add tab' in page header (#1424) * fix: test * fix: workflow test * fix: underscored with inherits * fix: underscored test * fix: process.env.DB_UNDERSCORED * fix: process.env.DB_UNDERSCORED === 'true' * fix: test * fix: pg test * fix: underscored table name * feat: tableName & fieldName conflict check * fix: test * fix: underscored index * fix: update field unique index * fix: sync default value * fix: collection manager create field * chore: field sync * fix: pg test * chore: test * fix: test * chore: default constraint name * chore: syncUniqueIndex * feat: field destory before check * feat: field type check * fix: test * fix: test * fix: improve * fix: should destroy when fields refer to the same field * fix: acl meta with underscored --------- Co-authored-by: chenos <chenlinxh@gmail.com>
2023-02-13 13:38:47 +00:00
import lodash from 'lodash';
type HandleAppendsQueryOptions = {
templateModel: any;
queryPromises: Array<any>;
};
export async function handleAppendsQuery(options: HandleAppendsQueryOptions) {
const { templateModel, queryPromises } = options;
if (!templateModel) {
return [];
}
const primaryKey = templateModel.constructor.primaryKeyAttribute;
const results = await Promise.all(queryPromises);
let rows: Array<Model>;
for (const appendedResult of results) {
if (!rows) {
rows = appendedResult.rows;
if (rows.length == 0) {
return [];
}
const modelOptions = templateModel['_options'];
for (const row of rows) {
row['_options'] = {
...row['_options'],
include: modelOptions['include'],
includeNames: modelOptions['includeNames'],
includeMap: modelOptions['includeMap'],
};
}
continue;
}
for (let i = 0; i < appendedResult.rows.length; i++) {
const appendingRow = appendedResult.rows[i];
const key = appendedResult.include.association;
const val = appendingRow.get(key);
const rowKey = appendingRow.get(primaryKey);
const targetIndex = rows.findIndex((row) => row.get(primaryKey) === rowKey);
if (targetIndex === -1) {
throw new Error('target row not found');
}
rows[targetIndex].set(key, val, {
2022-10-13 10:44:12 +00:00
raw: true,
});
}
}
return rows;
}
export function md5(value: string) {
return crypto.createHash('md5').update(value).digest('hex');
}
2022-10-14 06:51:19 +00:00
const MAX_IDENTIFIER_LENGTH = 63;
export function checkIdentifier(value: string) {
if (value.length > MAX_IDENTIFIER_LENGTH) {
throw new IdentifierError(`Identifier ${value} is too long`);
}
}
feat: provide the underscored option for the database (#1366) * feat: underscored options * feat: underscored using hook * feat: database underscored options * feat: underscored env * fix: collectionExistsInDb * fix: test * fix: nocobase install * fix: test * fix: belongsTo association * fix: test of underscored * chore: console.log * fix: list action test * fix: dump test * chore: snakeCase algo * fix: underscored field create * fix: underscored env * fix(acl): custom appends merge strategy (#1416) * Update index.md * fix(plugin-workflow): use promise to request (#1426) * Update index.md * Update collection.md * Update index.md * Update index.md * Update collection.md * Update field.md * Update repository.md * Update has-one-repository.md * Update has-many-repository.md * Update belongs-to-many-repository.md * Update index.md * chore: translate 'Add tab' in page header (#1424) * fix: test * fix: workflow test * fix: underscored with inherits * fix: underscored test * fix: process.env.DB_UNDERSCORED * fix: process.env.DB_UNDERSCORED === 'true' * fix: test * fix: pg test * fix: underscored table name * feat: tableName & fieldName conflict check * fix: test * fix: underscored index * fix: update field unique index * fix: sync default value * fix: collection manager create field * chore: field sync * fix: pg test * chore: test * fix: test * chore: default constraint name * chore: syncUniqueIndex * feat: field destory before check * feat: field type check * fix: test * fix: test * fix: improve * fix: should destroy when fields refer to the same field * fix: acl meta with underscored --------- Co-authored-by: chenos <chenlinxh@gmail.com>
2023-02-13 13:38:47 +00:00
export function getTableName(collectionName: string, options) {
return options.underscored ? snakeCase(collectionName) : collectionName;
}
export function snakeCase(name: string) {
return require('sequelize').Utils.underscore(name);
}