instant-local-throwaway
Advanced tools
Comparing version 1.0.4 to 1.0.5
import { createDB, save } from "../src/db"; | ||
import { toDatalogTransactions, transform } from "../src/instaml"; | ||
import { transformRemoves, transformSaves } from "../src/instaml"; | ||
import { query } from "../src/instaql"; | ||
@@ -8,4 +8,8 @@ | ||
expect( | ||
toDatalogTransactions( | ||
["update", "users", "1", { name: "Stepan", twitter: "stopachka" }], | ||
transformSaves( | ||
[ | ||
{ | ||
users: { id: "1", update: { name: "Stepan", twitter: "stopachka" } }, | ||
}, | ||
], | ||
db | ||
@@ -28,3 +32,3 @@ ) | ||
expect( | ||
toDatalogTransactions(["unlink", "users", "1", "bookshelves", "2"], db) | ||
transformSaves([{ users: { id: "1", unlink: { bookshelves: "2" } } }], db) | ||
).toEqual([["retract-triple", "1", "ref$users$bookshelves", "2"]]); | ||
@@ -35,9 +39,9 @@ }); | ||
const db = createDB([ | ||
["1", "user/name", "Stopa"], | ||
["2", "bookshelf/title", "Favs"], | ||
["1", "ref$user$bookshelf", "2"], | ||
["1", "users/name", "Stopa"], | ||
["2", "bookshelves/title", "Favs"], | ||
["1", "ref$users$bookshelves", "2"], | ||
]); | ||
expect(toDatalogTransactions(["delete", "bookshelf", "2"], db)).toEqual([ | ||
["retract-triple", "1", "ref$user$bookshelf", "2"], | ||
["retract-triple", "2", "bookshelf/title", "Favs"], | ||
expect(transformRemoves([{ bookshelves: "2" }], db)).toEqual([ | ||
["retract-triple", "1", "ref$users$bookshelves", "2"], | ||
["retract-triple", "2", "bookshelves/title", "Favs"], | ||
]); | ||
@@ -49,7 +53,12 @@ }); | ||
save( | ||
transform( | ||
transformSaves( | ||
[ | ||
["update", "user", "a", { name: "Stopa" }], | ||
["update", "bookshelf", "b", { title: "Favs" }], | ||
["link", "user", "a", "bookshelf", "b"], | ||
{ bookshelf: { id: "b", update: { title: "Favs" } } }, | ||
{ | ||
user: { | ||
id: "a", | ||
update: { name: "Stopa" }, | ||
link: { bookshelf: "b" }, | ||
}, | ||
}, | ||
], | ||
@@ -56,0 +65,0 @@ db |
import zenecaTriples from "../data/zenecaTriples.json"; | ||
import { createDB, save } from "../src/db"; | ||
import { buildWhereQuery, query } from "../src/instaql"; | ||
import { buildWhereQuery, enhanceWithDefaults, query } from "../src/instaql"; | ||
@@ -295,1 +295,25 @@ const db = createDB(zenecaTriples); | ||
}); | ||
test("enhanceWithDefaults", () => { | ||
expect( | ||
enhanceWithDefaults( | ||
{ | ||
users: { posts: { cardinality: "one" } }, | ||
posts: { comments: { where: { user: "foo" } } }, | ||
}, | ||
{ | ||
users: { | ||
posts: { $: { where: { owner: "foo" } }, comments: {}, editors: {} }, | ||
}, | ||
} | ||
) | ||
).toEqual({ | ||
users: { | ||
posts: { | ||
$: { where: { owner: "foo" }, cardinality: "one" }, | ||
comments: { $: { where: { user: "foo" } } }, | ||
editors: {}, | ||
}, | ||
}, | ||
}); | ||
}); |
@@ -1,6 +0,14 @@ | ||
export declare function useInit({ token }: { | ||
declare type Config = { | ||
token: string; | ||
}): boolean; | ||
defaults?: { | ||
[connectionA: string]: { | ||
[connectionB: string]: any; | ||
}; | ||
}; | ||
}; | ||
export declare function useInit(config: Config): boolean; | ||
export declare function useQuery(_q: Object): any; | ||
export declare function save(mutations: any[][]): void; | ||
export declare function save(...saveTransactions: any[]): void; | ||
export declare function remove(...removeTransactions: any[]): void; | ||
export {}; | ||
//# sourceMappingURL=instant.d.ts.map |
@@ -29,3 +29,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.save = exports.useQuery = exports.useInit = void 0; | ||
exports.remove = exports.save = exports.useQuery = exports.useInit = void 0; | ||
const react_1 = require("react"); | ||
@@ -38,3 +38,3 @@ const tripleDB = __importStar(require("./db")); | ||
const db = { | ||
_token: null, | ||
_config: null, | ||
_current: tripleDB.createDB([]), | ||
@@ -55,6 +55,6 @@ _subs: {}, | ||
}; | ||
function useInit({ token }) { | ||
function useInit(config) { | ||
const [isLoading, setIsLoading] = (0, react_1.useState)(true); | ||
(0, react_1.useEffect)(() => { | ||
db._token = token; | ||
db._config = config; | ||
const ws = new WebSocket("wss://instant-server.herokuapp.com", null); | ||
@@ -66,3 +66,3 @@ ws.onmessage = function (event) { | ||
ws.onopen = function () { | ||
ws.send(JSON.stringify({ token: token })); | ||
ws.send(JSON.stringify({ token: config.token })); | ||
}; | ||
@@ -92,3 +92,4 @@ return () => { | ||
function useQuery(_q) { | ||
const q = useStableValue(_q); | ||
var _a; | ||
const q = useStableValue(instaql.enhanceWithDefaults((_a = db._config) === null || _a === void 0 ? void 0 : _a.defaults, _q)); | ||
const [state, setState] = (0, react_1.useState)(instaql.query(q, db._current)); | ||
@@ -107,5 +108,5 @@ (0, react_1.useEffect)(() => { | ||
exports.useQuery = useQuery; | ||
function save(mutations) { | ||
const newDb = Object.assign({}, db._current); | ||
const ts = instaml.transform(mutations, newDb); | ||
function transact(ts, db) { | ||
var _a; | ||
const newDb = Object.assign({}, db); | ||
tripleDB.save(ts, newDb); | ||
@@ -116,3 +117,3 @@ fetch("https://instant-server.herokuapp.com/api/transact", { | ||
"content-type": "application/json", | ||
authorization: `Bearer ${db._token}`, | ||
authorization: `Bearer ${(_a = db._config) === null || _a === void 0 ? void 0 : _a.token}`, | ||
}, | ||
@@ -125,3 +126,12 @@ body: JSON.stringify({ transactions: ts }), | ||
} | ||
function save(...saveTransactions) { | ||
const currDB = db._current; | ||
transact(instaml.transformSaves(saveTransactions, currDB), currDB); | ||
} | ||
exports.save = save; | ||
function remove(...removeTransactions) { | ||
const currDB = db._current; | ||
transact(instaml.transformRemoves(removeTransactions, currDB), currDB); | ||
} | ||
exports.remove = remove; | ||
//# sourceMappingURL=instant.js.map |
@@ -1,6 +0,14 @@ | ||
export declare function useInit({ token }: { | ||
declare type Config = { | ||
token: string; | ||
}): boolean; | ||
defaults?: { | ||
[connectionA: string]: { | ||
[connectionB: string]: any; | ||
}; | ||
}; | ||
}; | ||
export declare function useInit(config: Config): boolean; | ||
export declare function useQuery(_q: Object): any; | ||
export declare function save(mutations: any[][]): void; | ||
export declare function save(...saveTransactions: any[]): void; | ||
export declare function remove(...removeTransactions: any[]): void; | ||
export {}; | ||
//# sourceMappingURL=instant.d.ts.map |
@@ -8,3 +8,3 @@ import { useState, useEffect } from "react"; | ||
const db = { | ||
_token: null, | ||
_config: null, | ||
_current: tripleDB.createDB([]), | ||
@@ -25,6 +25,6 @@ _subs: {}, | ||
}; | ||
export function useInit({ token }) { | ||
export function useInit(config) { | ||
const [isLoading, setIsLoading] = useState(true); | ||
useEffect(() => { | ||
db._token = token; | ||
db._config = config; | ||
const ws = new WebSocket("wss://instant-server.herokuapp.com", null); | ||
@@ -36,3 +36,3 @@ ws.onmessage = function (event) { | ||
ws.onopen = function () { | ||
ws.send(JSON.stringify({ token: token })); | ||
ws.send(JSON.stringify({ token: config.token })); | ||
}; | ||
@@ -61,3 +61,4 @@ return () => { | ||
export function useQuery(_q) { | ||
const q = useStableValue(_q); | ||
var _a; | ||
const q = useStableValue(instaql.enhanceWithDefaults((_a = db._config) === null || _a === void 0 ? void 0 : _a.defaults, _q)); | ||
const [state, setState] = useState(instaql.query(q, db._current)); | ||
@@ -75,5 +76,5 @@ useEffect(() => { | ||
} | ||
export function save(mutations) { | ||
const newDb = Object.assign({}, db._current); | ||
const ts = instaml.transform(mutations, newDb); | ||
function transact(ts, db) { | ||
var _a; | ||
const newDb = Object.assign({}, db); | ||
tripleDB.save(ts, newDb); | ||
@@ -84,3 +85,3 @@ fetch("https://instant-server.herokuapp.com/api/transact", { | ||
"content-type": "application/json", | ||
authorization: `Bearer ${db._token}`, | ||
authorization: `Bearer ${(_a = db._config) === null || _a === void 0 ? void 0 : _a.token}`, | ||
}, | ||
@@ -93,2 +94,10 @@ body: JSON.stringify({ transactions: ts }), | ||
} | ||
export function save(...saveTransactions) { | ||
const currDB = db._current; | ||
transact(instaml.transformSaves(saveTransactions, currDB), currDB); | ||
} | ||
export function remove(...removeTransactions) { | ||
const currDB = db._current; | ||
transact(instaml.transformRemoves(removeTransactions, currDB), currDB); | ||
} | ||
//# sourceMappingURL=instant.js.map |
{ | ||
"name": "instant-local-throwaway", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "This is an experiment for instant", | ||
@@ -28,3 +28,2 @@ "main": "dist/index.js", | ||
"lodash": "^4.17.21", | ||
"rollup": "^2.72.1", | ||
"typescript": "^4.6.4" | ||
@@ -31,0 +30,0 @@ }, |
@@ -62,3 +62,3 @@ import * as datalog from "./datalog"; | ||
export function toDatalogTransactions([action, ...args], db) { | ||
function toDatalogTransactions([action, ...args], db) { | ||
switch (action) { | ||
@@ -78,4 +78,44 @@ case "update": | ||
export function transform(transactions, db) { | ||
return transactions.flatMap((t) => toDatalogTransactions(t, db)); | ||
function saveToV1Transactions(v2) { | ||
return Object.entries(v2).reduce((result, [table, params]) => { | ||
const { id, update, link, unlink } = params; | ||
if (!id) { | ||
throw new Error(`Missing \`id\` field for ${table}`); | ||
} | ||
return result.concat( | ||
[ | ||
update && [["update", table, id, update]], | ||
unlink && | ||
Object.keys(unlink).map((relation) => { | ||
const relationId = unlink[relation]; | ||
return ["unlink", table, id, relation, relationId]; | ||
}), | ||
link && | ||
Object.keys(link).map((relation) => { | ||
const relationId = link[relation]; | ||
return ["link", table, id, relation, relationId]; | ||
}), | ||
] | ||
.filter((x) => x) | ||
.flatMap((x) => x) | ||
); | ||
}, []); | ||
} | ||
export function transformSaves(saveTransactions, db) { | ||
return saveTransactions.flatMap((v2) => | ||
saveToV1Transactions(v2).flatMap((v2) => toDatalogTransactions(v2, db)) | ||
); | ||
} | ||
function deleteToV1Transactions(v2) { | ||
return Object.entries(v2).map(([table, id]) => { | ||
return ["delete", table, id]; | ||
}, []); | ||
} | ||
export function transformRemoves(removeTransactions, db) { | ||
return removeTransactions.flatMap((v2) => | ||
deleteToV1Transactions(v2).flatMap((v2) => toDatalogTransactions(v2, db)) | ||
); | ||
} |
@@ -9,5 +9,20 @@ import { useState, useEffect } from "react"; | ||
type Config = { | ||
token: string; | ||
defaults?: { [connectionA: string]: { [connectionB: string]: any } }; | ||
}; | ||
type DB = { | ||
_config?: Config; | ||
_current: Object; | ||
_subs: { [key: string]: Function }; | ||
subscribe: (f: Function) => number; | ||
unsubscribe: (id: number) => void; | ||
change: (newV: Object) => void; | ||
}; | ||
let _id = 0; | ||
const db = { | ||
_token: null, | ||
const db: DB = { | ||
_config: null, | ||
_current: tripleDB.createDB([]), | ||
@@ -29,6 +44,6 @@ _subs: {}, | ||
export function useInit({ token }: { token: string }) { | ||
export function useInit(config: Config) { | ||
const [isLoading, setIsLoading] = useState(true); | ||
useEffect(() => { | ||
db._token = token; | ||
db._config = config; | ||
const ws = new WebSocket("wss://instant-server.herokuapp.com", null); | ||
@@ -40,3 +55,3 @@ ws.onmessage = function (event) { | ||
ws.onopen = function () { | ||
ws.send(JSON.stringify({ token: token })); | ||
ws.send(JSON.stringify({ token: config.token })); | ||
}; | ||
@@ -67,3 +82,5 @@ return () => { | ||
export function useQuery(_q: Object) { | ||
const q = useStableValue(_q); | ||
const q = useStableValue( | ||
instaql.enhanceWithDefaults(db._config?.defaults, _q) | ||
); | ||
const [state, setState] = useState(instaql.query(q, db._current)); | ||
@@ -82,5 +99,4 @@ useEffect(() => { | ||
export function save(mutations: any[][]) { | ||
const newDb = { ...db._current }; | ||
const ts = instaml.transform(mutations, newDb); | ||
function transact(ts: any[], db: any) { | ||
const newDb = { ...db }; | ||
tripleDB.save(ts, newDb); | ||
@@ -91,3 +107,3 @@ fetch("https://instant-server.herokuapp.com/api/transact", { | ||
"content-type": "application/json", | ||
authorization: `Bearer ${db._token}`, | ||
authorization: `Bearer ${db._config?.token}`, | ||
}, | ||
@@ -100,1 +116,11 @@ body: JSON.stringify({ transactions: ts }), | ||
} | ||
export function save(...saveTransactions: any[]) { | ||
const currDB = db._current; | ||
transact(instaml.transformSaves(saveTransactions, currDB), currDB); | ||
} | ||
export function remove(...removeTransactions: any[]) { | ||
const currDB = db._current; | ||
transact(instaml.transformRemoves(removeTransactions, currDB), currDB); | ||
} |
@@ -116,2 +116,13 @@ import * as datalog from "./datalog"; | ||
export function enhanceWithDefaults(defaults, q, prevConn) { | ||
const cs = getConnections(q); | ||
const updatedEntries = cs.map((conn) => { | ||
const innerQ = q[conn]; | ||
const $ = prevConn && defaults?.[prevConn]?.[conn]; | ||
const newQ = $ ? { ...innerQ, $: { ...innerQ.$, ...$ } } : innerQ; | ||
return [conn, enhanceWithDefaults(defaults, newQ, conn)]; | ||
}); | ||
return { ...q, ...Object.fromEntries(updatedEntries) }; | ||
} | ||
function namespaceForEid(eid, db) { | ||
@@ -118,0 +129,0 @@ const [[_, x]] = getAttrTups(eid, db); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1495634
13
24477
40