azarattum / crstore Goto Github PK
View Code? Open in Web Editor NEWConflict-free replicated store.
Home Page: https://npmjs.com/package/crstore
License: MIT License
Conflict-free replicated store.
Home Page: https://npmjs.com/package/crstore
License: MIT License
Hey @Azarattum, I wanted to play with CRStore, but could not get it started with vite locally.
This is the message:
$ yarn dev
yarn run v1.22.19
$ vite dev
failed to load config from CRStore/vite.config.js
error when starting dev server:
ReferenceError: BroadcastChannel is not defined
at database (file:///CRStore/vite.config.js.timestamp-1684107342218-0f51a313eeb19.mjs:470:19)
at file:///CRStore/vite.config.js.timestamp-1684107342218-0f51a313eeb19.mjs:697:28
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Same message with pnpm. Do I need to configure something else to have BroadcastChannel
available for vite to compile properly? Is this expected behaviour?
Cheers and have a good Monday,
Roman
Hello! ๐
I'm currently using your awesome package to build an internal local-first app at my company. I'm using Bun & SvelteKit to build my project, and so I noticed that this package has experimental support for Bun. Now I'm still a beginner in this whole local-first cr-sqlite CRDT world. But as I understand it, any SQLite instance that works with cr-sqlite
should be able to work with CRStore
, bun:sqlite
included.
When I try to run this file in Bun:
import { crr, primary } from "crstore";
import { boolean, object, string } from "superstruct";
import { database } from "crstore";
import Os from 'os';
import { Database } from 'bun:sqlite';
const macOS = Os.platform() === "darwin";
if (macOS) {
/**
* By default, macOS ships with Apple's proprietary build of SQLite
* which doesn't support extensions. (therefore does not support cr-sqlite)
* To use extensions, you'll need to install a vanilla build of SQLite.
* Reference: https://bun.sh/docs/api/sqlite#loadextension
*/
Database.setCustomSQLite("/opt/homebrew/opt/sqlite/lib/libsqlite3.dylib");
}
// Struct that represents the table
const todos = object({
id: string(),
title: string(),
text: string(),
completed: boolean(),
});
crr(todos); // Register table with conflict-free replicated relations
primary(todos, "id"); // Define a primary key (can be multi-column)
const schema = object({ todos });
const { close } = database(schema);
close();
I get this error:
53 | return Promise.resolve({
54 | rows: stmt.all(parameters),
55 | });
56 | }
57 | else {
58 | const { changes, lastInsertRowid } = stmt.run(parameters);
^
TypeError: Right side of assignment cannot be destructured
at executeQuery (/Users/fawaz/Projects/crstore-bun-issue/node_modules/kysely/dist/esm/dialect/sqlite/sqlite-driver.js:58:50)
at /Users/fawaz/Projects/crstore-bun-issue/node_modules/kysely/dist/esm/dialect/sqlite/sqlite-driver.js:35:15
at rollbackTransaction (/Users/fawaz/Projects/crstore-bun-issue/node_modules/kysely/dist/esm/dialect/sqlite/sqlite-driver.js:34:31)
at /Users/fawaz/Projects/crstore-bun-issue/node_modules/kysely/dist/esm/kysely.js:417:23
Minimal Reproduction: https://github.com/fawaz-alesayi/crstore-bun-issue
After a couple of minutes of investigation I noticed that you're using Kysely
when creating the new tables defined by superstruct
.
As I understand it, Kysely only supports bun:sqlite
if you're using a custom adapter like these ones
https://github.com/dylanblokhuis/kysely-bun-sqlite
https://github.com/subframe7536/kysely-sqlite-tools/tree/master/packages/dialect-bun-worker
Here are some ways I thought up of making Bun support better:
This is the solution I'm using in my fork and it works great. The new API for the database
function would be
import { object, string, type Infer } from "superstruct";
const db = database(schema, {
name: "test.db",
kysely: ({ database }) => {
return new Kysely<Schema<Infer<typeof schema>>>({
dialect: new SqliteDialect({ database }), // Or BunDialect, LibSQL, whatever works with cr-sqlite
plugins: [new JSONPlugin()],
});
},
});
Would look something like this:
// https://github.com/Azarattum/CRStore/blob/main/src/lib/database/index.ts
async function init<T extends CRSchema>(
file: string,
schema: T,
paths = defaultPaths,
) {
type DB = Schema<T>;
if (connections.has(file)) return connections.get(file) as Connection<DB>;
const { database, browser } = await load(file, paths);
const Dialect = browser ? CRDialect : process.isBun ? SqliteBunDialect : SqliteDialect;
const kysely = new Kysely<DB>({
dialect: new Dialect({ database }),
plugins: [new JSONPlugin()],
});
// ... rest of the code
I haven't had much time to dig into the actual error that is produced by Kysely and why it happens with Bun. but my guess is that there's some subtle API differences between bun:sqlite
and better-sqlite3
that causes this error.
I'm using cr-sqlite in a static SvelteKit project and stumbled upon this library and it looks nicer than running raw sql and handling async stores myself.
If my primary (only) data lives in the browser with a named sqlite db, what happens when I update the schema? My original plan with my method is to write migrations manually and apply them on each first page load, wondering if this library can handle this case.
$posts // --> []
$posts // --> [{},{},{},{}]
Because an empty array is a valid output, this can't be relied upon to assess subscription status (initialising, refreshing, loading...). It would be helpful to either initialise as undefined
or have array status methods (eg: $posts.loading
)
Hi! Would you mind giving this repo a license? I'd love to copy https://github.com/Azarattum/CRStore/blob/main/src/lib/database/dialect.ts for my own proj. Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.