@rebasepro/schema-inference 0.4.0 → 0.5.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
CHANGED
|
@@ -1 +1,76 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @rebasepro/schema-inference
|
|
2
|
+
|
|
3
|
+
Automatically infer Rebase collection property schemas from sample data — analyzes field types, detects enums, references, validation rules, and nested structures.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @rebasepro/schema-inference
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## What This Package Does
|
|
12
|
+
|
|
13
|
+
`@rebasepro/schema-inference` examines arrays of data objects and produces `Properties` definitions compatible with the Rebase collection schema. It uses statistical analysis to determine the most probable type for each field, detect enum values, identify reference patterns, and build nested map/array structures. Used by the Rebase introspection pipeline and data import tools.
|
|
14
|
+
|
|
15
|
+
## Key Exports
|
|
16
|
+
|
|
17
|
+
### Collection Builder
|
|
18
|
+
|
|
19
|
+
| Export | Type | Description |
|
|
20
|
+
|---|---|---|
|
|
21
|
+
| `buildEntityPropertiesFromData` | `(data: object[], getType: InferenceTypeBuilder) => Promise<Properties>` | Main entry — infer full property schema from data |
|
|
22
|
+
| `buildPropertyFromData` | `(data: unknown[], property: Property, getType: InferenceTypeBuilder) => Property` | Refine an existing property with new sample data |
|
|
23
|
+
| `buildPropertiesOrder` | `(properties: Properties, propertiesOrder?: string[], priorityKeys?: string[]) => string[]` | Sort property keys (title/name first, then images, then alphabetical) |
|
|
24
|
+
| `inferTypeFromValue` | `(value: unknown) => DataType` | Default type inference: string, number, boolean, array, map, vector |
|
|
25
|
+
| `InferenceTypeBuilder` | Type | `(value: unknown) => DataType` — pluggable type detector |
|
|
26
|
+
|
|
27
|
+
### String Utilities
|
|
28
|
+
|
|
29
|
+
| Export | Description |
|
|
30
|
+
|---|---|
|
|
31
|
+
| `parseReferenceString` | Parse `"path/entityId"` or `"db:::path/entityId"` format |
|
|
32
|
+
| `looksLikeReference` | Check if a string looks like a document reference |
|
|
33
|
+
| `findCommonInitialStringInPath` | Find shared collection path prefix in sample values |
|
|
34
|
+
| `removeInitialAndTrailingSlashes` | Path cleanup |
|
|
35
|
+
| `removeInitialSlash` / `removeTrailingSlash` | Path cleanup |
|
|
36
|
+
|
|
37
|
+
### General Utilities (re-exported from `@rebasepro/utils`)
|
|
38
|
+
|
|
39
|
+
| Export | Description |
|
|
40
|
+
|---|---|
|
|
41
|
+
| `extractEnumFromValues` | Extract enum entries from sample string values |
|
|
42
|
+
| `resolveEnumValues` | Normalize `EnumValues` to `EnumValueConfig[]` |
|
|
43
|
+
| `prettifyIdentifier` | `"snake_case"` → `"Snake Case"` |
|
|
44
|
+
| `unslugify` | `"my-slug"` → `"My Slug"` |
|
|
45
|
+
| `mergeDeep` | Deep merge objects |
|
|
46
|
+
|
|
47
|
+
## Quick Start
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import {
|
|
51
|
+
buildEntityPropertiesFromData,
|
|
52
|
+
inferTypeFromValue
|
|
53
|
+
} from "@rebasepro/schema-inference";
|
|
54
|
+
|
|
55
|
+
const sampleData = [
|
|
56
|
+
{ name: "Alice", age: 30, active: true, role: "admin" },
|
|
57
|
+
{ name: "Bob", age: 25, active: false, role: "editor" },
|
|
58
|
+
{ name: "Carol", age: 28, active: true, role: "admin" },
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
const properties = await buildEntityPropertiesFromData(
|
|
62
|
+
sampleData,
|
|
63
|
+
inferTypeFromValue
|
|
64
|
+
);
|
|
65
|
+
// properties = {
|
|
66
|
+
// name: { type: "string", name: "Name", ... },
|
|
67
|
+
// age: { type: "number", name: "Age", ... },
|
|
68
|
+
// active: { type: "boolean", name: "Active", ... },
|
|
69
|
+
// role: { type: "string", name: "Role", enum: [...], ... }
|
|
70
|
+
// }
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Related Packages
|
|
74
|
+
|
|
75
|
+
- `@rebasepro/types` — Provides `Property`, `DataType`, `Properties`, and related types
|
|
76
|
+
- `@rebasepro/utils` — General utilities re-exported by this package
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rebasepro/schema-inference",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
"ts-jest": "^29.4.10",
|
|
17
17
|
"typescript": "^5.9.3",
|
|
18
18
|
"vite": "^7.3.3",
|
|
19
|
-
"@rebasepro/types": "0.
|
|
20
|
-
"@rebasepro/utils": "0.
|
|
19
|
+
"@rebasepro/types": "0.5.0",
|
|
20
|
+
"@rebasepro/utils": "0.5.0"
|
|
21
21
|
},
|
|
22
22
|
"exports": {
|
|
23
23
|
".": {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { buildEntityPropertiesFromData } from "../collection_builder";
|
|
2
|
-
|
|
3
|
-
// import usage from "./usage.json" assert {
|
|
4
|
-
// type: "json",
|
|
5
|
-
// integrity: "sha384-ABC123"
|
|
6
|
-
// };
|
|
7
|
-
import usage from "./pop_products.json" assert {
|
|
8
|
-
type: "json",
|
|
9
|
-
integrity: "sha384-ABC123"
|
|
10
|
-
};
|
|
11
|
-
import * as util from "util";
|
|
12
|
-
import { DataType } from "@rebasepro/types";
|
|
13
|
-
|
|
14
|
-
buildEntityPropertiesFromData(usage, getType)
|
|
15
|
-
.then((res) => console.log(util.inspect(res, { showHidden: false,
|
|
16
|
-
depth: null,
|
|
17
|
-
colors: true })));
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
function getType(value: any): DataType {
|
|
21
|
-
if (typeof value === "number")
|
|
22
|
-
return "number";
|
|
23
|
-
else if (typeof value === "string")
|
|
24
|
-
return "string";
|
|
25
|
-
else if (typeof value === "boolean")
|
|
26
|
-
return "boolean";
|
|
27
|
-
else if (Array.isArray(value))
|
|
28
|
-
return "array";
|
|
29
|
-
else if (value && "_seconds" in value && "_nanoseconds" in value)
|
|
30
|
-
return "date";
|
|
31
|
-
else if (value && "id" in value && "path" in value)
|
|
32
|
-
return "reference";
|
|
33
|
-
return "map";
|
|
34
|
-
}
|