Gathering detailed insights and metrics for better-sqlite3-schema
Gathering detailed insights and metrics for better-sqlite3-schema
Gathering detailed insights and metrics for better-sqlite3-schema
Gathering detailed insights and metrics for better-sqlite3-schema
helper utilities on top of better-sqlite3-helper
npm install better-sqlite3-schema
Typescript
Module System
Node Version
NPM Version
69.5
Supply Chain
97.1
Quality
74.9
Maintenance
100
Vulnerability
99.3
License
TypeScript (99.41%)
Shell (0.59%)
Total Downloads
10,911
Last Day
1
Last Week
55
Last Month
193
Last Year
2,328
BSD-2-Clause License
5 Stars
117 Commits
1 Forks
2 Watchers
1 Branches
1 Contributors
Updated on Feb 19, 2025
Latest Version
3.1.8
Package Id
better-sqlite3-schema@3.1.8
Unpacked Size
62.76 kB
Size
15.29 kB
File Count
29
NPM Version
10.9.2
Node Version
22.13.1
Published on
Feb 19, 2025
Cumulative downloads
Total Downloads
Last Day
-91.7%
1
Compared to previous day
Last Week
19.6%
55
Compared to previous week
Last Month
14.2%
193
Compared to previous month
Last Year
-16%
2,328
Compared to previous year
3
3
Migrate (nested and multi-dimensional) json data to/from sqlite database with better-sqlite3-helper
Sample json data type:
1interface Thread { 2 tid: number 3 subject: string 4 uid: string 5 author: string 6 posts: Post[] 7 tags: string[] 8} 9 10interface Post { 11 pid: number 12 uid: string 13 author: string 14 content: string 15 imgs: string[] 16}
Sample table schema:
1import { TableSchema } from 'better-sqlite3-schema' 2 3const threadSchema: TableSchema = { 4 table: 'thread', 5 fields: { 6 tid: 'integer', 7 subject: 'text', 8 uid: 'integer', 9 }, 10 refFields: ['type'], 11} 12 13const threadTagSchema: TableSchema = { 14 table: 'thread_tag', 15 fields: { 16 tid: 'integer', 17 }, 18 refFields: ['tag'], 19} 20 21const postSchema: TableSchema = { 22 table: 'post', 23 fields: { 24 pid: 'integer', 25 tid: 'integer', 26 uid: 'integer', 27 content: 'text', 28 }, 29} 30 31const postImgSchema: TableSchema = { 32 table: 'post_img', 33 fields: { 34 pid: 'integer', 35 }, 36 refFields: ['img'], 37}
The functional approach allows one to compose customizable helper functions at runtime.
Explore the dataset and auto built schema with
makeSchemaScanner()
Compose insert functions with
makeInsertRowFnFromSchema()
makeDeduplicatedInsertRowFnFromSchema()
Compose select functions with
makeSelectRowFnFromSchema()
makeSelectRefFieldArray()
makeGetRefValueFnFromSchema()
Detail example see makePredefinedInsertRowFn()
and makeGeneralInsertRowFn()
in functional-test.ts
The code generation approach allows one to compose customizable helper functions at build-time. Which can archive ~50% speed up compared to the runtime composing.
8GiB of HTTP proxy server log. Each line is a compact json text.
Sample text:
1{ 2 "timestamp": 1600713130016, 3 "type": "request", 4 "userAgent": "Mozilla/5.0 (Linux; Android 10; LIO-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Mobile Safari/537.36", 5 "referer": "https://www.example.net/sw.js", 6 "protocol": "https", 7 "host": "www.example.net", 8 "method": "GET", 9 "url": "/build/p-7794655c.js" 10}
When stored into sqlite3, the data are normalized into multiple tables to avoid duplication, e.g. only storing the full text of each type of user agent and url once.
File size in varies format:
storage | size | size compared with plain text | Remark |
---|---|---|---|
plain text | 8256M | - | |
sqlite without index | 920M | 11.1% | |
zip of non-indexed sqlite file | 220M | 2.7% | 23.9% of sqlite3 file |
sqlite with indices | 1147M | 13.9% | +24% of sqlite file |
zip of indexed sqlite file | 268M | 3.2% | 23.4% of indexed sqlite3 file |
Time used to import:
Optimization used:
PRAGMA synchronous = OFF
PRAGMA journal_mode = MEMORY
PRAGMA cache_size = ${(200 * 1000 ** 2) / 4}
(default page size is 4K, we largely increase the cache_size to avoid massive tedious disk write)Remark:
Using index increases the file size by 1/4, but hugely speeds up the import process.
To archive the best of both aspects, create indices during import; and remove indices (then VACUUM) for archive file.
It takes 4.9s to build the indices; and 16.3s to vacuum the database after removal of indices.
291119 sample json data crawled from online forum (threads and posts)
Total size: 843M
The objects have consistent shape.
Some data are duplicated, e.g. user name, and some common comments.
Same as the dataset used in binary-object
File size in varies format:
storage | size |
---|---|
json text | 843M |
sqlite3 with index | 669M |
sqlite3 without index | 628M |
zip of sqlite3 without index | 171M |
Remark: The data in sqlite3 are normalized to avoid duplication
No vulnerabilities found.