LoginSignup
2
0

More than 5 years have passed since last update.

firebaseのRealtimeDatabaseをtypescriptでいじりたい(webpack + typescript + firebase_admin)

Last updated at Posted at 2018-08-23

RealtimeDatabaseのデータをmigrationとかでいじりたいときあるっすよね。
想定してないデータができないようにルールを使うと思うんですが、
自分はtypescriptでルール通りに型をつくってデータを作成するようにすると安心できるので気に入ってます。

typescriptのコンパイルでwebpackを使ったんですが構成は下記みたいな感じ↓
https://github.com/chucker34/firebase_admin_pack

ちょっと詰まったところはwebpack.config.jsのresolve.mainFieldsmainを優先するようにしないとTypeError: rtdb.initStandalone is not a functionと怒られたこと。
俺たちのstackoverflowで同じように困っている人がいましたよと。
https://stackoverflow.com/questions/48266195/typeerror-rtdb-initstandalone-is-not-a-function
target: nodeにしてもブラウザ向けのファイルが使われていたのが原因。webpackだから普通そうなるけどね。

// webpack.config.js
const Dotenv = require('dotenv-webpack');

module.exports = {
  target: 'node',
  mode: 'development',
  entry: './src/main.ts',
  output: {
    path: `${__dirname}/dist`,
    filename: 'main.js'
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: 'ts-loader'
      }
    ]
  },
  resolve: {
    extensions: [ '.ts', '.js', '.json' ],
    mainFields: ['main', 'module']
  },
  plugins: [
    new Dotenv()
  ]
};
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0