@evenicanpm/portal-user-db 1.5.0 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evenicanpm/portal-user-db",
3
- "version": "1.5.0",
3
+ "version": "1.6.0",
4
4
  "description": "",
5
5
  "license": "ISC",
6
6
  "author": "",
@@ -17,7 +17,8 @@
17
17
  ".": "./src/index.ts"
18
18
  },
19
19
  "files": [
20
- "dist"
20
+ "dist",
21
+ "src"
21
22
  ],
22
23
  "types": "./dist/index.d.ts",
23
24
  "devDependencies": {
@@ -33,5 +34,5 @@
33
34
  "kysely": "^0.28.10",
34
35
  "pg": "^8.17.2"
35
36
  },
36
- "gitHead": "5c5e3fe19b92a17cbaf897b4cee20ee59331603f"
37
+ "gitHead": "9b18fe6294cc0e57332ec8703825a609ac7b73b2"
37
38
  }
package/src/db/db.ts ADDED
@@ -0,0 +1,29 @@
1
+ /**
2
+ * This file was generated by kysely-codegen.
3
+ * Please do not edit it manually.
4
+ */
5
+
6
+ import type { ColumnType } from "kysely";
7
+
8
+ export type Generated<T> =
9
+ T extends ColumnType<infer S, infer I, infer U>
10
+ ? ColumnType<S, I | undefined, U>
11
+ : ColumnType<T, T | undefined, T>;
12
+
13
+ export type Timestamp = ColumnType<Date, Date | string, Date | string>;
14
+
15
+ export interface SchemaMigrations {
16
+ version: string;
17
+ }
18
+
19
+ export interface UsersUsers {
20
+ createdat: Generated<Timestamp | null>;
21
+ email: string;
22
+ id: Generated<string>;
23
+ user_role: string;
24
+ }
25
+
26
+ export interface DB {
27
+ schema_migrations: SchemaMigrations;
28
+ "users.users": UsersUsers;
29
+ }
@@ -0,0 +1,43 @@
1
+ import { Kysely, type PostgresDialect } from "kysely";
2
+ import type { DB } from "./db";
3
+ export type { DB } from "./db";
4
+
5
+ /**
6
+ * User service for user related queries
7
+ * @param dialect
8
+ * @returns
9
+ */
10
+ const createUserService = (dialect: PostgresDialect) => {
11
+ const db = new Kysely<DB>({ dialect });
12
+ return {
13
+ find: async (input: { email: string }) => {
14
+ return await db
15
+ .selectFrom("users.users")
16
+ .selectAll()
17
+ .where("email", "=", input?.email)
18
+
19
+ .executeTakeFirst();
20
+ },
21
+ hasAnyUsers: async () => {
22
+ const result = await db
23
+ .selectFrom("users.users")
24
+ .select("email")
25
+ .limit(1)
26
+ .executeTakeFirst();
27
+
28
+ return !!result;
29
+ },
30
+ create: async (input: { email: string; userRole: string }) => {
31
+ return await db
32
+ .insertInto("users.users")
33
+ .values({
34
+ email: input.email,
35
+ user_role: input.userRole,
36
+ })
37
+ .returningAll()
38
+ .executeTakeFirst();
39
+ },
40
+ };
41
+ };
42
+
43
+ export { createUserService as userService };
@@ -0,0 +1,15 @@
1
+ -- migrate:up
2
+ CREATE SCHEMA IF NOT EXISTS users;
3
+
4
+ -- Used for gen_random_uuid() but could also just do serial for poc
5
+ CREATE EXTENSION IF NOT EXISTS pgcrypto;
6
+
7
+ CREATE TABLE users.users (
8
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
9
+ email text NOT NULL UNIQUE,
10
+ user_role text NOT NULL,
11
+ createdat timestamptz DEFAULT now()
12
+ );
13
+
14
+ -- migrate:down
15
+ DROP TABLE IF EXISTS users.users;
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { type DB, userService } from "./db";