How to use rambdax - 10 common examples

To help you get started, we’ve selected a few rambdax examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Nozbe / WatermelonDB / scripts / publish.js View on Github external
const buildTasks = options => {
  const { version } = options

  const isPrerelease = includes('-', version)
  const tag = isPrerelease ? 'next' : 'latest'

  // eslint-disable-next-line
  console.warn(`Will publish with NPM tag ${tag}`)

  return [
    {
      title: 'ping npm registry',
      task: () =>
        timeout(
          execa('npm', ['ping']).catch(throwError('connection to npm registry failed')),
          5000,
          'Connection to npm registry timed out',
        ),
    },
    ...(isPrerelease
github Nozbe / WatermelonDB / src / Schema / migrations / index.js View on Github external
+toVersion: SchemaVersion,
  +steps: MigrationStep[],
}>

type SchemaMigrationsSpec = $Exact<{
  +migrations: Migration[],
}>

export type SchemaMigrations = $Exact<{
  +validated: true,
  +minVersion: SchemaVersion,
  +maxVersion: SchemaVersion,
  +sortedMigrations: Migration[],
}>

const sortMigrations = sortBy(prop('toVersion'))

// Creates a specification of how to migrate between different versions of
// database schema. Every time you change the database schema, you must
// create a corresponding migration.
//
// See docs for more details
//
// Example:
//
// schemaMigrations({
//   migrations: [
//     {
//       toVersion: 3,
//       steps: [
//         createTable({
//           name: 'comments',
github Nozbe / WatermelonDB / src / adapters / lokijs / worker / encodeQuery / index.js View on Github external
// 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) => ({
  table,
github Nozbe / WatermelonDB / scripts / make.js View on Github external
const buildFile = file => {
    if (file.match(/\.js$/)) {
      buildSrcModule(file)
      buildCjsModule(file)
    } else if (file.match(/\.js$/)) {
      fs.copySync(file, path.join(DEV_PATH, replace(SOURCE_PATH, '', file)))
    } else {
      // native files
      fs.copySync(file, path.join(DEV_PATH, replace(resolvePath(), '', file)))
    }
  }
github Nozbe / WatermelonDB / src / sync / impl / fetchLocal.js View on Github external
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')
}
github Nozbe / WatermelonDB / src / sync / impl / fetchLocal.js View on Github external
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
github Nozbe / WatermelonDB / src / Schema / migrations / helpers.js View on Github external
// @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) {
github Nozbe / WatermelonDB / src / adapters / sqlite / encodeUpdate / index.js View on Github external
// @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]
}
github Nozbe / WatermelonDB / scripts / make.js View on Github external
/\.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)
github Nozbe / WatermelonDB / src / adapters / sqlite / encodeInsert / index.js View on Github external
// @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)