Skip to content

Commit

Permalink
demo-env, seed db(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
tantaman committed Jul 25, 2022
1 parent 4f159d0 commit 03be605
Show file tree
Hide file tree
Showing 17 changed files with 239 additions and 402 deletions.
4 changes: 4 additions & 0 deletions prototype/demo-env/README.md
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 added prototype/demo-env/dbs/todo-base.db
Binary file not shown.
29 changes: 29 additions & 0 deletions prototype/demo-env/package.json
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 added prototype/demo-env/src/empty.ts
Empty file.
9 changes: 9 additions & 0 deletions prototype/demo-env/tsconfig.json
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" }]
}
3 changes: 3 additions & 0 deletions prototype/demo-lib/README.md
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.
36 changes: 36 additions & 0 deletions prototype/demo-lib/package.json
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"
]
}
}
98 changes: 98 additions & 0 deletions prototype/demo-lib/src/index.ts
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();
}
1 change: 1 addition & 0 deletions prototype/demo-lib/src/script.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { setupProtoDB } from "./index.js";
9 changes: 9 additions & 0 deletions prototype/demo-lib/tsconfig.json
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" }]
}
2 changes: 1 addition & 1 deletion prototype/migrator/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@aphro/crsqlite-migrator",
"name": "@aphro/cfsqlite-migrator",
"version": "0.0.1",
"main": "bin/cli.js",
"description": "",
Expand Down
Loading

0 comments on commit 03be605

Please sign in to comment.