forked from vlcn-io/cr-sqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
239 additions
and
402 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Demo Env | ||
|
||
Installs node modules required for demoing. Intent is you can open a repl in this dir and have access to all | ||
the modules you need to run `cfsqlite`. |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "@aphro/cfsqlite-demoenv", | ||
"version": "0.0.1", | ||
"description": "", | ||
"main": "lib/index.js", | ||
"type": "module", | ||
"scripts": { | ||
"build": "tsc --build", | ||
"test": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js", | ||
"watch": "tsc --build --watch", | ||
"deep-clean": "rm -rf ./bin || true && rm tsconfig.tsbuildinfo || true" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@aphro/cfsqlite-demolib": "workspace:*", | ||
"nanoid": "^4.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^17.0.45", | ||
"typescript": "^4.7.4" | ||
}, | ||
"jest": { | ||
"testMatch": [ | ||
"**/*.test.js" | ||
] | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "../tsconfig-lib.json", | ||
"compilerOptions": { | ||
"outDir": "./lib/", | ||
"rootDir": "./src" | ||
}, | ||
"include": ["./src/"], | ||
"references": [{ "path": "../demo-lib" }] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Demo Lib | ||
|
||
Library of functions to aid in doing demos/presentations of cfsqlite. |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"name": "@aphro/cfsqlite-demolib", | ||
"version": "0.0.1", | ||
"description": "", | ||
"main": "lib/index.js", | ||
"type": "module", | ||
"scripts": { | ||
"build": "tsc --build", | ||
"test": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js", | ||
"watch": "tsc --build --watch", | ||
"deep-clean": "rm -rf ./bin || true && rm tsconfig.tsbuildinfo || true" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@aphro/cfsqlite-replicator": "workspace:*", | ||
"better-sqlite3": "^7.6.2" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.18.9", | ||
"@babel/preset-env": "^7.18.9", | ||
"@types/better-sqlite3": "^7.5.0", | ||
"@types/jest": "^28.1.6", | ||
"@types/node": "^17.0.45", | ||
"fast-check": "^2.25.0", | ||
"jest": "^28.1.3", | ||
"nanoid": "^4.0.0", | ||
"typescript": "^4.7.4" | ||
}, | ||
"jest": { | ||
"testMatch": [ | ||
"**/*.test.js" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// @ts-ignore -- @types/better-sqlite3 is incorect on export | ||
import Database from "better-sqlite3"; | ||
import { clock, Clock, queries } from "@aphro/cfsqlite-replicator"; | ||
import { Database as DB } from "better-sqlite3"; | ||
import * as fs from "fs"; | ||
|
||
export function open(file?: string): DB { | ||
const db = new Database(file ?? ":memory:") as DB; | ||
db.defaultSafeIntegers(); | ||
return db; | ||
} | ||
|
||
export function q(db: DB, query: string, args?: any[]): any[] { | ||
args = args || []; | ||
return db.prepare(query).all(...args); | ||
} | ||
|
||
export function q0(db: DB, query: string, args?: any[]) { | ||
args = args || []; | ||
db.prepare(query).run(...args); | ||
} | ||
|
||
export function tables(db: DB) { | ||
return db.pragma("table_list"); | ||
} | ||
|
||
export function tableInfo(db: DB, table: string) { | ||
return db.pragma(`table_info(${table})`); | ||
} | ||
|
||
/** | ||
* Marge the changes from the left to the right for the given table. | ||
* @param table | ||
* @param left | ||
* @param right | ||
*/ | ||
export function sync(table: string, left: DB, right: DB) { | ||
const rightClock = clock.collapse(q(right, ...queries.currentClock(table))); | ||
const deltas = q(left, ...queries.deltas(table, "id", rightClock)); | ||
|
||
q0(right, ...queries.patch(table, deltas)); | ||
} | ||
|
||
/** | ||
* Figure out what deltas make left ahead of right for the given table. | ||
* @param table | ||
* @param left | ||
* @param right | ||
*/ | ||
export function getDeltas(table: string, left: DB, right: DB): any[] { | ||
const rightClock = clock.collapse(q(right, ...queries.currentClock(table))); | ||
return q(left, ...queries.deltas(table, "id", rightClock)); | ||
} | ||
|
||
/** | ||
* Compute the clock for `table` on db `db` | ||
* @param table | ||
* @param db | ||
* @returns | ||
*/ | ||
export function getClock(table: string, db: DB): Clock { | ||
return clock.collapse(q(db, ...queries.currentClock(table))); | ||
} | ||
|
||
/** | ||
* Sets up the first ever cf db in-memory | ||
* @param file | ||
* @returns | ||
*/ | ||
export function setupProtoDB(file?: string): DB { | ||
// { verbose: console.log } | ||
const db = new Database(file ?? ":memory:") as DB; | ||
|
||
db.defaultSafeIntegers(); | ||
|
||
runfile(db, "crr_db_version.sqlite.sql"); | ||
runfile(db, "crr_site_id.sqlite.sql"); | ||
runfile(db, "prime_version.sqlite.sql"); | ||
runfile(db, "prime_site_id.sqlite.sql"); | ||
runfile(db, "todo_crr.sqlite.sql"); | ||
runfile(db, "todo_view.sqlite.sql"); | ||
runfile(db, "insert_todo_trig.sqlite.sql"); | ||
runfile(db, "update_todo_trig.sqlite.sql"); | ||
runfile(db, "delete_todo_trig.sqlite.sql"); | ||
runfile(db, "todo_patch.sqlite.sql"); | ||
runfile(db, "insert_todo_patch.sqlite.sql"); | ||
runfile(db, "todo_crr_clocks.sqlite.sql"); | ||
|
||
return db; | ||
} | ||
|
||
function runfile(db: DB, file: string) { | ||
// console.log('Running: ' + file); | ||
const contents = fs.readFileSync("../test-schemas/" + file, { | ||
encoding: "utf8", | ||
}); | ||
db.prepare(contents).run(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import { setupProtoDB } from "./index.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "../tsconfig-lib.json", | ||
"compilerOptions": { | ||
"outDir": "./lib/", | ||
"rootDir": "./src" | ||
}, | ||
"include": ["./src/"], | ||
"references": [{ "path": "../replicator" }] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.