Skip to content

Commit

Permalink
store: native support for sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
terrablue committed Jul 21, 2024
1 parent 364eb43 commit 3718f86
Show file tree
Hide file tree
Showing 24 changed files with 119 additions and 56 deletions.
17 changes: 14 additions & 3 deletions packages/store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"db:surreal": "surreal start --user test --pass test"
},
"dependencies": {
"rcompat": "^0.17.0"
"rcompat": "^0.17.0",
"@rcompat/sql": "^0.1.0"
},
"devDependencies": {
"@primate/types": "workspace:^",
Expand Down Expand Up @@ -55,7 +56,17 @@
"exports": {
".": "./src/index.js",
"./errors/*": "./src/errors/*.js",
"./memory": "./src/drivers/memory/index.js",
"./json": "./src/drivers/json/index.js"
"./memory": {
"runtime": "./src/drivers/memory/runtime/index.js",
"default": "./src/drivers/memory/default/index.js"
},
"./json": {
"runtime": "./src/drivers/json/runtime/index.js",
"default": "./src/drivers/json/default/index.js"
},
"./sqlite": {
"runtime": "./src/drivers/sqlite/runtime/index.js",
"default": "./src/drivers/sqlite/default/index.js"
}
}
}
4 changes: 3 additions & 1 deletion packages/store/src/build.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import NoStoreDirectory from "@primate/store/errors/no-store-directory";

export default directory => async (app, next) => {
export default (directory, driver_build) => async (app, next) => {
const root = app.root.join(directory);
if (!await root.exists()) {
NoStoreDirectory.warn(app.log, root);
return next(app);
}

await driver_build();

await app.stage(app.root.join(directory), directory);

app.server_build.push("stores");
Expand Down
16 changes: 12 additions & 4 deletions packages/store/src/depend.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import NoDependencies from "@primate/store/errors/no-dependencies";
import { tryreturn } from "rcompat/async";
import { packager } from "rcompat/package";
import * as O from "rcompat/object";
import { manifest, packager } from "rcompat/package";

export default async (dependencies, from) => {
const peers = async () =>
({ ...(await manifest(import.meta.filename)).peerDependencies });
const MODULE_NOT_FOUND = "ERR_MODULE_NOT_FOUND";

export default async (on, name) => {
const from = `frontend:${name}`;
const dependencies = O.filter(await peers(), ([key]) => on.includes(key));
const modules = Object.keys(dependencies);

const results = await Promise.all(modules.map(module =>
tryreturn(_ => import(module)).orelse(_ => module),
));
tryreturn(_ => import(module))
.orelse(({ code }) => code === MODULE_NOT_FOUND ? module : {})));

const errored = results.filter(result => typeof result === "string");
const versions = Object.entries(dependencies)
.filter(([dependency]) => errored.includes(dependency))
Expand Down
2 changes: 2 additions & 0 deletions packages/store/src/drivers/json/default/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// noop
export default name => async () => {};
9 changes: 9 additions & 0 deletions packages/store/src/drivers/json/default/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import build from "./build.js";
import serve from "../runtime/serve.js";

const name = "json";

export default ({ filename }) => ({
build: build(name),
serve: serve(filename),
});
5 changes: 5 additions & 0 deletions packages/store/src/drivers/json/runtime/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import serve from "./serve.js";

export default ({ filename }) => ({
serve: serve(filename),
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { File } from "rcompat/fs";
import * as O from "rcompat/object";
import { is } from "rcompat/invariant";
import wrap from "../../wrap.js";
import Facade from "../memory/Facade.js";
import types from "../memory/types.js";
import wrap from "../../../wrap.js";
import Facade from "../../memory/runtime/Facade.js";
import types from "../../memory/runtime/types.js";

export default ({ filename }) => async () => {
export default filename => async () => {
is(filename).string();

const path = new File(filename);
Expand Down
2 changes: 2 additions & 0 deletions packages/store/src/drivers/memory/default/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// noop
export default name => async () => {};
9 changes: 9 additions & 0 deletions packages/store/src/drivers/memory/default/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import build from "./build.js";
import serve from "../runtime/serve.js";

const name = "memory";

export default () => ({
build: build(name),
serve: serve(),
});
File renamed without changes.
5 changes: 5 additions & 0 deletions packages/store/src/drivers/memory/runtime/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import serve from "./serve.js";

export default () => ({
serve: serve(),
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import types from "./types.js";
import Facade from "./Facade.js";
import wrap from "../../wrap.js";
import wrap from "../../../wrap.js";

// no conf
export default () => async () => {
const database = {
collections: {},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import memory from "./driver.js";
import base from "../base.test.js";
import base from "../../base.test.js";

export default test => base(test, () => memory()());
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ident from "../ident.js";
import ident from "../../ident.js";

// we can't depend on @primate/types here
const valid = /^[^\W_]{8}-[^\W_]{4}-[^\W_]{4}-[^\W_]{4}-[^\W_]{12}$/u;
Expand Down
12 changes: 12 additions & 0 deletions packages/store/src/drivers/sqlite/default/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { platform } from "rcompat/package";
import depend from "../../../depend.js";

const dependencies = ["better-sqlite3"];

export default name => async () => {
const bun = platform() === "bun";
if (bun) {
return;
}
await depend(dependencies, name);
};
14 changes: 14 additions & 0 deletions packages/store/src/drivers/sqlite/default/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import build from "./build.js";
import serve from "../runtime/serve.js";

const defaults = {
filename: ":memory:",
};
const name = "sqlite";

export default ({
filename = defaults.filename,
} = {}) => ({
build: build(name),
serve: serve(filename),
});
13 changes: 0 additions & 13 deletions packages/store/src/drivers/sqlite/driver.spec.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as O from "rcompat/object";
import { platform } from "rcompat/package";
import typemap from "./typemap.js";
import { make_sort } from "../sql/exports.js";
import { make_sort } from "../../sql/exports.js";

const is_bun = platform() === "bun";

Expand Down
11 changes: 11 additions & 0 deletions packages/store/src/drivers/sqlite/runtime/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import serve from "./serve.js";

const defaults = {
filename: ":memory:",
};

export default ({
filename = defaults.filename,
} = {}) => ({
serve: serve(filename),
});
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
import Database from "@rcompat/sql/sqlite";
import { numeric } from "rcompat/invariant";
import * as O from "rcompat/object";
import { platform } from "rcompat/package";
import ident from "../ident.js";
import { peers } from "../common/exports.js";
import depend from "../../depend.js";
import Pool from "../../pool/exports.js";
import wrap from "../../wrap.js";
import Pool from "../../../pool/exports.js";
import wrap from "../../../wrap.js";
import ident from "../../ident.js";
import Facade from "./Facade.js";

const name = "sqlite";
const dependencies = ["better-sqlite3"];
const on = O.filter(peers, ([key]) => dependencies.includes(key));
const defaults = {
filename: ":memory:",
};

export default ({
filename = defaults.filename,
} = {}) => async () => {
const $on = platform() === "bun" ? { "bun:sqlite": null } : on;
const [{ default: Connection }] = await depend($on, `store:${name}`);
export default filename => () => {
const pool = new Pool({
manager: {
new: () => new Connection(filename, { create: true }),
new: () => new Database(filename, { create: true }),
kill: connection => connection.close(),
},
});
Expand Down
4 changes: 2 additions & 2 deletions packages/store/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export default ({

return {
name: "primate:store",
build: build(directory),
serve: serve(directory, mode, driver, env),
build: build(directory, driver.build),
serve: serve(directory, mode, driver.serve, env),
route: route(env),
};
};
11 changes: 4 additions & 7 deletions packages/store/src/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ import { dim } from "rcompat/colors";
import * as O from "rcompat/object";
import primary from "./primary.js";

const last = -1;
const ending = -3;

const valid_type = ({ base, validate }) =>
base !== undefined && typeof validate === "function";

const valid = (type, name, store) =>
valid_type(type) ? type : InvalidType.throw(name, store);

export default (directory, mode, driver, env) => async (app, next) => {
export default (directory, mode, driver_serve, env) => async (app, next) => {
const root = app.runpath(directory);
const module = "primate/store";

Expand Down Expand Up @@ -56,7 +53,7 @@ export default (directory, mode, driver, env) => async (app, next) => {

app.log.info("all stores nominal", { module });

const default_driver = await driver();
const default_driver = await driver_serve();

env.root = root;
env.log = app.log;
Expand All @@ -65,8 +62,8 @@ export default (directory, mode, driver, env) => async (app, next) => {
driver: default_driver,
...defaults,
};
env.drivers = [...new Set(stores.map(({ driver: $driver }) =>
$driver ?? default_driver))];
env.drivers = [...new Set(stores.map(({ driver }) =>
driver ?? default_driver))];
env.active = true;

return next({ ...app, stores });
Expand Down

0 comments on commit 3718f86

Please sign in to comment.