2.0.4 • Published 9 days ago

alter-firestore v2.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
9 days ago

alter-firestore

Description

This npm package is a CLI that executes TypeScript files to manage Firestore migration or alteration processes within your repository.

demo-alter-firestore

installation

Please set the 'type' field in your package.json to 'module' since this package supports Pure ESM.

{
  "type": "module"
}
$ npm i alter-firestore
$ ts-node-esm node_modules/.bin/alfs
# or
$ node --loader ts-node/esm node_modules/.bin/alfs

or

{
  "scripts": {
    "alfs": "ts-node-esm node_modules/.bin/alfs"
  }
}

then

$ npm run alfs -- migrate --project staging
$ npm run alfs -- migrate # default project will be emulator

set .alfsrc.js

export default {
  emulator: {
    options: {
      projectId: '',
    },
    ports: {
      auth: '', // ex: 9099
      firestore: '', // ex: 8080
      storage: '', // ex: 9199
    },
  },
  credentialPaths: {
    '{this key use for --project option}': '', // ex: staging: './.envs/staging/firebase-admin-sdk.json'
  },
  migration: {
    collectionName: 'migrations', // <-- default
    directoryPath: 'migrations-files', // <-- default ex: firestore/migrations/files
  },
  alter: {
    directoryPath: 'alter-files', // <-- default ex: firestore/alters/files
    aliases: {
      '{key}': ['{filepath}'],
    },
  },
}

Migration File

import { ActionArg } from 'alter-firestore/types/command'
import { MigrateOptions, RefreshOptions } from 'alter-firestore/types/options'

export const up = async ({ firestore }: ActionArg<MigrateOptions>) => {
  const snapshot = await firestore.getDoc('{path}', '{documentId}')
  if (snapshot.exists) {
    throw Error(`${'{path}'}${'{documentId}'}/${snapshot.id} is already exists`)
  }
  return firestore.create('{path}', '{documentId}', value)
}

export const down = async ({ firestore }: ActionArg<RefreshOptions>) => {
  const snapshot = await firestore.getDoc(PATH, DOCUMENT_ID)
  if (!snapshot.exists) {
    throw Error(`${PATH}${DOCUMENT_ID}/${snapshot.id} is not exists`)
  }
  return firestore.recursiveDelete(PATH, snapshot.ref)
}

Alter File

import { ActionArg } from 'alter-firestore/types/command'
import { AlterOptions } from 'alter-firestore/types/options'

export const exec = async ({ firestore }: ActionArg<AlterOptions>) => {
  const snapshot = await firestore.getDoc('{path}', '{documentId}')
  if (snapshot.exists) {
    throw Error(`${'{path}'}${'{documentId}'}/${snapshot.id} is already exists`)
  }
  return firestore.create('{path}', '{documentId}', value)
}

Commands

commanddescription
alterrun target file
generategenerate templates
migratemigrate firestore collection / document from target directory files
migrate:rollbackrollback the previous step
migrate:refreshrollback the previous step and migrate

Implementation Plan

  • migrate without cli
  • enrich log
2.0.3

9 days ago

2.0.4

9 days ago

2.0.2

2 months ago

2.0.1

2 months ago

1.0.5

7 months ago

1.0.4

7 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.3

10 months ago

1.0.0

12 months ago

0.1.1

1 year ago

0.1.0

1 year ago

0.0.0

1 year ago