typed-csv 1.0.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 +190 -0
- package/dist/csv-loader/esbuild.d.mts +45 -0
- package/dist/csv-loader/esbuild.d.ts +45 -0
- package/dist/csv-loader/esbuild.js +2020 -0
- package/dist/csv-loader/esbuild.mjs +1985 -0
- package/dist/csv-loader/loader.d.mts +154 -0
- package/dist/csv-loader/loader.d.ts +154 -0
- package/dist/csv-loader/loader.js +1736 -0
- package/dist/csv-loader/loader.mjs +1701 -0
- package/dist/csv-loader/rollup.d.mts +60 -0
- package/dist/csv-loader/rollup.d.ts +60 -0
- package/dist/csv-loader/rollup.js +2006 -0
- package/dist/csv-loader/rollup.mjs +1971 -0
- package/dist/csv-loader/webpack.d.mts +42 -0
- package/dist/csv-loader/webpack.d.ts +42 -0
- package/dist/csv-loader/webpack.js +1979 -0
- package/dist/csv-loader/webpack.mjs +1948 -0
- package/dist/index.d.mts +65 -0
- package/dist/index.d.ts +65 -0
- package/dist/index.js +877 -0
- package/dist/index.mjs +845 -0
- package/package.json +76 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
type SchemaType = "string" | "number" | "int" | "float" | "boolean";
|
|
2
|
+
interface PrimitiveSchema {
|
|
3
|
+
type: SchemaType;
|
|
4
|
+
}
|
|
5
|
+
interface NamedSchema {
|
|
6
|
+
name?: string;
|
|
7
|
+
schema: Schema;
|
|
8
|
+
}
|
|
9
|
+
interface TupleSchema {
|
|
10
|
+
type: "tuple";
|
|
11
|
+
elements: NamedSchema[];
|
|
12
|
+
}
|
|
13
|
+
interface ArraySchema {
|
|
14
|
+
type: "array";
|
|
15
|
+
element: Schema;
|
|
16
|
+
}
|
|
17
|
+
interface ReferenceSchema {
|
|
18
|
+
type: "reference";
|
|
19
|
+
/** Referenced table name (e.g., 'parts' from '@parts[]') */
|
|
20
|
+
tableName: string;
|
|
21
|
+
/** Whether it's an array reference */
|
|
22
|
+
isArray: boolean;
|
|
23
|
+
/** Whether it's optional (allows empty input resolving to null) */
|
|
24
|
+
isOptional?: boolean;
|
|
25
|
+
}
|
|
26
|
+
interface ReverseReferenceSchema {
|
|
27
|
+
type: "reverseReference";
|
|
28
|
+
/** Referenced table name (e.g., 'orders' from '~orders(user)') */
|
|
29
|
+
tableName: string;
|
|
30
|
+
/** The foreign key field name in the referenced table (e.g., 'user' from '~orders(user)') */
|
|
31
|
+
foreignKey: string;
|
|
32
|
+
/** Whether it's optional (null if no matches, instead of empty array) */
|
|
33
|
+
isOptional?: boolean;
|
|
34
|
+
}
|
|
35
|
+
interface StringLiteralSchema {
|
|
36
|
+
type: "stringLiteral";
|
|
37
|
+
value: string;
|
|
38
|
+
}
|
|
39
|
+
interface UnionSchema {
|
|
40
|
+
type: "union";
|
|
41
|
+
members: Schema[];
|
|
42
|
+
}
|
|
43
|
+
type Schema = PrimitiveSchema | TupleSchema | ArraySchema | ReferenceSchema | ReverseReferenceSchema | StringLiteralSchema | UnionSchema;
|
|
44
|
+
|
|
45
|
+
interface CsvLoaderOptions {
|
|
46
|
+
delimiter?: string;
|
|
47
|
+
quote?: string;
|
|
48
|
+
escape?: string;
|
|
49
|
+
bom?: boolean;
|
|
50
|
+
comment?: string | false;
|
|
51
|
+
trim?: boolean;
|
|
52
|
+
/** Generate TypeScript declaration file (.d.ts) */
|
|
53
|
+
emitTypes?: boolean;
|
|
54
|
+
/** Output directory for generated type files (relative to output path) */
|
|
55
|
+
typesOutputDir?: string;
|
|
56
|
+
/** Write .d.ts files to disk (useful for dev server) */
|
|
57
|
+
writeToDisk?: boolean;
|
|
58
|
+
/** Base directory for resolving referenced CSV files (default: directory of current file) */
|
|
59
|
+
refBaseDir?: string;
|
|
60
|
+
/** Primary key field name for referenced tables (default: 'id') */
|
|
61
|
+
defaultPrimaryKey?: string;
|
|
62
|
+
/** Current file path (used to resolve relative references) */
|
|
63
|
+
currentFilePath?: string;
|
|
64
|
+
/**
|
|
65
|
+
* When false, reference fields store parsed IDs instead of resolved objects.
|
|
66
|
+
* Used by csvToModule to emit accessor-based code with lazy resolution.
|
|
67
|
+
* Default: true (resolves references eagerly by loading referenced CSV files).
|
|
68
|
+
*/
|
|
69
|
+
resolveReferences?: boolean;
|
|
70
|
+
}
|
|
71
|
+
interface ReferenceFieldInfo {
|
|
72
|
+
/** Column name in the CSV */
|
|
73
|
+
name: string;
|
|
74
|
+
/** Referenced table name */
|
|
75
|
+
tableName: string;
|
|
76
|
+
/** Whether it's an array reference */
|
|
77
|
+
isArray: boolean;
|
|
78
|
+
/** The schema of this field (for nested references) */
|
|
79
|
+
schema: Schema;
|
|
80
|
+
/** For reverse references: the foreign key field name in the referenced table */
|
|
81
|
+
foreignKey?: string;
|
|
82
|
+
}
|
|
83
|
+
interface CsvParseResult {
|
|
84
|
+
/** Parsed CSV data as array of objects */
|
|
85
|
+
data: Record<string, unknown>[];
|
|
86
|
+
/** Generated TypeScript type definition string (if emitTypes is true) */
|
|
87
|
+
typeDefinition?: string;
|
|
88
|
+
/** Property configurations for the CSV columns */
|
|
89
|
+
propertyConfigs: PropertyConfig[];
|
|
90
|
+
/** Referenced table names */
|
|
91
|
+
references: Set<string>;
|
|
92
|
+
/** Reference field metadata (populated when resolveReferences is false) */
|
|
93
|
+
referenceFields: ReferenceFieldInfo[];
|
|
94
|
+
/** Reverse reference declarations parsed from comment lines */
|
|
95
|
+
reverseReferences: ReverseReferenceDeclaration[];
|
|
96
|
+
/** Type declarations parsed from comment lines */
|
|
97
|
+
typeDeclarations: TypeDeclaration[];
|
|
98
|
+
}
|
|
99
|
+
interface PropertyConfig {
|
|
100
|
+
name: string;
|
|
101
|
+
schema: any;
|
|
102
|
+
validator: (value: unknown) => boolean;
|
|
103
|
+
parser: (valueString: string) => unknown;
|
|
104
|
+
/** Whether this property is a reference to another table */
|
|
105
|
+
isReference?: boolean;
|
|
106
|
+
/** Referenced table name (if isReference is true) */
|
|
107
|
+
referenceTableName?: string;
|
|
108
|
+
/** Whether it's an array reference */
|
|
109
|
+
referenceIsArray?: boolean;
|
|
110
|
+
/** Whether this is a reverse reference (one-to-many) */
|
|
111
|
+
isReverseReference?: boolean;
|
|
112
|
+
/** Foreign key field name for reverse references */
|
|
113
|
+
reverseReferenceForeignKey?: string;
|
|
114
|
+
/** When a column uses a declared type name, this stores that name */
|
|
115
|
+
declaredTypeName?: string;
|
|
116
|
+
/** The original schema string for the column (preserves type name references for output) */
|
|
117
|
+
schemaString?: string;
|
|
118
|
+
}
|
|
119
|
+
/** Parsed reverse reference declaration from a comment line */
|
|
120
|
+
interface ReverseReferenceDeclaration {
|
|
121
|
+
/** Field name in the current table */
|
|
122
|
+
fieldName: string;
|
|
123
|
+
/** Referenced table name */
|
|
124
|
+
tableName: string;
|
|
125
|
+
/** Foreign key field name in the referenced table */
|
|
126
|
+
foreignKey: string;
|
|
127
|
+
/** Whether it's optional */
|
|
128
|
+
isOptional: boolean;
|
|
129
|
+
/** The parsed schema */
|
|
130
|
+
schema: ReverseReferenceSchema;
|
|
131
|
+
}
|
|
132
|
+
/** Parsed type declaration from a comment line */
|
|
133
|
+
interface TypeDeclaration {
|
|
134
|
+
/** Name of the type being defined */
|
|
135
|
+
name: string;
|
|
136
|
+
/** The original schema string (preserves type name references for output) */
|
|
137
|
+
schemaString: string;
|
|
138
|
+
/** The parsed schema for this type */
|
|
139
|
+
schema: Schema;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Parse CSV content string into structured data with schema validation.
|
|
144
|
+
* This is a standalone function that doesn't depend on webpack/rspack LoaderContext.
|
|
145
|
+
*
|
|
146
|
+
* @param content - CSV content string (must have at least headers + schema row + 1 data row)
|
|
147
|
+
* @param options - Parsing options
|
|
148
|
+
* @returns CsvParseResult containing parsed data and optional type definitions
|
|
149
|
+
*/
|
|
150
|
+
declare function parseCsv(content: string, options?: CsvLoaderOptions & {
|
|
151
|
+
resourceName?: string;
|
|
152
|
+
}): CsvParseResult;
|
|
153
|
+
|
|
154
|
+
export { parseCsv };
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
type SchemaType = "string" | "number" | "int" | "float" | "boolean";
|
|
2
|
+
interface PrimitiveSchema {
|
|
3
|
+
type: SchemaType;
|
|
4
|
+
}
|
|
5
|
+
interface NamedSchema {
|
|
6
|
+
name?: string;
|
|
7
|
+
schema: Schema;
|
|
8
|
+
}
|
|
9
|
+
interface TupleSchema {
|
|
10
|
+
type: "tuple";
|
|
11
|
+
elements: NamedSchema[];
|
|
12
|
+
}
|
|
13
|
+
interface ArraySchema {
|
|
14
|
+
type: "array";
|
|
15
|
+
element: Schema;
|
|
16
|
+
}
|
|
17
|
+
interface ReferenceSchema {
|
|
18
|
+
type: "reference";
|
|
19
|
+
/** Referenced table name (e.g., 'parts' from '@parts[]') */
|
|
20
|
+
tableName: string;
|
|
21
|
+
/** Whether it's an array reference */
|
|
22
|
+
isArray: boolean;
|
|
23
|
+
/** Whether it's optional (allows empty input resolving to null) */
|
|
24
|
+
isOptional?: boolean;
|
|
25
|
+
}
|
|
26
|
+
interface ReverseReferenceSchema {
|
|
27
|
+
type: "reverseReference";
|
|
28
|
+
/** Referenced table name (e.g., 'orders' from '~orders(user)') */
|
|
29
|
+
tableName: string;
|
|
30
|
+
/** The foreign key field name in the referenced table (e.g., 'user' from '~orders(user)') */
|
|
31
|
+
foreignKey: string;
|
|
32
|
+
/** Whether it's optional (null if no matches, instead of empty array) */
|
|
33
|
+
isOptional?: boolean;
|
|
34
|
+
}
|
|
35
|
+
interface StringLiteralSchema {
|
|
36
|
+
type: "stringLiteral";
|
|
37
|
+
value: string;
|
|
38
|
+
}
|
|
39
|
+
interface UnionSchema {
|
|
40
|
+
type: "union";
|
|
41
|
+
members: Schema[];
|
|
42
|
+
}
|
|
43
|
+
type Schema = PrimitiveSchema | TupleSchema | ArraySchema | ReferenceSchema | ReverseReferenceSchema | StringLiteralSchema | UnionSchema;
|
|
44
|
+
|
|
45
|
+
interface CsvLoaderOptions {
|
|
46
|
+
delimiter?: string;
|
|
47
|
+
quote?: string;
|
|
48
|
+
escape?: string;
|
|
49
|
+
bom?: boolean;
|
|
50
|
+
comment?: string | false;
|
|
51
|
+
trim?: boolean;
|
|
52
|
+
/** Generate TypeScript declaration file (.d.ts) */
|
|
53
|
+
emitTypes?: boolean;
|
|
54
|
+
/** Output directory for generated type files (relative to output path) */
|
|
55
|
+
typesOutputDir?: string;
|
|
56
|
+
/** Write .d.ts files to disk (useful for dev server) */
|
|
57
|
+
writeToDisk?: boolean;
|
|
58
|
+
/** Base directory for resolving referenced CSV files (default: directory of current file) */
|
|
59
|
+
refBaseDir?: string;
|
|
60
|
+
/** Primary key field name for referenced tables (default: 'id') */
|
|
61
|
+
defaultPrimaryKey?: string;
|
|
62
|
+
/** Current file path (used to resolve relative references) */
|
|
63
|
+
currentFilePath?: string;
|
|
64
|
+
/**
|
|
65
|
+
* When false, reference fields store parsed IDs instead of resolved objects.
|
|
66
|
+
* Used by csvToModule to emit accessor-based code with lazy resolution.
|
|
67
|
+
* Default: true (resolves references eagerly by loading referenced CSV files).
|
|
68
|
+
*/
|
|
69
|
+
resolveReferences?: boolean;
|
|
70
|
+
}
|
|
71
|
+
interface ReferenceFieldInfo {
|
|
72
|
+
/** Column name in the CSV */
|
|
73
|
+
name: string;
|
|
74
|
+
/** Referenced table name */
|
|
75
|
+
tableName: string;
|
|
76
|
+
/** Whether it's an array reference */
|
|
77
|
+
isArray: boolean;
|
|
78
|
+
/** The schema of this field (for nested references) */
|
|
79
|
+
schema: Schema;
|
|
80
|
+
/** For reverse references: the foreign key field name in the referenced table */
|
|
81
|
+
foreignKey?: string;
|
|
82
|
+
}
|
|
83
|
+
interface CsvParseResult {
|
|
84
|
+
/** Parsed CSV data as array of objects */
|
|
85
|
+
data: Record<string, unknown>[];
|
|
86
|
+
/** Generated TypeScript type definition string (if emitTypes is true) */
|
|
87
|
+
typeDefinition?: string;
|
|
88
|
+
/** Property configurations for the CSV columns */
|
|
89
|
+
propertyConfigs: PropertyConfig[];
|
|
90
|
+
/** Referenced table names */
|
|
91
|
+
references: Set<string>;
|
|
92
|
+
/** Reference field metadata (populated when resolveReferences is false) */
|
|
93
|
+
referenceFields: ReferenceFieldInfo[];
|
|
94
|
+
/** Reverse reference declarations parsed from comment lines */
|
|
95
|
+
reverseReferences: ReverseReferenceDeclaration[];
|
|
96
|
+
/** Type declarations parsed from comment lines */
|
|
97
|
+
typeDeclarations: TypeDeclaration[];
|
|
98
|
+
}
|
|
99
|
+
interface PropertyConfig {
|
|
100
|
+
name: string;
|
|
101
|
+
schema: any;
|
|
102
|
+
validator: (value: unknown) => boolean;
|
|
103
|
+
parser: (valueString: string) => unknown;
|
|
104
|
+
/** Whether this property is a reference to another table */
|
|
105
|
+
isReference?: boolean;
|
|
106
|
+
/** Referenced table name (if isReference is true) */
|
|
107
|
+
referenceTableName?: string;
|
|
108
|
+
/** Whether it's an array reference */
|
|
109
|
+
referenceIsArray?: boolean;
|
|
110
|
+
/** Whether this is a reverse reference (one-to-many) */
|
|
111
|
+
isReverseReference?: boolean;
|
|
112
|
+
/** Foreign key field name for reverse references */
|
|
113
|
+
reverseReferenceForeignKey?: string;
|
|
114
|
+
/** When a column uses a declared type name, this stores that name */
|
|
115
|
+
declaredTypeName?: string;
|
|
116
|
+
/** The original schema string for the column (preserves type name references for output) */
|
|
117
|
+
schemaString?: string;
|
|
118
|
+
}
|
|
119
|
+
/** Parsed reverse reference declaration from a comment line */
|
|
120
|
+
interface ReverseReferenceDeclaration {
|
|
121
|
+
/** Field name in the current table */
|
|
122
|
+
fieldName: string;
|
|
123
|
+
/** Referenced table name */
|
|
124
|
+
tableName: string;
|
|
125
|
+
/** Foreign key field name in the referenced table */
|
|
126
|
+
foreignKey: string;
|
|
127
|
+
/** Whether it's optional */
|
|
128
|
+
isOptional: boolean;
|
|
129
|
+
/** The parsed schema */
|
|
130
|
+
schema: ReverseReferenceSchema;
|
|
131
|
+
}
|
|
132
|
+
/** Parsed type declaration from a comment line */
|
|
133
|
+
interface TypeDeclaration {
|
|
134
|
+
/** Name of the type being defined */
|
|
135
|
+
name: string;
|
|
136
|
+
/** The original schema string (preserves type name references for output) */
|
|
137
|
+
schemaString: string;
|
|
138
|
+
/** The parsed schema for this type */
|
|
139
|
+
schema: Schema;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Parse CSV content string into structured data with schema validation.
|
|
144
|
+
* This is a standalone function that doesn't depend on webpack/rspack LoaderContext.
|
|
145
|
+
*
|
|
146
|
+
* @param content - CSV content string (must have at least headers + schema row + 1 data row)
|
|
147
|
+
* @param options - Parsing options
|
|
148
|
+
* @returns CsvParseResult containing parsed data and optional type definitions
|
|
149
|
+
*/
|
|
150
|
+
declare function parseCsv(content: string, options?: CsvLoaderOptions & {
|
|
151
|
+
resourceName?: string;
|
|
152
|
+
}): CsvParseResult;
|
|
153
|
+
|
|
154
|
+
export { parseCsv };
|