edinburgh 0.4.6 → 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/README.md +403 -461
- package/build/src/datapack.d.ts +9 -9
- package/build/src/datapack.js +10 -10
- package/build/src/datapack.js.map +1 -1
- package/build/src/edinburgh.d.ts +21 -10
- package/build/src/edinburgh.js +33 -55
- package/build/src/edinburgh.js.map +1 -1
- package/build/src/indexes.d.ts +99 -288
- package/build/src/indexes.js +253 -636
- package/build/src/indexes.js.map +1 -1
- package/build/src/migrate.js +17 -39
- package/build/src/migrate.js.map +1 -1
- package/build/src/models.d.ts +177 -113
- package/build/src/models.js +487 -259
- package/build/src/models.js.map +1 -1
- package/build/src/types.d.ts +41 -51
- package/build/src/types.js +39 -52
- package/build/src/types.js.map +1 -1
- package/build/src/utils.d.ts +4 -4
- package/build/src/utils.js +4 -4
- package/package.json +1 -3
- package/skill/AnyModelClass.md +7 -0
- package/skill/FindOptions.md +37 -0
- package/skill/Lifecycle Hooks.md +24 -0
- package/skill/{Model_delete.md → Lifecycle Hooks_delete.md } +2 -2
- package/skill/{Model_getPrimaryKeyHash.md → Lifecycle Hooks_getPrimaryKeyHash.md } +1 -1
- package/skill/{Model_isValid.md → Lifecycle Hooks_isValid.md } +1 -1
- package/skill/Lifecycle Hooks_migrate.md +26 -0
- package/skill/{Model_preCommit.md → Lifecycle Hooks_preCommit.md } +3 -5
- package/skill/{Model_preventPersist.md → Lifecycle Hooks_preventPersist.md } +2 -2
- package/skill/{Model_validate.md → Lifecycle Hooks_validate.md } +2 -2
- package/skill/ModelBase.md +7 -0
- package/skill/ModelClass.md +8 -0
- package/skill/SKILL.md +253 -215
- package/skill/Schema Evolution.md +19 -0
- package/skill/TypeWrapper_containsNull.md +11 -0
- package/skill/TypeWrapper_deserialize.md +9 -0
- package/skill/TypeWrapper_getError.md +11 -0
- package/skill/TypeWrapper_serialize.md +10 -0
- package/skill/TypeWrapper_serializeType.md +9 -0
- package/skill/array.md +2 -2
- package/skill/defineModel.md +23 -0
- package/skill/deleteEverything.md +8 -0
- package/skill/field.md +4 -4
- package/skill/link.md +12 -10
- package/skill/literal.md +1 -1
- package/skill/opt.md +1 -1
- package/skill/or.md +1 -1
- package/skill/record.md +1 -1
- package/skill/set.md +2 -2
- package/skill/setOnSaveCallback.md +2 -2
- package/skill/transact.md +3 -3
- package/src/datapack.ts +10 -10
- package/src/edinburgh.ts +46 -58
- package/src/indexes.ts +338 -802
- package/src/migrate.ts +15 -37
- package/src/models.ts +617 -314
- package/src/types.ts +61 -54
- package/src/utils.ts +4 -4
- package/skill/BaseIndex.md +0 -16
- package/skill/BaseIndex_batchProcess.md +0 -10
- package/skill/BaseIndex_find.md +0 -7
- package/skill/Model.md +0 -22
- package/skill/Model_findAll.md +0 -12
- package/skill/Model_migrate.md +0 -34
- package/skill/Model_replaceInto.md +0 -16
- package/skill/PrimaryIndex.md +0 -8
- package/skill/PrimaryIndex_get.md +0 -17
- package/skill/PrimaryIndex_getLazy.md +0 -13
- package/skill/SecondaryIndex.md +0 -9
- package/skill/UniqueIndex.md +0 -9
- package/skill/UniqueIndex_get.md +0 -17
- package/skill/dump.md +0 -8
- package/skill/index.md +0 -32
- package/skill/primary.md +0 -26
- package/skill/registerModel.md +0 -26
- package/skill/unique.md +0 -32
package/build/src/utils.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as lowlevel from "olmdb/lowlevel";
|
|
2
2
|
/**
|
|
3
3
|
* Assert function for runtime checks with TypeScript assertion support.
|
|
4
|
-
* @param cond
|
|
5
|
-
* @param message
|
|
4
|
+
* @param cond Condition to check.
|
|
5
|
+
* @param message Optional error message.
|
|
6
6
|
* @throws {Error} If condition is false.
|
|
7
7
|
*/
|
|
8
8
|
export function assert(cond, message) {
|
|
@@ -50,8 +50,8 @@ export function dbDel(txnId, key) {
|
|
|
50
50
|
export const ERROR_AT = /^(.*) at ([a-zA-Z0-9_.]+)$/;
|
|
51
51
|
/**
|
|
52
52
|
* Add a path segment to an exception for better error reporting.
|
|
53
|
-
* @param error
|
|
54
|
-
* @param path
|
|
53
|
+
* @param error The (Database)Error to modify.
|
|
54
|
+
* @param path The path segment to add (string or number).
|
|
55
55
|
* @returns The modified DatabaseError.
|
|
56
56
|
*/
|
|
57
57
|
export function addErrorPath(error, path) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "edinburgh",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"author": "Frank van Viegen",
|
|
5
5
|
"devDependencies": {
|
|
6
6
|
"@types/bun": "^1.2.16",
|
|
@@ -49,8 +49,6 @@
|
|
|
49
49
|
"test:node": "bun x vitest --bail 1 --watch=false --silent=passed-only",
|
|
50
50
|
"prepack": "npm run build && npm run test && npm run build:docs",
|
|
51
51
|
"build:docs": "rm -rf skill ; mkdir skill ; cat skill-header.md README.md > skill/SKILL.md && readme-tsdoc --split --file skill/SKILL.md && readme-tsdoc --repo-url https://github.com/vanviegen/edinburgh",
|
|
52
|
-
|
|
53
|
-
|
|
54
52
|
"postinstall": "[ -f .local-post-install.sh ] && sh .local-post-install.sh || true"
|
|
55
53
|
},
|
|
56
54
|
"trustedDependencies": [
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
### FindOptions · type
|
|
2
|
+
|
|
3
|
+
Range-query options accepted by `find()`, `findBy()`, `batchProcess()`, and `batchProcessBy()`.
|
|
4
|
+
|
|
5
|
+
Supports exact-match lookups via `is`, inclusive bounds via `from` / `to`,
|
|
6
|
+
exclusive bounds via `after` / `before`, and reverse scans.
|
|
7
|
+
|
|
8
|
+
For single-field indexes, values can be passed directly. For composite indexes,
|
|
9
|
+
pass tuples or partial tuples for prefix matching.
|
|
10
|
+
|
|
11
|
+
**Type:** `(
|
|
12
|
+
(
|
|
13
|
+
{is: ArrayOrOnlyItem<ARG_TYPES>;} // Shortcut for setting `from` and `to` to the same value
|
|
14
|
+
|
|
|
15
|
+
(
|
|
16
|
+
(
|
|
17
|
+
{from: ArrayOrOnlyItem<ARG_TYPES>;}
|
|
18
|
+
|
|
|
19
|
+
{after: ArrayOrOnlyItem<ARG_TYPES>;}
|
|
20
|
+
|
|
|
21
|
+
{}
|
|
22
|
+
)
|
|
23
|
+
&
|
|
24
|
+
(
|
|
25
|
+
{to: ArrayOrOnlyItem<ARG_TYPES>;}
|
|
26
|
+
|
|
|
27
|
+
{before: ArrayOrOnlyItem<ARG_TYPES>;}
|
|
28
|
+
|
|
|
29
|
+
{}
|
|
30
|
+
)
|
|
31
|
+
)
|
|
32
|
+
) &
|
|
33
|
+
{
|
|
34
|
+
reverse?: boolean;
|
|
35
|
+
}
|
|
36
|
+
& (FETCH extends undefined ? { fetch?: undefined } : { fetch: FETCH })
|
|
37
|
+
)`
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
### Lifecycle Hooks
|
|
2
|
+
|
|
3
|
+
- **`static migrate(record)`**: Called when deserializing rows written with an older schema
|
|
4
|
+
version. Receives a plain record object; mutate it in-place to match the current schema.
|
|
5
|
+
|
|
6
|
+
- **`preCommit()`**: Called on each modified instance right before the transaction commits.
|
|
7
|
+
Useful for computing derived fields, enforcing cross-field invariants, or creating related
|
|
8
|
+
instances.
|
|
9
|
+
|
|
10
|
+
**Examples:**
|
|
11
|
+
|
|
12
|
+
```typescript
|
|
13
|
+
const User = E.defineModel("User", class {
|
|
14
|
+
id = E.field(E.identifier);
|
|
15
|
+
name = E.field(E.string);
|
|
16
|
+
email = E.field(E.string);
|
|
17
|
+
}, {
|
|
18
|
+
pk: "id",
|
|
19
|
+
unique: { email: "email" },
|
|
20
|
+
});
|
|
21
|
+
// Optional: declare a companion type so `let u: User` works.
|
|
22
|
+
// Not needed if you only use `new User()`, `User.find()`, etc.
|
|
23
|
+
type User = InstanceType<typeof User>;
|
|
24
|
+
```
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
####
|
|
1
|
+
#### modelBase.delete · method
|
|
2
2
|
|
|
3
3
|
Delete this model instance from the database.
|
|
4
4
|
|
|
@@ -9,6 +9,6 @@ Removes the instance and all its index entries from the database and prevents fu
|
|
|
9
9
|
**Examples:**
|
|
10
10
|
|
|
11
11
|
```typescript
|
|
12
|
-
const user = User.
|
|
12
|
+
const user = User.get("user123");
|
|
13
13
|
user.delete(); // Removes from database
|
|
14
14
|
```
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#### ModelBase.migrate · static method
|
|
2
|
+
|
|
3
|
+
Optional migration function called when deserializing rows written with an older schema version.
|
|
4
|
+
Receives a plain record with all fields and should mutate it in-place to match the current schema.
|
|
5
|
+
It runs during lazy loading and during `runMigration()`. Changing this method creates a new schema version.
|
|
6
|
+
If it updates values used by secondary or unique indexes, those index entries are refreshed only by `runMigration()`.
|
|
7
|
+
|
|
8
|
+
**Signature:** `(record: Record<string, any>) => void`
|
|
9
|
+
|
|
10
|
+
**Parameters:**
|
|
11
|
+
|
|
12
|
+
- `record: Record<string, any>` - A plain object containing the row's field values from the older schema version.
|
|
13
|
+
|
|
14
|
+
**Examples:**
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
const User = E.defineModel("User", class {
|
|
18
|
+
id = E.field(E.identifier);
|
|
19
|
+
name = E.field(E.string);
|
|
20
|
+
role = E.field(E.string);
|
|
21
|
+
|
|
22
|
+
static migrate(record: Record<string, any>) {
|
|
23
|
+
record.role ??= "user";
|
|
24
|
+
}
|
|
25
|
+
}, { pk: "id" });
|
|
26
|
+
```
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
####
|
|
1
|
+
#### modelBase.preCommit · method
|
|
2
2
|
|
|
3
3
|
Optional hook called on each modified instance right before the transaction commits.
|
|
4
4
|
Runs before data is written to disk, so changes made here are included in the commit.
|
|
@@ -14,9 +14,7 @@ Common use cases:
|
|
|
14
14
|
**Examples:**
|
|
15
15
|
|
|
16
16
|
```typescript
|
|
17
|
-
|
|
18
|
-
class Post extends E.Model<Post> {
|
|
19
|
-
static pk = E.primary(Post, "id");
|
|
17
|
+
const Post = E.defineModel("Post", class {
|
|
20
18
|
id = E.field(E.identifier);
|
|
21
19
|
title = E.field(E.string);
|
|
22
20
|
slug = E.field(E.string);
|
|
@@ -24,5 +22,5 @@ class Post extends E.Model<Post> {
|
|
|
24
22
|
preCommit() {
|
|
25
23
|
this.slug = this.title.toLowerCase().replace(/\s+/g, "-");
|
|
26
24
|
}
|
|
27
|
-
}
|
|
25
|
+
}, { pk: "id" });
|
|
28
26
|
```
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
####
|
|
1
|
+
#### modelBase.preventPersist · method
|
|
2
2
|
|
|
3
3
|
Prevent this instance from being persisted to the database.
|
|
4
4
|
|
|
@@ -9,7 +9,7 @@ Prevent this instance from being persisted to the database.
|
|
|
9
9
|
**Examples:**
|
|
10
10
|
|
|
11
11
|
```typescript
|
|
12
|
-
const user = User.
|
|
12
|
+
const user = User.get("user123");
|
|
13
13
|
user.name = "New Name";
|
|
14
14
|
user.preventPersist(); // Changes won't be saved
|
|
15
15
|
```
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
####
|
|
1
|
+
#### modelBase.validate · method
|
|
2
2
|
|
|
3
3
|
Validate all fields in this model instance.
|
|
4
4
|
|
|
@@ -6,7 +6,7 @@ Validate all fields in this model instance.
|
|
|
6
6
|
|
|
7
7
|
**Parameters:**
|
|
8
8
|
|
|
9
|
-
- `raise: boolean` (optional) -
|
|
9
|
+
- `raise: boolean` (optional) - If true, throw on first validation error.
|
|
10
10
|
|
|
11
11
|
**Returns:** Array of validation errors (empty if valid).
|
|
12
12
|
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
### ModelBase · abstract class
|
|
2
|
+
|
|
3
|
+
Base class for all database models in the Edinburgh ORM.
|
|
4
|
+
|
|
5
|
+
Models represent database entities with typed fields, automatic serialization,
|
|
6
|
+
change tracking, and relationship management. Model classes are created using
|
|
7
|
+
`E.defineModel()`.
|