@xyo-network/archivist-firebase 3.2.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 +165 -0
- package/README.md +13 -0
- package/dist/browser/Archivist.d.ts +16 -0
- package/dist/browser/Archivist.d.ts.map +1 -0
- package/dist/browser/Config.d.ts +10 -0
- package/dist/browser/Config.d.ts.map +1 -0
- package/dist/browser/Params.d.ts +8 -0
- package/dist/browser/Params.d.ts.map +1 -0
- package/dist/browser/Schema.d.ts +3 -0
- package/dist/browser/Schema.d.ts.map +1 -0
- package/dist/browser/index.d.ts +5 -0
- package/dist/browser/index.d.ts.map +1 -0
- package/dist/browser/index.mjs +86 -0
- package/dist/browser/index.mjs.map +1 -0
- package/package.json +59 -0
- package/src/Archivist.ts +84 -0
- package/src/Config.ts +12 -0
- package/src/Params.ts +9 -0
- package/src/Schema.ts +2 -0
- package/src/index.ts +4 -0
- package/src/spec/firestore.rules +14 -0
- package/typedoc.json +5 -0
- package/xy.config.ts +10 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
6
|
+
of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
11
|
+
License, supplemented by the additional permissions listed below.
|
|
12
|
+
|
|
13
|
+
0. Additional Definitions.
|
|
14
|
+
|
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
17
|
+
General Public License.
|
|
18
|
+
|
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
|
20
|
+
other than an Application or a Combined Work as defined below.
|
|
21
|
+
|
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
25
|
+
of using an interface provided by the Library.
|
|
26
|
+
|
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
+
Application with the Library. The particular version of the Library
|
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
|
30
|
+
Version".
|
|
31
|
+
|
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
+
based on the Application, and not on the Linked Version.
|
|
36
|
+
|
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
+
object code and/or source code for the Application, including any data
|
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
+
|
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
43
|
+
|
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
|
46
|
+
|
|
47
|
+
2. Conveying Modified Versions.
|
|
48
|
+
|
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
|
51
|
+
that uses the facility (other than as an argument passed when the
|
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
|
53
|
+
version:
|
|
54
|
+
|
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
|
56
|
+
ensure that, in the event an Application does not supply the
|
|
57
|
+
function or data, the facility still operates, and performs
|
|
58
|
+
whatever part of its purpose remains meaningful, or
|
|
59
|
+
|
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
+
this License applicable to that copy.
|
|
62
|
+
|
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
+
|
|
65
|
+
The object code form of an Application may incorporate material from
|
|
66
|
+
a header file that is part of the Library. You may convey such object
|
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
|
68
|
+
material is not limited to numerical parameters, data structure
|
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
|
71
|
+
|
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
|
73
|
+
Library is used in it and that the Library and its use are
|
|
74
|
+
covered by this License.
|
|
75
|
+
|
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
+
document.
|
|
78
|
+
|
|
79
|
+
4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
|
82
|
+
taken together, effectively do not restrict modification of the
|
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
|
85
|
+
the following:
|
|
86
|
+
|
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
+
the Library is used in it and that the Library and its use are
|
|
89
|
+
covered by this License.
|
|
90
|
+
|
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
+
document.
|
|
93
|
+
|
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
|
95
|
+
execution, include the copyright notice for the Library among
|
|
96
|
+
these notices, as well as a reference directing the user to the
|
|
97
|
+
copies of the GNU GPL and this license document.
|
|
98
|
+
|
|
99
|
+
d) Do one of the following:
|
|
100
|
+
|
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
|
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
+
a copy of the Library already present on the user's computer
|
|
112
|
+
system, and (b) will operate properly with a modified version
|
|
113
|
+
of the Library that is interface-compatible with the Linked
|
|
114
|
+
Version.
|
|
115
|
+
|
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
+
be required to provide such information under section 6 of the
|
|
118
|
+
GNU GPL, and only to the extent that such information is
|
|
119
|
+
necessary to install and execute a modified version of the
|
|
120
|
+
Combined Work produced by recombining or relinking the
|
|
121
|
+
Application with a modified version of the Linked Version. (If
|
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
+
for conveying Corresponding Source.)
|
|
127
|
+
|
|
128
|
+
5. Combined Libraries.
|
|
129
|
+
|
|
130
|
+
You may place library facilities that are a work based on the
|
|
131
|
+
Library side by side in a single library together with other library
|
|
132
|
+
facilities that are not Applications and are not covered by this
|
|
133
|
+
License, and convey such a combined library under terms of your
|
|
134
|
+
choice, if you do both of the following:
|
|
135
|
+
|
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
|
137
|
+
on the Library, uncombined with any other library facilities,
|
|
138
|
+
conveyed under the terms of this License.
|
|
139
|
+
|
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
|
141
|
+
is a work based on the Library, and explaining where to find the
|
|
142
|
+
accompanying uncombined form of the same work.
|
|
143
|
+
|
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
+
|
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
+
versions will be similar in spirit to the present version, but may
|
|
149
|
+
differ in detail to address new problems or concerns.
|
|
150
|
+
|
|
151
|
+
Each version is given a distinguishing version number. If the
|
|
152
|
+
Library as you received it specifies that a certain numbered version
|
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
|
154
|
+
applies to it, you have the option of following the terms and
|
|
155
|
+
conditions either of that published version or of any later version
|
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
|
160
|
+
|
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
+
permanent authorization for you to choose that version for the
|
|
165
|
+
Library.
|
package/README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
[![logo][]](https://xyo.network)
|
|
2
|
+
|
|
3
|
+
Part of [sdk-xyo-client-js](https://www.npmjs.com/package/@xyo-network/sdk-xyo-client-js)
|
|
4
|
+
|
|
5
|
+
## License
|
|
6
|
+
|
|
7
|
+
> See the [LICENSE](LICENSE) file for license details
|
|
8
|
+
|
|
9
|
+
## Credits
|
|
10
|
+
|
|
11
|
+
[Made with 🔥 and ❄️ by XYO](https://xyo.network)
|
|
12
|
+
|
|
13
|
+
[logo]: https://cdn.xy.company/img/brand/XYO_full_colored.png
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Hash } from '@xylabs/hex';
|
|
2
|
+
import { AbstractArchivist } from '@xyo-network/archivist-abstract';
|
|
3
|
+
import { ArchivistModuleEventData } from '@xyo-network/archivist-model';
|
|
4
|
+
import { Payload, Schema, WithMeta } from '@xyo-network/payload-model';
|
|
5
|
+
import { FirebaseArchivistParams } from './Params.ts';
|
|
6
|
+
export declare class FirebaseArchivist<TParams extends FirebaseArchivistParams = FirebaseArchivistParams, TEventData extends ArchivistModuleEventData = ArchivistModuleEventData> extends AbstractArchivist<TParams, TEventData> {
|
|
7
|
+
static readonly configSchemas: Schema[];
|
|
8
|
+
static readonly defaultConfigSchema: Schema;
|
|
9
|
+
get queries(): string[];
|
|
10
|
+
private get collection();
|
|
11
|
+
private get firebaseApp();
|
|
12
|
+
private get firestore();
|
|
13
|
+
protected getHandler(hashes: Hash[]): Promise<WithMeta<Payload>[]>;
|
|
14
|
+
protected insertHandler(payloads: Payload[]): Promise<WithMeta<Payload>[]>;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=Archivist.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Archivist.d.ts","sourceRoot":"","sources":["../../src/Archivist.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AAClC,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAA;AACnE,OAAO,EAKL,wBAAwB,EAEzB,MAAM,8BAA8B,CAAA;AAGrC,OAAO,EACL,OAAO,EAAE,MAAM,EACf,QAAQ,EACT,MAAM,4BAA4B,CAAA;AAGnC,OAAO,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAA;AAErD,qBACa,iBAAiB,CAC5B,OAAO,SAAS,uBAAuB,GAAG,uBAAuB,EACjE,UAAU,SAAS,wBAAwB,GAAG,wBAAwB,CACtE,SAAQ,iBAAiB,CAAC,OAAO,EAAE,UAAU,CAAC;IAC9C,gBAAyB,aAAa,EAAE,MAAM,EAAE,CAA0D;IAC1G,gBAAyB,mBAAmB,EAAE,MAAM,CAAgC;IAEpF,IAAa,OAAO,aASnB;IAED,OAAO,KAAK,UAAU,GAErB;IAED,OAAO,KAAK,WAAW,GAEtB;IAED,OAAO,KAAK,SAAS,GAEpB;cAEwB,UAAU,CAAC,MAAM,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;cAYxD,aAAa,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;CAW1F"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ArchivistConfig } from '@xyo-network/archivist-model';
|
|
2
|
+
import { FirebaseArchivistSchema } from './Schema.ts';
|
|
3
|
+
export type FirebaseArchivistConfigSchema = `${FirebaseArchivistSchema}.config`;
|
|
4
|
+
export declare const FirebaseArchivistConfigSchema: FirebaseArchivistConfigSchema;
|
|
5
|
+
export type FirebaseArchivistConfig = ArchivistConfig<{
|
|
6
|
+
collection?: string;
|
|
7
|
+
dbId?: string;
|
|
8
|
+
schema: FirebaseArchivistConfigSchema;
|
|
9
|
+
}>;
|
|
10
|
+
//# sourceMappingURL=Config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Config.d.ts","sourceRoot":"","sources":["../../src/Config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAA;AAEnE,OAAO,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAA;AAErD,MAAM,MAAM,6BAA6B,GAAG,GAAG,uBAAuB,SAAS,CAAA;AAC/E,eAAO,MAAM,6BAA6B,EAAE,6BAAmE,CAAA;AAE/G,MAAM,MAAM,uBAAuB,GAAG,eAAe,CAAC;IACpD,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,6BAA6B,CAAA;CACtC,CAAC,CAAA"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { FirebaseApp } from '@firebase/app';
|
|
2
|
+
import type { ArchivistParams } from '@xyo-network/archivist-model';
|
|
3
|
+
import type { AnyConfigSchema } from '@xyo-network/module-model';
|
|
4
|
+
import type { FirebaseArchivistConfig } from './Config.ts';
|
|
5
|
+
export type FirebaseArchivistParams = ArchivistParams<AnyConfigSchema<FirebaseArchivistConfig>, {
|
|
6
|
+
firebaseApp?: FirebaseApp;
|
|
7
|
+
}>;
|
|
8
|
+
//# sourceMappingURL=Params.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Params.d.ts","sourceRoot":"","sources":["../../src/Params.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAChD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAA;AACnE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAA;AAEhE,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAA;AAE1D,MAAM,MAAM,uBAAuB,GAAG,eAAe,CAAC,eAAe,CAAC,uBAAuB,CAAC,EAAE;IAC9F,WAAW,CAAC,EAAE,WAAW,CAAA;CAC1B,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../../src/Schema.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,uBAAuB,mCAAmC,CAAA;AACvE,MAAM,MAAM,uBAAuB,GAAG,OAAO,uBAAuB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA;AAC9B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/Archivist.ts
|
|
5
|
+
import { collection, doc, getDoc, getFirestore, setDoc } from "@firebase/firestore";
|
|
6
|
+
import { assertEx } from "@xylabs/assert";
|
|
7
|
+
import { exists } from "@xylabs/exists";
|
|
8
|
+
import { AbstractArchivist } from "@xyo-network/archivist-abstract";
|
|
9
|
+
import { ArchivistAllQuerySchema, ArchivistClearQuerySchema, ArchivistDeleteQuerySchema, ArchivistInsertQuerySchema, ArchivistNextQuerySchema } from "@xyo-network/archivist-model";
|
|
10
|
+
import { creatableModule } from "@xyo-network/module-model";
|
|
11
|
+
import { PayloadBuilder } from "@xyo-network/payload-builder";
|
|
12
|
+
|
|
13
|
+
// src/Schema.ts
|
|
14
|
+
var FirebaseArchivistSchema = "network.xyo.archivist.firebase";
|
|
15
|
+
|
|
16
|
+
// src/Config.ts
|
|
17
|
+
var FirebaseArchivistConfigSchema = `${FirebaseArchivistSchema}.config`;
|
|
18
|
+
|
|
19
|
+
// src/Archivist.ts
|
|
20
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
21
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
22
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
23
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
24
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
25
|
+
}
|
|
26
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
27
|
+
var FirebaseArchivist = class extends AbstractArchivist {
|
|
28
|
+
static {
|
|
29
|
+
__name(this, "FirebaseArchivist");
|
|
30
|
+
}
|
|
31
|
+
static configSchemas = [
|
|
32
|
+
...super.configSchemas,
|
|
33
|
+
FirebaseArchivistConfigSchema
|
|
34
|
+
];
|
|
35
|
+
static defaultConfigSchema = FirebaseArchivistConfigSchema;
|
|
36
|
+
get queries() {
|
|
37
|
+
return [
|
|
38
|
+
ArchivistNextQuerySchema,
|
|
39
|
+
ArchivistAllQuerySchema,
|
|
40
|
+
ArchivistClearQuerySchema,
|
|
41
|
+
ArchivistDeleteQuerySchema,
|
|
42
|
+
ArchivistInsertQuerySchema,
|
|
43
|
+
...super.queries
|
|
44
|
+
];
|
|
45
|
+
}
|
|
46
|
+
get collection() {
|
|
47
|
+
return collection(this.firestore, assertEx(this.config.collection, () => "Missing collection name"));
|
|
48
|
+
}
|
|
49
|
+
get firebaseApp() {
|
|
50
|
+
return assertEx(this.params.firebaseApp, () => "Missing FirebaseApp");
|
|
51
|
+
}
|
|
52
|
+
get firestore() {
|
|
53
|
+
return getFirestore(this.firebaseApp, assertEx(this.config.dbId, () => "no dbId specified"));
|
|
54
|
+
}
|
|
55
|
+
async getHandler(hashes) {
|
|
56
|
+
const payloadCollection = this.collection;
|
|
57
|
+
return (await Promise.all(hashes.map(async (hash) => {
|
|
58
|
+
const docRef = doc(payloadCollection, hash);
|
|
59
|
+
const docSnap = await getDoc(docRef);
|
|
60
|
+
if (!docSnap.exists()) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
return PayloadBuilder.build(docSnap.data());
|
|
64
|
+
}))).filter(exists);
|
|
65
|
+
}
|
|
66
|
+
async insertHandler(payloads) {
|
|
67
|
+
const pairs = await PayloadBuilder.hashPairs(payloads, {
|
|
68
|
+
stamp: false
|
|
69
|
+
});
|
|
70
|
+
const payloadCollection = this.collection;
|
|
71
|
+
return await Promise.all(pairs.map(async ([payload, hash]) => {
|
|
72
|
+
const docRef = doc(payloadCollection, hash);
|
|
73
|
+
await setDoc(docRef, payload);
|
|
74
|
+
return payload;
|
|
75
|
+
}));
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
FirebaseArchivist = _ts_decorate([
|
|
79
|
+
creatableModule()
|
|
80
|
+
], FirebaseArchivist);
|
|
81
|
+
export {
|
|
82
|
+
FirebaseArchivist,
|
|
83
|
+
FirebaseArchivistConfigSchema,
|
|
84
|
+
FirebaseArchivistSchema
|
|
85
|
+
};
|
|
86
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/Archivist.ts","../../src/Schema.ts","../../src/Config.ts"],"sourcesContent":["import {\n collection,\n doc,\n getDoc,\n getFirestore,\n setDoc,\n} from '@firebase/firestore'\nimport { assertEx } from '@xylabs/assert'\nimport { exists } from '@xylabs/exists'\nimport { Hash } from '@xylabs/hex'\nimport { AbstractArchivist } from '@xyo-network/archivist-abstract'\nimport {\n ArchivistAllQuerySchema,\n ArchivistClearQuerySchema,\n ArchivistDeleteQuerySchema,\n ArchivistInsertQuerySchema,\n ArchivistModuleEventData,\n ArchivistNextQuerySchema,\n} from '@xyo-network/archivist-model'\nimport { creatableModule } from '@xyo-network/module-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport {\n Payload, Schema,\n WithMeta,\n} from '@xyo-network/payload-model'\n\nimport { FirebaseArchivistConfigSchema } from './Config.ts'\nimport { FirebaseArchivistParams } from './Params.ts'\n\n@creatableModule()\nexport class FirebaseArchivist<\n TParams extends FirebaseArchivistParams = FirebaseArchivistParams,\n TEventData extends ArchivistModuleEventData = ArchivistModuleEventData,\n> extends AbstractArchivist<TParams, TEventData> {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, FirebaseArchivistConfigSchema]\n static override readonly defaultConfigSchema: Schema = FirebaseArchivistConfigSchema\n\n override get queries() {\n return [\n ArchivistNextQuerySchema,\n ArchivistAllQuerySchema,\n ArchivistClearQuerySchema,\n ArchivistDeleteQuerySchema,\n ArchivistInsertQuerySchema,\n ...super.queries,\n ]\n }\n\n private get collection() {\n return collection(this.firestore, assertEx(this.config.collection, () => 'Missing collection name'))\n }\n\n private get firebaseApp() {\n return assertEx(this.params.firebaseApp, () => 'Missing FirebaseApp')\n }\n\n private get firestore() {\n return getFirestore(this.firebaseApp, assertEx(this.config.dbId, () => 'no dbId specified'))\n }\n\n protected override async getHandler(hashes: Hash[]): Promise<WithMeta<Payload>[]> {\n const payloadCollection = this.collection\n return (await Promise.all(hashes.map(async (hash) => {\n const docRef = doc(payloadCollection, hash)\n const docSnap = await getDoc(docRef)\n if (!docSnap.exists()) {\n return null\n }\n return PayloadBuilder.build(docSnap.data() as Payload)\n }))).filter(exists)\n }\n\n protected override async insertHandler(payloads: Payload[]): Promise<WithMeta<Payload>[]> {\n const pairs = await PayloadBuilder.hashPairs(payloads, { stamp: false })\n const payloadCollection = this.collection\n return await Promise.all(pairs.map(\n async ([payload, hash]) => {\n const docRef = doc(payloadCollection, hash)\n await setDoc(docRef, payload)\n return payload\n },\n ))\n }\n}\n","export const FirebaseArchivistSchema = 'network.xyo.archivist.firebase'\nexport type FirebaseArchivistSchema = typeof FirebaseArchivistSchema\n","import type { ArchivistConfig } from '@xyo-network/archivist-model'\n\nimport { FirebaseArchivistSchema } from './Schema.ts'\n\nexport type FirebaseArchivistConfigSchema = `${FirebaseArchivistSchema}.config`\nexport const FirebaseArchivistConfigSchema: FirebaseArchivistConfigSchema = `${FirebaseArchivistSchema}.config`\n\nexport type FirebaseArchivistConfig = ArchivistConfig<{\n collection?: string\n dbId?: string\n schema: FirebaseArchivistConfigSchema\n}>\n"],"mappings":";;;;AAAA,SACEA,YACAC,KACAC,QACAC,cACAC,cACK;AACP,SAASC,gBAAgB;AACzB,SAASC,cAAc;AAEvB,SAASC,yBAAyB;AAClC,SACEC,yBACAC,2BACAC,4BACAC,4BAEAC,gCACK;AACP,SAASC,uBAAuB;AAChC,SAASC,sBAAsB;;;ACpBxB,IAAMC,0BAA0B;;;ACKhC,IAAMC,gCAA+D,GAAGC,uBAAAA;;;;;;;;;;AFyBxE,IAAMC,oBAAN,cAGGC,kBAAAA;SAAAA;;;EACR,OAAyBC,gBAA0B;OAAI,MAAMA;IAAeC;;EAC5E,OAAyBC,sBAA8BD;EAEvD,IAAaE,UAAU;AACrB,WAAO;MACLC;MACAC;MACAC;MACAC;MACAC;SACG,MAAML;;EAEb;EAEA,IAAYM,aAAa;AACvB,WAAOA,WAAW,KAAKC,WAAWC,SAAS,KAAKC,OAAOH,YAAY,MAAM,yBAAA,CAAA;EAC3E;EAEA,IAAYI,cAAc;AACxB,WAAOF,SAAS,KAAKG,OAAOD,aAAa,MAAM,qBAAA;EACjD;EAEA,IAAYH,YAAY;AACtB,WAAOK,aAAa,KAAKF,aAAaF,SAAS,KAAKC,OAAOI,MAAM,MAAM,mBAAA,CAAA;EACzE;EAEA,MAAyBC,WAAWC,QAA8C;AAChF,UAAMC,oBAAoB,KAAKV;AAC/B,YAAQ,MAAMW,QAAQC,IAAIH,OAAOI,IAAI,OAAOC,SAAAA;AAC1C,YAAMC,SAASC,IAAIN,mBAAmBI,IAAAA;AACtC,YAAMG,UAAU,MAAMC,OAAOH,MAAAA;AAC7B,UAAI,CAACE,QAAQE,OAAM,GAAI;AACrB,eAAO;MACT;AACA,aAAOC,eAAeC,MAAMJ,QAAQK,KAAI,CAAA;IAC1C,CAAA,CAAA,GAAKC,OAAOJ,MAAAA;EACd;EAEA,MAAyBK,cAAcC,UAAmD;AACxF,UAAMC,QAAQ,MAAMN,eAAeO,UAAUF,UAAU;MAAEG,OAAO;IAAM,CAAA;AACtE,UAAMlB,oBAAoB,KAAKV;AAC/B,WAAO,MAAMW,QAAQC,IAAIc,MAAMb,IAC7B,OAAO,CAACgB,SAASf,IAAAA,MAAK;AACpB,YAAMC,SAASC,IAAIN,mBAAmBI,IAAAA;AACtC,YAAMgB,OAAOf,QAAQc,OAAAA;AACrB,aAAOA;IACT,CAAA,CAAA;EAEJ;AACF;;;;","names":["collection","doc","getDoc","getFirestore","setDoc","assertEx","exists","AbstractArchivist","ArchivistAllQuerySchema","ArchivistClearQuerySchema","ArchivistDeleteQuerySchema","ArchivistInsertQuerySchema","ArchivistNextQuerySchema","creatableModule","PayloadBuilder","FirebaseArchivistSchema","FirebaseArchivistConfigSchema","FirebaseArchivistSchema","FirebaseArchivist","AbstractArchivist","configSchemas","FirebaseArchivistConfigSchema","defaultConfigSchema","queries","ArchivistNextQuerySchema","ArchivistAllQuerySchema","ArchivistClearQuerySchema","ArchivistDeleteQuerySchema","ArchivistInsertQuerySchema","collection","firestore","assertEx","config","firebaseApp","params","getFirestore","dbId","getHandler","hashes","payloadCollection","Promise","all","map","hash","docRef","doc","docSnap","getDoc","exists","PayloadBuilder","build","data","filter","insertHandler","payloads","pairs","hashPairs","stamp","payload","setDoc"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xyo-network/archivist-firebase",
|
|
3
|
+
"version": "3.2.0",
|
|
4
|
+
"description": "Primary SDK for using XYO Protocol 2.0",
|
|
5
|
+
"homepage": "https://xyo.network",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "git+https://github.com/XYOracleNetwork/sdk-xyo-client-js/issues",
|
|
8
|
+
"email": "support@xyo.network"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/XYOracleNetwork/sdk-xyo-client-js.git"
|
|
13
|
+
},
|
|
14
|
+
"license": "LGPL-3.0-only",
|
|
15
|
+
"author": {
|
|
16
|
+
"name": "XYO Development Team",
|
|
17
|
+
"email": "support@xyo.network",
|
|
18
|
+
"url": "https://xyo.network"
|
|
19
|
+
},
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"type": "module",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/browser/index.d.ts",
|
|
25
|
+
"default": "./dist/browser/index.mjs"
|
|
26
|
+
},
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"module": "dist/browser/index.mjs",
|
|
30
|
+
"types": "dist/browser/index.d.ts",
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@firebase/app": "^0.10.12",
|
|
33
|
+
"@firebase/firestore": "^4.7.3",
|
|
34
|
+
"@xylabs/assert": "^4.1.0",
|
|
35
|
+
"@xylabs/exists": "^4.1.0",
|
|
36
|
+
"@xylabs/hex": "^4.1.0",
|
|
37
|
+
"@xylabs/lodash": "^4.1.0",
|
|
38
|
+
"@xyo-network/archivist-abstract": "^3.2.0",
|
|
39
|
+
"@xyo-network/archivist-model": "^3.2.0",
|
|
40
|
+
"@xyo-network/module-model": "^3.2.0",
|
|
41
|
+
"@xyo-network/payload-builder": "^3.2.0",
|
|
42
|
+
"@xyo-network/payload-model": "^3.2.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@firebase/app-compat": "^0.2.42",
|
|
46
|
+
"@firebase/rules-unit-testing": "^3.0.4",
|
|
47
|
+
"@xylabs/object": "^4.1.0",
|
|
48
|
+
"@xylabs/ts-scripts-yarn3": "^4.1.0",
|
|
49
|
+
"@xylabs/tsconfig": "^4.1.0",
|
|
50
|
+
"@xyo-network/account": "^3.2.0",
|
|
51
|
+
"@xyo-network/id-payload-plugin": "^3.2.0",
|
|
52
|
+
"@xyo-network/payload-wrapper": "^3.2.0",
|
|
53
|
+
"firebase": "^10.14.0",
|
|
54
|
+
"typescript": "^5.6.2"
|
|
55
|
+
},
|
|
56
|
+
"publishConfig": {
|
|
57
|
+
"access": "public"
|
|
58
|
+
}
|
|
59
|
+
}
|
package/src/Archivist.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import {
|
|
2
|
+
collection,
|
|
3
|
+
doc,
|
|
4
|
+
getDoc,
|
|
5
|
+
getFirestore,
|
|
6
|
+
setDoc,
|
|
7
|
+
} from '@firebase/firestore'
|
|
8
|
+
import { assertEx } from '@xylabs/assert'
|
|
9
|
+
import { exists } from '@xylabs/exists'
|
|
10
|
+
import { Hash } from '@xylabs/hex'
|
|
11
|
+
import { AbstractArchivist } from '@xyo-network/archivist-abstract'
|
|
12
|
+
import {
|
|
13
|
+
ArchivistAllQuerySchema,
|
|
14
|
+
ArchivistClearQuerySchema,
|
|
15
|
+
ArchivistDeleteQuerySchema,
|
|
16
|
+
ArchivistInsertQuerySchema,
|
|
17
|
+
ArchivistModuleEventData,
|
|
18
|
+
ArchivistNextQuerySchema,
|
|
19
|
+
} from '@xyo-network/archivist-model'
|
|
20
|
+
import { creatableModule } from '@xyo-network/module-model'
|
|
21
|
+
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
22
|
+
import {
|
|
23
|
+
Payload, Schema,
|
|
24
|
+
WithMeta,
|
|
25
|
+
} from '@xyo-network/payload-model'
|
|
26
|
+
|
|
27
|
+
import { FirebaseArchivistConfigSchema } from './Config.ts'
|
|
28
|
+
import { FirebaseArchivistParams } from './Params.ts'
|
|
29
|
+
|
|
30
|
+
@creatableModule()
|
|
31
|
+
export class FirebaseArchivist<
|
|
32
|
+
TParams extends FirebaseArchivistParams = FirebaseArchivistParams,
|
|
33
|
+
TEventData extends ArchivistModuleEventData = ArchivistModuleEventData,
|
|
34
|
+
> extends AbstractArchivist<TParams, TEventData> {
|
|
35
|
+
static override readonly configSchemas: Schema[] = [...super.configSchemas, FirebaseArchivistConfigSchema]
|
|
36
|
+
static override readonly defaultConfigSchema: Schema = FirebaseArchivistConfigSchema
|
|
37
|
+
|
|
38
|
+
override get queries() {
|
|
39
|
+
return [
|
|
40
|
+
ArchivistNextQuerySchema,
|
|
41
|
+
ArchivistAllQuerySchema,
|
|
42
|
+
ArchivistClearQuerySchema,
|
|
43
|
+
ArchivistDeleteQuerySchema,
|
|
44
|
+
ArchivistInsertQuerySchema,
|
|
45
|
+
...super.queries,
|
|
46
|
+
]
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
private get collection() {
|
|
50
|
+
return collection(this.firestore, assertEx(this.config.collection, () => 'Missing collection name'))
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private get firebaseApp() {
|
|
54
|
+
return assertEx(this.params.firebaseApp, () => 'Missing FirebaseApp')
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private get firestore() {
|
|
58
|
+
return getFirestore(this.firebaseApp, assertEx(this.config.dbId, () => 'no dbId specified'))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
protected override async getHandler(hashes: Hash[]): Promise<WithMeta<Payload>[]> {
|
|
62
|
+
const payloadCollection = this.collection
|
|
63
|
+
return (await Promise.all(hashes.map(async (hash) => {
|
|
64
|
+
const docRef = doc(payloadCollection, hash)
|
|
65
|
+
const docSnap = await getDoc(docRef)
|
|
66
|
+
if (!docSnap.exists()) {
|
|
67
|
+
return null
|
|
68
|
+
}
|
|
69
|
+
return PayloadBuilder.build(docSnap.data() as Payload)
|
|
70
|
+
}))).filter(exists)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
protected override async insertHandler(payloads: Payload[]): Promise<WithMeta<Payload>[]> {
|
|
74
|
+
const pairs = await PayloadBuilder.hashPairs(payloads, { stamp: false })
|
|
75
|
+
const payloadCollection = this.collection
|
|
76
|
+
return await Promise.all(pairs.map(
|
|
77
|
+
async ([payload, hash]) => {
|
|
78
|
+
const docRef = doc(payloadCollection, hash)
|
|
79
|
+
await setDoc(docRef, payload)
|
|
80
|
+
return payload
|
|
81
|
+
},
|
|
82
|
+
))
|
|
83
|
+
}
|
|
84
|
+
}
|
package/src/Config.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ArchivistConfig } from '@xyo-network/archivist-model'
|
|
2
|
+
|
|
3
|
+
import { FirebaseArchivistSchema } from './Schema.ts'
|
|
4
|
+
|
|
5
|
+
export type FirebaseArchivistConfigSchema = `${FirebaseArchivistSchema}.config`
|
|
6
|
+
export const FirebaseArchivistConfigSchema: FirebaseArchivistConfigSchema = `${FirebaseArchivistSchema}.config`
|
|
7
|
+
|
|
8
|
+
export type FirebaseArchivistConfig = ArchivistConfig<{
|
|
9
|
+
collection?: string
|
|
10
|
+
dbId?: string
|
|
11
|
+
schema: FirebaseArchivistConfigSchema
|
|
12
|
+
}>
|
package/src/Params.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { FirebaseApp } from '@firebase/app'
|
|
2
|
+
import type { ArchivistParams } from '@xyo-network/archivist-model'
|
|
3
|
+
import type { AnyConfigSchema } from '@xyo-network/module-model'
|
|
4
|
+
|
|
5
|
+
import type { FirebaseArchivistConfig } from './Config.ts'
|
|
6
|
+
|
|
7
|
+
export type FirebaseArchivistParams = ArchivistParams<AnyConfigSchema<FirebaseArchivistConfig>, {
|
|
8
|
+
firebaseApp?: FirebaseApp
|
|
9
|
+
}>
|
package/src/Schema.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
service cloud.firestore {
|
|
2
|
+
match /databases/{database}/documents {
|
|
3
|
+
|
|
4
|
+
// Match any document in the "users" collection
|
|
5
|
+
match /users/{userId} {
|
|
6
|
+
|
|
7
|
+
// Allow read and write access if the requesting user is authenticated and their UID matches the document ID
|
|
8
|
+
allow read, write: if request.auth != null && request.auth.uid == userId;
|
|
9
|
+
|
|
10
|
+
// Deny all other access
|
|
11
|
+
allow read, write: if false;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
package/typedoc.json
ADDED