namespace-guard 0.1.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.
@@ -0,0 +1,36 @@
1
+ import { NamespaceAdapter } from '../index.js';
2
+
3
+ type KnexQueryBuilder = {
4
+ select: (columns: string[]) => KnexQueryBuilder;
5
+ where: (column: string, value: unknown) => KnexQueryBuilder;
6
+ first: () => Promise<Record<string, unknown> | undefined>;
7
+ };
8
+ type KnexInstance = {
9
+ (tableName: string): KnexQueryBuilder;
10
+ };
11
+ /**
12
+ * Create a namespace adapter for Knex
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * import Knex from "knex";
17
+ * import { createNamespaceGuard } from "namespace-guard";
18
+ * import { createKnexAdapter } from "namespace-guard/adapters/knex";
19
+ *
20
+ * const knex = Knex({ client: "pg", connection: process.env.DATABASE_URL });
21
+ *
22
+ * const guard = createNamespaceGuard(
23
+ * {
24
+ * reserved: ["admin", "api", "settings"],
25
+ * sources: [
26
+ * { name: "users", column: "handle", scopeKey: "id" },
27
+ * { name: "organizations", column: "slug", scopeKey: "id" },
28
+ * ],
29
+ * },
30
+ * createKnexAdapter(knex)
31
+ * );
32
+ * ```
33
+ */
34
+ declare function createKnexAdapter(knex: KnexInstance): NamespaceAdapter;
35
+
36
+ export { createKnexAdapter };
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/adapters/knex.ts
21
+ var knex_exports = {};
22
+ __export(knex_exports, {
23
+ createKnexAdapter: () => createKnexAdapter
24
+ });
25
+ module.exports = __toCommonJS(knex_exports);
26
+ function createKnexAdapter(knex) {
27
+ return {
28
+ async findOne(source, value) {
29
+ const idColumn = source.idColumn ?? "id";
30
+ const columns = source.scopeKey && source.scopeKey !== idColumn ? [idColumn, source.scopeKey] : [idColumn];
31
+ const row = await knex(source.name).select(columns).where(source.column, value).first();
32
+ return row ?? null;
33
+ }
34
+ };
35
+ }
36
+ // Annotate the CommonJS export names for ESM import in node:
37
+ 0 && (module.exports = {
38
+ createKnexAdapter
39
+ });
@@ -0,0 +1,14 @@
1
+ // src/adapters/knex.ts
2
+ function createKnexAdapter(knex) {
3
+ return {
4
+ async findOne(source, value) {
5
+ const idColumn = source.idColumn ?? "id";
6
+ const columns = source.scopeKey && source.scopeKey !== idColumn ? [idColumn, source.scopeKey] : [idColumn];
7
+ const row = await knex(source.name).select(columns).where(source.column, value).first();
8
+ return row ?? null;
9
+ }
10
+ };
11
+ }
12
+ export {
13
+ createKnexAdapter
14
+ };
@@ -0,0 +1,37 @@
1
+ import { NamespaceAdapter } from '../index.mjs';
2
+
3
+ type KyselyQueryBuilder = {
4
+ select: (columns: string[]) => KyselyQueryBuilder;
5
+ where: (column: string, operator: string, value: unknown) => KyselyQueryBuilder;
6
+ limit: (limit: number) => KyselyQueryBuilder;
7
+ executeTakeFirst: () => Promise<Record<string, unknown> | undefined>;
8
+ };
9
+ type KyselyDb = {
10
+ selectFrom: (table: string) => KyselyQueryBuilder;
11
+ };
12
+ /**
13
+ * Create a namespace adapter for Kysely
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { Kysely, PostgresDialect } from "kysely";
18
+ * import { createNamespaceGuard } from "namespace-guard";
19
+ * import { createKyselyAdapter } from "namespace-guard/adapters/kysely";
20
+ *
21
+ * const db = new Kysely<Database>({ dialect: new PostgresDialect({ pool }) });
22
+ *
23
+ * const guard = createNamespaceGuard(
24
+ * {
25
+ * reserved: ["admin", "api", "settings"],
26
+ * sources: [
27
+ * { name: "users", column: "handle", scopeKey: "id" },
28
+ * { name: "organizations", column: "slug", scopeKey: "id" },
29
+ * ],
30
+ * },
31
+ * createKyselyAdapter(db)
32
+ * );
33
+ * ```
34
+ */
35
+ declare function createKyselyAdapter(db: KyselyDb): NamespaceAdapter;
36
+
37
+ export { createKyselyAdapter };
@@ -0,0 +1,37 @@
1
+ import { NamespaceAdapter } from '../index.js';
2
+
3
+ type KyselyQueryBuilder = {
4
+ select: (columns: string[]) => KyselyQueryBuilder;
5
+ where: (column: string, operator: string, value: unknown) => KyselyQueryBuilder;
6
+ limit: (limit: number) => KyselyQueryBuilder;
7
+ executeTakeFirst: () => Promise<Record<string, unknown> | undefined>;
8
+ };
9
+ type KyselyDb = {
10
+ selectFrom: (table: string) => KyselyQueryBuilder;
11
+ };
12
+ /**
13
+ * Create a namespace adapter for Kysely
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { Kysely, PostgresDialect } from "kysely";
18
+ * import { createNamespaceGuard } from "namespace-guard";
19
+ * import { createKyselyAdapter } from "namespace-guard/adapters/kysely";
20
+ *
21
+ * const db = new Kysely<Database>({ dialect: new PostgresDialect({ pool }) });
22
+ *
23
+ * const guard = createNamespaceGuard(
24
+ * {
25
+ * reserved: ["admin", "api", "settings"],
26
+ * sources: [
27
+ * { name: "users", column: "handle", scopeKey: "id" },
28
+ * { name: "organizations", column: "slug", scopeKey: "id" },
29
+ * ],
30
+ * },
31
+ * createKyselyAdapter(db)
32
+ * );
33
+ * ```
34
+ */
35
+ declare function createKyselyAdapter(db: KyselyDb): NamespaceAdapter;
36
+
37
+ export { createKyselyAdapter };
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/adapters/kysely.ts
21
+ var kysely_exports = {};
22
+ __export(kysely_exports, {
23
+ createKyselyAdapter: () => createKyselyAdapter
24
+ });
25
+ module.exports = __toCommonJS(kysely_exports);
26
+ function createKyselyAdapter(db) {
27
+ return {
28
+ async findOne(source, value) {
29
+ const idColumn = source.idColumn ?? "id";
30
+ const columns = source.scopeKey && source.scopeKey !== idColumn ? [idColumn, source.scopeKey] : [idColumn];
31
+ const row = await db.selectFrom(source.name).select(columns).where(source.column, "=", value).limit(1).executeTakeFirst();
32
+ return row ?? null;
33
+ }
34
+ };
35
+ }
36
+ // Annotate the CommonJS export names for ESM import in node:
37
+ 0 && (module.exports = {
38
+ createKyselyAdapter
39
+ });
@@ -0,0 +1,14 @@
1
+ // src/adapters/kysely.ts
2
+ function createKyselyAdapter(db) {
3
+ return {
4
+ async findOne(source, value) {
5
+ const idColumn = source.idColumn ?? "id";
6
+ const columns = source.scopeKey && source.scopeKey !== idColumn ? [idColumn, source.scopeKey] : [idColumn];
7
+ const row = await db.selectFrom(source.name).select(columns).where(source.column, "=", value).limit(1).executeTakeFirst();
8
+ return row ?? null;
9
+ }
10
+ };
11
+ }
12
+ export {
13
+ createKyselyAdapter
14
+ };
@@ -0,0 +1,36 @@
1
+ import { NamespaceAdapter } from '../index.mjs';
2
+
3
+ type PrismaClient = {
4
+ [key: string]: {
5
+ findFirst: (args: {
6
+ where: Record<string, unknown>;
7
+ select?: Record<string, boolean>;
8
+ }) => Promise<Record<string, unknown> | null>;
9
+ };
10
+ };
11
+ /**
12
+ * Create a namespace adapter for Prisma
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * import { PrismaClient } from "@prisma/client";
17
+ * import { createNamespaceGuard } from "namespace-guard";
18
+ * import { createPrismaAdapter } from "namespace-guard/adapters/prisma";
19
+ *
20
+ * const prisma = new PrismaClient();
21
+ *
22
+ * const guard = createNamespaceGuard(
23
+ * {
24
+ * reserved: ["admin", "api", "settings"],
25
+ * sources: [
26
+ * { name: "user", column: "handle", scopeKey: "userId" },
27
+ * { name: "organization", column: "slug", scopeKey: "orgId" },
28
+ * ],
29
+ * },
30
+ * createPrismaAdapter(prisma)
31
+ * );
32
+ * ```
33
+ */
34
+ declare function createPrismaAdapter(prisma: PrismaClient): NamespaceAdapter;
35
+
36
+ export { createPrismaAdapter };
@@ -0,0 +1,36 @@
1
+ import { NamespaceAdapter } from '../index.js';
2
+
3
+ type PrismaClient = {
4
+ [key: string]: {
5
+ findFirst: (args: {
6
+ where: Record<string, unknown>;
7
+ select?: Record<string, boolean>;
8
+ }) => Promise<Record<string, unknown> | null>;
9
+ };
10
+ };
11
+ /**
12
+ * Create a namespace adapter for Prisma
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * import { PrismaClient } from "@prisma/client";
17
+ * import { createNamespaceGuard } from "namespace-guard";
18
+ * import { createPrismaAdapter } from "namespace-guard/adapters/prisma";
19
+ *
20
+ * const prisma = new PrismaClient();
21
+ *
22
+ * const guard = createNamespaceGuard(
23
+ * {
24
+ * reserved: ["admin", "api", "settings"],
25
+ * sources: [
26
+ * { name: "user", column: "handle", scopeKey: "userId" },
27
+ * { name: "organization", column: "slug", scopeKey: "orgId" },
28
+ * ],
29
+ * },
30
+ * createPrismaAdapter(prisma)
31
+ * );
32
+ * ```
33
+ */
34
+ declare function createPrismaAdapter(prisma: PrismaClient): NamespaceAdapter;
35
+
36
+ export { createPrismaAdapter };
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/adapters/prisma.ts
21
+ var prisma_exports = {};
22
+ __export(prisma_exports, {
23
+ createPrismaAdapter: () => createPrismaAdapter
24
+ });
25
+ module.exports = __toCommonJS(prisma_exports);
26
+ function createPrismaAdapter(prisma) {
27
+ return {
28
+ async findOne(source, value) {
29
+ const model = prisma[source.name];
30
+ if (!model) {
31
+ throw new Error(`Prisma model "${source.name}" not found`);
32
+ }
33
+ const idColumn = source.idColumn ?? "id";
34
+ return model.findFirst({
35
+ where: { [source.column]: value },
36
+ select: {
37
+ [idColumn]: true,
38
+ ...source.scopeKey ? { [source.scopeKey]: true } : {}
39
+ }
40
+ });
41
+ }
42
+ };
43
+ }
44
+ // Annotate the CommonJS export names for ESM import in node:
45
+ 0 && (module.exports = {
46
+ createPrismaAdapter
47
+ });
@@ -0,0 +1,22 @@
1
+ // src/adapters/prisma.ts
2
+ function createPrismaAdapter(prisma) {
3
+ return {
4
+ async findOne(source, value) {
5
+ const model = prisma[source.name];
6
+ if (!model) {
7
+ throw new Error(`Prisma model "${source.name}" not found`);
8
+ }
9
+ const idColumn = source.idColumn ?? "id";
10
+ return model.findFirst({
11
+ where: { [source.column]: value },
12
+ select: {
13
+ [idColumn]: true,
14
+ ...source.scopeKey ? { [source.scopeKey]: true } : {}
15
+ }
16
+ });
17
+ }
18
+ };
19
+ }
20
+ export {
21
+ createPrismaAdapter
22
+ };
@@ -0,0 +1,34 @@
1
+ import { NamespaceAdapter } from '../index.mjs';
2
+
3
+ type QueryExecutor = (sql: string, params: unknown[]) => Promise<{
4
+ rows: Record<string, unknown>[];
5
+ }>;
6
+ /**
7
+ * Create a namespace adapter for raw SQL queries
8
+ *
9
+ * Works with any database client that can execute parameterized queries
10
+ * (pg, mysql2, better-sqlite3, etc.)
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { Pool } from "pg";
15
+ * import { createNamespaceGuard } from "namespace-guard";
16
+ * import { createRawAdapter } from "namespace-guard/adapters/raw";
17
+ *
18
+ * const pool = new Pool();
19
+ *
20
+ * const guard = createNamespaceGuard(
21
+ * {
22
+ * reserved: ["admin", "api", "settings"],
23
+ * sources: [
24
+ * { name: "users", column: "handle", scopeKey: "id" },
25
+ * { name: "organizations", column: "slug", scopeKey: "id" },
26
+ * ],
27
+ * },
28
+ * createRawAdapter((sql, params) => pool.query(sql, params))
29
+ * );
30
+ * ```
31
+ */
32
+ declare function createRawAdapter(execute: QueryExecutor): NamespaceAdapter;
33
+
34
+ export { createRawAdapter };
@@ -0,0 +1,34 @@
1
+ import { NamespaceAdapter } from '../index.js';
2
+
3
+ type QueryExecutor = (sql: string, params: unknown[]) => Promise<{
4
+ rows: Record<string, unknown>[];
5
+ }>;
6
+ /**
7
+ * Create a namespace adapter for raw SQL queries
8
+ *
9
+ * Works with any database client that can execute parameterized queries
10
+ * (pg, mysql2, better-sqlite3, etc.)
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { Pool } from "pg";
15
+ * import { createNamespaceGuard } from "namespace-guard";
16
+ * import { createRawAdapter } from "namespace-guard/adapters/raw";
17
+ *
18
+ * const pool = new Pool();
19
+ *
20
+ * const guard = createNamespaceGuard(
21
+ * {
22
+ * reserved: ["admin", "api", "settings"],
23
+ * sources: [
24
+ * { name: "users", column: "handle", scopeKey: "id" },
25
+ * { name: "organizations", column: "slug", scopeKey: "id" },
26
+ * ],
27
+ * },
28
+ * createRawAdapter((sql, params) => pool.query(sql, params))
29
+ * );
30
+ * ```
31
+ */
32
+ declare function createRawAdapter(execute: QueryExecutor): NamespaceAdapter;
33
+
34
+ export { createRawAdapter };
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/adapters/raw.ts
21
+ var raw_exports = {};
22
+ __export(raw_exports, {
23
+ createRawAdapter: () => createRawAdapter
24
+ });
25
+ module.exports = __toCommonJS(raw_exports);
26
+ function createRawAdapter(execute) {
27
+ return {
28
+ async findOne(source, value) {
29
+ const idColumn = source.idColumn ?? "id";
30
+ const columns = source.scopeKey && source.scopeKey !== idColumn ? `"${idColumn}", "${source.scopeKey}"` : `"${idColumn}"`;
31
+ const sql = `SELECT ${columns} FROM "${source.name}" WHERE "${source.column}" = $1 LIMIT 1`;
32
+ const result = await execute(sql, [value]);
33
+ return result.rows[0] ?? null;
34
+ }
35
+ };
36
+ }
37
+ // Annotate the CommonJS export names for ESM import in node:
38
+ 0 && (module.exports = {
39
+ createRawAdapter
40
+ });
@@ -0,0 +1,15 @@
1
+ // src/adapters/raw.ts
2
+ function createRawAdapter(execute) {
3
+ return {
4
+ async findOne(source, value) {
5
+ const idColumn = source.idColumn ?? "id";
6
+ const columns = source.scopeKey && source.scopeKey !== idColumn ? `"${idColumn}", "${source.scopeKey}"` : `"${idColumn}"`;
7
+ const sql = `SELECT ${columns} FROM "${source.name}" WHERE "${source.column}" = $1 LIMIT 1`;
8
+ const result = await execute(sql, [value]);
9
+ return result.rows[0] ?? null;
10
+ }
11
+ };
12
+ }
13
+ export {
14
+ createRawAdapter
15
+ };
package/dist/cli.d.mts ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+ declare function run(argv?: string[]): Promise<number>;
3
+
4
+ export { run };
package/dist/cli.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+ declare function run(argv?: string[]): Promise<number>;
3
+
4
+ export { run };