@orkestrel/database 0.0.2 → 0.0.3

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.
@@ -6,6 +6,7 @@ import { EmitterInterface } from '@orkestrel/emitter';
6
6
  import { FieldPath } from '@orkestrel/contract';
7
7
  import { Infer } from '@orkestrel/contract';
8
8
  import { JSONSchema } from '@orkestrel/contract';
9
+ import { RandomFunction } from '@orkestrel/contract';
9
10
 
10
11
  /** An aggregate computed over a numeric column. */
11
12
  export declare type AggregateFunction = 'count' | 'sum' | 'average' | 'minimum' | 'maximum';
@@ -837,6 +838,35 @@ export declare function extractKey(row: Row, column: string): Key | undefined;
837
838
  */
838
839
  export declare function filterRows(rows: readonly Row[], conditions: readonly Condition[]): readonly Row[];
839
840
 
841
+ /**
842
+ * Generate an RFC 4122 version 4 UUID from a number source — no host crypto global.
843
+ *
844
+ * @remarks
845
+ * Draws exactly {@link UUID_BYTE_COUNT} values from `random`, one per byte, then
846
+ * forces the version (`4`) and variant (`10xx`) bits. The default source is
847
+ * `Math.random` — a pure-ECMAScript intrinsic, so generation works on every host;
848
+ * pass a seeded source (`seededRandom` from `@orkestrel/contract`) and reuse it
849
+ * across calls for reproducible sequences in tests and fixtures — production
850
+ * identifiers should keep the default source, whose engine entropy is far larger
851
+ * than a 32-bit seed. Each byte is floored and masked, so a source straying
852
+ * outside `[0, 1)` (negative, `>= 1`, `NaN`, `Infinity`) can never yield a
853
+ * malformed UUID. Suitable as a collision-resistant record identifier — not a
854
+ * cryptographic token; never use one as a secret.
855
+ *
856
+ * @param random - A number source returning values in the half-open range `[0, 1)` (defaults to `Math.random`)
857
+ * @returns A lowercase RFC 4122 version 4 UUID
858
+ *
859
+ * @example
860
+ * ```ts
861
+ * import { generateUUID } from '@orkestrel/database'
862
+ * import { seededRandom } from '@orkestrel/contract'
863
+ *
864
+ * generateUUID() // e.g. '9b2f7c1e-3d4a-4f6b-8e2d-5a1c0b9f8e7d'
865
+ * generateUUID(seededRandom(42)) // the same UUID on every run
866
+ * ```
867
+ */
868
+ export declare function generateUUID(random?: RandomFunction): string;
869
+
840
870
  export declare function globMatch(value: string, pattern: string): boolean;
841
871
 
842
872
  /**
@@ -1694,6 +1724,12 @@ export declare interface TransactionInterface {
1694
1724
  rollback(): Promise<void>;
1695
1725
  }
1696
1726
 
1727
+ /** The number of bytes encoded by an RFC 4122 UUID. */
1728
+ export declare const UUID_BYTE_COUNT = 16;
1729
+
1730
+ /** The number of distinct values one UUID byte may hold. */
1731
+ export declare const UUID_BYTE_RANGE = 256;
1732
+
1697
1733
  /**
1698
1734
  * Match a value against a wildcard pattern in LINEAR time — the shared, ReDoS-SAFE
1699
1735
  * engine behind {@link likeMatch} and {@link globMatch}.
@@ -6,6 +6,7 @@ import { EmitterInterface } from '@orkestrel/emitter';
6
6
  import { FieldPath } from '@orkestrel/contract';
7
7
  import { Infer } from '@orkestrel/contract';
8
8
  import { JSONSchema } from '@orkestrel/contract';
9
+ import { RandomFunction } from '@orkestrel/contract';
9
10
 
10
11
  /** An aggregate computed over a numeric column. */
11
12
  export declare type AggregateFunction = 'count' | 'sum' | 'average' | 'minimum' | 'maximum';
@@ -837,6 +838,35 @@ export declare function extractKey(row: Row, column: string): Key | undefined;
837
838
  */
838
839
  export declare function filterRows(rows: readonly Row[], conditions: readonly Condition[]): readonly Row[];
839
840
 
841
+ /**
842
+ * Generate an RFC 4122 version 4 UUID from a number source — no host crypto global.
843
+ *
844
+ * @remarks
845
+ * Draws exactly {@link UUID_BYTE_COUNT} values from `random`, one per byte, then
846
+ * forces the version (`4`) and variant (`10xx`) bits. The default source is
847
+ * `Math.random` — a pure-ECMAScript intrinsic, so generation works on every host;
848
+ * pass a seeded source (`seededRandom` from `@orkestrel/contract`) and reuse it
849
+ * across calls for reproducible sequences in tests and fixtures — production
850
+ * identifiers should keep the default source, whose engine entropy is far larger
851
+ * than a 32-bit seed. Each byte is floored and masked, so a source straying
852
+ * outside `[0, 1)` (negative, `>= 1`, `NaN`, `Infinity`) can never yield a
853
+ * malformed UUID. Suitable as a collision-resistant record identifier — not a
854
+ * cryptographic token; never use one as a secret.
855
+ *
856
+ * @param random - A number source returning values in the half-open range `[0, 1)` (defaults to `Math.random`)
857
+ * @returns A lowercase RFC 4122 version 4 UUID
858
+ *
859
+ * @example
860
+ * ```ts
861
+ * import { generateUUID } from '@orkestrel/database'
862
+ * import { seededRandom } from '@orkestrel/contract'
863
+ *
864
+ * generateUUID() // e.g. '9b2f7c1e-3d4a-4f6b-8e2d-5a1c0b9f8e7d'
865
+ * generateUUID(seededRandom(42)) // the same UUID on every run
866
+ * ```
867
+ */
868
+ export declare function generateUUID(random?: RandomFunction): string;
869
+
840
870
  export declare function globMatch(value: string, pattern: string): boolean;
841
871
 
842
872
  /**
@@ -1694,6 +1724,12 @@ export declare interface TransactionInterface {
1694
1724
  rollback(): Promise<void>;
1695
1725
  }
1696
1726
 
1727
+ /** The number of bytes encoded by an RFC 4122 UUID. */
1728
+ export declare const UUID_BYTE_COUNT = 16;
1729
+
1730
+ /** The number of distinct values one UUID byte may hold. */
1731
+ export declare const UUID_BYTE_RANGE = 256;
1732
+
1697
1733
  /**
1698
1734
  * Match a value against a wildcard pattern in LINEAR time — the shared, ReDoS-SAFE
1699
1735
  * engine behind {@link likeMatch} and {@link globMatch}.
@@ -23,6 +23,10 @@ var DEFAULT_PRIMARY = "id";
23
23
  * `VALIDATION` {@link DatabaseError}; the cap is generous for any legitimate search.
24
24
  */
25
25
  var MAX_PATTERN_LENGTH = 1024;
26
+ /** The number of bytes encoded by an RFC 4122 UUID. */
27
+ var UUID_BYTE_COUNT = 16;
28
+ /** The number of distinct values one UUID byte may hold. */
29
+ var UUID_BYTE_RANGE = 256;
26
30
  //#endregion
27
31
  //#region src/core/errors.ts
28
32
  /**
@@ -1311,6 +1315,40 @@ async function auditDriver(factory) {
1311
1315
  for await (const finding of driverFindings(factory)) findings.push(finding);
1312
1316
  return findings;
1313
1317
  }
1318
+ /**
1319
+ * Generate an RFC 4122 version 4 UUID from a number source — no host crypto global.
1320
+ *
1321
+ * @remarks
1322
+ * Draws exactly {@link UUID_BYTE_COUNT} values from `random`, one per byte, then
1323
+ * forces the version (`4`) and variant (`10xx`) bits. The default source is
1324
+ * `Math.random` — a pure-ECMAScript intrinsic, so generation works on every host;
1325
+ * pass a seeded source (`seededRandom` from `@orkestrel/contract`) and reuse it
1326
+ * across calls for reproducible sequences in tests and fixtures — production
1327
+ * identifiers should keep the default source, whose engine entropy is far larger
1328
+ * than a 32-bit seed. Each byte is floored and masked, so a source straying
1329
+ * outside `[0, 1)` (negative, `>= 1`, `NaN`, `Infinity`) can never yield a
1330
+ * malformed UUID. Suitable as a collision-resistant record identifier — not a
1331
+ * cryptographic token; never use one as a secret.
1332
+ *
1333
+ * @param random - A number source returning values in the half-open range `[0, 1)` (defaults to `Math.random`)
1334
+ * @returns A lowercase RFC 4122 version 4 UUID
1335
+ *
1336
+ * @example
1337
+ * ```ts
1338
+ * import { generateUUID } from '@orkestrel/database'
1339
+ * import { seededRandom } from '@orkestrel/contract'
1340
+ *
1341
+ * generateUUID() // e.g. '9b2f7c1e-3d4a-4f6b-8e2d-5a1c0b9f8e7d'
1342
+ * generateUUID(seededRandom(42)) // the same UUID on every run
1343
+ * ```
1344
+ */
1345
+ function generateUUID(random = Math.random) {
1346
+ const bytes = Array.from({ length: 16 }, () => Math.floor(random() * 256) & 255);
1347
+ bytes[6] = bytes[6] & 15 | 64;
1348
+ bytes[8] = bytes[8] & 63 | 128;
1349
+ const hex = bytes.map((byte) => byte.toString(16).padStart(2, "0"));
1350
+ return `${hex.slice(0, 4).join("")}-${hex.slice(4, 6).join("")}-${hex.slice(6, 8).join("")}-${hex.slice(8, 10).join("")}-${hex.slice(10).join("")}`;
1351
+ }
1314
1352
  //#endregion
1315
1353
  //#region src/core/Cursor.ts
1316
1354
  /**
@@ -2416,6 +2454,6 @@ function createMemoryDriver() {
2416
2454
  return new MemoryDriver();
2417
2455
  }
2418
2456
  //#endregion
2419
- export { Clause, Cursor, DEFAULT_PRIMARY, Database, DatabaseError, MAX_PATTERN_LENGTH, MemoryDriver, Query, Table, applyCriteria, auditDriver, checkAbort, compareValues, computeAggregate, conformDriver, createDatabase, createMemoryDriver, deepEqual, driverFindings, extractKey, filterRows, globMatch, isDatabaseError, isDriverMeta, likeMatch, matchesCondition, matchesCriteria, migrateRows, planMigration, shapeToColumnType, sortRows, wildcardMatch };
2457
+ export { Clause, Cursor, DEFAULT_PRIMARY, Database, DatabaseError, MAX_PATTERN_LENGTH, MemoryDriver, Query, Table, UUID_BYTE_COUNT, UUID_BYTE_RANGE, applyCriteria, auditDriver, checkAbort, compareValues, computeAggregate, conformDriver, createDatabase, createMemoryDriver, deepEqual, driverFindings, extractKey, filterRows, generateUUID, globMatch, isDatabaseError, isDriverMeta, likeMatch, matchesCondition, matchesCriteria, migrateRows, planMigration, shapeToColumnType, sortRows, wildcardMatch };
2420
2458
 
2421
2459
  //# sourceMappingURL=index.js.map