Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// @flow
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)
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(
propEq('type', 'belongs_to'),
prop('key'),
always(columnName('id')),
)
// @flow
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]
}
)
async function fetchLocalChangesForCollection(
collection: Collection,
): Promise<[SyncTableChangeSet, T[]]> {
const changedRecords = await collection.query(notSyncedQuery).fetch()
const changeSet = {
created: rawsForStatus('created', changedRecords),
updated: rawsForStatus('updated', changedRecords),
deleted: await collection.database.adapter.getDeletedRecords(collection.table),
}
return [changeSet, changedRecords]
}
const extractChanges = map(([changeSet]) => changeSet)
const extractAllAffectedRecords = pipe(
values,
map(([, records]) => records),
unnest,
)
export default function fetchLocalChanges(db: Database): Promise {
ensureActionsEnabled(db)
return db.action(async () => {
const changes = await promiseAllObject(
map(
fetchLocalChangesForCollection,
// $FlowFixMe
db.collections.map,
),
)
// TODO: deep-freeze changes object (in dev mode only) to detect mutations (user bug)
const encodeAndOr: LokiKeyword => (And | Or) => LokiRawQuery = op =>
pipe(
prop('conditions'),
map(encodeCondition),
objOf(op),
)
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),
})
const groupByTable: (On[]) => On[][] = pipe(
groupBy(prop('table')),
values,
)
const zipAssociationsConditions: (AssociationArgs[], On[]) => [AssociationArgs, On[]][] = (
associations,
conditions,
) => zip(associations, groupByTable(conditions))
const encodeJoins: (AssociationArgs[], On[]) => LokiJoin[] = (associations, on) => {
const conditions = zipAssociationsConditions(associations, on)
return map(([association, _on]) => encodeJoin(association, _on), conditions)
}
export default function encodeQuery(query: SerializedQuery): LokiQuery {
const {
[typeEq('where'), encodeWhereDescription],
]): any)(where)
const encodeAnd: And => Matcher<*> = pipe(
prop('conditions'),
map(encodeWhere),
allPass,
)
const encodeOr: Or => Matcher<*> = pipe(
prop('conditions'),
map(encodeWhere),
anyPass,
)
const encodeConditions: (Where[]) => Matcher<*> = pipe(
map(encodeWhere),
allPass,
)
export default function encodeMatcher(query: QueryDescription): Matcher<element> {
const { join, where } = query
invariant(!join.length, `Queries with joins can't be encoded into a matcher`)
return (encodeConditions(where): any)
}
</element>
/integrationTest/,
/__mocks__/,
/\.DS_Store/,
/package\.json/,
]
const isNotIncludedInBuildPaths = value => !anymatch(DO_NOT_BUILD_PATHS, value)
const cleanFolder = dir => rimraf.sync(dir)
const takeFiles = pipe(
prop('path'),
both(endsWith('.js'), isNotIncludedInBuildPaths),
)
const takeModules = pipe(
filter(takeFiles),
map(prop('path')),
)
const removeSourcePath = replace(SOURCE_PATH, '')
const createModulePath = format => {
const formatPathSegment = format === CJS_MODULES ? [] : [format]
const modulePath = resolvePath(DIR_PATH, ...formatPathSegment)
return replace(SOURCE_PATH, modulePath)
}
const createFolder = dir => mkdirp.sync(resolvePath(dir))
const babelTransform = (format, file) => {
if (format === SRC_MODULES) {
const typeEq = propEq('type')
const encodeWhere: Where => Matcher<*> = where =>
(cond([
[typeEq('and'), encodeAnd],
[typeEq('or'), encodeOr],
[typeEq('where'), encodeWhereDescription],
]): any)(where)
const encodeAnd: And => Matcher<*> = pipe(
prop('conditions'),
map(encodeWhere),
allPass,
)
const encodeOr: Or => Matcher<*> = pipe(
prop('conditions'),
map(encodeWhere),
anyPass,
)
const encodeConditions: (Where[]) => Matcher<*> = pipe(
map(encodeWhere),
allPass,
)
export default function encodeMatcher(query: QueryDescription): Matcher<element> {
const { join, where } = query
invariant(!join.length, `Queries with joins can't be encoded into a matcher`)
return (encodeConditions(where): any)</element>
const DO_NOT_BUILD_PATHS = [
/__tests__/,
/adapters\/__tests__/,
/test\.js/,
/integrationTest/,
/__mocks__/,
/\.DS_Store/,
/package\.json/,
]
const isNotIncludedInBuildPaths = value => !anymatch(DO_NOT_BUILD_PATHS, value)
const cleanFolder = dir => rimraf.sync(dir)
const takeFiles = pipe(
prop('path'),
both(endsWith('.js'), isNotIncludedInBuildPaths),
)
const takeModules = pipe(
filter(takeFiles),
map(prop('path')),
)
const removeSourcePath = replace(SOURCE_PATH, '')
const createModulePath = format => {
const formatPathSegment = format === CJS_MODULES ? [] : [format]
const modulePath = resolvePath(DIR_PATH, ...formatPathSegment)
return replace(SOURCE_PATH, modulePath)
}