2021-06-27 00:09:46 +02:00
|
|
|
import { SqlQuery } from "../definitions/datasource"
|
2021-07-03 12:15:01 +02:00
|
|
|
import { Datasource } from "../definitions/common"
|
|
|
|
import { SourceNames } from "../definitions/datasource"
|
2021-06-16 17:27:33 +02:00
|
|
|
const { DocumentTypes, SEPARATOR } = require("../db/utils")
|
2021-06-18 13:29:25 +02:00
|
|
|
const { FieldTypes } = require("../constants")
|
2021-06-16 17:27:33 +02:00
|
|
|
|
2021-06-17 15:42:30 +02:00
|
|
|
const DOUBLE_SEPARATOR = `${SEPARATOR}${SEPARATOR}`
|
|
|
|
|
2021-06-25 14:46:02 +02:00
|
|
|
export function isExternalTable(tableId: string) {
|
2021-06-16 17:27:33 +02:00
|
|
|
return tableId.includes(DocumentTypes.DATASOURCE)
|
|
|
|
}
|
|
|
|
|
2021-06-25 14:46:02 +02:00
|
|
|
export function buildExternalTableId(datasourceId: string, tableName: string) {
|
2021-06-17 15:42:30 +02:00
|
|
|
return `${datasourceId}${DOUBLE_SEPARATOR}${tableName}`
|
2021-06-16 17:27:33 +02:00
|
|
|
}
|
|
|
|
|
2021-06-25 14:46:02 +02:00
|
|
|
export function breakExternalTableId(tableId: string) {
|
2021-06-17 15:42:30 +02:00
|
|
|
const parts = tableId.split(DOUBLE_SEPARATOR)
|
2021-06-16 17:27:33 +02:00
|
|
|
let tableName = parts.pop()
|
2021-06-17 15:42:30 +02:00
|
|
|
// if they need joined
|
|
|
|
let datasourceId = parts.join(DOUBLE_SEPARATOR)
|
2021-06-16 17:27:33 +02:00
|
|
|
return { datasourceId, tableName }
|
|
|
|
}
|
2021-06-16 19:38:00 +02:00
|
|
|
|
2021-06-25 14:46:02 +02:00
|
|
|
export function generateRowIdField(keyProps: any[] = []) {
|
2021-06-16 19:38:00 +02:00
|
|
|
if (!Array.isArray(keyProps)) {
|
|
|
|
keyProps = [keyProps]
|
|
|
|
}
|
|
|
|
// this conserves order and types
|
2021-07-05 16:14:45 +02:00
|
|
|
// we have to swap the double quotes to single quotes for use in HBS statements
|
|
|
|
// when using the literal helper the double quotes can break things
|
2021-06-24 00:35:53 +02:00
|
|
|
return encodeURIComponent(JSON.stringify(keyProps).replace(/"/g, "'"))
|
2021-06-16 19:38:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// should always return an array
|
2021-09-01 20:40:47 +02:00
|
|
|
export function breakRowIdField(_id: string | { _id: string }): any[] {
|
2021-06-16 19:38:00 +02:00
|
|
|
if (!_id) {
|
2021-07-05 18:33:36 +02:00
|
|
|
return []
|
2021-06-16 19:38:00 +02:00
|
|
|
}
|
2021-07-05 16:14:45 +02:00
|
|
|
// have to replace on the way back as we swapped out the double quotes
|
|
|
|
// when encoding, but JSON can't handle the single quotes
|
2021-09-01 20:40:47 +02:00
|
|
|
const id = typeof _id === "string" ? _id : _id._id
|
|
|
|
const decoded: string = decodeURIComponent(id).replace(/'/g, '"')
|
2021-08-05 20:24:29 +02:00
|
|
|
try {
|
|
|
|
const parsed = JSON.parse(decoded)
|
|
|
|
return Array.isArray(parsed) ? parsed : [parsed]
|
|
|
|
} catch (err) {
|
|
|
|
// wasn't json - likely was handlebars for a many to many
|
|
|
|
return [_id]
|
|
|
|
}
|
2021-06-16 19:38:00 +02:00
|
|
|
}
|
2021-06-18 13:29:25 +02:00
|
|
|
|
2021-06-25 14:46:02 +02:00
|
|
|
export function convertType(type: string, map: { [key: string]: any }) {
|
2021-06-18 13:29:25 +02:00
|
|
|
for (let [external, internal] of Object.entries(map)) {
|
|
|
|
if (type.toLowerCase().includes(external)) {
|
|
|
|
return internal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FieldTypes.STRING
|
|
|
|
}
|
2021-06-25 14:46:02 +02:00
|
|
|
|
|
|
|
export function getSqlQuery(query: SqlQuery | string): SqlQuery {
|
|
|
|
if (typeof query === "string") {
|
|
|
|
return { sql: query }
|
|
|
|
} else {
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
}
|
2021-07-03 12:15:01 +02:00
|
|
|
|
|
|
|
export function isSQL(datasource: Datasource): boolean {
|
|
|
|
if (!datasource || !datasource.source) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const SQL = [SourceNames.POSTGRES, SourceNames.SQL_SERVER, SourceNames.MYSQL]
|
|
|
|
return SQL.indexOf(datasource.source) !== -1
|
|
|
|
}
|
2021-07-12 11:51:30 +02:00
|
|
|
|
|
|
|
export function isIsoDateString(str: string) {
|
|
|
|
if (!/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(str)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
let d = new Date(str)
|
|
|
|
return d.toISOString() === str
|
|
|
|
}
|
2021-09-22 18:46:54 +02:00
|
|
|
|
|
|
|
// add the existing relationships from the entities if they exist, to prevent them from being overridden
|
|
|
|
export function copyExistingPropsOver(tableName: string, tables: { [key: string]: any }, entities: { [key: string]: any }) {
|
|
|
|
if (entities && entities[tableName]) {
|
|
|
|
if (entities[tableName].primaryDisplay) {
|
|
|
|
tables[tableName].primaryDisplay = entities[tableName].primaryDisplay
|
|
|
|
}
|
|
|
|
const existingTableSchema = entities[tableName].schema
|
|
|
|
for (let key in existingTableSchema) {
|
|
|
|
if (!existingTableSchema.hasOwnProperty(key)) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (existingTableSchema[key].type === "link") {
|
|
|
|
tables[tableName].schema[key] = existingTableSchema[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|