@aiao/rxdb-adapter-pglite 0.0.1 → 0.0.2
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/index.js +55 -55
- package/package.json +3 -3
package/index.js
CHANGED
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
import { RxDBError as
|
|
2
|
-
import { Observable as
|
|
3
|
-
import { isFunction as
|
|
4
|
-
import { PGlite as
|
|
5
|
-
class
|
|
1
|
+
import { RxDBError as b, PropertyType as r, TransactionBeginEvent as f, TransactionCommitEvent as w, TransactionRollbackEvent as x, RxDBAdapterLocalBase as $, getEntityMetadata as T } from "@aiao/rxdb";
|
|
2
|
+
import { Observable as g, shareReplay as _, switchMap as u, from as y, map as m, firstValueFrom as p } from "rxjs";
|
|
3
|
+
import { isFunction as R, isString as q } from "@aiao/utils";
|
|
4
|
+
import { PGlite as v } from "@electric-sql/pglite";
|
|
5
|
+
class a extends b {
|
|
6
6
|
}
|
|
7
7
|
const A = (s) => {
|
|
8
8
|
switch (s.type) {
|
|
9
|
-
case
|
|
9
|
+
case r.uuid:
|
|
10
10
|
return "uuid";
|
|
11
|
-
case
|
|
11
|
+
case r.string:
|
|
12
12
|
return "varchar";
|
|
13
|
-
case
|
|
13
|
+
case r.json:
|
|
14
14
|
return "jsonb";
|
|
15
|
-
case
|
|
15
|
+
case r.number:
|
|
16
16
|
return "numeric";
|
|
17
|
-
case
|
|
17
|
+
case r.integer:
|
|
18
18
|
return "integer";
|
|
19
|
-
case
|
|
19
|
+
case r.boolean:
|
|
20
20
|
return "boolean";
|
|
21
|
-
case
|
|
21
|
+
case r.date:
|
|
22
22
|
return "timestamptz";
|
|
23
23
|
}
|
|
24
24
|
}, L = (s) => {
|
|
25
25
|
switch (s.type) {
|
|
26
|
-
case
|
|
26
|
+
case r.uuid:
|
|
27
27
|
return "uuid_ops";
|
|
28
|
-
case
|
|
28
|
+
case r.string:
|
|
29
29
|
return "bpchar_ops";
|
|
30
|
-
case
|
|
30
|
+
case r.json:
|
|
31
31
|
return "jsonb_ops";
|
|
32
|
-
case
|
|
32
|
+
case r.number:
|
|
33
33
|
return "numeric_ops";
|
|
34
|
-
case
|
|
34
|
+
case r.integer:
|
|
35
35
|
return "int4_ops";
|
|
36
36
|
default:
|
|
37
|
-
throw new
|
|
37
|
+
throw new a(`rxDBColumnTypeToPGliteTypeIndexName: type '${s.type}' not support`);
|
|
38
38
|
}
|
|
39
39
|
}, C = (s, e) => `idx_${s.namespace}_${s.name}_${e.name}`, D = (s, e) => {
|
|
40
40
|
let t = `CREATE TABLE ${e.namespace}."${e.name}" (`;
|
|
41
|
-
const
|
|
41
|
+
const i = [], l = [], h = [];
|
|
42
42
|
e.propertyMap.forEach((n) => {
|
|
43
43
|
let o = "";
|
|
44
44
|
o += `"${n.name}"`;
|
|
45
|
-
const
|
|
46
|
-
if (n.primary ? n.type ===
|
|
45
|
+
const E = A(n);
|
|
46
|
+
if (n.primary ? n.type === r.integer ? o += " serial PRIMARY KEY" : o += ` ${E} PRIMARY KEY` : o += ` ${E}`, !n.nullable) o += " NOT NULL";
|
|
47
47
|
else if (Reflect.get(n, "default") !== void 0) {
|
|
48
|
-
let
|
|
49
|
-
|
|
48
|
+
let c = n.default;
|
|
49
|
+
R(n.default) && (c = n.default()), q(c) ? ["CURRENT_TIMESTAMP", "now()"].includes(c) ? o += " DEFAULT now()" : o += ` DEFAULT '${c}'` : o += ` DEFAULT ${String(c)}`;
|
|
50
50
|
}
|
|
51
|
-
n.unique &&
|
|
51
|
+
n.unique && i.push(
|
|
52
52
|
`CREATE UNIQUE INDEX "${C(e, n)}" on ${e.namespace}."${e.name}" ("${n.name}" ${L(n)});`
|
|
53
|
-
),
|
|
53
|
+
), h.push(o);
|
|
54
54
|
});
|
|
55
|
-
const
|
|
56
|
-
if (
|
|
57
|
-
t +=
|
|
55
|
+
const d = [...h, ...l];
|
|
56
|
+
if (d.length)
|
|
57
|
+
t += d.map((n) => `
|
|
58
58
|
${n}`).join(","), t += `
|
|
59
59
|
);`;
|
|
60
60
|
else
|
|
61
|
-
throw new
|
|
62
|
-
return
|
|
63
|
-
` +
|
|
61
|
+
throw new a("columns is empty!");
|
|
62
|
+
return i.length && (t += `
|
|
63
|
+
` + i.join(`
|
|
64
64
|
`)), t;
|
|
65
65
|
}, M = (s, e) => "", P = (s, e) => {
|
|
66
66
|
let t = "";
|
|
@@ -69,7 +69,7 @@ ${n}`).join(","), t += `
|
|
|
69
69
|
class N {
|
|
70
70
|
#e;
|
|
71
71
|
async init(e, t) {
|
|
72
|
-
return this.#e = new
|
|
72
|
+
return this.#e = new v({
|
|
73
73
|
dataDir: t.store === "memory" ? void 0 : `idb://${e}`,
|
|
74
74
|
database: e
|
|
75
75
|
}), this.#e.waitReady;
|
|
@@ -80,8 +80,8 @@ class N {
|
|
|
80
80
|
exec(e, t) {
|
|
81
81
|
return this.#e.exec(e, t);
|
|
82
82
|
}
|
|
83
|
-
query(e, t,
|
|
84
|
-
return this.#e.query(e, t,
|
|
83
|
+
query(e, t, i) {
|
|
84
|
+
return this.#e.query(e, t, i);
|
|
85
85
|
}
|
|
86
86
|
describeQuery(e, t) {
|
|
87
87
|
return this.#e.describeQuery(e, t);
|
|
@@ -93,78 +93,78 @@ class N {
|
|
|
93
93
|
return this.#e.runExclusive(e);
|
|
94
94
|
}
|
|
95
95
|
disconnect() {
|
|
96
|
-
throw new
|
|
96
|
+
throw new a("Method not implemented.");
|
|
97
97
|
}
|
|
98
98
|
async version() {
|
|
99
99
|
return (await this.#e.query("SELECT version()")).rows[0].version;
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
|
-
class U extends
|
|
102
|
+
class U extends $ {
|
|
103
103
|
constructor(e, t) {
|
|
104
104
|
super(e), this.options = t;
|
|
105
105
|
}
|
|
106
|
-
#e = new
|
|
106
|
+
#e = new g((e) => {
|
|
107
107
|
const t = new N();
|
|
108
108
|
t.init(this.rxdb.options.dbName, this.options).then(() => e.next(t));
|
|
109
|
-
}).pipe(
|
|
109
|
+
}).pipe(_(1));
|
|
110
110
|
/**
|
|
111
111
|
* 数据库版本
|
|
112
112
|
*/
|
|
113
113
|
version$ = this.#e.pipe(
|
|
114
|
-
|
|
115
|
-
|
|
114
|
+
u((e) => y(e.version())),
|
|
115
|
+
_(1)
|
|
116
116
|
);
|
|
117
117
|
name = S;
|
|
118
118
|
connect() {
|
|
119
|
-
return this.#e.pipe(
|
|
119
|
+
return this.#e.pipe(m(() => this));
|
|
120
120
|
}
|
|
121
121
|
disconnect() {
|
|
122
|
-
throw new
|
|
122
|
+
throw new a("Method disconnect not implemented.");
|
|
123
123
|
}
|
|
124
124
|
/**
|
|
125
125
|
* 获取版本
|
|
126
126
|
*/
|
|
127
127
|
version() {
|
|
128
|
-
return
|
|
128
|
+
return p(this.version$);
|
|
129
129
|
}
|
|
130
130
|
getRepository(e) {
|
|
131
|
-
throw new
|
|
131
|
+
throw new a("Method getRepository not implemented.");
|
|
132
132
|
}
|
|
133
133
|
createTable(e) {
|
|
134
|
-
const t =
|
|
135
|
-
return t.log,
|
|
134
|
+
const t = T(e), i = P(this, t);
|
|
135
|
+
return t.log, p(this.#n(i).pipe(m(() => !0)));
|
|
136
136
|
}
|
|
137
137
|
async isTableExisted(e) {
|
|
138
|
-
return
|
|
138
|
+
return p(this.isTableExisted$(e));
|
|
139
139
|
}
|
|
140
140
|
/**
|
|
141
141
|
* 判断表是否存在
|
|
142
142
|
* @param EntityType
|
|
143
143
|
*/
|
|
144
144
|
isTableExisted$(e) {
|
|
145
|
-
const t =
|
|
145
|
+
const t = T(e);
|
|
146
146
|
return this.#t(
|
|
147
147
|
`SELECT EXISTS (
|
|
148
148
|
SELECT 1
|
|
149
149
|
FROM information_schema.tables
|
|
150
150
|
WHERE table_schema = '${t.namespace}'
|
|
151
151
|
AND table_name = '${t.name}');`
|
|
152
|
-
).pipe(
|
|
152
|
+
).pipe(m((i) => i.rows[0]?.exists));
|
|
153
153
|
}
|
|
154
154
|
transaction(e) {
|
|
155
|
-
throw new
|
|
155
|
+
throw new a("Method transaction not implemented.");
|
|
156
156
|
}
|
|
157
|
-
#t(e, t,
|
|
158
|
-
return this.#e.pipe(
|
|
157
|
+
#t(e, t, i) {
|
|
158
|
+
return this.#e.pipe(u((l) => l.query(e, t, i)));
|
|
159
159
|
}
|
|
160
160
|
#n(e, t) {
|
|
161
|
-
return this.#e.pipe(
|
|
161
|
+
return this.#e.pipe(u((i) => i.exec(e, t)));
|
|
162
162
|
}
|
|
163
163
|
}
|
|
164
|
-
new b();
|
|
165
164
|
new f();
|
|
166
165
|
new w();
|
|
166
|
+
new x();
|
|
167
167
|
export {
|
|
168
168
|
U as RxDBAdapterPGlite,
|
|
169
|
-
|
|
169
|
+
a as RxdbAdapterPGliteError
|
|
170
170
|
};
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aiao/rxdb-adapter-pglite",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"types": "./index.d.ts",
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"@electric-sql/pglite": "^0.3.6",
|
|
9
|
-
"@aiao/rxdb": "0.0.
|
|
9
|
+
"@aiao/rxdb": "0.0.2",
|
|
10
10
|
"rxjs": "~7.8.2",
|
|
11
|
-
"@aiao/utils": "0.0.
|
|
11
|
+
"@aiao/utils": "0.0.2"
|
|
12
12
|
},
|
|
13
13
|
"publishConfig": {
|
|
14
14
|
"access": "public"
|