(projectId: string, options: Options)
| 25 | } |
| 26 | |
| 27 | export function getRulesConfig(projectId: string, options: Options): RulesInstanceConfig[] { |
| 28 | const dbConfig = options.config.src.database; |
| 29 | if (dbConfig === undefined) { |
| 30 | return []; |
| 31 | } |
| 32 | |
| 33 | const rc = options.rc; |
| 34 | let allDatabases = !options.only; |
| 35 | const onlyDatabases = new Set<string>(); |
| 36 | if (options.only) { |
| 37 | const split = options.only.split(","); |
| 38 | if (split.includes("database")) { |
| 39 | allDatabases = true; |
| 40 | } else { |
| 41 | for (const value of split) { |
| 42 | if (value.startsWith("database:")) { |
| 43 | const target = value.split(":")[1]; |
| 44 | onlyDatabases.add(target); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | if (!Array.isArray(dbConfig)) { |
| 51 | if (dbConfig && dbConfig.rules) { |
| 52 | utils.assertIsStringOrUndefined(options.instance); |
| 53 | const instance = options.instance || `${options.project}-default-rtdb`; |
| 54 | return [{ rules: dbConfig.rules, instance }]; |
| 55 | } else { |
| 56 | logger.debug("Possibly invalid database config: ", JSON.stringify(dbConfig)); |
| 57 | return []; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | const results: RulesInstanceConfig[] = []; |
| 62 | for (const c of dbConfig) { |
| 63 | const { instance, target } = c; |
| 64 | if (target) { |
| 65 | if (allDatabases || onlyDatabases.has(target)) { |
| 66 | // Make sure the target exists (this will throw otherwise) |
| 67 | rc.requireTarget(projectId, "database", target); |
| 68 | // Get a list of db instances the target maps to |
| 69 | const instances = rc.target(projectId, "database", target); |
| 70 | for (const i of instances) { |
| 71 | results.push({ instance: i, rules: c.rules }); |
| 72 | } |
| 73 | onlyDatabases.delete(target); |
| 74 | } |
| 75 | } else if (instance) { |
| 76 | if (allDatabases) { |
| 77 | results.push(c as RulesInstanceConfig); |
| 78 | } |
| 79 | } else { |
| 80 | throw new FirebaseError('Must supply either "target" or "instance" in database config'); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if (!allDatabases && onlyDatabases.size !== 0) { |
no test coverage detected
searching dependent graphs…