@rebasepro/schema-inference 0.0.1-canary.09e5ec5

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.
@@ -0,0 +1,34 @@
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
+ }
package/src/types.ts ADDED
@@ -0,0 +1,43 @@
1
+ import { DataType } from "@rebasepro/types";
2
+
3
+ export type TypesCount = {
4
+ number?: number,
5
+ string?: number,
6
+ boolean?: number,
7
+ map?: TypesCountRecord,
8
+ array?: TypesCount,
9
+ date?: number,
10
+ geopoint?: number,
11
+ reference?: number,
12
+ relation?: number,
13
+ };
14
+
15
+ export type TypesCountRecord<K extends keyof DataType = any> = {
16
+ [P in K]: TypesCount
17
+ };
18
+
19
+ export type ValuesCountEntry = {
20
+ values: any[];
21
+ valuesCount: Map<any, number>;
22
+ mapValues?: ValuesCountRecord;
23
+ };
24
+
25
+ export type ValuesCountRecord = Record<string, ValuesCountEntry>;
26
+
27
+ export type InferencePropertyBuilderProps = {
28
+
29
+ /**
30
+ * Name of the property
31
+ */
32
+ name: string;
33
+
34
+ /**
35
+ * Total documents this props are built from
36
+ */
37
+ totalDocsCount: number;
38
+
39
+ /**
40
+ * How many times does each value show up
41
+ */
42
+ valuesResult?: ValuesCountEntry;
43
+ };
package/src/util.ts ADDED
@@ -0,0 +1,44 @@
1
+ import { EnumValueConfig, EnumValues } from "@rebasepro/types";
2
+ import { unslugify } from "@rebasepro/utils";
3
+
4
+ // Canonical utility functions — single source of truth in @rebasepro/utils
5
+ export { unslugify, prettifyIdentifier, isObject, isPlainObject, mergeDeep } from "@rebasepro/utils";
6
+
7
+ /**
8
+ * Extract enum values from a list of sample values.
9
+ * This is schema-inference-specific logic (not a general utility).
10
+ */
11
+ export function extractEnumFromValues(values: unknown[]) {
12
+ if (!Array.isArray(values)) {
13
+ return [];
14
+ }
15
+ const enumValues = values
16
+ .map((value) => {
17
+ if (typeof value === "string") {
18
+ return ({
19
+ id: value,
20
+ label: unslugify(value)
21
+ });
22
+ } else
23
+ return null;
24
+ }).filter(Boolean) as Array<{ id: string, label: string }>;
25
+ enumValues.sort((a, b) => a.label.localeCompare(b.label));
26
+ return enumValues;
27
+ }
28
+
29
+ export function resolveEnumValues(input: EnumValues): EnumValueConfig[] | undefined {
30
+ // Check Array.isArray first since typeof [] === "object" is true in JavaScript
31
+ if (Array.isArray(input)) {
32
+ return input as EnumValueConfig[];
33
+ } else if (typeof input === "object" && input !== null) {
34
+ return Object.entries(input).map(([id, value]) =>
35
+ (typeof value === "string"
36
+ ? {
37
+ id,
38
+ label: value
39
+ }
40
+ : value));
41
+ } else {
42
+ return undefined;
43
+ }
44
+ }