@evenicanpm/portal-user-db 1.4.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/.env ADDED
@@ -0,0 +1,4 @@
1
+ # Connects to local Strapi PostgreSQL database server
2
+ # Ensure the database server is running and accessible at the specified URL
3
+ # Adjust the credentials and database name as necessary
4
+ DATABASE_URL=postgres://admin:admin@127.0.0.1:5432/strapi?sslmode=disable
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ ## Users Db
2
+
3
+ - Lite Backend Pattern
4
+ - Allows for having RBAC and user management while running e4 headless without e4API
5
+ - For e4 Headless implementations that use NextJS Api Routes as backend instead of full e4API
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@evenicanpm/portal-user-db",
3
+ "version": "1.4.0",
4
+ "description": "",
5
+ "license": "ISC",
6
+ "author": "",
7
+ "type": "module",
8
+ "scripts": {
9
+ "kysely-codegen": "kysely-codegen",
10
+ "test": "echo \"Error: no test specified\" && exit 1",
11
+ "docs": "npx typedoc src --plugin typedoc-github-theme",
12
+ "db:seed": "tsx ./src/db/seeds/view-config.ts",
13
+ "format": "npx @biomejs/biome format --write ./src",
14
+ "build": "tsc"
15
+ },
16
+ "exports": {
17
+ ".": "./src/index.ts"
18
+ },
19
+ "types": "./dist/index.d.ts",
20
+ "devDependencies": {
21
+ "@types/pg": "^8.16.0",
22
+ "kysely-codegen": "^0.19.0",
23
+ "tsx": "^4.21.0",
24
+ "typedoc": "^0.28.15",
25
+ "typedoc-github-theme": "^0.3.1",
26
+ "typescript": "^5.6.3"
27
+ },
28
+ "dependencies": {
29
+ "@arrows/multimethod": "^2.1.0",
30
+ "kysely": "^0.28.10",
31
+ "pg": "^8.17.2"
32
+ },
33
+ "gitHead": "0614726ec6b7ce9564706514a6b756b390a13e96"
34
+ }
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";
package/tsconfig.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES6",
4
+ "useDefineForClassFields": true,
5
+ "lib": ["ES2022", "DOM", "DOM.Iterable"],
6
+ "module": "ESNext",
7
+ "sourceMap": true,
8
+ "esModuleInterop": true,
9
+ "skipLibCheck": true,
10
+ "declaration": true,
11
+ "preserveSymlinks": true,
12
+ "moduleResolution": "Node",
13
+ "resolveJsonModule": true,
14
+ "isolatedModules": true,
15
+ "emitDeclarationOnly": true,
16
+ "jsx": "preserve",
17
+ "strict": true,
18
+ "noUnusedLocals": true,
19
+ "noUnusedParameters": true,
20
+ "noFallthroughCasesInSwitch": true,
21
+ "allowJs": true,
22
+ "forceConsistentCasingInFileNames": true,
23
+ "incremental": true,
24
+ "baseUrl": "./",
25
+ "outDir": "./dist"
26
+ },
27
+ "include": ["src"]
28
+ }