Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const statement = primary
? [`table.increments('${column}').primary()`]
: specificType
? [`table.specificType('${column}', '${specificType}')`]
: [`table.${knexType}('${column}')`]
statement.push(
unsigned && 'unsigned()',
!primary && required && 'notNullable()',
nullable && 'nullable()',
unique && 'unique()',
index && 'index()'
)
if (_default !== undefined) {
let value = defaultValues[_default]
if (!value) {
value = isArray(_default) || isObject(_default)
? JSON.stringify(_default)
: _default
if (isString(value)) {
value = `'${value}'`
}
}
statement.push(`defaultTo(${value})`)
}
if (foreign) {
for (const relation of Object.values(relations)) {
// TODO: Support composite keys for foreign references:
// Use `asArray(from)`, `asArray(to)`
const { from, to, owner } = relation
const [fromClass, fromProperty] = from?.split('.') || []
if (fromProperty === name) {
if (fromClass !== modelClass.name) {
schema = {
type: 'array',
items: schema.length > 1 ? schema : schema[0],
// The array short-forms sets an empty array as the default.
default: []
}
} else if (
// Expand objects to `type: 'object'`...
isObject(schema) &&
!(
// ...but only if they don't define any of these properties:
isString(schema.type) ||
isString(schema.$ref) ||
isArray(schema.allOf) ||
isArray(schema.anyOf) ||
isArray(schema.oneOf) ||
isObject(schema.not)
)
) {
schema = {
type: 'object',
properties: {
...schema
},
additionalProperties: false
}
}
return schema
}
unwrapListData(data) {
if (
this.isListSource &&
isObject(data) &&
isNumber(data.total) &&
isArray(data.results)
) {
// If @ditojs/server sends data in the form of `{ results, total }`
// replace the value with result, but remember the total in the store.
this.total = data.total
this.value = data.results
return this.value
}
},
insert(data, returning) {
// Only PostgreSQL is able to insert multiple entries at once it seems,
// all others have to fall back on insertGraph() to do so for now:
return !this.isPostgreSQL() && isArray(data) && data.length > 1
? this.insertGraph(data)
: super.insert(data, returning)
}
filterData(data) {
// Filters out arrays and objects that are back by data resources
// themselves, as those are already taking care of through their own API
// resource end-points and shouldn't be set.
const copy = {}
for (const [key, value] of Object.entries(data)) {
if (isArray(value) || isObject(value)) {
if (this.getComponent(key)?.providesData) {
continue
}
}
copy[key] = value
}
return copy
},
get(accept) {
return isArray(accept) ? accept.join(',') : accept
}
}),
_handle(method, event, callback) {
if (isString(event)) {
super[method](event, callback)
} else if (isArray(event)) {
for (const ev of event) {
super[method](ev, callback)
}
} else if (isPlainObject(event)) {
for (const key in event) {
super[method](key, event[key])
}
}
return this
}
function hasDefaults(obj) {
if (isArray(obj)) {
for (const val of obj) {
if (val && hasDefaults(val)) {
return true
}
}
} else if (isObject(obj)) {
for (const [key, val] of Object.entries(obj)) {
if (key === 'default' || val && hasDefaults(val)) {
return true
}
}
}
return false
}