@space-arch/util-drizzle 1.7.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/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # @space-arch/util-drizzle
2
+
3
+ Utility functions for Drizzle ORM.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @space-arch/util-drizzle
9
+ # or
10
+ pnpm add @space-arch/util-drizzle
11
+ ```
12
+
13
+ ## Features
14
+
15
+ - 🐻 **Lazy Initialization**: Create Drizzle instances only when needed
16
+ - 🆔 **UUIDv7 Support**: Built-in column types for UUIDv7 with automatic generation
17
+
18
+ ## Usage
19
+
20
+ ### Lazy Drizzle
21
+
22
+ Create a lazily initialized Drizzle instance. Useful for serverless environments where you need to resolve connection details at runtime.
23
+
24
+ ```typescript
25
+ import { drizzle } from 'drizzle-orm/postgres-js';
26
+ import postgres from 'postgres';
27
+
28
+ import { lazyDrizzle } from '@space-arch/util-drizzle';
29
+
30
+ const db = lazyDrizzle(
31
+ (url) => drizzle(postgres(url)),
32
+ () => process.env.DATABASE_URL
33
+ );
34
+
35
+ // Before using the db:
36
+ process.env['DATABASE_URL'] = await resolveConnectionString();
37
+
38
+ // Connection is established only when first query is executed
39
+ const users = await db.select().from(usersTable);
40
+ ```
41
+
42
+ ### UUIDv7 Column Types
43
+
44
+ Drizzle column types for UUIDv7 with automatic generation.
45
+
46
+ ```typescript
47
+ import { pgTable } from 'drizzle-orm/pg-core';
48
+ import { uuidV7, uuidV7Nullable } from '@space-arch/util-drizzle';
49
+
50
+ const users = pgTable('users', {
51
+ id: uuidV7('id').primaryKey(),
52
+ parentId: uuidV7Nullable('parent_id')
53
+ });
54
+ ```
55
+
56
+ - `uuidV7()` - Not null with automatic default value generation
57
+ - `uuidV7Nullable()` - Nullable without default value
58
+
59
+ ## Contributing
60
+
61
+ This package is part of the [Space Architects monorepo](../../README.md).
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("drizzle-orm/pg-core"),i=require("uuid"),d=(r,s)=>{let o=null;const u=()=>(o||(o=r(s())),o);return new Proxy({},{get(t,n){const e=u(),c=e[n];return typeof c=="function"?c.bind(e):c},getPrototypeOf(){const t=u();return Object.getPrototypeOf(t)},has(t,n){const e=u();return n in e},ownKeys(){const t=u();return Reflect.ownKeys(t)},getOwnPropertyDescriptor(t,n){const e=u();return Reflect.getOwnPropertyDescriptor(e,n)}})},a=r=>l.customType({dataType:()=>"uuid"})(r).notNull().$defaultFn(()=>i.v7()),y=r=>l.customType({dataType:()=>"uuid"})(r);exports.lazyDrizzle=d;exports.uuidV7=a;exports.uuidV7Nullable=y;
@@ -0,0 +1,3 @@
1
+ export { lazyDrizzle } from './lib/lazy-drizzle.js';
2
+ export { uuidV7, uuidV7Nullable } from './lib/uuidv7.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,37 @@
1
+ import { customType as s } from "drizzle-orm/pg-core";
2
+ import { v7 as l } from "uuid";
3
+ const a = (r, d) => {
4
+ let u = null;
5
+ const o = () => (u || (u = r(d())), u);
6
+ return new Proxy({}, {
7
+ get(t, n) {
8
+ const e = o(), c = e[n];
9
+ return typeof c == "function" ? c.bind(e) : c;
10
+ },
11
+ getPrototypeOf() {
12
+ const t = o();
13
+ return Object.getPrototypeOf(t);
14
+ },
15
+ has(t, n) {
16
+ const e = o();
17
+ return n in e;
18
+ },
19
+ ownKeys() {
20
+ const t = o();
21
+ return Reflect.ownKeys(t);
22
+ },
23
+ getOwnPropertyDescriptor(t, n) {
24
+ const e = o();
25
+ return Reflect.getOwnPropertyDescriptor(e, n);
26
+ }
27
+ });
28
+ }, y = (r) => s({
29
+ dataType: () => "uuid"
30
+ })(r).notNull().$defaultFn(() => l()), b = (r) => s({
31
+ dataType: () => "uuid"
32
+ })(r);
33
+ export {
34
+ a as lazyDrizzle,
35
+ y as uuidV7,
36
+ b as uuidV7Nullable
37
+ };
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Create lazily initialized drizzle proxy
3
+ *
4
+ * Allows for configuring the connection before creating the instance.
5
+ * For example, in Lambda, you might want to resolve the connection url secret and set
6
+ * it to the env before accessing the db.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * // drizzle is not initialized here
11
+ * const db = lazyDrizzle(drizzle, () => process.env.DATABASE_URL);
12
+ *
13
+ * process.env['DATABASE_URL'] = await getConnectionUrl();
14
+ *
15
+ * // drizzle will be initialized only here:
16
+ * const [user] = await db.select().from(users).where(eq(users.id, 1));
17
+ * ```
18
+ *
19
+ * @param drizzleFactory - A function that creates a drizzle instance
20
+ * @param connectionUrl - A function that returns the connection url
21
+ */
22
+ export declare const lazyDrizzle: <T extends object>(drizzleFactory: (connectionUrl: string) => T, connectionUrl: () => string) => T;
23
+ //# sourceMappingURL=lazy-drizzle.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lazy-drizzle.d.ts","sourceRoot":"","sources":["../../src/lib/lazy-drizzle.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,SAAS,MAAM,EAC1C,gBAAgB,CAAC,aAAa,EAAE,MAAM,KAAK,CAAC,EAC5C,eAAe,MAAM,MAAM,MAuC5B,CAAC"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * A custom type for UUIDv7 columns. Not null
3
+ */
4
+ export declare const uuidV7: (name: string) => import('drizzle-orm').HasRuntimeDefault<import('drizzle-orm').HasDefault<import('drizzle-orm').NotNull<import('drizzle-orm/pg-core').PgCustomColumnBuilder<import('drizzle-orm/pg-core').ConvertCustomConfig<string, {
5
+ data: string;
6
+ notNull: true;
7
+ default: true;
8
+ }>>>>>;
9
+ /**
10
+ * A custom type for UUIDv7 columns. Nullable
11
+ */
12
+ export declare const uuidV7Nullable: (name: string) => import('drizzle-orm/pg-core').PgCustomColumnBuilder<{
13
+ name: string;
14
+ dataType: "custom";
15
+ columnType: "PgCustomColumn";
16
+ data: string;
17
+ driverParam: unknown;
18
+ enumValues: undefined;
19
+ }>;
20
+ //# sourceMappingURL=uuidv7.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uuidv7.d.ts","sourceRoot":"","sources":["../../src/lib/uuidv7.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,eAAO,MAAM,MAAM,GAAI,MAAM,MAAM;UACd,MAAM;aAAW,IAAI;aAAW,IAAI;MAI9B,CAAC;AAE5B;;GAEG;AACH,eAAO,MAAM,cAAc,GAAI,MAAM,MAAM;;;;;;;EAGjC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@space-arch/util-drizzle",
3
+ "version": "1.7.0",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ "./package.json": "./package.json",
10
+ ".": {
11
+ "@space-arch/source": "./src/index.ts",
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js",
14
+ "require": "./dist/index.cjs"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "!**/*.tsbuildinfo"
20
+ ],
21
+ "nx": {
22
+ "name": "util-drizzle",
23
+ "tags": [
24
+ "npm:public"
25
+ ]
26
+ },
27
+ "peerDependencies": {
28
+ "uuid": "^11.1.0",
29
+ "drizzle-orm": "^0.44.2"
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "homepage": "https://github.com/alexandr2110pro/space-arch#readme",
35
+ "bugs": {
36
+ "url": "https://github.com/alexandr2110pro/space-arch/issues"
37
+ },
38
+ "author": "Space Architects",
39
+ "license": "MIT",
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "https://github.com/alexandr2110pro/space-arch.git",
43
+ "directory": "packages/util/drizzle"
44
+ },
45
+ "keywords": [
46
+ "drizzle",
47
+ "drizzle-orm",
48
+ "space-arch",
49
+ "util",
50
+ "uuidv7",
51
+ "lazy"
52
+ ]
53
+ }