Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)
return {
// $FlowFixMe
changes: extractChanges(changes),
affectedRecords: extractAllAffectedRecords(changes),
}
}, 'sync-fetchLocalChanges')
}
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)
return {
// $FlowFixMe
// @flow
import { prop, pipe, map } from 'rambdax'
import { unnest } from '../../utils/fp'
import { type SchemaMigrations, type MigrationStep } from './index'
import { type SchemaVersion } from '../index'
const getAllSteps = pipe(
map(prop('steps')),
unnest,
)
export function stepsForMigration({
migrations: schemaMigrations,
fromVersion,
toVersion,
}: $Exact<{
migrations: SchemaMigrations,
fromVersion: SchemaVersion,
toVersion: SchemaVersion,
}>): ?(MigrationStep[]) {
const { sortedMigrations, minVersion, maxVersion } = schemaMigrations
// see if migrations in this range are available
if (fromVersion < minVersion || toVersion > maxVersion) {
// @flow
import { pipe, map, join, keys, values, append } from 'rambdax'
import type { TableName } from '../../../Schema'
import type { RawRecord } from '../../../RawRecord'
import type { SQL, SQLiteQuery, SQLiteArg } from '../index'
import encodeName from '../encodeName'
const encodeSetPlaceholders: RawRecord => SQL = pipe(
keys,
map(encodeName),
map(key => `${key}=?`),
join(', '),
)
const getArgs: RawRecord => SQLiteArg[] = raw =>
pipe(
values,
append(raw.id), // for `where id is ?`
)(raw)
export default function encodeUpdate(table: TableName, raw: RawRecord): SQLiteQuery {
const sql = `update ${encodeName(table)} set ${encodeSetPlaceholders(raw)} where "id" is ?`
const args = getArgs(raw)
return [sql, args]
}
/\.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) {
// no transform, just return source
return fs.readFileSync(file)
const allLinks = bookmarksContent
.split('\n')
.s(reject(includes('gist.')))
.s(reject(includes('?tab')))
.s(reject(includes('trending')))
.s(filter(x => x.includes('github.com') || x.includes('npmjs')))
const withCorrectLinks = await mapAsync(async x => {
if (x.includes('github.com')) return x
const url = await toGithubURL(x)
return url
}, allLinks)
return withCorrectLinks.s(filter(Boolean)).s(
map(x => {
const replaced = replace(/(git:)|(ssh:)/, 'https:', x)
return remove('git@', replaced)
})
)
}
return {
type: 'on',
table,
left: whereDescription.left,
comparison: whereDescription.comparison,
}
}
const syncStatusColumn = columnName('_status')
const getJoins: (Condition[]) => [On[], Where[]] = (partition(propEq('type', 'on')): any)
const whereNotDeleted = where(syncStatusColumn, notEq('deleted'))
const joinsWithoutDeleted = pipe(
map(prop('table')),
uniq,
map(table => on(table, syncStatusColumn, notEq('deleted'))),
)
export function buildQueryDescription(conditions: Condition[]): QueryDescription {
const [join, whereConditions] = getJoins(conditions)
return { join, where: whereConditions }
}
export function queryWithoutDeleted(query: QueryDescription): QueryDescription {
const { join, where: whereConditions } = query
return {
join: [...join, ...joinsWithoutDeleted(join)],
where: [...whereConditions, whereNotDeleted],
}
}
const encodeAndOr: LokiKeyword => (And | Or) => LokiRawQuery = op =>
pipe(
prop('conditions'),
map(encodeCondition),
objOf(op),
)
return operator(left, right)
}
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</element>
const encodeJoins: (AssociationArgs[], On[]) => LokiJoin[] = (associations, on) => {
const conditions = zipAssociationsConditions(associations, on)
return map(([association, _on]) => encodeJoin(association, _on), conditions)
}