@primate/postgresql 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) Terrablue <terrablue@proton.me> and contributors.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@primate/postgresql",
3
+ "version": "0.1.0",
4
+ "description": "Primate PostgreSQL database",
5
+ "homepage": "https://primatejs.com/modules/postgresql",
6
+ "bugs": "https://github.com/primatejs/primate/issues",
7
+ "license": "MIT",
8
+ "files": [
9
+ "src/**/*.js",
10
+ "!src/**/*.spec.js"
11
+ ],
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/primatejs/primate",
15
+ "directory": "packages/postgresql"
16
+ },
17
+ "dependencies": {
18
+ "@rcompat/invariant": "^0.5.0",
19
+ "@rcompat/object": "^0.5.0",
20
+ "@primate/core": "^0.1.0",
21
+ "@primate/store": "^0.25.0"
22
+ },
23
+ "devDependencies": {
24
+ "postgres": "^3.4.4"
25
+ },
26
+ "peerDependencies": {
27
+ "postgres": "3"
28
+ },
29
+ "type": "module",
30
+ "imports": {
31
+ "#*": {
32
+ "@primate/lt": "./src/private/*.js",
33
+ "default": "./src/private/*.js"
34
+ }
35
+ },
36
+ "exports": {
37
+ ".": "./src/default.js"
38
+ }
39
+ }
package/src/default.js ADDED
@@ -0,0 +1,12 @@
1
+ import defaults from "#defaults";
2
+ import serve from "#serve";
3
+
4
+ export default ({
5
+ host = defaults.host,
6
+ port = defaults.port,
7
+ database,
8
+ username,
9
+ password,
10
+ } = {}) => ({
11
+ serve: serve({ host, port, database, username, password }),
12
+ });
@@ -0,0 +1,97 @@
1
+ import typemap from "#typemap";
2
+ import make_sort from "@primate/store/sql/make-sort";
3
+ import filter from "@rcompat/object/filter";
4
+ import valmap from "@rcompat/object/valmap";
5
+
6
+ const filter_null = object => filter(object, ([, value]) => value !== null);
7
+ const filter_nulls = objects => objects.map(object => filter_null(object));
8
+
9
+ export default class Connection {
10
+ schema = {
11
+ create: async (name, description) => {
12
+ const { connection } = this;
13
+ const body =
14
+ Object.entries(valmap(description, value => typemap(value.base)))
15
+ .map(([column, dataType]) => `"${column}" ${dataType}`).join(",");
16
+ await connection`
17
+ create table if not exists
18
+ ${connection(name)} (${connection.unsafe(body)})
19
+ `;
20
+ },
21
+ delete: async name => {
22
+ const { connection } = this;
23
+ await connection`drop table if exists ${connection(name)};`;
24
+ },
25
+ };
26
+
27
+ constructor(connection) {
28
+ this.connection = connection;
29
+ }
30
+
31
+ async find(collection, criteria = {}, projection = [], options = {}) {
32
+ const { connection } = this;
33
+ const rest = make_sort(options);
34
+ const select = projection.length === 0 ? "*" : projection.join(", ");
35
+ return filter_nulls(await connection`
36
+ select ${connection.unsafe(select)}
37
+ from ${connection(collection)}
38
+ where ${Object.entries(criteria).reduce((acc, [key, value]) =>
39
+ connection`${acc} and ${connection(key)} = ${value}`, connection`true`)}
40
+ ${connection.unsafe(rest)}`);
41
+ }
42
+
43
+ async count(collection, criteria = {}) {
44
+ const { connection } = this;
45
+ const [{ count }] = await connection`
46
+ select count(*)
47
+ from ${connection(collection)}
48
+ where ${Object.entries(criteria).reduce((acc, [key, value]) =>
49
+ connection`${acc} and ${connection(key)} = ${value}`, connection`true`)}
50
+ `;
51
+ return Number(count);
52
+ }
53
+
54
+ async get(collection, primary, value) {
55
+ const { connection } = this;
56
+ const [result] = await connection`
57
+ select *
58
+ from ${connection(collection)}
59
+ where ${connection(primary)}=${value};
60
+ `;
61
+ return result === undefined ? result : filter_null(result);
62
+ }
63
+
64
+ async insert(collection, primary, document) {
65
+ const { connection } = this;
66
+ const columns = Object.keys(document);
67
+ const [result] = await this.connection`insert into
68
+ ${connection(collection)}
69
+ ${columns.length > 0
70
+ ? connection`(${connection(columns)}) values ${connection(document)}`
71
+ : connection.unsafe("default values")}
72
+ returning *;
73
+ `;
74
+ return filter_null(result);
75
+ }
76
+
77
+ async update(collection, criteria = {}, delta = {}) {
78
+ const { connection } = this;
79
+ return (await connection`
80
+ update ${connection(collection)}
81
+ set ${connection({ ...delta })}
82
+ where ${Object.entries(criteria).reduce((acc, [key, value]) =>
83
+ connection`${acc} and ${connection(key)} = ${value}`, connection`true`)}
84
+ returning *;
85
+ `).length;
86
+ }
87
+
88
+ async delete(collection, criteria = {}) {
89
+ const { connection } = this;
90
+ return (await connection`
91
+ delete from ${connection(collection)}
92
+ where ${Object.entries(criteria).reduce((acc, [key, value]) =>
93
+ connection`${acc} and ${connection(key)} = ${value}`, connection`true`)}
94
+ returning *;
95
+ `).length;
96
+ }
97
+ }
@@ -0,0 +1,10 @@
1
+ import Driver from "postgres";
2
+
3
+ export default ({ host, port, database, username, password }) =>
4
+ new Driver({
5
+ host,
6
+ port,
7
+ db: database,
8
+ user: username,
9
+ pass: password,
10
+ });
@@ -0,0 +1,4 @@
1
+ export default {
2
+ host: "localhost",
3
+ port: 5432,
4
+ };
@@ -0,0 +1,63 @@
1
+ import Facade from "#Facade";
2
+ import connect from "#connect";
3
+ import ident from "@primate/store/core/ident";
4
+ import wrap from "@primate/store/core/wrap";
5
+ import numeric from "@rcompat/invariant/numeric";
6
+
7
+ export default options => async () => {
8
+ const client = connect(options);
9
+
10
+ const types = {
11
+ primary: {
12
+ validate(value) {
13
+ if (typeof value === "number" || numeric(value)) {
14
+ return Number(value);
15
+ }
16
+ throw new Error(`\`${value}\` is not a valid primary key value`);
17
+ },
18
+ ...ident,
19
+ },
20
+ object: {
21
+ in(value) {
22
+ return JSON.stringify(value);
23
+ },
24
+ out(value) {
25
+ return JSON.parse(value);
26
+ },
27
+ },
28
+ number: ident,
29
+ bigint: {
30
+ in(value) {
31
+ return value.toString();
32
+ },
33
+ out(value) {
34
+ return BigInt(value);
35
+ },
36
+ },
37
+ boolean: ident,
38
+ date: {
39
+ in(value) {
40
+ return value;
41
+ },
42
+ out(value) {
43
+ return new Date(value);
44
+ },
45
+ },
46
+ string: ident,
47
+ };
48
+
49
+ return {
50
+ name: "@primate/postgresql",
51
+ types,
52
+ async transact(stores) {
53
+ return (others, next) =>
54
+ client.begin(async connection => {
55
+ const facade = new Facade(connection);
56
+ return next([
57
+ ...others, ...stores.map(([name, store]) =>
58
+ [name, wrap(store, facade, types)]),
59
+ ]);
60
+ });
61
+ },
62
+ };
63
+ };
@@ -0,0 +1,21 @@
1
+ const types = {
2
+ /* array */
3
+ blob: "bytea",
4
+ boolean: "boolean",
5
+ datetime: "timestamp",
6
+ embedded: "text",
7
+ f64: "real",
8
+ i8: "smallint",
9
+ i16: "integer",
10
+ i32: "bigint",
11
+ i64: "decimal",
12
+ json: "json",
13
+ primary: "serial primary key",
14
+ string: "text",
15
+ time: "time",
16
+ u8: "smallint",
17
+ u16: "integer",
18
+ u32: "integer",
19
+ };
20
+
21
+ export default value => types[value];