@pgpm/uuid 0.4.0 → 0.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/Makefile
CHANGED
package/README.md
CHANGED
|
@@ -2,4 +2,343 @@
|
|
|
2
2
|
|
|
3
3
|
UUID utilities and extensions for PostgreSQL.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
`@pgpm/uuid` provides PostgreSQL functions for generating pseudo-ordered UUIDs that maintain some temporal ordering while preserving UUID randomness. This package is particularly useful for multi-tenant applications where you want UUIDs that cluster by tenant or time period for better database performance.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **pseudo_order_uuid()**: Generates time-influenced UUIDs for better index performance
|
|
12
|
+
- **pseudo_order_seed_uuid(seed)**: Generates UUIDs with a seed prefix for multi-tenant scenarios
|
|
13
|
+
- **trigger_set_uuid_seed**: Trigger function to automatically set UUID from a seed column
|
|
14
|
+
- **trigger_set_uuid_related_field**: Trigger function to set UUID based on another column value
|
|
15
|
+
- Version 4 UUID format compliance
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
If you have `pgpm` installed:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pgpm install @pgpm/uuid
|
|
23
|
+
pgpm deploy
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
This is a quick way to get started. The sections below provide more detailed installation options.
|
|
27
|
+
|
|
28
|
+
### Prerequisites
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
# Install pgpm globally
|
|
32
|
+
npm install -g pgpm
|
|
33
|
+
|
|
34
|
+
# Start PostgreSQL
|
|
35
|
+
pgpm docker start
|
|
36
|
+
|
|
37
|
+
# Set environment variables
|
|
38
|
+
eval "$(pgpm env)"
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Deploy
|
|
42
|
+
|
|
43
|
+
#### Option 1: Deploy by installing with pgpm
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
pgpm install @pgpm/uuid
|
|
47
|
+
pgpm deploy
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
#### Option 2: Deploy from Package Directory
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
cd packages/data-types/uuid
|
|
54
|
+
pgpm deploy --createdb
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
#### Option 3: Deploy from Workspace Root
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
# Install workspace dependencies
|
|
61
|
+
pnpm install
|
|
62
|
+
|
|
63
|
+
# Deploy with dependencies
|
|
64
|
+
pgpm deploy mydb1 --yes --createdb
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Usage
|
|
68
|
+
|
|
69
|
+
### Basic UUID Generation
|
|
70
|
+
|
|
71
|
+
```sql
|
|
72
|
+
-- Generate a pseudo-ordered UUID
|
|
73
|
+
SELECT uuids.pseudo_order_uuid();
|
|
74
|
+
-- Returns: e.g., '3a7f-8b2c-4d1e-9f3a-1b2c3d4e5f6a'
|
|
75
|
+
|
|
76
|
+
-- Use as default value in a table
|
|
77
|
+
CREATE TABLE items (
|
|
78
|
+
id UUID DEFAULT uuids.pseudo_order_uuid() PRIMARY KEY,
|
|
79
|
+
name TEXT
|
|
80
|
+
);
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Seeded UUID Generation (Multi-Tenant)
|
|
84
|
+
|
|
85
|
+
```sql
|
|
86
|
+
-- Generate UUID with tenant seed
|
|
87
|
+
SELECT uuids.pseudo_order_seed_uuid('tenant-123');
|
|
88
|
+
-- UUIDs for the same tenant will share a common prefix
|
|
89
|
+
|
|
90
|
+
-- Useful for multi-tenant applications
|
|
91
|
+
CREATE TABLE tenant_data (
|
|
92
|
+
id UUID DEFAULT uuids.pseudo_order_seed_uuid('default-tenant'),
|
|
93
|
+
tenant_id TEXT,
|
|
94
|
+
data JSONB
|
|
95
|
+
);
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Automatic UUID Setting with Triggers
|
|
99
|
+
|
|
100
|
+
#### Set UUID from Seed Column
|
|
101
|
+
|
|
102
|
+
```sql
|
|
103
|
+
CREATE TABLE items (
|
|
104
|
+
id UUID,
|
|
105
|
+
name TEXT,
|
|
106
|
+
tenant_id TEXT,
|
|
107
|
+
custom_uuid UUID
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
-- Automatically set custom_uuid based on tenant_id
|
|
111
|
+
CREATE TRIGGER set_custom_uuid
|
|
112
|
+
BEFORE INSERT ON items
|
|
113
|
+
FOR EACH ROW
|
|
114
|
+
EXECUTE FUNCTION uuids.trigger_set_uuid_seed('custom_uuid', 'tenant_id');
|
|
115
|
+
|
|
116
|
+
-- Insert will automatically generate custom_uuid from tenant_id
|
|
117
|
+
INSERT INTO items (name, tenant_id) VALUES ('Item 1', 'tenant-abc');
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
#### Set UUID from Related Field
|
|
121
|
+
|
|
122
|
+
```sql
|
|
123
|
+
CREATE TABLE tenant_records (
|
|
124
|
+
id UUID,
|
|
125
|
+
tenant TEXT
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
-- Automatically set id based on tenant column
|
|
129
|
+
CREATE TRIGGER set_id_from_tenant
|
|
130
|
+
BEFORE INSERT ON tenant_records
|
|
131
|
+
FOR EACH ROW
|
|
132
|
+
EXECUTE FUNCTION uuids.trigger_set_uuid_related_field('id', 'tenant');
|
|
133
|
+
|
|
134
|
+
-- Insert will automatically generate id from tenant value
|
|
135
|
+
INSERT INTO tenant_records (tenant) VALUES ('tenant-xyz');
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## Functions
|
|
139
|
+
|
|
140
|
+
### uuids.pseudo_order_uuid()
|
|
141
|
+
|
|
142
|
+
Generates a pseudo-ordered UUID that incorporates temporal information for better index performance.
|
|
143
|
+
|
|
144
|
+
**Returns**: `uuid`
|
|
145
|
+
|
|
146
|
+
**Algorithm**:
|
|
147
|
+
- First 4 characters: MD5 hash of current year + week number (provides weekly clustering)
|
|
148
|
+
- Remaining characters: Random values with timestamp influence
|
|
149
|
+
- Format: Version 4 UUID compliant
|
|
150
|
+
|
|
151
|
+
**Benefits**:
|
|
152
|
+
- Better B-tree index performance compared to random UUIDs
|
|
153
|
+
- Reduces index fragmentation
|
|
154
|
+
- Maintains UUID uniqueness guarantees
|
|
155
|
+
|
|
156
|
+
### uuids.pseudo_order_seed_uuid(seed text)
|
|
157
|
+
|
|
158
|
+
Generates a pseudo-ordered UUID with a seed prefix for multi-tenant scenarios.
|
|
159
|
+
|
|
160
|
+
**Parameters**:
|
|
161
|
+
- `seed` (text): Seed value (typically tenant ID or organization ID)
|
|
162
|
+
|
|
163
|
+
**Returns**: `uuid`
|
|
164
|
+
|
|
165
|
+
**Algorithm**:
|
|
166
|
+
- First 2 characters: MD5 hash of seed (provides tenant clustering)
|
|
167
|
+
- Next 2 characters: MD5 hash of year + week
|
|
168
|
+
- Remaining characters: Random values with timestamp influence
|
|
169
|
+
- Format: Version 4 UUID compliant
|
|
170
|
+
|
|
171
|
+
**Benefits**:
|
|
172
|
+
- UUIDs for same tenant cluster together in indexes
|
|
173
|
+
- Improves query performance for tenant-specific queries
|
|
174
|
+
- Maintains uniqueness across tenants
|
|
175
|
+
|
|
176
|
+
### uuids.trigger_set_uuid_seed(uuid_column, seed_column)
|
|
177
|
+
|
|
178
|
+
Trigger function that automatically sets a UUID column based on a seed column value.
|
|
179
|
+
|
|
180
|
+
**Parameters**:
|
|
181
|
+
- `uuid_column` (text): Name of the UUID column to set
|
|
182
|
+
- `seed_column` (text): Name of the column containing the seed value
|
|
183
|
+
|
|
184
|
+
**Usage**:
|
|
185
|
+
```sql
|
|
186
|
+
CREATE TRIGGER trigger_name
|
|
187
|
+
BEFORE INSERT ON table_name
|
|
188
|
+
FOR EACH ROW
|
|
189
|
+
EXECUTE FUNCTION uuids.trigger_set_uuid_seed('id_column', 'seed_column');
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### uuids.trigger_set_uuid_related_field(uuid_column, related_column)
|
|
193
|
+
|
|
194
|
+
Trigger function that automatically sets a UUID column based on another column's value.
|
|
195
|
+
|
|
196
|
+
**Parameters**:
|
|
197
|
+
- `uuid_column` (text): Name of the UUID column to set
|
|
198
|
+
- `related_column` (text): Name of the column to use as seed
|
|
199
|
+
|
|
200
|
+
**Usage**:
|
|
201
|
+
```sql
|
|
202
|
+
CREATE TRIGGER trigger_name
|
|
203
|
+
BEFORE INSERT ON table_name
|
|
204
|
+
FOR EACH ROW
|
|
205
|
+
EXECUTE FUNCTION uuids.trigger_set_uuid_related_field('id_column', 'related_column');
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
## Use Cases
|
|
209
|
+
|
|
210
|
+
### Multi-Tenant Applications
|
|
211
|
+
|
|
212
|
+
```sql
|
|
213
|
+
CREATE TABLE tenant_users (
|
|
214
|
+
id UUID,
|
|
215
|
+
tenant_id TEXT NOT NULL,
|
|
216
|
+
email TEXT NOT NULL,
|
|
217
|
+
created_at TIMESTAMPTZ DEFAULT NOW()
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
CREATE TRIGGER set_user_id
|
|
221
|
+
BEFORE INSERT ON tenant_users
|
|
222
|
+
FOR EACH ROW
|
|
223
|
+
EXECUTE FUNCTION uuids.trigger_set_uuid_seed('id', 'tenant_id');
|
|
224
|
+
|
|
225
|
+
-- All users for tenant 'acme' will have UUIDs with same prefix
|
|
226
|
+
INSERT INTO tenant_users (tenant_id, email) VALUES
|
|
227
|
+
('acme', 'user1@acme.com'),
|
|
228
|
+
('acme', 'user2@acme.com');
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
### Time-Series Data with Better Index Performance
|
|
232
|
+
|
|
233
|
+
```sql
|
|
234
|
+
CREATE TABLE events (
|
|
235
|
+
id UUID DEFAULT uuids.pseudo_order_uuid() PRIMARY KEY,
|
|
236
|
+
event_type TEXT,
|
|
237
|
+
payload JSONB,
|
|
238
|
+
created_at TIMESTAMPTZ DEFAULT NOW()
|
|
239
|
+
);
|
|
240
|
+
|
|
241
|
+
-- UUIDs will cluster by week, improving range query performance
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## Dependencies
|
|
245
|
+
|
|
246
|
+
- `@pgpm/verify`: Verification utilities for database objects
|
|
247
|
+
|
|
248
|
+
## Testing
|
|
249
|
+
|
|
250
|
+
```bash
|
|
251
|
+
pnpm test
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
The test suite validates:
|
|
255
|
+
- UUID format compliance (version 4)
|
|
256
|
+
- Pseudo-ordered UUID generation
|
|
257
|
+
- Seeded UUID generation with consistent prefixes
|
|
258
|
+
- Trigger functions for automatic UUID setting
|
|
259
|
+
|
|
260
|
+
## Development
|
|
261
|
+
|
|
262
|
+
See the [Development](#development) section below for information on working with this package.
|
|
263
|
+
|
|
264
|
+
---
|
|
265
|
+
|
|
266
|
+
## Development
|
|
267
|
+
|
|
268
|
+
### **Before You Begin**
|
|
269
|
+
|
|
270
|
+
```bash
|
|
271
|
+
# 1. Install pgpm
|
|
272
|
+
npm install -g pgpm
|
|
273
|
+
|
|
274
|
+
# 2. Start Postgres (Docker or local)
|
|
275
|
+
pgpm docker start
|
|
276
|
+
|
|
277
|
+
# 3. Load PG* environment variables (PGHOST, PGUSER, ...)
|
|
278
|
+
eval "$(pgpm env)"
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
---
|
|
282
|
+
|
|
283
|
+
### **Starting a New Project**
|
|
284
|
+
|
|
285
|
+
```bash
|
|
286
|
+
# 1. Create a workspace
|
|
287
|
+
pgpm init --workspace
|
|
288
|
+
cd my-app
|
|
289
|
+
|
|
290
|
+
# 2. Create your first module
|
|
291
|
+
pgpm init
|
|
292
|
+
|
|
293
|
+
# 3. Add a migration
|
|
294
|
+
pgpm add some_change
|
|
295
|
+
|
|
296
|
+
# 4. Deploy (auto-creates database)
|
|
297
|
+
pgpm deploy --createdb
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
---
|
|
301
|
+
|
|
302
|
+
### **Working With an Existing Project**
|
|
303
|
+
|
|
304
|
+
```bash
|
|
305
|
+
# 1. Clone and enter the project
|
|
306
|
+
git clone <repo> && cd <project>
|
|
307
|
+
|
|
308
|
+
# 2. Install dependencies
|
|
309
|
+
pnpm install
|
|
310
|
+
|
|
311
|
+
# 3. Deploy locally
|
|
312
|
+
pgpm deploy --createdb
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
---
|
|
316
|
+
|
|
317
|
+
### **Testing a Module Inside a Workspace**
|
|
318
|
+
|
|
319
|
+
```bash
|
|
320
|
+
# 1. Install workspace deps
|
|
321
|
+
pnpm install
|
|
322
|
+
|
|
323
|
+
# 2. Enter the module directory
|
|
324
|
+
cd packages/<some-module>
|
|
325
|
+
|
|
326
|
+
# 3. Run tests in watch mode
|
|
327
|
+
pnpm test:watch
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
## Related Tooling
|
|
331
|
+
|
|
332
|
+
* [pgpm](https://github.com/launchql/launchql/tree/main/packages/pgpm): **🖥️ PostgreSQL Package Manager** for modular Postgres development. Works with database workspaces, scaffolding, migrations, seeding, and installing database packages.
|
|
333
|
+
* [pgsql-test](https://github.com/launchql/launchql/tree/main/packages/pgsql-test): **📊 Isolated testing environments** with per-test transaction rollbacks—ideal for integration tests, complex migrations, and RLS simulation.
|
|
334
|
+
* [supabase-test](https://github.com/launchql/launchql/tree/main/packages/supabase-test): **🧪 Supabase-native test harness** preconfigured for the local Supabase stack—per-test rollbacks, JWT/role context helpers, and CI/GitHub Actions ready.
|
|
335
|
+
* [graphile-test](https://github.com/launchql/launchql/tree/main/packages/graphile-test): **🔐 Authentication mocking** for Graphile-focused test helpers and emulating row-level security contexts.
|
|
336
|
+
* [pgsql-parser](https://github.com/launchql/pgsql-parser): **🔄 SQL conversion engine** that interprets and converts PostgreSQL syntax.
|
|
337
|
+
* [libpg-query-node](https://github.com/launchql/libpg-query-node): **🌉 Node.js bindings** for `libpg_query`, converting SQL into parse trees.
|
|
338
|
+
* [pg-proto-parser](https://github.com/launchql/pg-proto-parser): **📦 Protobuf parser** for parsing PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
|
|
339
|
+
|
|
340
|
+
## Disclaimer
|
|
341
|
+
|
|
342
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
343
|
+
|
|
344
|
+
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|
package/launchql-uuid.control
CHANGED
package/package.json
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pgpm/uuid",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "UUID utilities and extensions for PostgreSQL",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
8
8
|
"scripts": {
|
|
9
|
-
"bundle": "
|
|
9
|
+
"bundle": "pgpm package",
|
|
10
10
|
"test": "jest",
|
|
11
11
|
"test:watch": "jest --watch"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@pgpm/verify": "0.
|
|
14
|
+
"@pgpm/verify": "0.6.0"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
|
-
"
|
|
17
|
+
"pgpm": "^0.2.0"
|
|
18
18
|
},
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|
|
21
|
-
"url": "https://github.com/launchql/
|
|
21
|
+
"url": "https://github.com/launchql/pgpm-modules"
|
|
22
22
|
},
|
|
23
|
-
"homepage": "https://github.com/launchql/
|
|
23
|
+
"homepage": "https://github.com/launchql/pgpm-modules",
|
|
24
24
|
"bugs": {
|
|
25
|
-
"url": "https://github.com/launchql/
|
|
25
|
+
"url": "https://github.com/launchql/pgpm-modules/issues"
|
|
26
26
|
},
|
|
27
|
-
"gitHead": "
|
|
27
|
+
"gitHead": "c7d0eae588d7a764b382a330c8b853b341b13fb2"
|
|
28
28
|
}
|
package/sqitch.plan
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
%syntax-version=1.0.0
|
|
2
|
-
%project=launchql-uuid
|
|
3
|
-
%uri=launchql-uuid
|
|
4
|
-
|
|
5
|
-
schemas/uuids/schema 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/uuids/schema
|
|
6
|
-
schemas/uuids/procedures/pseudo_order_seed_uuid [schemas/uuids/schema] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/uuids/procedures/pseudo_order_seed_uuid
|
|
7
|
-
schemas/uuids/procedures/pseudo_order_uuid [schemas/uuids/schema] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/uuids/procedures/pseudo_order_uuid
|
|
8
|
-
schemas/uuids/procedures/trigger_set_uuid_related_field [schemas/uuids/schema schemas/uuids/procedures/pseudo_order_seed_uuid] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/uuids/procedures/trigger_set_uuid_related_field
|
|
9
|
-
schemas/uuids/procedures/trigger_set_uuid_seed [schemas/uuids/schema schemas/uuids/procedures/pseudo_order_seed_uuid] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/uuids/procedures/trigger_set_uuid_seed
|
|
File without changes
|
|
File without changes
|