@sudobility/entity_service 1.0.7 → 1.0.8
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/dist/helpers/EntityHelper.js +25 -29
- package/dist/helpers/EntityHelper.js.map +1 -1
- package/dist/helpers/EntityMemberHelper.js +26 -30
- package/dist/helpers/EntityMemberHelper.js.map +1 -1
- package/dist/helpers/InvitationHelper.js +28 -32
- package/dist/helpers/InvitationHelper.js.map +1 -1
- package/dist/helpers/PermissionHelper.js +13 -17
- package/dist/helpers/PermissionHelper.js.map +1 -1
- package/dist/helpers/index.js +4 -11
- package/dist/helpers/index.js.map +1 -1
- package/dist/index.js +9 -38
- package/dist/index.js.map +1 -1
- package/dist/middleware/hono.js +18 -24
- package/dist/middleware/hono.js.map +1 -1
- package/dist/middleware/index.js +1 -8
- package/dist/middleware/index.js.map +1 -1
- package/dist/migrations/001_add_entities.js +2 -6
- package/dist/migrations/001_add_entities.js.map +1 -1
- package/dist/migrations/index.js +1 -6
- package/dist/migrations/index.js.map +1 -1
- package/dist/schema/entities.js +125 -135
- package/dist/schema/entities.js.map +1 -1
- package/dist/types/index.js +1 -8
- package/dist/types/index.js.map +1 -1
- package/dist/utils/index.js +1 -17
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/slug-generator.js +6 -14
- package/dist/utils/slug-generator.js.map +1 -1
- package/package.json +9 -16
- package/dist/helpers/EntityHelper.cjs +0 -253
- package/dist/helpers/EntityMemberHelper.cjs +0 -254
- package/dist/helpers/InvitationHelper.cjs +0 -256
- package/dist/helpers/PermissionHelper.cjs +0 -193
- package/dist/helpers/index.cjs +0 -15
- package/dist/index.cjs +0 -76
- package/dist/middleware/hono.cjs +0 -148
- package/dist/middleware/index.cjs +0 -12
- package/dist/migrations/001_add_entities.cjs +0 -330
- package/dist/migrations/index.cjs +0 -10
- package/dist/schema/entities.cjs +0 -311
- package/dist/types/index.cjs +0 -14
- package/dist/utils/index.cjs +0 -21
- package/dist/utils/slug-generator.cjs +0 -92
package/dist/schema/entities.cjs
DELETED
|
@@ -1,311 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* @fileoverview Drizzle Schema for Entity/Organization Tables
|
|
4
|
-
* @description Database schema definitions for entities, members, and invitations
|
|
5
|
-
*
|
|
6
|
-
* Provides:
|
|
7
|
-
* - Factory functions for custom PostgreSQL schemas
|
|
8
|
-
* - Default tables for public schema
|
|
9
|
-
* - Initialization functions for table creation
|
|
10
|
-
*/
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.entityInvitations = exports.entityMembers = exports.entities = void 0;
|
|
13
|
-
exports.createEntitiesTable = createEntitiesTable;
|
|
14
|
-
exports.createEntitiesTablePublic = createEntitiesTablePublic;
|
|
15
|
-
exports.createEntityMembersTable = createEntityMembersTable;
|
|
16
|
-
exports.createEntityMembersTablePublic = createEntityMembersTablePublic;
|
|
17
|
-
exports.createEntityInvitationsTable = createEntityInvitationsTable;
|
|
18
|
-
exports.createEntityInvitationsTablePublic = createEntityInvitationsTablePublic;
|
|
19
|
-
exports.initEntityTables = initEntityTables;
|
|
20
|
-
const pg_core_1 = require("drizzle-orm/pg-core");
|
|
21
|
-
// ========================================
|
|
22
|
-
// ENTITIES TABLE
|
|
23
|
-
// ========================================
|
|
24
|
-
/**
|
|
25
|
-
* Create an entities table for a specific PostgreSQL schema.
|
|
26
|
-
* Note: Ownership is tracked via entity_members table (role = 'owner').
|
|
27
|
-
*
|
|
28
|
-
* @param schema - The Drizzle pgSchema object
|
|
29
|
-
* @param indexPrefix - Prefix for index names to avoid conflicts
|
|
30
|
-
* @returns Drizzle table definition
|
|
31
|
-
*/
|
|
32
|
-
function createEntitiesTable(schema, indexPrefix) {
|
|
33
|
-
return schema.table('entities', {
|
|
34
|
-
id: (0, pg_core_1.uuid)('id').primaryKey().defaultRandom(),
|
|
35
|
-
entity_slug: (0, pg_core_1.varchar)('entity_slug', { length: 12 }).notNull().unique(),
|
|
36
|
-
entity_type: (0, pg_core_1.varchar)('entity_type', { length: 20 }).notNull(),
|
|
37
|
-
display_name: (0, pg_core_1.varchar)('display_name', { length: 255 }).notNull(),
|
|
38
|
-
description: (0, pg_core_1.text)('description'),
|
|
39
|
-
avatar_url: (0, pg_core_1.text)('avatar_url'),
|
|
40
|
-
created_at: (0, pg_core_1.timestamp)('created_at', { withTimezone: true }).defaultNow(),
|
|
41
|
-
updated_at: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true }).defaultNow(),
|
|
42
|
-
}, (table) => ({
|
|
43
|
-
slugIdx: (0, pg_core_1.uniqueIndex)(`${indexPrefix}_entities_slug_idx`).on(table.entity_slug),
|
|
44
|
-
typeIdx: (0, pg_core_1.index)(`${indexPrefix}_entities_type_idx`).on(table.entity_type),
|
|
45
|
-
}));
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Create an entities table for the public schema.
|
|
49
|
-
* Note: Ownership is tracked via entity_members table (role = 'owner').
|
|
50
|
-
*/
|
|
51
|
-
function createEntitiesTablePublic(indexPrefix) {
|
|
52
|
-
return (0, pg_core_1.pgTable)('entities', {
|
|
53
|
-
id: (0, pg_core_1.uuid)('id').primaryKey().defaultRandom(),
|
|
54
|
-
entity_slug: (0, pg_core_1.varchar)('entity_slug', { length: 12 }).notNull().unique(),
|
|
55
|
-
entity_type: (0, pg_core_1.varchar)('entity_type', { length: 20 }).notNull(),
|
|
56
|
-
display_name: (0, pg_core_1.varchar)('display_name', { length: 255 }).notNull(),
|
|
57
|
-
description: (0, pg_core_1.text)('description'),
|
|
58
|
-
avatar_url: (0, pg_core_1.text)('avatar_url'),
|
|
59
|
-
created_at: (0, pg_core_1.timestamp)('created_at', { withTimezone: true }).defaultNow(),
|
|
60
|
-
updated_at: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true }).defaultNow(),
|
|
61
|
-
}, (table) => ({
|
|
62
|
-
slugIdx: (0, pg_core_1.uniqueIndex)(`${indexPrefix}_entities_slug_idx`).on(table.entity_slug),
|
|
63
|
-
typeIdx: (0, pg_core_1.index)(`${indexPrefix}_entities_type_idx`).on(table.entity_type),
|
|
64
|
-
}));
|
|
65
|
-
}
|
|
66
|
-
// ========================================
|
|
67
|
-
// ENTITY MEMBERS TABLE
|
|
68
|
-
// ========================================
|
|
69
|
-
/**
|
|
70
|
-
* Create an entity_members table for a specific PostgreSQL schema.
|
|
71
|
-
* This table manages all user-entity relationships including ownership.
|
|
72
|
-
* Role can be: owner, admin, manager, viewer
|
|
73
|
-
*/
|
|
74
|
-
function createEntityMembersTable(schema, indexPrefix) {
|
|
75
|
-
return schema.table('entity_members', {
|
|
76
|
-
id: (0, pg_core_1.uuid)('id').primaryKey().defaultRandom(),
|
|
77
|
-
entity_id: (0, pg_core_1.uuid)('entity_id').notNull(),
|
|
78
|
-
user_id: (0, pg_core_1.varchar)('user_id', { length: 128 }).notNull(), // firebase_uid
|
|
79
|
-
role: (0, pg_core_1.varchar)('role', { length: 20 }).notNull(),
|
|
80
|
-
is_active: (0, pg_core_1.boolean)('is_active').notNull().default(true),
|
|
81
|
-
joined_at: (0, pg_core_1.timestamp)('joined_at', { withTimezone: true }).defaultNow(),
|
|
82
|
-
created_at: (0, pg_core_1.timestamp)('created_at', { withTimezone: true }).defaultNow(),
|
|
83
|
-
updated_at: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true }).defaultNow(),
|
|
84
|
-
}, (table) => ({
|
|
85
|
-
entityUserUniqueIdx: (0, pg_core_1.uniqueIndex)(`${indexPrefix}_entity_members_entity_user_idx`).on(table.entity_id, table.user_id),
|
|
86
|
-
entityIdx: (0, pg_core_1.index)(`${indexPrefix}_entity_members_entity_idx`).on(table.entity_id),
|
|
87
|
-
userIdx: (0, pg_core_1.index)(`${indexPrefix}_entity_members_user_idx`).on(table.user_id),
|
|
88
|
-
activeIdx: (0, pg_core_1.index)(`${indexPrefix}_entity_members_active_idx`).on(table.is_active),
|
|
89
|
-
}));
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Create an entity_members table for the public schema.
|
|
93
|
-
* This table manages all user-entity relationships including ownership.
|
|
94
|
-
* Role can be: owner, admin, manager, viewer
|
|
95
|
-
*/
|
|
96
|
-
function createEntityMembersTablePublic(indexPrefix) {
|
|
97
|
-
return (0, pg_core_1.pgTable)('entity_members', {
|
|
98
|
-
id: (0, pg_core_1.uuid)('id').primaryKey().defaultRandom(),
|
|
99
|
-
entity_id: (0, pg_core_1.uuid)('entity_id').notNull(),
|
|
100
|
-
user_id: (0, pg_core_1.varchar)('user_id', { length: 128 }).notNull(), // firebase_uid
|
|
101
|
-
role: (0, pg_core_1.varchar)('role', { length: 20 }).notNull(),
|
|
102
|
-
is_active: (0, pg_core_1.boolean)('is_active').notNull().default(true),
|
|
103
|
-
joined_at: (0, pg_core_1.timestamp)('joined_at', { withTimezone: true }).defaultNow(),
|
|
104
|
-
created_at: (0, pg_core_1.timestamp)('created_at', { withTimezone: true }).defaultNow(),
|
|
105
|
-
updated_at: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true }).defaultNow(),
|
|
106
|
-
}, (table) => ({
|
|
107
|
-
entityUserUniqueIdx: (0, pg_core_1.uniqueIndex)(`${indexPrefix}_entity_members_entity_user_idx`).on(table.entity_id, table.user_id),
|
|
108
|
-
entityIdx: (0, pg_core_1.index)(`${indexPrefix}_entity_members_entity_idx`).on(table.entity_id),
|
|
109
|
-
userIdx: (0, pg_core_1.index)(`${indexPrefix}_entity_members_user_idx`).on(table.user_id),
|
|
110
|
-
activeIdx: (0, pg_core_1.index)(`${indexPrefix}_entity_members_active_idx`).on(table.is_active),
|
|
111
|
-
}));
|
|
112
|
-
}
|
|
113
|
-
// ========================================
|
|
114
|
-
// ENTITY INVITATIONS TABLE
|
|
115
|
-
// ========================================
|
|
116
|
-
/**
|
|
117
|
-
* Create an entity_invitations table for a specific PostgreSQL schema.
|
|
118
|
-
*/
|
|
119
|
-
function createEntityInvitationsTable(schema, indexPrefix) {
|
|
120
|
-
return schema.table('entity_invitations', {
|
|
121
|
-
id: (0, pg_core_1.uuid)('id').primaryKey().defaultRandom(),
|
|
122
|
-
entity_id: (0, pg_core_1.uuid)('entity_id').notNull(),
|
|
123
|
-
email: (0, pg_core_1.varchar)('email', { length: 255 }).notNull(),
|
|
124
|
-
role: (0, pg_core_1.varchar)('role', { length: 20 }).notNull(),
|
|
125
|
-
status: (0, pg_core_1.varchar)('status', { length: 20 }).notNull().default('pending'),
|
|
126
|
-
invited_by_user_id: (0, pg_core_1.varchar)('invited_by_user_id', { length: 128 }).notNull(), // firebase_uid
|
|
127
|
-
token: (0, pg_core_1.varchar)('token', { length: 64 }).notNull().unique(),
|
|
128
|
-
expires_at: (0, pg_core_1.timestamp)('expires_at', { withTimezone: true }).notNull(),
|
|
129
|
-
accepted_at: (0, pg_core_1.timestamp)('accepted_at', { withTimezone: true }),
|
|
130
|
-
created_at: (0, pg_core_1.timestamp)('created_at', { withTimezone: true }).defaultNow(),
|
|
131
|
-
updated_at: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true }).defaultNow(),
|
|
132
|
-
}, (table) => ({
|
|
133
|
-
tokenIdx: (0, pg_core_1.uniqueIndex)(`${indexPrefix}_entity_invitations_token_idx`).on(table.token),
|
|
134
|
-
entityIdx: (0, pg_core_1.index)(`${indexPrefix}_entity_invitations_entity_idx`).on(table.entity_id),
|
|
135
|
-
emailIdx: (0, pg_core_1.index)(`${indexPrefix}_entity_invitations_email_idx`).on(table.email),
|
|
136
|
-
statusIdx: (0, pg_core_1.index)(`${indexPrefix}_entity_invitations_status_idx`).on(table.status),
|
|
137
|
-
}));
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Create an entity_invitations table for the public schema.
|
|
141
|
-
*/
|
|
142
|
-
function createEntityInvitationsTablePublic(indexPrefix) {
|
|
143
|
-
return (0, pg_core_1.pgTable)('entity_invitations', {
|
|
144
|
-
id: (0, pg_core_1.uuid)('id').primaryKey().defaultRandom(),
|
|
145
|
-
entity_id: (0, pg_core_1.uuid)('entity_id').notNull(),
|
|
146
|
-
email: (0, pg_core_1.varchar)('email', { length: 255 }).notNull(),
|
|
147
|
-
role: (0, pg_core_1.varchar)('role', { length: 20 }).notNull(),
|
|
148
|
-
status: (0, pg_core_1.varchar)('status', { length: 20 }).notNull().default('pending'),
|
|
149
|
-
invited_by_user_id: (0, pg_core_1.varchar)('invited_by_user_id', { length: 128 }).notNull(), // firebase_uid
|
|
150
|
-
token: (0, pg_core_1.varchar)('token', { length: 64 }).notNull().unique(),
|
|
151
|
-
expires_at: (0, pg_core_1.timestamp)('expires_at', { withTimezone: true }).notNull(),
|
|
152
|
-
accepted_at: (0, pg_core_1.timestamp)('accepted_at', { withTimezone: true }),
|
|
153
|
-
created_at: (0, pg_core_1.timestamp)('created_at', { withTimezone: true }).defaultNow(),
|
|
154
|
-
updated_at: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true }).defaultNow(),
|
|
155
|
-
}, (table) => ({
|
|
156
|
-
tokenIdx: (0, pg_core_1.uniqueIndex)(`${indexPrefix}_entity_invitations_token_idx`).on(table.token),
|
|
157
|
-
entityIdx: (0, pg_core_1.index)(`${indexPrefix}_entity_invitations_entity_idx`).on(table.entity_id),
|
|
158
|
-
emailIdx: (0, pg_core_1.index)(`${indexPrefix}_entity_invitations_email_idx`).on(table.email),
|
|
159
|
-
statusIdx: (0, pg_core_1.index)(`${indexPrefix}_entity_invitations_status_idx`).on(table.status),
|
|
160
|
-
}));
|
|
161
|
-
}
|
|
162
|
-
// ========================================
|
|
163
|
-
// DEFAULT TABLES (Public Schema)
|
|
164
|
-
// ========================================
|
|
165
|
-
/** Default entities table for public schema */
|
|
166
|
-
exports.entities = (0, pg_core_1.pgTable)('entities', {
|
|
167
|
-
id: (0, pg_core_1.uuid)('id').primaryKey().defaultRandom(),
|
|
168
|
-
entity_slug: (0, pg_core_1.varchar)('entity_slug', { length: 12 }).notNull().unique(),
|
|
169
|
-
entity_type: (0, pg_core_1.varchar)('entity_type', { length: 20 }).notNull(),
|
|
170
|
-
display_name: (0, pg_core_1.varchar)('display_name', { length: 255 }).notNull(),
|
|
171
|
-
description: (0, pg_core_1.text)('description'),
|
|
172
|
-
avatar_url: (0, pg_core_1.text)('avatar_url'),
|
|
173
|
-
created_at: (0, pg_core_1.timestamp)('created_at', { withTimezone: true }).defaultNow(),
|
|
174
|
-
updated_at: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true }).defaultNow(),
|
|
175
|
-
}, (table) => ({
|
|
176
|
-
slugIdx: (0, pg_core_1.uniqueIndex)('entities_slug_idx').on(table.entity_slug),
|
|
177
|
-
typeIdx: (0, pg_core_1.index)('entities_type_idx').on(table.entity_type),
|
|
178
|
-
}));
|
|
179
|
-
/** Default entity_members table for public schema */
|
|
180
|
-
exports.entityMembers = (0, pg_core_1.pgTable)('entity_members', {
|
|
181
|
-
id: (0, pg_core_1.uuid)('id').primaryKey().defaultRandom(),
|
|
182
|
-
entity_id: (0, pg_core_1.uuid)('entity_id').notNull(),
|
|
183
|
-
user_id: (0, pg_core_1.varchar)('user_id', { length: 128 }).notNull(), // firebase_uid
|
|
184
|
-
role: (0, pg_core_1.varchar)('role', { length: 20 }).notNull(),
|
|
185
|
-
is_active: (0, pg_core_1.boolean)('is_active').notNull().default(true),
|
|
186
|
-
joined_at: (0, pg_core_1.timestamp)('joined_at', { withTimezone: true }).defaultNow(),
|
|
187
|
-
created_at: (0, pg_core_1.timestamp)('created_at', { withTimezone: true }).defaultNow(),
|
|
188
|
-
updated_at: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true }).defaultNow(),
|
|
189
|
-
}, (table) => ({
|
|
190
|
-
entityUserUniqueIdx: (0, pg_core_1.uniqueIndex)('entity_members_entity_user_idx').on(table.entity_id, table.user_id),
|
|
191
|
-
entityIdx: (0, pg_core_1.index)('entity_members_entity_idx').on(table.entity_id),
|
|
192
|
-
userIdx: (0, pg_core_1.index)('entity_members_user_idx').on(table.user_id),
|
|
193
|
-
activeIdx: (0, pg_core_1.index)('entity_members_active_idx').on(table.is_active),
|
|
194
|
-
}));
|
|
195
|
-
/** Default entity_invitations table for public schema */
|
|
196
|
-
exports.entityInvitations = (0, pg_core_1.pgTable)('entity_invitations', {
|
|
197
|
-
id: (0, pg_core_1.uuid)('id').primaryKey().defaultRandom(),
|
|
198
|
-
entity_id: (0, pg_core_1.uuid)('entity_id').notNull(),
|
|
199
|
-
email: (0, pg_core_1.varchar)('email', { length: 255 }).notNull(),
|
|
200
|
-
role: (0, pg_core_1.varchar)('role', { length: 20 }).notNull(),
|
|
201
|
-
status: (0, pg_core_1.varchar)('status', { length: 20 }).notNull().default('pending'),
|
|
202
|
-
invited_by_user_id: (0, pg_core_1.varchar)('invited_by_user_id', { length: 128 }).notNull(), // firebase_uid
|
|
203
|
-
token: (0, pg_core_1.varchar)('token', { length: 64 }).notNull().unique(),
|
|
204
|
-
expires_at: (0, pg_core_1.timestamp)('expires_at', { withTimezone: true }).notNull(),
|
|
205
|
-
accepted_at: (0, pg_core_1.timestamp)('accepted_at', { withTimezone: true }),
|
|
206
|
-
created_at: (0, pg_core_1.timestamp)('created_at', { withTimezone: true }).defaultNow(),
|
|
207
|
-
updated_at: (0, pg_core_1.timestamp)('updated_at', { withTimezone: true }).defaultNow(),
|
|
208
|
-
}, (table) => ({
|
|
209
|
-
tokenIdx: (0, pg_core_1.uniqueIndex)('entity_invitations_token_idx').on(table.token),
|
|
210
|
-
entityIdx: (0, pg_core_1.index)('entity_invitations_entity_idx').on(table.entity_id),
|
|
211
|
-
emailIdx: (0, pg_core_1.index)('entity_invitations_email_idx').on(table.email),
|
|
212
|
-
statusIdx: (0, pg_core_1.index)('entity_invitations_status_idx').on(table.status),
|
|
213
|
-
}));
|
|
214
|
-
// ========================================
|
|
215
|
-
// INITIALIZATION FUNCTIONS
|
|
216
|
-
// ========================================
|
|
217
|
-
/**
|
|
218
|
-
* Initialize all entity tables in the database.
|
|
219
|
-
* Note: Ownership is tracked via entity_members table (role = 'owner').
|
|
220
|
-
*
|
|
221
|
-
* @param client - postgres-js client instance
|
|
222
|
-
* @param schemaName - PostgreSQL schema name (null for public)
|
|
223
|
-
* @param indexPrefix - Prefix for index names
|
|
224
|
-
*/
|
|
225
|
-
async function initEntityTables(client, schemaName, indexPrefix) {
|
|
226
|
-
const prefix = schemaName ? `${schemaName}.` : '';
|
|
227
|
-
// Create entities table (ownership tracked via entity_members)
|
|
228
|
-
await client.unsafe(`
|
|
229
|
-
CREATE TABLE IF NOT EXISTS ${prefix}entities (
|
|
230
|
-
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
231
|
-
entity_slug VARCHAR(12) NOT NULL UNIQUE,
|
|
232
|
-
entity_type VARCHAR(20) NOT NULL CHECK (entity_type IN ('personal', 'organization')),
|
|
233
|
-
display_name VARCHAR(255) NOT NULL,
|
|
234
|
-
description TEXT,
|
|
235
|
-
avatar_url TEXT,
|
|
236
|
-
created_at TIMESTAMPTZ DEFAULT NOW(),
|
|
237
|
-
updated_at TIMESTAMPTZ DEFAULT NOW()
|
|
238
|
-
)
|
|
239
|
-
`);
|
|
240
|
-
await client.unsafe(`
|
|
241
|
-
CREATE UNIQUE INDEX IF NOT EXISTS ${indexPrefix}_entities_slug_idx
|
|
242
|
-
ON ${prefix}entities (entity_slug)
|
|
243
|
-
`);
|
|
244
|
-
await client.unsafe(`
|
|
245
|
-
CREATE INDEX IF NOT EXISTS ${indexPrefix}_entities_type_idx
|
|
246
|
-
ON ${prefix}entities (entity_type)
|
|
247
|
-
`);
|
|
248
|
-
// Create entity_members table (tracks all user-entity relationships including ownership)
|
|
249
|
-
await client.unsafe(`
|
|
250
|
-
CREATE TABLE IF NOT EXISTS ${prefix}entity_members (
|
|
251
|
-
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
252
|
-
entity_id UUID NOT NULL REFERENCES ${prefix}entities(id) ON DELETE CASCADE,
|
|
253
|
-
user_id VARCHAR(128) NOT NULL,
|
|
254
|
-
role VARCHAR(20) NOT NULL CHECK (role IN ('owner', 'admin', 'member')),
|
|
255
|
-
is_active BOOLEAN NOT NULL DEFAULT true,
|
|
256
|
-
joined_at TIMESTAMPTZ DEFAULT NOW(),
|
|
257
|
-
created_at TIMESTAMPTZ DEFAULT NOW(),
|
|
258
|
-
updated_at TIMESTAMPTZ DEFAULT NOW(),
|
|
259
|
-
UNIQUE(entity_id, user_id)
|
|
260
|
-
)
|
|
261
|
-
`);
|
|
262
|
-
await client.unsafe(`
|
|
263
|
-
CREATE UNIQUE INDEX IF NOT EXISTS ${indexPrefix}_entity_members_entity_user_idx
|
|
264
|
-
ON ${prefix}entity_members (entity_id, user_id)
|
|
265
|
-
`);
|
|
266
|
-
await client.unsafe(`
|
|
267
|
-
CREATE INDEX IF NOT EXISTS ${indexPrefix}_entity_members_entity_idx
|
|
268
|
-
ON ${prefix}entity_members (entity_id)
|
|
269
|
-
`);
|
|
270
|
-
await client.unsafe(`
|
|
271
|
-
CREATE INDEX IF NOT EXISTS ${indexPrefix}_entity_members_user_idx
|
|
272
|
-
ON ${prefix}entity_members (user_id)
|
|
273
|
-
`);
|
|
274
|
-
await client.unsafe(`
|
|
275
|
-
CREATE INDEX IF NOT EXISTS ${indexPrefix}_entity_members_active_idx
|
|
276
|
-
ON ${prefix}entity_members (is_active)
|
|
277
|
-
`);
|
|
278
|
-
// Create entity_invitations table
|
|
279
|
-
await client.unsafe(`
|
|
280
|
-
CREATE TABLE IF NOT EXISTS ${prefix}entity_invitations (
|
|
281
|
-
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
282
|
-
entity_id UUID NOT NULL REFERENCES ${prefix}entities(id) ON DELETE CASCADE,
|
|
283
|
-
email VARCHAR(255) NOT NULL,
|
|
284
|
-
role VARCHAR(20) NOT NULL CHECK (role IN ('admin', 'member')),
|
|
285
|
-
status VARCHAR(20) NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'accepted', 'declined', 'expired')),
|
|
286
|
-
invited_by_user_id VARCHAR(128) NOT NULL,
|
|
287
|
-
token VARCHAR(64) NOT NULL UNIQUE,
|
|
288
|
-
expires_at TIMESTAMPTZ NOT NULL,
|
|
289
|
-
accepted_at TIMESTAMPTZ,
|
|
290
|
-
created_at TIMESTAMPTZ DEFAULT NOW(),
|
|
291
|
-
updated_at TIMESTAMPTZ DEFAULT NOW()
|
|
292
|
-
)
|
|
293
|
-
`);
|
|
294
|
-
await client.unsafe(`
|
|
295
|
-
CREATE UNIQUE INDEX IF NOT EXISTS ${indexPrefix}_entity_invitations_token_idx
|
|
296
|
-
ON ${prefix}entity_invitations (token)
|
|
297
|
-
`);
|
|
298
|
-
await client.unsafe(`
|
|
299
|
-
CREATE INDEX IF NOT EXISTS ${indexPrefix}_entity_invitations_entity_idx
|
|
300
|
-
ON ${prefix}entity_invitations (entity_id)
|
|
301
|
-
`);
|
|
302
|
-
await client.unsafe(`
|
|
303
|
-
CREATE INDEX IF NOT EXISTS ${indexPrefix}_entity_invitations_email_idx
|
|
304
|
-
ON ${prefix}entity_invitations (email)
|
|
305
|
-
`);
|
|
306
|
-
await client.unsafe(`
|
|
307
|
-
CREATE INDEX IF NOT EXISTS ${indexPrefix}_entity_invitations_status_idx
|
|
308
|
-
ON ${prefix}entity_invitations (status)
|
|
309
|
-
`);
|
|
310
|
-
}
|
|
311
|
-
//# sourceMappingURL=entities.js.map
|
package/dist/types/index.cjs
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* @fileoverview Internal Type Definitions for Entity Service
|
|
4
|
-
* @description Types used internally by the entity service helpers
|
|
5
|
-
*/
|
|
6
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.ROLE_PERMISSIONS = exports.InvitationStatus = exports.EntityRole = exports.EntityType = void 0;
|
|
8
|
-
// Re-export enums as values (not just types) so they can be used at runtime
|
|
9
|
-
var types_1 = require("@sudobility/types");
|
|
10
|
-
Object.defineProperty(exports, "EntityType", { enumerable: true, get: function () { return types_1.EntityType; } });
|
|
11
|
-
Object.defineProperty(exports, "EntityRole", { enumerable: true, get: function () { return types_1.EntityRole; } });
|
|
12
|
-
Object.defineProperty(exports, "InvitationStatus", { enumerable: true, get: function () { return types_1.InvitationStatus; } });
|
|
13
|
-
Object.defineProperty(exports, "ROLE_PERMISSIONS", { enumerable: true, get: function () { return types_1.ROLE_PERMISSIONS; } });
|
|
14
|
-
//# sourceMappingURL=index.js.map
|
package/dist/utils/index.cjs
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* @fileoverview Utility Exports
|
|
4
|
-
*/
|
|
5
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
6
|
-
if (k2 === undefined) k2 = k;
|
|
7
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
9
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
10
|
-
}
|
|
11
|
-
Object.defineProperty(o, k2, desc);
|
|
12
|
-
}) : (function(o, m, k, k2) {
|
|
13
|
-
if (k2 === undefined) k2 = k;
|
|
14
|
-
o[k2] = m[k];
|
|
15
|
-
}));
|
|
16
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
17
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
18
|
-
};
|
|
19
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
__exportStar(require("./slug-generator"), exports);
|
|
21
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* @fileoverview Entity Slug Generation Utilities
|
|
4
|
-
* @description Functions for generating unique entity slugs and invitation tokens
|
|
5
|
-
*/
|
|
6
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.generateEntitySlug = generateEntitySlug;
|
|
8
|
-
exports.generateInvitationToken = generateInvitationToken;
|
|
9
|
-
exports.normalizeSlug = normalizeSlug;
|
|
10
|
-
exports.validateSlug = validateSlug;
|
|
11
|
-
exports.generateUniqueSlug = generateUniqueSlug;
|
|
12
|
-
exports.calculateInvitationExpiry = calculateInvitationExpiry;
|
|
13
|
-
/** Characters allowed in entity slugs (lowercase alphanumeric) */
|
|
14
|
-
const SLUG_CHARS = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
15
|
-
/** Default length for generated entity slugs */
|
|
16
|
-
const DEFAULT_SLUG_LENGTH = 8;
|
|
17
|
-
/** Length for invitation tokens (hex characters) */
|
|
18
|
-
const INVITATION_TOKEN_LENGTH = 32;
|
|
19
|
-
/**
|
|
20
|
-
* Generate a random entity slug.
|
|
21
|
-
* @param length - Length of the slug (default 8, max 12)
|
|
22
|
-
* @returns A random alphanumeric slug
|
|
23
|
-
*/
|
|
24
|
-
function generateEntitySlug(length = DEFAULT_SLUG_LENGTH) {
|
|
25
|
-
const actualLength = Math.min(Math.max(length, 8), 12);
|
|
26
|
-
let slug = '';
|
|
27
|
-
for (let i = 0; i < actualLength; i++) {
|
|
28
|
-
slug += SLUG_CHARS[Math.floor(Math.random() * SLUG_CHARS.length)];
|
|
29
|
-
}
|
|
30
|
-
return slug;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Generate a unique invitation token.
|
|
34
|
-
* @returns A 64-character hex string
|
|
35
|
-
*/
|
|
36
|
-
function generateInvitationToken() {
|
|
37
|
-
const bytes = new Uint8Array(INVITATION_TOKEN_LENGTH);
|
|
38
|
-
crypto.getRandomValues(bytes);
|
|
39
|
-
return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Normalize a user-provided slug to valid format.
|
|
43
|
-
* Converts to lowercase and removes invalid characters.
|
|
44
|
-
* @param input - The user-provided slug
|
|
45
|
-
* @returns Normalized slug
|
|
46
|
-
*/
|
|
47
|
-
function normalizeSlug(input) {
|
|
48
|
-
return input
|
|
49
|
-
.toLowerCase()
|
|
50
|
-
.replace(/[^a-z0-9]/g, '')
|
|
51
|
-
.slice(0, 12);
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Validate that a slug meets the requirements.
|
|
55
|
-
* @param slug - The slug to validate
|
|
56
|
-
* @returns Whether the slug is valid
|
|
57
|
-
*/
|
|
58
|
-
function validateSlug(slug) {
|
|
59
|
-
return /^[a-z0-9]{8,12}$/.test(slug);
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Generate a slug with a numeric suffix to avoid collisions.
|
|
63
|
-
* @param baseSlug - The base slug to append suffix to
|
|
64
|
-
* @param existingSlugs - Set of existing slugs to avoid
|
|
65
|
-
* @returns A unique slug with suffix if needed
|
|
66
|
-
*/
|
|
67
|
-
function generateUniqueSlug(baseSlug, existingSlugs) {
|
|
68
|
-
const normalized = normalizeSlug(baseSlug).slice(0, 8);
|
|
69
|
-
if (!existingSlugs.has(normalized) && validateSlug(normalized)) {
|
|
70
|
-
return normalized;
|
|
71
|
-
}
|
|
72
|
-
// Try adding numeric suffixes
|
|
73
|
-
for (let i = 1; i < 1000; i++) {
|
|
74
|
-
const suffix = i.toString();
|
|
75
|
-
const candidate = (normalized.slice(0, 12 - suffix.length) + suffix).slice(0, 12);
|
|
76
|
-
if (!existingSlugs.has(candidate) && validateSlug(candidate)) {
|
|
77
|
-
return candidate;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
// Fall back to random slug
|
|
81
|
-
return generateEntitySlug();
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Calculate invitation expiry date (7 days from now).
|
|
85
|
-
* @returns ISO 8601 string for expiry date
|
|
86
|
-
*/
|
|
87
|
-
function calculateInvitationExpiry() {
|
|
88
|
-
const expiryDate = new Date();
|
|
89
|
-
expiryDate.setDate(expiryDate.getDate() + 7);
|
|
90
|
-
return expiryDate.toISOString();
|
|
91
|
-
}
|
|
92
|
-
//# sourceMappingURL=slug-generator.js.map
|