diff --git a/packages/store/package.json b/packages/store/package.json index fa16c9a4..944f77f4 100644 --- a/packages/store/package.json +++ b/packages/store/package.json @@ -55,6 +55,9 @@ "type": "module", "exports": { ".": "./src/index.js", + "./*/common": "./src/server/*/common.js", + "./base/*": "./src/base/*.js", + "./sql/*": "./src/sql/*.js", "./errors/*": "./src/errors/*.js", "./*": { "runtime": "./src/server/*/runtime/index.js", diff --git a/packages/store/src/depend.js b/packages/store/src/base/depend.js similarity index 100% rename from packages/store/src/depend.js rename to packages/store/src/base/depend.js diff --git a/packages/store/src/server/ident.js b/packages/store/src/base/ident.js similarity index 100% rename from packages/store/src/server/ident.js rename to packages/store/src/base/ident.js diff --git a/packages/store/src/modes.js b/packages/store/src/base/modes.js similarity index 100% rename from packages/store/src/modes.js rename to packages/store/src/base/modes.js diff --git a/packages/store/src/base/pool.js b/packages/store/src/base/pool.js new file mode 100644 index 00000000..8ab92687 --- /dev/null +++ b/packages/store/src/base/pool.js @@ -0,0 +1 @@ +export { default } from "./pool/Pool.js"; diff --git a/packages/store/src/pool/Pool.js b/packages/store/src/base/pool/Pool.js similarity index 100% rename from packages/store/src/pool/Pool.js rename to packages/store/src/base/pool/Pool.js diff --git a/packages/store/src/pool/Pool.spec.js b/packages/store/src/base/pool/Pool.spec.js similarity index 100% rename from packages/store/src/pool/Pool.spec.js rename to packages/store/src/base/pool/Pool.spec.js diff --git a/packages/store/src/pool/errors.js b/packages/store/src/base/pool/errors.js similarity index 100% rename from packages/store/src/pool/errors.js rename to packages/store/src/base/pool/errors.js diff --git a/packages/store/src/primary.js b/packages/store/src/base/primary.js similarity index 100% rename from packages/store/src/primary.js rename to packages/store/src/base/primary.js diff --git a/packages/store/src/server/base.test.js b/packages/store/src/base/test.js similarity index 100% rename from packages/store/src/server/base.test.js rename to packages/store/src/base/test.js diff --git a/packages/store/src/wrap.js b/packages/store/src/base/wrap.js similarity index 97% rename from packages/store/src/wrap.js rename to packages/store/src/base/wrap.js index 9706864b..f004062b 100644 --- a/packages/store/src/wrap.js +++ b/packages/store/src/base/wrap.js @@ -1,12 +1,12 @@ -import InvalidValue from "@primate/store/errors/invalid-value"; import InvalidDocument from "@primate/store/errors/invalid-document"; +import InvalidValue from "@primate/store/errors/invalid-value"; import NoDocument from "@primate/store/errors/no-document"; import { maybe } from "rcompat/invariant"; import * as O from "rcompat/object"; import { tryreturn } from "rcompat/sync"; -import bases from "./bases.js"; import primary from "./primary.js"; -import validate from "./validate.js"; +import bases from "./wrap/bases.js"; +import validate from "./wrap/validate.js"; const transform = to => ({ types, schema, document = {}, path, mode }) => O.transform(document, entry => entry diff --git a/packages/store/src/bases.js b/packages/store/src/base/wrap/bases.js similarity index 100% rename from packages/store/src/bases.js rename to packages/store/src/base/wrap/bases.js diff --git a/packages/store/src/validate.js b/packages/store/src/base/wrap/validate.js similarity index 95% rename from packages/store/src/validate.js rename to packages/store/src/base/wrap/validate.js index 96cb5d02..1689cea3 100644 --- a/packages/store/src/validate.js +++ b/packages/store/src/base/wrap/validate.js @@ -1,4 +1,5 @@ -import modes from "./modes.js"; +import modes from "@primate/store/base/modes"; + const normalize = string => string.trim() === "" ? undefined : string; const is_strict = mode => mode === modes.strict; diff --git a/packages/store/src/index.js b/packages/store/src/index.js index 2ebd49c3..41f53471 100644 --- a/packages/store/src/index.js +++ b/packages/store/src/index.js @@ -1,7 +1,7 @@ +import modes from "@primate/store/base/modes"; import memory from "@primate/store/memory"; import { assert } from "rcompat/invariant"; import build from "./build.js"; -import modes from "./modes.js"; import route from "./route.js"; import serve from "./serve.js"; diff --git a/packages/store/src/pool/exports.js b/packages/store/src/pool/exports.js deleted file mode 100644 index 9556ddf4..00000000 --- a/packages/store/src/pool/exports.js +++ /dev/null @@ -1 +0,0 @@ -export { default } from "./Pool.js"; diff --git a/packages/store/src/serve.js b/packages/store/src/serve.js index 7345cad6..d7bd0568 100644 --- a/packages/store/src/serve.js +++ b/packages/store/src/serve.js @@ -1,10 +1,10 @@ +import primary from "@primate/store/base/primary"; import EmptyStoreDirectory from "@primate/store/errors/empty-store-directory"; import InvalidType from "@primate/store/errors/invalid-type"; import NoPrimaryKey from "@primate/store/errors/no-primary-key"; import * as A from "rcompat/array"; import { dim } from "rcompat/colors"; import * as O from "rcompat/object"; -import primary from "./primary.js"; const valid_type = ({ base, validate }) => base !== undefined && typeof validate === "function"; diff --git a/packages/store/src/server/common/exports.js b/packages/store/src/server/common/exports.js deleted file mode 100644 index 2e3f0182..00000000 --- a/packages/store/src/server/common/exports.js +++ /dev/null @@ -1 +0,0 @@ -export { default as peers } from "./peers.js"; diff --git a/packages/store/src/server/common/peers.js b/packages/store/src/server/common/peers.js deleted file mode 100644 index e933708f..00000000 --- a/packages/store/src/server/common/peers.js +++ /dev/null @@ -1,3 +0,0 @@ -import { manifest } from "rcompat/package"; - -export default { ...(await manifest(import.meta.filename)).peerDependencies }; diff --git a/packages/store/src/server/json/common.js b/packages/store/src/server/json/common.js new file mode 100644 index 00000000..064abcff --- /dev/null +++ b/packages/store/src/server/json/common.js @@ -0,0 +1 @@ +export const name = "json"; diff --git a/packages/store/src/server/json/default/build.js b/packages/store/src/server/json/default/build.js index c085bd97..187e6038 100644 --- a/packages/store/src/server/json/default/build.js +++ b/packages/store/src/server/json/default/build.js @@ -1,2 +1,2 @@ // noop -export default name => async () => {}; +export default async () => {}; diff --git a/packages/store/src/server/json/default/index.js b/packages/store/src/server/json/default/index.js index 348e448d..9f4753eb 100644 --- a/packages/store/src/server/json/default/index.js +++ b/packages/store/src/server/json/default/index.js @@ -1,9 +1,7 @@ -import build from "./build.js"; import serve from "../runtime/serve.js"; - -const name = "json"; +import build from "./build.js"; export default ({ database }) => ({ - build: build(name), - serve: serve(database), + build, + serve: serve({ database }), }); diff --git a/packages/store/src/server/json/runtime/driver.js b/packages/store/src/server/json/runtime/driver.js new file mode 100644 index 00000000..8f858979 --- /dev/null +++ b/packages/store/src/server/json/runtime/driver.js @@ -0,0 +1,23 @@ +import { File } from "rcompat/fs"; +import { is } from "rcompat/invariant"; +import * as O from "rcompat/object"; + +export const connect = async ({ database }) => { + is(database).string(); + + const path = new File(database); + const db = { + collections: await path.exists() ? await path.json() : {}, + }; + + return { + read(name) { + return db.collections[name] ?? []; + }, + async write(name, callback) { + db.collections[name] = await callback(this.read(name)); + // write to file + await path.write(O.stringify(db.collections)); + }, + }; +}; diff --git a/packages/store/src/server/json/runtime/index.js b/packages/store/src/server/json/runtime/index.js index 9f7c0f4b..467d70b4 100644 --- a/packages/store/src/server/json/runtime/index.js +++ b/packages/store/src/server/json/runtime/index.js @@ -1,5 +1,5 @@ import serve from "./serve.js"; export default ({ database }) => ({ - serve: serve(database), + serve: serve({ database }), }); diff --git a/packages/store/src/server/json/runtime/serve.js b/packages/store/src/server/json/runtime/serve.js index 16e82646..96d7081a 100644 --- a/packages/store/src/server/json/runtime/serve.js +++ b/packages/store/src/server/json/runtime/serve.js @@ -1,35 +1,18 @@ -import { File } from "rcompat/fs"; -import * as O from "rcompat/object"; -import { is } from "rcompat/invariant"; -import wrap from "../../../wrap.js"; +import wrap from "@primate/store/base/wrap"; +import { name } from "@primate/store/json/common"; import Facade from "../../memory/runtime/Facade.js"; import types from "../../memory/runtime/types.js"; +import { connect } from "./driver.js"; -export default database => async () => { - is(database).string(); - - const path = new File(database); - const db = { - collections: await path.exists() ? await path.json() : {}, - }; - - const connection = { - read(name) { - return db.collections[name] ?? []; - }, - async write(name, callback) { - db.collections[name] = await callback(this.read(name)); - // write to file - await path.write(O.stringify(db.collections)); - }, - }; +export default options => async () => { + const client = await connect(options); return { - name: "json", + name, types, async transact(stores) { return (others, next) => { - const facade = new Facade(connection); + const facade = new Facade(client); return next([ ...others, ...stores.map(([name, store]) => [name, wrap(store, facade, types)]), diff --git a/packages/store/src/server/json/runtime/serve.spec.js b/packages/store/src/server/json/runtime/serve.spec.js index a0134952..00a3adb5 100644 --- a/packages/store/src/server/json/runtime/serve.spec.js +++ b/packages/store/src/server/json/runtime/serve.spec.js @@ -1,6 +1,6 @@ +import base from "@primate/store/base/test"; import { File } from "rcompat/fs"; import serve from "./serve.js"; -import base from "../base.test.js"; const database = new File(import.meta.url).up(1).join("db.json"); diff --git a/packages/store/src/server/memory/common.js b/packages/store/src/server/memory/common.js new file mode 100644 index 00000000..30ae6fc5 --- /dev/null +++ b/packages/store/src/server/memory/common.js @@ -0,0 +1 @@ +export const name = "memory"; diff --git a/packages/store/src/server/memory/default/build.js b/packages/store/src/server/memory/default/build.js index c085bd97..38cf94a4 100644 --- a/packages/store/src/server/memory/default/build.js +++ b/packages/store/src/server/memory/default/build.js @@ -1,2 +1,2 @@ // noop -export default name => async () => {}; +export default () => {}; diff --git a/packages/store/src/server/memory/default/index.js b/packages/store/src/server/memory/default/index.js index 106ed2c0..0357d88b 100644 --- a/packages/store/src/server/memory/default/index.js +++ b/packages/store/src/server/memory/default/index.js @@ -1,9 +1,7 @@ import build from "./build.js"; import serve from "../runtime/serve.js"; -const name = "memory"; - export default () => ({ - build: build(name), + build, serve: serve(), }); diff --git a/packages/store/src/server/memory/runtime/driver.js b/packages/store/src/server/memory/runtime/driver.js new file mode 100644 index 00000000..c6d05ffa --- /dev/null +++ b/packages/store/src/server/memory/runtime/driver.js @@ -0,0 +1,14 @@ +export const connect = () => { + const database = { + collections: {}, + }; + return { + read(name) { + return database.collections[name] ?? []; + }, + write(name, callback) { + // do a read + database.collections[name] = callback(this.read(name)); + }, + }; +}; diff --git a/packages/store/src/server/memory/runtime/serve.js b/packages/store/src/server/memory/runtime/serve.js index fca5d520..d3e1bf3c 100644 --- a/packages/store/src/server/memory/runtime/serve.js +++ b/packages/store/src/server/memory/runtime/serve.js @@ -1,28 +1,19 @@ -import types from "./types.js"; +import wrap from "@primate/store/base/wrap"; +import { name } from "@primate/store/memory/common"; +import { connect } from "./driver.js"; import Facade from "./Facade.js"; -import wrap from "../../../wrap.js"; +import types from "./types.js"; // no conf export default () => async () => { - const database = { - collections: {}, - }; - const connection = { - read(name) { - return database.collections[name] ?? []; - }, - write(name, callback) { - // do a read - database.collections[name] = callback(this.read(name)); - }, - }; + const client = await connect(); return { - name: "memory", + name, types, async transact(stores) { return (others, next) => { - const facade = new Facade(connection); + const facade = new Facade(client); return next([ ...others, ...stores.map(([name, store]) => [name, wrap(store, facade, types)]), diff --git a/packages/store/src/server/memory/runtime/serve.spec.js b/packages/store/src/server/memory/runtime/serve.spec.js index fe6e0d93..fddcb557 100644 --- a/packages/store/src/server/memory/runtime/serve.spec.js +++ b/packages/store/src/server/memory/runtime/serve.spec.js @@ -1,4 +1,4 @@ +import base from "@primate/store/base/test"; import serve from "./serve.js"; -import base from "../../base.test.js"; export default test => base(test, () => serve()()); diff --git a/packages/store/src/server/memory/runtime/types.js b/packages/store/src/server/memory/runtime/types.js index 5734e144..73f15ec3 100644 --- a/packages/store/src/server/memory/runtime/types.js +++ b/packages/store/src/server/memory/runtime/types.js @@ -1,4 +1,4 @@ -import ident from "../../ident.js"; +import ident from "@primate/store/base/ident"; // we can't depend on @primate/types here const valid = /^[^\W_]{8}-[^\W_]{4}-[^\W_]{4}-[^\W_]{4}-[^\W_]{12}$/u; diff --git a/packages/store/src/server/mongodb/common.js b/packages/store/src/server/mongodb/common.js new file mode 100644 index 00000000..acd20d7e --- /dev/null +++ b/packages/store/src/server/mongodb/common.js @@ -0,0 +1,8 @@ +export const dependencies = ["mongodb"]; + +export const defaults = { + host: "localhost", + port: 27017, +}; + +export const name = "mongodb"; diff --git a/packages/store/src/server/mongodb/default/build.js b/packages/store/src/server/mongodb/default/build.js index 94529877..08fb1863 100644 --- a/packages/store/src/server/mongodb/default/build.js +++ b/packages/store/src/server/mongodb/default/build.js @@ -1,7 +1,6 @@ -import depend from "../../../depend.js"; +import depend from "@primate/store/base/depend"; +import { name, dependencies } from "@primate/store/mongodb/common"; -const dependencies = ["mongodb"]; - -export default name => async () => { +export default async () => { await depend(dependencies, name); }; diff --git a/packages/store/src/server/mongodb/default/index.js b/packages/store/src/server/mongodb/default/index.js index 78e3c278..f7dc7bf4 100644 --- a/packages/store/src/server/mongodb/default/index.js +++ b/packages/store/src/server/mongodb/default/index.js @@ -1,17 +1,12 @@ -import build from "./build.js"; +import { defaults } from "@primate/store/mongodb/common"; import serve from "../runtime/serve.js"; - -const defaults = { - host: "localhost", - port: 27017, -}; -const name = "mongodb"; +import build from "./build.js"; export default ({ host = defaults.host, port = defaults.port, database, } = {}) => ({ - build: build(name), + build, serve: serve({ host, port, database }), }); diff --git a/packages/store/src/server/mongodb/runtime/driver.js b/packages/store/src/server/mongodb/runtime/driver.js new file mode 100644 index 00000000..29317299 --- /dev/null +++ b/packages/store/src/server/mongodb/runtime/driver.js @@ -0,0 +1,11 @@ +import { Decimal128, MongoClient, ObjectId } from "mongodb"; + +export const connect = async ({ host, port }) => { + const url = `mongodb://${host}:${port}?replicaSet=rs0&directConnection=true`; + const client = new MongoClient(url); + await client.connect(); + return client; +}; + +export { Decimal128, ObjectId }; + diff --git a/packages/store/src/server/mongodb/runtime/index.js b/packages/store/src/server/mongodb/runtime/index.js index ab470c63..4d7504fe 100644 --- a/packages/store/src/server/mongodb/runtime/index.js +++ b/packages/store/src/server/mongodb/runtime/index.js @@ -1,10 +1,6 @@ +import { defaults } from "@primate/store/mongodb/common"; import serve from "./serve.js"; -const defaults = { - host: "localhost", - port: 27017, -}; - export default ({ host = defaults.host, port = defaults.port, diff --git a/packages/store/src/server/mongodb/runtime/serve.js b/packages/store/src/server/mongodb/runtime/serve.js index 13b78138..917e80cb 100644 --- a/packages/store/src/server/mongodb/runtime/serve.js +++ b/packages/store/src/server/mongodb/runtime/serve.js @@ -1,14 +1,11 @@ -import { Decimal128, MongoClient, ObjectId } from "mongodb"; -import wrap from "../../../wrap.js"; -import ident from "../../ident.js"; +import ident from "@primate/store/base/ident"; +import wrap from "@primate/store/base/wrap"; +import { name } from "@primate/store/mongodb/common"; import Facade from "./Facade.js"; - -const name = "mongodb"; +import { connect, Decimal128, ObjectId } from "./driver.js"; export default ({ host, port, database } = {}) => async _ => { - const url = `mongodb://${host}:${port}?replicaSet=rs0&directConnection=true`; - const client = new MongoClient(url); - await client.connect(); + const client = await connect({ host, port }); const types = { primary: { diff --git a/packages/store/src/server/mongodb/runtime/serve.spec.js b/packages/store/src/server/mongodb/runtime/serve.spec.js index c369b756..cf06dd20 100644 --- a/packages/store/src/server/mongodb/runtime/serve.spec.js +++ b/packages/store/src/server/mongodb/runtime/serve.spec.js @@ -1,4 +1,4 @@ import serve from "./serve.js"; -import base from "../../base.test.js"; +import base from "@primate/store/base/test"; export default async test => base(test, () => serve({ database: "primate" })()); diff --git a/packages/store/src/server/mysql/common.js b/packages/store/src/server/mysql/common.js new file mode 100644 index 00000000..f297a526 --- /dev/null +++ b/packages/store/src/server/mysql/common.js @@ -0,0 +1,8 @@ +export const dependencies = ["mysql2"]; + +export const defaults = { + host: "localhost", + port: 3306, +}; + +export const name = "mysql"; diff --git a/packages/store/src/server/mysql/default/build.js b/packages/store/src/server/mysql/default/build.js index e2f94f11..4190b35c 100644 --- a/packages/store/src/server/mysql/default/build.js +++ b/packages/store/src/server/mysql/default/build.js @@ -1,7 +1,6 @@ -import depend from "../../../depend.js"; +import depend from "@primate/store/base/depend"; +import { dependencies, name } from "@primate/store/mysql/common"; -const dependencies = ["mysql2"]; - -export default name => async () => { +export default async () => { await depend(dependencies, name); }; diff --git a/packages/store/src/server/mysql/default/index.js b/packages/store/src/server/mysql/default/index.js index b805873b..8652127a 100644 --- a/packages/store/src/server/mysql/default/index.js +++ b/packages/store/src/server/mysql/default/index.js @@ -1,11 +1,6 @@ import build from "./build.js"; import serve from "../runtime/serve.js"; - -const defaults = { - host: "localhost", - port: 3306, -}; -const name = "mysql"; +import { defaults } from "@primate/store/mysql/common"; export default ({ host = defaults.host, @@ -14,6 +9,6 @@ export default ({ username, password, } = {}) => ({ - build: build(name), + build, serve: serve({ host, port, database, username, password }), }); diff --git a/packages/store/src/server/mysql/runtime/Facade.js b/packages/store/src/server/mysql/runtime/Facade.js index 41ca5af6..f6cbd1c5 100644 --- a/packages/store/src/server/mysql/runtime/Facade.js +++ b/packages/store/src/server/mysql/runtime/Facade.js @@ -1,5 +1,5 @@ import * as O from "rcompat/object"; -import { make_sort } from "../../sql/exports.js"; +import make_sort from "@primate/store/sql/make-sort"; import typemap from "./typemap.js"; const filter_null = object => O.filter(object, ([, value]) => value !== null); diff --git a/packages/store/src/server/mysql/runtime/driver.js b/packages/store/src/server/mysql/runtime/driver.js new file mode 100644 index 00000000..e98a839e --- /dev/null +++ b/packages/store/src/server/mysql/runtime/driver.js @@ -0,0 +1,16 @@ +import mysql from "mysql2/promise"; + +export const connect = ({ host, port, database, username, password }) => + mysql.createPool({ + host, + port, + database, + user: username, + password, + waitForConnections: true, + connectionLimit: 10, + queueLimit: 0, + enableKeepAlive: true, + keepAliveInitialDelay: 0, + namedPlaceholders: true, + }); diff --git a/packages/store/src/server/mysql/runtime/index.js b/packages/store/src/server/mysql/runtime/index.js index b7372923..e2175ff0 100644 --- a/packages/store/src/server/mysql/runtime/index.js +++ b/packages/store/src/server/mysql/runtime/index.js @@ -1,10 +1,6 @@ +import { defaults } from "@primate/store/mysql/common"; import serve from "./serve.js"; -const defaults = { - host: "localhost", - port: 3306, -}; - export default ({ host = defaults.host, port = defaults.port, diff --git a/packages/store/src/server/mysql/runtime/serve.js b/packages/store/src/server/mysql/runtime/serve.js index d9b176d0..e3c4d6e7 100644 --- a/packages/store/src/server/mysql/runtime/serve.js +++ b/packages/store/src/server/mysql/runtime/serve.js @@ -1,25 +1,12 @@ -import mysql from "mysql2/promise"; +import ident from "@primate/store/base/ident"; +import wrap from "@primate/store/base/wrap"; +import { name } from "@primate/store/mysql/common"; import { numeric } from "rcompat/invariant"; -import wrap from "../../../wrap.js"; -import ident from "../../ident.js"; import Facade from "./Facade.js"; +import { connect } from "./driver.js"; -const name = "mysql"; - -export default ({ host, port, database, username, password }) => async () => { - const pool = mysql.createPool({ - host, - port, - database, - user: username, - password, - waitForConnections: true, - connectionLimit: 10, - queueLimit: 0, - enableKeepAlive: true, - keepAliveInitialDelay: 0, - namedPlaceholders: true, - }); +export default options => async () => { + const client = await connect(options); const types = { primary: { @@ -72,7 +59,7 @@ export default ({ host, port, database, username, password }) => async () => { types, async transact(stores) { return async (others, next) => { - const connection = await pool.getConnection(); + const connection = await client.getConnection(); const facade = new Facade(connection); try { await connection.query("start transaction"); @@ -87,7 +74,7 @@ export default ({ host, port, database, username, password }) => async () => { throw error; } finally { // noop, no end transaction - pool.releaseConnection(connection); + client.releaseConnection(connection); } }; }, diff --git a/packages/store/src/server/mysql/runtime/serve.spec.js b/packages/store/src/server/mysql/runtime/serve.spec.js index 822ba489..38675dde 100644 --- a/packages/store/src/server/mysql/runtime/serve.spec.js +++ b/packages/store/src/server/mysql/runtime/serve.spec.js @@ -1,4 +1,4 @@ -import base from "../../base.test.js"; +import base from "@primate/store/base/test"; import serve from "./serve.js"; export default async test => base(test, () => serve({ diff --git a/packages/store/src/server/mysql/runtime/typemap.js b/packages/store/src/server/mysql/runtime/typemap.js index fdf581a8..b2102d36 100644 --- a/packages/store/src/server/mysql/runtime/typemap.js +++ b/packages/store/src/server/mysql/runtime/typemap.js @@ -22,4 +22,3 @@ const types = { }; export default value => types[value]; - diff --git a/packages/store/src/server/postgresql/common.js b/packages/store/src/server/postgresql/common.js new file mode 100644 index 00000000..f9726408 --- /dev/null +++ b/packages/store/src/server/postgresql/common.js @@ -0,0 +1,8 @@ +export const dependencies = ["postgres"]; + +export const defaults = { + host: "localhost", + port: 5432, +}; + +export const name = "postgresql"; diff --git a/packages/store/src/server/postgresql/default/build.js b/packages/store/src/server/postgresql/default/build.js index f7ddf3df..8fe48d44 100644 --- a/packages/store/src/server/postgresql/default/build.js +++ b/packages/store/src/server/postgresql/default/build.js @@ -1,7 +1,6 @@ -import depend from "../../../depend.js"; +import depend from "@primate/store/base/depend"; +import { dependencies, name } from "@primate/store/postgresql/common"; -const dependencies = ["postgres"]; - -export default name => async () => { +export default async () => { await depend(dependencies, name); }; diff --git a/packages/store/src/server/postgresql/default/index.js b/packages/store/src/server/postgresql/default/index.js index 155284eb..4dc5537e 100644 --- a/packages/store/src/server/postgresql/default/index.js +++ b/packages/store/src/server/postgresql/default/index.js @@ -1,11 +1,6 @@ -import build from "./build.js"; +import { defaults } from "@primate/store/postgresql/common"; import serve from "../runtime/serve.js"; - -const defaults = { - host: "localhost", - port: 5432, -}; -const name = "postgresql"; +import build from "./build.js"; export default ({ host = defaults.host, @@ -14,6 +9,6 @@ export default ({ username, password, } = {}) => ({ - build: build(name), + build, serve: serve({ host, port, database, username, password }), }); diff --git a/packages/store/src/server/postgresql/runtime/Facade.js b/packages/store/src/server/postgresql/runtime/Facade.js index e4bf2c31..36a3b149 100644 --- a/packages/store/src/server/postgresql/runtime/Facade.js +++ b/packages/store/src/server/postgresql/runtime/Facade.js @@ -1,6 +1,6 @@ +import make_sort from "@primate/store/sql/make-sort"; import * as O from "rcompat/object"; import typemap from "./typemap.js"; -import { make_sort } from "../../sql/exports.js"; const filter_null = object => O.filter(object, ([, value]) => value !== null); const filter_nulls = objects => objects.map(object => filter_null(object)); diff --git a/packages/store/src/server/postgresql/runtime/driver.js b/packages/store/src/server/postgresql/runtime/driver.js new file mode 100644 index 00000000..3b503c4c --- /dev/null +++ b/packages/store/src/server/postgresql/runtime/driver.js @@ -0,0 +1,10 @@ +import Driver from "postgres"; + +export const connect = ({ host, port, database, username, password }) => + new Driver({ + host, + port, + db: database, + user: username, + pass: password, + }); diff --git a/packages/store/src/server/postgresql/runtime/index.js b/packages/store/src/server/postgresql/runtime/index.js index d7b48d29..1c1a4518 100644 --- a/packages/store/src/server/postgresql/runtime/index.js +++ b/packages/store/src/server/postgresql/runtime/index.js @@ -1,10 +1,6 @@ +import { defaults } from "@primate/store/postgresql/common"; import serve from "../runtime/serve.js"; -const defaults = { - host: "localhost", - port: 5432, -}; - export default ({ host = defaults.host, port = defaults.port, diff --git a/packages/store/src/server/postgresql/runtime/serve.js b/packages/store/src/server/postgresql/runtime/serve.js index 812de10a..29e9a467 100644 --- a/packages/store/src/server/postgresql/runtime/serve.js +++ b/packages/store/src/server/postgresql/runtime/serve.js @@ -1,19 +1,12 @@ -import Driver from "postgres"; +import ident from "@primate/store/base/ident"; +import wrap from "@primate/store/base/wrap"; +import { name } from "@primate/store/postgresql/common"; import { numeric } from "rcompat/invariant"; -import wrap from "../../../wrap.js"; -import ident from "../../ident.js"; +import { connect } from "./driver.js"; import Facade from "./Facade.js"; -const name = "postgresql"; - -export default ({ host, port, database, username, password }) => async () => { - const driver = new Driver({ - host, - port, - db: database, - user: username, - pass: password, - }); +export default options => async () => { + const client = connect(options); const types = { primary: { @@ -59,7 +52,7 @@ export default ({ host, port, database, username, password }) => async () => { types, async transact(stores) { return (others, next) => - driver.begin(async connection => { + client.begin(async connection => { const facade = new Facade(connection); return next([ ...others, ...stores.map(([name, store]) => diff --git a/packages/store/src/server/sql/exports.js b/packages/store/src/server/sql/exports.js deleted file mode 100644 index 18232ba5..00000000 --- a/packages/store/src/server/sql/exports.js +++ /dev/null @@ -1 +0,0 @@ -export { default as make_sort } from "./make-sort.js"; diff --git a/packages/store/src/server/sqlite/common.js b/packages/store/src/server/sqlite/common.js new file mode 100644 index 00000000..773caf20 --- /dev/null +++ b/packages/store/src/server/sqlite/common.js @@ -0,0 +1,7 @@ +export const dependencies = ["better-sqlite3"]; + +export const defaults = { + database: ":memory:", +}; + +export const name = "sqlite"; diff --git a/packages/store/src/server/sqlite/default/build.js b/packages/store/src/server/sqlite/default/build.js index 5d3b2310..7e42eff6 100644 --- a/packages/store/src/server/sqlite/default/build.js +++ b/packages/store/src/server/sqlite/default/build.js @@ -1,9 +1,8 @@ +import depend from "@primate/store/base/depend"; +import { dependencies, name } from "@primate/store/sqlite/common"; import { platform } from "rcompat/package"; -import depend from "../../../depend.js"; -const dependencies = ["better-sqlite3"]; - -export default name => async () => { +export default async () => { const bun = platform() === "bun"; if (bun) { return; diff --git a/packages/store/src/server/sqlite/default/index.js b/packages/store/src/server/sqlite/default/index.js index 0ed668fe..d9bf2dbf 100644 --- a/packages/store/src/server/sqlite/default/index.js +++ b/packages/store/src/server/sqlite/default/index.js @@ -1,14 +1,10 @@ -import build from "./build.js"; +import { defaults } from "@primate/store/sqlite/common"; import serve from "../runtime/serve.js"; - -const defaults = { - database: ":memory:", -}; -const name = "sqlite"; +import build from "./build.js"; export default ({ database = defaults.database, } = {}) => ({ - build: build(name), - serve: serve(database), + build, + serve: serve({ database }), }); diff --git a/packages/store/src/server/sqlite/runtime/Facade.js b/packages/store/src/server/sqlite/runtime/Facade.js index 8cd13a2e..1ebb5977 100644 --- a/packages/store/src/server/sqlite/runtime/Facade.js +++ b/packages/store/src/server/sqlite/runtime/Facade.js @@ -1,7 +1,7 @@ +import make_sort from "@primate/store/sql/make-sort"; import * as O from "rcompat/object"; import { platform } from "rcompat/package"; import typemap from "./typemap.js"; -import { make_sort } from "../../sql/exports.js"; const is_bun = platform() === "bun"; diff --git a/packages/store/src/server/sqlite/runtime/driver.js b/packages/store/src/server/sqlite/runtime/driver.js new file mode 100644 index 00000000..47411ad7 --- /dev/null +++ b/packages/store/src/server/sqlite/runtime/driver.js @@ -0,0 +1,9 @@ +import Pool from "@primate/store/base/pool"; +import Database from "@rcompat/sql/sqlite"; + +export const connect = ({ database }) => new Pool({ + manager: { + new: () => new Database(database, { create: true }), + kill: connection => connection.close(), + }, +}); diff --git a/packages/store/src/server/sqlite/runtime/index.js b/packages/store/src/server/sqlite/runtime/index.js index 246a7c65..573a6c92 100644 --- a/packages/store/src/server/sqlite/runtime/index.js +++ b/packages/store/src/server/sqlite/runtime/index.js @@ -1,11 +1,8 @@ +import { defaults } from "@primate/store/sqlite/common"; import serve from "./serve.js"; -const defaults = { - database: ":memory:", -}; - export default ({ database = defaults.database, } = {}) => ({ - serve: serve(database), + serve: serve({ database }), }); diff --git a/packages/store/src/server/sqlite/runtime/serve.js b/packages/store/src/server/sqlite/runtime/serve.js index 0bbe23b5..66f1e7ce 100644 --- a/packages/store/src/server/sqlite/runtime/serve.js +++ b/packages/store/src/server/sqlite/runtime/serve.js @@ -1,19 +1,12 @@ -import Database from "@rcompat/sql/sqlite"; +import ident from "@primate/store/base/ident"; +import wrap from "@primate/store/base/wrap"; +import { name } from "@primate/store/sqlite/common"; import { numeric } from "rcompat/invariant"; -import Pool from "../../../pool/exports.js"; -import wrap from "../../../wrap.js"; -import ident from "../../ident.js"; import Facade from "./Facade.js"; +import { connect } from "./driver.js"; -const name = "sqlite"; - -export default database => () => { - const pool = new Pool({ - manager: { - new: () => new Database(database, { create: true }), - kill: connection => connection.close(), - }, - }); +export default options => async () => { + const client = await connect(options); const types = { primary: { @@ -73,7 +66,7 @@ export default database => () => { types, async transact(stores) { return async (others, next) => { - const connection = await pool.acquire(); + const connection = await client.acquire(); const facade = new Facade(connection); try { connection.prepare("begin transaction").run(); @@ -88,7 +81,7 @@ export default database => () => { throw error; } finally { // noop, no end transaction - pool.release(connection); + client.release(connection); } }; }, diff --git a/packages/store/src/server/surrealdb/common.js b/packages/store/src/server/surrealdb/common.js new file mode 100644 index 00000000..89675895 --- /dev/null +++ b/packages/store/src/server/surrealdb/common.js @@ -0,0 +1,9 @@ +export const dependencies = ["surrealdb.js"]; + +export const defaults = { + host: "http://localhost", + port: 8000, + path: "rpc", +}; + +export const name = "surrealdb"; diff --git a/packages/store/src/server/surrealdb/default/build.js b/packages/store/src/server/surrealdb/default/build.js index 88ef65e8..6d14f2ce 100644 --- a/packages/store/src/server/surrealdb/default/build.js +++ b/packages/store/src/server/surrealdb/default/build.js @@ -1,7 +1,6 @@ -import depend from "../../../depend.js"; +import depend from "@primate/store/base/depend"; +import { dependencies, name } from "@primate/store/surrealdb/common"; -const dependencies = ["surrealdb.js"]; - -export default name => async () => { +export default async () => { await depend(dependencies, name); }; diff --git a/packages/store/src/server/surrealdb/default/index.js b/packages/store/src/server/surrealdb/default/index.js index cb310e62..988093b4 100644 --- a/packages/store/src/server/surrealdb/default/index.js +++ b/packages/store/src/server/surrealdb/default/index.js @@ -1,12 +1,6 @@ -import build from "./build.js"; +import { defaults } from "@primate/store/surrealdb/common"; import serve from "../runtime/serve.js"; - -const defaults = { - host: "http://localhost", - port: 8000, - path: "rpc", -}; -const name = "surrealdb"; +import build from "./build.js"; export default ({ host = defaults.host, @@ -17,6 +11,6 @@ export default ({ username, password, } = {}) => ({ - build: build(name), + build, serve: serve({ host, port, path, namespace, database, username, password }), }); diff --git a/packages/store/src/server/surrealdb/runtime/Facade.js b/packages/store/src/server/surrealdb/runtime/Facade.js index 73d35806..59ff0833 100644 --- a/packages/store/src/server/surrealdb/runtime/Facade.js +++ b/packages/store/src/server/surrealdb/runtime/Facade.js @@ -1,6 +1,6 @@ +import make_sort from "@primate/store/sql/make-sort"; import * as O from "rcompat/object"; import typemap from "./typemap.js"; -import { make_sort } from "../../sql/exports.js"; const null_to_undefined = delta => O.valmap(delta, value => value === null ? undefined : value); diff --git a/packages/store/src/server/surrealdb/runtime/driver.js b/packages/store/src/server/surrealdb/runtime/driver.js new file mode 100644 index 00000000..01a55aee --- /dev/null +++ b/packages/store/src/server/surrealdb/runtime/driver.js @@ -0,0 +1,25 @@ +import { Surreal } from "surrealdb.js"; + +export const connect = async ({ + host, + port, + path, + username, + password, + namespace, + database, +}) => { + const auth = username !== undefined && password !== undefined ? + { + username, + password, + } + : {}; + const url = `${host}:${port}/${path}`; + const options = { namespace, database, auth }; + + const client = new Surreal(); + await client.connect(url, options); + + return client; +}; diff --git a/packages/store/src/server/surrealdb/runtime/index.js b/packages/store/src/server/surrealdb/runtime/index.js index 55c15285..e6ba1006 100644 --- a/packages/store/src/server/surrealdb/runtime/index.js +++ b/packages/store/src/server/surrealdb/runtime/index.js @@ -1,11 +1,6 @@ +import { defaults } from "@primate/store/surrealdb/common"; import serve from "./serve.js"; -const defaults = { - host: "http://localhost", - port: 8000, - path: "rpc", -}; - export default ({ host = defaults.host, port = defaults.port, diff --git a/packages/store/src/server/surrealdb/runtime/serve.js b/packages/store/src/server/surrealdb/runtime/serve.js index d983b4e5..78c8bf00 100644 --- a/packages/store/src/server/surrealdb/runtime/serve.js +++ b/packages/store/src/server/surrealdb/runtime/serve.js @@ -1,29 +1,11 @@ -import { Surreal } from "surrealdb.js"; -import wrap from "../../../wrap.js"; -import ident from "../../ident.js"; +import ident from "@primate/store/base/ident"; +import wrap from "@primate/store/base/wrap"; +import { name } from "@primate/store/surrealdb/common"; import Facade from "./Facade.js"; +import { connect } from "./driver.js"; -const name = "surrealdb"; - -export default ({ - host, - port, - path, - namespace, - database, - username, - password, -} = {}) => async _ => { - const client = new Surreal(); - - const url = `${host}:${port}/${path}`; - const auth = username !== undefined && password !== undefined ? - { - username, - password, - } - : {}; - await client.connect(url, { namespace, database, auth }); +export default options => async () => { + const client = await connect(options); const types = { primary: { diff --git a/packages/store/src/server/surrealdb/runtime/serve.spec.js b/packages/store/src/server/surrealdb/runtime/serve.spec.js index 6482f192..c5498892 100644 --- a/packages/store/src/server/surrealdb/runtime/serve.spec.js +++ b/packages/store/src/server/surrealdb/runtime/serve.spec.js @@ -1,5 +1,5 @@ +import base from "@primate/store/base/test"; import serve from "./serve.js"; -import base from "../../base.test.js"; export default async test => base(test, () => serve({ diff --git a/packages/store/src/server/sql/make-sort.js b/packages/store/src/sql/make-sort.js similarity index 100% rename from packages/store/src/server/sql/make-sort.js rename to packages/store/src/sql/make-sort.js