Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { ensureDecoratorUsedProperly } from '../common'
// Defines a model property that's (de)serialized to and from JSON using custom sanitizer function.
//
// Pass the database column name as first argument, and sanitizer function as second.
//
// Stored value will be parsed to JSON if possible, and passed to sanitizer as argument, or
// undefined will be passed on parsing error. Field value will be result of sanitizer call.
//
// Value assigned to field will be passed to sanitizer and its results will be stored as stringified
// value.
//
// Examples:
// @json('contact_info', jsonValue => jasonValue || {}) contactInfo: ContactInfo
const parseJSON = tryCatch(JSON.parse, always(undefined))
export const jsonDecorator = makeDecorator(
(rawFieldName: ColumnName, sanitizer: any => any) => (
target: Object,
key: string,
descriptor: Object,
) => {
ensureDecoratorUsedProperly(rawFieldName, target, key, descriptor)
return {
configurable: true,
enumerable: true,
get(): any {
const rawValue = this.asModel._getRaw(rawFieldName)
const parsedValue = parseJSON(rawValue)
// Note: empty query returns `undefined` because
// Loki's Collection.count() works but count({}) doesn't
const concatRawQueries: (LokiRawQuery[]) => LokiRawQuery = (cond([
[lengthEq(0), always(undefined)],
[lengthEq(1), head],
[T, objOf('$and')],
]): any)
const encodeConditions: (Where[] | On[]) => LokiRawQuery = pipe(
conditions => map(encodeCondition, conditions),
concatRawQueries,
)
const encodeMapKey: AssociationInfo => ColumnName = ifElse(
propEq('type', 'belongs_to'),
always(columnName('id')),
prop('foreignKey'),
)
const encodeJoinKey: AssociationInfo => ColumnName = ifElse(
propEq('type', 'belongs_to'),
prop('key'),
always(columnName('id')),
)
const encodeOriginalConditions: (On[]) => Where[] = map(({ left, comparison }) => ({
type: 'where',
left,
comparison,
}))
const encodeJoin: (AssociationArgs, On[]) => LokiJoin = ([table, associationInfo], conditions) => ({
objOf(op),
)
const encodeAnd: And => LokiRawQuery = encodeAndOr('$and')
const encodeOr: Or => LokiRawQuery = encodeAndOr('$or')
const lengthEq = n =>
pipe(
length,
identical(n),
)
// Note: empty query returns `undefined` because
// Loki's Collection.count() works but count({}) doesn't
const concatRawQueries: (LokiRawQuery[]) => LokiRawQuery = (cond([
[lengthEq(0), always(undefined)],
[lengthEq(1), head],
[T, objOf('$and')],
]): any)
const encodeConditions: (Where[] | On[]) => LokiRawQuery = pipe(
conditions => map(encodeCondition, conditions),
concatRawQueries,
)
const encodeMapKey: AssociationInfo => ColumnName = ifElse(
propEq('type', 'belongs_to'),
always(columnName('id')),
prop('foreignKey'),
)
const encodeJoinKey: AssociationInfo => ColumnName = ifElse(
import { pipe, join, keys, values, always, map } from 'rambdax'
import type Model from '../../../Model'
import type { SQLiteQuery } from '../index'
import encodeName from '../encodeName'
const columnNames = pipe(
keys,
map(encodeName),
join(', '),
)
const valuePlaceholders = pipe(
values,
map(always('?')),
join(', '),
)
export default function encodeInsert(model: Model): SQLiteQuery {
const { _raw: raw, table } = model
const sql = `insert into ${table} (${columnNames(raw)}) values (${valuePlaceholders(raw)})`
const args = values(raw)
return [sql, args]
}
const encodeConditions: (Where[] | On[]) => LokiRawQuery = pipe(
conditions => map(encodeCondition, conditions),
concatRawQueries,
)
const encodeMapKey: AssociationInfo => ColumnName = ifElse(
propEq('type', 'belongs_to'),
always(columnName('id')),
prop('foreignKey'),
)
const encodeJoinKey: AssociationInfo => ColumnName = ifElse(
propEq('type', 'belongs_to'),
prop('key'),
always(columnName('id')),
)
const encodeOriginalConditions: (On[]) => Where[] = map(({ left, comparison }) => ({
type: 'where',
left,
comparison,
}))
const encodeJoin: (AssociationArgs, On[]) => LokiJoin = ([table, associationInfo], conditions) => ({
table,
query: encodeConditions(conditions),
originalConditions: encodeOriginalConditions(conditions),
mapKey: encodeMapKey(associationInfo),
joinKey: encodeJoinKey(associationInfo),
})