Skip to content

Commit

Permalink
feat: detect the key type
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanCassiere committed Jun 25, 2024
1 parent c17763d commit 2e0ea66
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/utils/db.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { env } from "@/config/env";
import { createId } from "@paralleldrive/cuid2";

const dbPrefixes = {
Expand All @@ -9,19 +10,23 @@ const dbPrefixes = {
} as const;
const dbEnv = {
live: "live_",
test: "test_",
dev: "dev_",
};
/**
* Returns a unique database ID
* @param key
* @param env
* @param key_env
* @returns
* @example
* ```ts
* createDbId("user", "live");
* //=> "user_live_01B1E5Z5KQZ
* ```
*/
export const createDbId = (key: keyof typeof dbPrefixes, env: keyof typeof dbEnv = "live") => {
return [dbPrefixes[key], dbEnv[env], createId()].filter(Boolean).join("");
export const createDbId = (
key: keyof typeof dbPrefixes,
key_env: keyof typeof dbEnv = env.NODE_ENV === "production" ? "live" : "dev",
) => {
return [dbPrefixes[key], dbEnv[key_env], createId()].filter(Boolean).join("");
};

0 comments on commit 2e0ea66

Please sign in to comment.