dirsql 0.3.6 → 0.3.7
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 +2 -1
- package/dist/index.d.ts +7 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/docs/api/index.md +4 -4
- package/docs/getting-started.md +9 -8
- package/docs/guide/async.md +4 -3
- package/docs/guide/crdt.md +4 -3
- package/docs/guide/tables.md +28 -27
- package/docs/guide/watching.md +4 -3
- package/docs/index.md +4 -3
- package/package.json +11 -11
package/README.md
CHANGED
|
@@ -17,6 +17,7 @@ Prebuilt binaries ship for linux-x64, linux-arm64, darwin-x64, darwin-arm64, and
|
|
|
17
17
|
## Usage
|
|
18
18
|
|
|
19
19
|
```typescript
|
|
20
|
+
import { readFileSync } from "node:fs";
|
|
20
21
|
import { DirSQL } from "dirsql";
|
|
21
22
|
|
|
22
23
|
const db = new DirSQL({
|
|
@@ -25,7 +26,7 @@ const db = new DirSQL({
|
|
|
25
26
|
{
|
|
26
27
|
ddl: "CREATE TABLE users (name TEXT, age INTEGER)",
|
|
27
28
|
glob: "data/*.json",
|
|
28
|
-
extract: (
|
|
29
|
+
extract: (filePath) => JSON.parse(readFileSync(filePath, "utf8")),
|
|
29
30
|
},
|
|
30
31
|
],
|
|
31
32
|
});
|
package/dist/index.d.ts
CHANGED
|
@@ -4,8 +4,13 @@ export interface TableDef {
|
|
|
4
4
|
ddl: string;
|
|
5
5
|
/** Glob pattern (relative to the DirSQL root) for files backing this table. */
|
|
6
6
|
glob: string;
|
|
7
|
-
/**
|
|
8
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Produce the rows a matched file contributes. Receives the absolute
|
|
9
|
+
* filesystem path of the file. dirsql does not read file contents; if the
|
|
10
|
+
* callback needs the file body it reads the path itself (e.g.
|
|
11
|
+
* `fs.readFileSync(filePath, "utf8")`). Returns an array of row objects.
|
|
12
|
+
*/
|
|
13
|
+
extract: (filePath: string) => Record<string, unknown>[];
|
|
9
14
|
/** If true, reject rows with columns not declared in `ddl`. */
|
|
10
15
|
strict?: boolean;
|
|
11
16
|
}
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":"AAcA,iEAAiE;AACjE,MAAM,WAAW,QAAQ;IACvB,6EAA6E;IAC7E,GAAG,EAAE,MAAM,CAAC;IACZ,+EAA+E;IAC/E,IAAI,EAAE,MAAM,CAAC;IACb
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":"AAcA,iEAAiE;AACjE,MAAM,WAAW,QAAQ;IACvB,6EAA6E;IAC7E,GAAG,EAAE,MAAM,CAAC;IACZ,+EAA+E;IAC/E,IAAI,EAAE,MAAM,CAAC;IACb;;;;;OAKG;IACH,OAAO,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC;IACzD,+DAA+D;IAC/D,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED;;;;;;GAMG;AACH,MAAM,WAAW,aAAa;IAC5B,8BAA8B;IAC9B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,8EAA8E;IAC9E,MAAM,CAAC,EAAE,QAAQ,EAAE,CAAC;IACpB,oDAAoD;IACpD,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB;;;;;;OAMG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;;OAIG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,qDAAqD;AACrD,MAAM,WAAW,QAAQ;IACvB;;;;OAIG;IACH,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,MAAM,EAAE,QAAQ,GAAG,QAAQ,GAAG,QAAQ,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7B,OAAO,EAAE,aAAa;IAkBlC;;;;;;OAMG;IACG,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC;IAK5D;;;;;;;OAOG;IACG,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;IAKnC;;;;;;;OAOG;IACG,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC;IAKxD;;;;;;;;;;OAUG;IACI,KAAK,IAAI,cAAc,CAAC,QAAQ,EAAE,IAAI,EAAE,OAAO,CAAC;CAaxD"}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":"AAAA,yBAAyB;AACzB,EAAE;AACF,wEAAwE;AACxE,kEAAkE;AAClE,qEAAqE;AACrE,qCAAqC;AACrC,EAAE;AACF,qEAAqE;AACrE,uEAAuE;AACvE,uEAAuE;AACvE,mEAAmE;AAEnE,OAAO,EAAE,cAAc,IAAI,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":"AAAA,yBAAyB;AACzB,EAAE;AACF,wEAAwE;AACxE,kEAAkE;AAClE,qEAAqE;AACrE,qCAAqC;AACrC,EAAE;AACF,qEAAqE;AACrE,uEAAuE;AACvE,uEAAuE;AACvE,mEAAmE;AAEnE,OAAO,EAAE,cAAc,IAAI,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AA6F9E,yEAAyE;AACzE,sEAAsE;AACtE,IAAI,IAAI,GAAsB,IAAI,CAAC;AAEnC,SAAS,OAAO;IACd,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;QAClB,IAAI,GAAG,qBAAqB,EAAgB,CAAC;IAC/C,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;A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MAAM,IAAI,CAAC,KAAK,CAAC;QACjB,OAAO,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC;IAC3C,CAAC;IAED;;;;;;;;;;OAUG;IACH,KAAK,CAAC,CAAC,KAAK;QACV,MAAM,IAAI,CAAC,KAAK,CAAC;QACjB,MAAM,IAAI,CAAC,MAAM,CAAC,YAAY,EAAE,CAAC;QACjC,OAAO,IAAI,EAAE,CAAC;YACZ,qEAAqE;YACrE,gEAAgE;YAChE,sEAAsE;YACtE,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;YACjD,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;gBAC3B,MAAM,KAAK,CAAC;YACd,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
|
package/docs/api/index.md
CHANGED
|
@@ -168,15 +168,15 @@ import { Table } from 'dirsql';
|
|
|
168
168
|
::: code-group
|
|
169
169
|
|
|
170
170
|
```python [Python]
|
|
171
|
-
Table(*, ddl: str, glob: str, extract: Callable[[str
|
|
171
|
+
Table(*, ddl: str, glob: str, extract: Callable[[str], list[dict]])
|
|
172
172
|
```
|
|
173
173
|
|
|
174
174
|
```rust [Rust]
|
|
175
|
-
Table::new(ddl: &str, glob: &str, extract: fn(&str
|
|
175
|
+
Table::new(ddl: &str, glob: &str, extract: fn(&str) -> Vec<Value>)
|
|
176
176
|
```
|
|
177
177
|
|
|
178
178
|
```typescript [TypeScript]
|
|
179
|
-
new Table({ ddl: string, glob: string, extract: (path: string
|
|
179
|
+
new Table({ ddl: string, glob: string, extract: (path: string) => Record<string, unknown>[] })
|
|
180
180
|
```
|
|
181
181
|
|
|
182
182
|
:::
|
|
@@ -187,7 +187,7 @@ Defines a mapping from files to SQLite table rows.
|
|
|
187
187
|
|
|
188
188
|
- `ddl` -- A `CREATE TABLE` statement. The table name is parsed from this DDL.
|
|
189
189
|
- `glob` -- A glob pattern matched against file paths relative to the root directory.
|
|
190
|
-
- `extract` -- A callable `(path
|
|
190
|
+
- `extract` -- A callable `(path) -> list[dict]`. Receives the absolute filesystem path of the matched file. `dirsql` does not read file contents; a callback that needs the file body reads `path` itself. Returns a list of dicts/maps mapping column names to values. Return an empty list to skip a file.
|
|
191
191
|
|
|
192
192
|
**Attributes:**
|
|
193
193
|
|
package/docs/getting-started.md
CHANGED
|
@@ -63,12 +63,12 @@ async def main():
|
|
|
63
63
|
Table(
|
|
64
64
|
ddl="CREATE TABLE posts (title TEXT, author TEXT)",
|
|
65
65
|
glob="posts/*.json",
|
|
66
|
-
extract=lambda path
|
|
66
|
+
extract=lambda path: [json.loads(open(path, encoding="utf-8").read())],
|
|
67
67
|
),
|
|
68
68
|
Table(
|
|
69
69
|
ddl="CREATE TABLE authors (id TEXT, name TEXT)",
|
|
70
70
|
glob="authors/*.json",
|
|
71
|
-
extract=lambda path
|
|
71
|
+
extract=lambda path: [json.loads(open(path, encoding="utf-8").read())],
|
|
72
72
|
),
|
|
73
73
|
],
|
|
74
74
|
)
|
|
@@ -120,12 +120,12 @@ let db = DirSQL::new(
|
|
|
120
120
|
Table::new(
|
|
121
121
|
"CREATE TABLE posts (title TEXT, author TEXT)",
|
|
122
122
|
"posts/*.json",
|
|
123
|
-
|
|
|
123
|
+
|path| vec![row_from_json(&std::fs::read_to_string(path).unwrap())],
|
|
124
124
|
),
|
|
125
125
|
Table::new(
|
|
126
126
|
"CREATE TABLE authors (id TEXT, name TEXT)",
|
|
127
127
|
"authors/*.json",
|
|
128
|
-
|
|
|
128
|
+
|path| vec![row_from_json(&std::fs::read_to_string(path).unwrap())],
|
|
129
129
|
),
|
|
130
130
|
],
|
|
131
131
|
)?;
|
|
@@ -139,18 +139,19 @@ let results = db.query(
|
|
|
139
139
|
```
|
|
140
140
|
|
|
141
141
|
```typescript [TypeScript]
|
|
142
|
+
import { readFileSync } from 'node:fs';
|
|
142
143
|
import { DirSQL, type TableDef } from 'dirsql';
|
|
143
144
|
|
|
144
145
|
const tables: TableDef[] = [
|
|
145
146
|
{
|
|
146
147
|
ddl: 'CREATE TABLE posts (title TEXT, author TEXT)',
|
|
147
148
|
glob: 'posts/*.json',
|
|
148
|
-
extract: (
|
|
149
|
+
extract: (path) => [JSON.parse(readFileSync(path, 'utf8'))],
|
|
149
150
|
},
|
|
150
151
|
{
|
|
151
152
|
ddl: 'CREATE TABLE authors (id TEXT, name TEXT)',
|
|
152
153
|
glob: 'authors/*.json',
|
|
153
|
-
extract: (
|
|
154
|
+
extract: (path) => [JSON.parse(readFileSync(path, 'utf8'))],
|
|
154
155
|
},
|
|
155
156
|
];
|
|
156
157
|
|
|
@@ -169,8 +170,8 @@ const results = await db.query(`
|
|
|
169
170
|
## What happens at startup
|
|
170
171
|
|
|
171
172
|
1. `dirsql` walks the directory tree
|
|
172
|
-
2. Files matching each table's glob pattern are
|
|
173
|
-
3. The `extract` function
|
|
173
|
+
2. Files matching each table's glob pattern are identified
|
|
174
|
+
3. The `extract` function receives each matched file's absolute path and returns rows
|
|
174
175
|
4. Rows are inserted into an in-memory SQLite database
|
|
175
176
|
5. SQL queries run against that database
|
|
176
177
|
|
package/docs/guide/async.md
CHANGED
|
@@ -24,7 +24,7 @@ async def main():
|
|
|
24
24
|
Table(
|
|
25
25
|
ddl="CREATE TABLE items (name TEXT, value INTEGER)",
|
|
26
26
|
glob="data/*.json",
|
|
27
|
-
extract=lambda path
|
|
27
|
+
extract=lambda path: [json.loads(open(path, encoding="utf-8").read())],
|
|
28
28
|
),
|
|
29
29
|
],
|
|
30
30
|
)
|
|
@@ -47,7 +47,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
47
47
|
Table::new(
|
|
48
48
|
"CREATE TABLE items (name TEXT, value INTEGER)",
|
|
49
49
|
"data/*.json",
|
|
50
|
-
|
|
|
50
|
+
|path| vec![serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap()],
|
|
51
51
|
),
|
|
52
52
|
],
|
|
53
53
|
)?;
|
|
@@ -59,6 +59,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
59
59
|
```
|
|
60
60
|
|
|
61
61
|
```typescript [TypeScript]
|
|
62
|
+
import { readFileSync } from 'node:fs';
|
|
62
63
|
import { DirSQL, Table } from 'dirsql';
|
|
63
64
|
|
|
64
65
|
const db = new DirSQL({
|
|
@@ -67,7 +68,7 @@ const db = new DirSQL({
|
|
|
67
68
|
new Table({
|
|
68
69
|
ddl: 'CREATE TABLE items (name TEXT, value INTEGER)',
|
|
69
70
|
glob: 'data/*.json',
|
|
70
|
-
extract: (
|
|
71
|
+
extract: (path) => [JSON.parse(readFileSync(path, 'utf8'))],
|
|
71
72
|
}),
|
|
72
73
|
],
|
|
73
74
|
});
|
package/docs/guide/crdt.md
CHANGED
|
@@ -60,7 +60,7 @@ db = DirSQL(
|
|
|
60
60
|
ddl="CREATE TABLE posts (id TEXT, title TEXT, body TEXT, updated INTEGER)",
|
|
61
61
|
# Match the JSON view, not the raw CRDT binary.
|
|
62
62
|
glob="posts/*/view.json",
|
|
63
|
-
extract=lambda path
|
|
63
|
+
extract=lambda path: [json.loads(open(path, encoding="utf-8").read())],
|
|
64
64
|
),
|
|
65
65
|
],
|
|
66
66
|
)
|
|
@@ -76,20 +76,21 @@ let db = DirSQL::new(
|
|
|
76
76
|
Table::new(
|
|
77
77
|
"CREATE TABLE posts (id TEXT, title TEXT, body TEXT, updated INTEGER)",
|
|
78
78
|
"posts/*/view.json",
|
|
79
|
-
|
|
|
79
|
+
|path| vec![row_from_json(&std::fs::read_to_string(path).unwrap())],
|
|
80
80
|
),
|
|
81
81
|
],
|
|
82
82
|
)?;
|
|
83
83
|
```
|
|
84
84
|
|
|
85
85
|
```typescript [TypeScript]
|
|
86
|
+
import { readFileSync } from 'node:fs';
|
|
86
87
|
import { DirSQL, type TableDef } from 'dirsql';
|
|
87
88
|
|
|
88
89
|
const tables: TableDef[] = [
|
|
89
90
|
{
|
|
90
91
|
ddl: 'CREATE TABLE posts (id TEXT, title TEXT, body TEXT, updated INTEGER)',
|
|
91
92
|
glob: 'posts/*/view.json',
|
|
92
|
-
extract: (
|
|
93
|
+
extract: (path) => [JSON.parse(readFileSync(path, 'utf8'))],
|
|
93
94
|
},
|
|
94
95
|
];
|
|
95
96
|
|
package/docs/guide/tables.md
CHANGED
|
@@ -18,7 +18,7 @@ from dirsql import Table
|
|
|
18
18
|
table = Table(
|
|
19
19
|
ddl="CREATE TABLE comments (id TEXT, body TEXT, author TEXT)",
|
|
20
20
|
glob="comments/**/index.jsonl",
|
|
21
|
-
extract=lambda path
|
|
21
|
+
extract=lambda path: [
|
|
22
22
|
{"id": "...", "body": "...", "author": "..."}
|
|
23
23
|
],
|
|
24
24
|
)
|
|
@@ -31,7 +31,7 @@ use std::collections::HashMap;
|
|
|
31
31
|
let table = Table::new(
|
|
32
32
|
"CREATE TABLE comments (id TEXT, body TEXT, author TEXT)",
|
|
33
33
|
"comments/**/index.jsonl",
|
|
34
|
-
|_path
|
|
34
|
+
|_path| {
|
|
35
35
|
let mut row: HashMap<String, Value> = HashMap::new();
|
|
36
36
|
row.insert("id".into(), Value::Text("...".into()));
|
|
37
37
|
row.insert("body".into(), Value::Text("...".into()));
|
|
@@ -47,7 +47,7 @@ import type { TableDef } from 'dirsql';
|
|
|
47
47
|
const table: TableDef = {
|
|
48
48
|
ddl: 'CREATE TABLE comments (id TEXT, body TEXT, author TEXT)',
|
|
49
49
|
glob: 'comments/**/index.jsonl',
|
|
50
|
-
extract: (_path
|
|
50
|
+
extract: (_path) => [
|
|
51
51
|
{ id: '...', body: '...', author: '...' },
|
|
52
52
|
],
|
|
53
53
|
};
|
|
@@ -89,38 +89,38 @@ Glob syntax follows standard Unix globbing rules. `**` matches any number of dir
|
|
|
89
89
|
|
|
90
90
|
### `extract`
|
|
91
91
|
|
|
92
|
-
A callable `(path: str
|
|
92
|
+
A callable `(path: str) -> list[dict]` that converts a file into rows.
|
|
93
93
|
|
|
94
|
-
- `path` is the
|
|
95
|
-
- `content` is the file content as a string
|
|
94
|
+
- `path` is the **absolute filesystem path** of the matched file
|
|
96
95
|
- Return a list of dicts, where each dict maps column names to values
|
|
97
96
|
- Return an empty list to skip a file
|
|
98
97
|
|
|
98
|
+
`dirsql` does not read file contents for you. If your extract needs the file
|
|
99
|
+
body, read it inside the callback using `path`. Callbacks that derive columns
|
|
100
|
+
only from the path (or that rely solely on the auto-injected filesystem-fact
|
|
101
|
+
columns) never touch the file at all.
|
|
102
|
+
|
|
99
103
|
```python
|
|
100
104
|
import json
|
|
101
105
|
|
|
102
106
|
# Single-object JSON files: one row per file
|
|
103
|
-
extract
|
|
107
|
+
def extract(path):
|
|
108
|
+
with open(path, encoding="utf-8") as f:
|
|
109
|
+
return [json.loads(f.read())]
|
|
104
110
|
|
|
105
111
|
# JSONL files: one row per line
|
|
106
|
-
extract
|
|
107
|
-
|
|
108
|
-
]
|
|
112
|
+
def extract(path):
|
|
113
|
+
with open(path, encoding="utf-8") as f:
|
|
114
|
+
return [json.loads(line) for line in f]
|
|
109
115
|
|
|
110
|
-
# Derive
|
|
116
|
+
# Derive a value from the file path alone -- no file read
|
|
111
117
|
import os
|
|
112
|
-
extract=lambda path
|
|
113
|
-
{
|
|
114
|
-
"id": os.path.basename(os.path.dirname(path)),
|
|
115
|
-
"body": json.loads(line)["body"],
|
|
116
|
-
}
|
|
117
|
-
for line in content.splitlines()
|
|
118
|
-
for _ in [json.loads(line)]
|
|
119
|
-
]
|
|
118
|
+
extract = lambda path: [{"id": os.path.basename(os.path.dirname(path))}]
|
|
120
119
|
|
|
121
120
|
# Conditionally skip files
|
|
122
|
-
def extract(path
|
|
123
|
-
|
|
121
|
+
def extract(path):
|
|
122
|
+
with open(path, encoding="utf-8") as f:
|
|
123
|
+
data = json.loads(f.read())
|
|
124
124
|
if data.get("draft"):
|
|
125
125
|
return []
|
|
126
126
|
return [data]
|
|
@@ -142,12 +142,12 @@ db = DirSQL(
|
|
|
142
142
|
Table(
|
|
143
143
|
ddl="CREATE TABLE posts (title TEXT, author_id TEXT)",
|
|
144
144
|
glob="posts/*.json",
|
|
145
|
-
extract=lambda path
|
|
145
|
+
extract=lambda path: [json.loads(open(path, encoding="utf-8").read())],
|
|
146
146
|
),
|
|
147
147
|
Table(
|
|
148
148
|
ddl="CREATE TABLE authors (id TEXT, name TEXT)",
|
|
149
149
|
glob="authors/*.json",
|
|
150
|
-
extract=lambda path
|
|
150
|
+
extract=lambda path: [json.loads(open(path, encoding="utf-8").read())],
|
|
151
151
|
),
|
|
152
152
|
],
|
|
153
153
|
)
|
|
@@ -184,12 +184,12 @@ let db = DirSQL::new(
|
|
|
184
184
|
Table::new(
|
|
185
185
|
"CREATE TABLE posts (title TEXT, author_id TEXT)",
|
|
186
186
|
"posts/*.json",
|
|
187
|
-
|
|
|
187
|
+
|path| vec![row_from_json(&std::fs::read_to_string(path).unwrap())],
|
|
188
188
|
),
|
|
189
189
|
Table::new(
|
|
190
190
|
"CREATE TABLE authors (id TEXT, name TEXT)",
|
|
191
191
|
"authors/*.json",
|
|
192
|
-
|
|
|
192
|
+
|path| vec![row_from_json(&std::fs::read_to_string(path).unwrap())],
|
|
193
193
|
),
|
|
194
194
|
],
|
|
195
195
|
)?;
|
|
@@ -197,17 +197,18 @@ let db = DirSQL::new(
|
|
|
197
197
|
|
|
198
198
|
```typescript [TypeScript]
|
|
199
199
|
import { DirSQL, type TableDef } from 'dirsql';
|
|
200
|
+
import { readFileSync } from 'node:fs';
|
|
200
201
|
|
|
201
202
|
const tables: TableDef[] = [
|
|
202
203
|
{
|
|
203
204
|
ddl: 'CREATE TABLE posts (title TEXT, author_id TEXT)',
|
|
204
205
|
glob: 'posts/*.json',
|
|
205
|
-
extract: (
|
|
206
|
+
extract: (path) => [JSON.parse(readFileSync(path, 'utf8'))],
|
|
206
207
|
},
|
|
207
208
|
{
|
|
208
209
|
ddl: 'CREATE TABLE authors (id TEXT, name TEXT)',
|
|
209
210
|
glob: 'authors/*.json',
|
|
210
|
-
extract: (
|
|
211
|
+
extract: (path) => [JSON.parse(readFileSync(path, 'utf8'))],
|
|
211
212
|
},
|
|
212
213
|
];
|
|
213
214
|
|
package/docs/guide/watching.md
CHANGED
|
@@ -26,7 +26,7 @@ db = DirSQL(
|
|
|
26
26
|
Table(
|
|
27
27
|
ddl="CREATE TABLE comments (id TEXT, body TEXT, author TEXT)",
|
|
28
28
|
glob="comments/**/*.json",
|
|
29
|
-
extract=lambda path
|
|
29
|
+
extract=lambda path: [json.loads(open(path, encoding="utf-8").read())],
|
|
30
30
|
),
|
|
31
31
|
],
|
|
32
32
|
)
|
|
@@ -67,7 +67,7 @@ let db = DirSQL::new(
|
|
|
67
67
|
Table::new(
|
|
68
68
|
"CREATE TABLE comments (id TEXT, body TEXT, author TEXT)",
|
|
69
69
|
"comments/**/*.json",
|
|
70
|
-
|
|
|
70
|
+
|path| vec![row_from_json(&std::fs::read_to_string(path).unwrap())],
|
|
71
71
|
),
|
|
72
72
|
],
|
|
73
73
|
)?;
|
|
@@ -92,13 +92,14 @@ while let Some(event) = stream.next().await {
|
|
|
92
92
|
```
|
|
93
93
|
|
|
94
94
|
```typescript [TypeScript]
|
|
95
|
+
import { readFileSync } from 'node:fs';
|
|
95
96
|
import { DirSQL, type TableDef } from 'dirsql';
|
|
96
97
|
|
|
97
98
|
const tables: TableDef[] = [
|
|
98
99
|
{
|
|
99
100
|
ddl: 'CREATE TABLE comments (id TEXT, body TEXT, author TEXT)',
|
|
100
101
|
glob: 'comments/**/*.json',
|
|
101
|
-
extract: (
|
|
102
|
+
extract: (path) => [JSON.parse(readFileSync(path, 'utf8'))],
|
|
102
103
|
},
|
|
103
104
|
];
|
|
104
105
|
|
package/docs/index.md
CHANGED
|
@@ -35,7 +35,7 @@ db = DirSQL(
|
|
|
35
35
|
Table(
|
|
36
36
|
ddl="CREATE TABLE files (name TEXT, size INTEGER, type TEXT)",
|
|
37
37
|
glob="data/*.json",
|
|
38
|
-
extract=lambda path
|
|
38
|
+
extract=lambda path: [json.loads(open(path, encoding="utf-8").read())],
|
|
39
39
|
),
|
|
40
40
|
],
|
|
41
41
|
)
|
|
@@ -53,7 +53,7 @@ let db = DirSQL::new(
|
|
|
53
53
|
Table::new(
|
|
54
54
|
"CREATE TABLE files (name TEXT, size INTEGER, type TEXT)",
|
|
55
55
|
"data/*.json",
|
|
56
|
-
|
|
|
56
|
+
|path| vec![serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap()],
|
|
57
57
|
),
|
|
58
58
|
],
|
|
59
59
|
)?;
|
|
@@ -62,6 +62,7 @@ let large = db.query("SELECT * FROM files WHERE size > 1000")?;
|
|
|
62
62
|
```
|
|
63
63
|
|
|
64
64
|
```typescript [TypeScript]
|
|
65
|
+
import { readFileSync } from 'node:fs';
|
|
65
66
|
import { DirSQL, Table } from 'dirsql';
|
|
66
67
|
|
|
67
68
|
const db = new DirSQL({
|
|
@@ -70,7 +71,7 @@ const db = new DirSQL({
|
|
|
70
71
|
new Table({
|
|
71
72
|
ddl: 'CREATE TABLE files (name TEXT, size INTEGER, type TEXT)',
|
|
72
73
|
glob: 'data/*.json',
|
|
73
|
-
extract: (
|
|
74
|
+
extract: (path) => [JSON.parse(readFileSync(path, 'utf8'))],
|
|
74
75
|
}),
|
|
75
76
|
],
|
|
76
77
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dirsql",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.7",
|
|
4
4
|
"description": "Ephemeral SQL index over a local directory",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "https://github.com/thekevinscott/dirsql",
|
|
@@ -191,15 +191,15 @@
|
|
|
191
191
|
]
|
|
192
192
|
},
|
|
193
193
|
"optionalDependencies": {
|
|
194
|
-
"@dirsql/lib-linux-x64-gnu": "0.3.
|
|
195
|
-
"@dirsql/lib-linux-arm64-gnu": "0.3.
|
|
196
|
-
"@dirsql/lib-darwin-x64": "0.3.
|
|
197
|
-
"@dirsql/lib-darwin-arm64": "0.3.
|
|
198
|
-
"@dirsql/lib-win32-x64-msvc": "0.3.
|
|
199
|
-
"@dirsql/cli-linux-x64-gnu": "0.3.
|
|
200
|
-
"@dirsql/cli-linux-arm64-gnu": "0.3.
|
|
201
|
-
"@dirsql/cli-darwin-x64": "0.3.
|
|
202
|
-
"@dirsql/cli-darwin-arm64": "0.3.
|
|
203
|
-
"@dirsql/cli-win32-x64-msvc": "0.3.
|
|
194
|
+
"@dirsql/lib-linux-x64-gnu": "0.3.7",
|
|
195
|
+
"@dirsql/lib-linux-arm64-gnu": "0.3.7",
|
|
196
|
+
"@dirsql/lib-darwin-x64": "0.3.7",
|
|
197
|
+
"@dirsql/lib-darwin-arm64": "0.3.7",
|
|
198
|
+
"@dirsql/lib-win32-x64-msvc": "0.3.7",
|
|
199
|
+
"@dirsql/cli-linux-x64-gnu": "0.3.7",
|
|
200
|
+
"@dirsql/cli-linux-arm64-gnu": "0.3.7",
|
|
201
|
+
"@dirsql/cli-darwin-x64": "0.3.7",
|
|
202
|
+
"@dirsql/cli-darwin-arm64": "0.3.7",
|
|
203
|
+
"@dirsql/cli-win32-x64-msvc": "0.3.7"
|
|
204
204
|
}
|
|
205
205
|
}
|