@blibliki/models 0.5.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/dist/index.d.ts +104 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +37 -0
- package/src/Device.ts +135 -0
- package/src/Patch.ts +155 -0
- package/src/db.ts +33 -0
- package/src/index.ts +8 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { FirebaseApp } from 'firebase/app';
|
|
2
|
+
import { Firestore } from 'firebase/firestore';
|
|
3
|
+
import { IAnyModuleSerialize, IEngineSerialize } from '@blibliki/engine';
|
|
4
|
+
import { AnyObject, Optional } from '@blibliki/utils';
|
|
5
|
+
|
|
6
|
+
type FirebaseConfig = {
|
|
7
|
+
apiKey: string;
|
|
8
|
+
authDomain: string;
|
|
9
|
+
projectId: string;
|
|
10
|
+
storageBucket: string;
|
|
11
|
+
messagingSenderId: string;
|
|
12
|
+
appId: string;
|
|
13
|
+
};
|
|
14
|
+
declare const initializeFirebase: (config: FirebaseConfig) => void;
|
|
15
|
+
declare const getApp: () => FirebaseApp;
|
|
16
|
+
declare const getDb: () => Firestore;
|
|
17
|
+
|
|
18
|
+
type IPatch = {
|
|
19
|
+
id: string;
|
|
20
|
+
name: string;
|
|
21
|
+
userId: string;
|
|
22
|
+
config: IConfig;
|
|
23
|
+
};
|
|
24
|
+
type Viewport = {
|
|
25
|
+
x: number;
|
|
26
|
+
y: number;
|
|
27
|
+
zoom: number;
|
|
28
|
+
};
|
|
29
|
+
type Node = {
|
|
30
|
+
data: AnyObject;
|
|
31
|
+
dragging?: boolean;
|
|
32
|
+
id: string;
|
|
33
|
+
measured?: {
|
|
34
|
+
height?: number;
|
|
35
|
+
width?: number;
|
|
36
|
+
};
|
|
37
|
+
position: {
|
|
38
|
+
x: number;
|
|
39
|
+
y: number;
|
|
40
|
+
};
|
|
41
|
+
selected?: boolean;
|
|
42
|
+
type?: string;
|
|
43
|
+
};
|
|
44
|
+
type Edge = {
|
|
45
|
+
id: string;
|
|
46
|
+
source: string;
|
|
47
|
+
sourceHandle?: string | null;
|
|
48
|
+
target: string;
|
|
49
|
+
targetHandle?: string | null;
|
|
50
|
+
};
|
|
51
|
+
type IGridNodes = {
|
|
52
|
+
nodes: Node[];
|
|
53
|
+
edges: Edge[];
|
|
54
|
+
viewport: Viewport;
|
|
55
|
+
};
|
|
56
|
+
type IConfig = {
|
|
57
|
+
modules: IAnyModuleSerialize[];
|
|
58
|
+
gridNodes: IGridNodes;
|
|
59
|
+
};
|
|
60
|
+
declare class Patch implements IPatch {
|
|
61
|
+
id: string;
|
|
62
|
+
name: string;
|
|
63
|
+
userId: string;
|
|
64
|
+
config: IConfig;
|
|
65
|
+
static find(id: string): Promise<Patch>;
|
|
66
|
+
static all(): Promise<Patch[]>;
|
|
67
|
+
constructor(props: Optional<IPatch, "id">);
|
|
68
|
+
save(): Promise<void>;
|
|
69
|
+
delete(): Promise<void>;
|
|
70
|
+
serialize(): IPatch;
|
|
71
|
+
engineSerialize(): IEngineSerialize;
|
|
72
|
+
private get docRef();
|
|
73
|
+
private get props();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
type IDevice = {
|
|
77
|
+
id: string;
|
|
78
|
+
token: string;
|
|
79
|
+
name: string;
|
|
80
|
+
patchId: string | null;
|
|
81
|
+
userId: string;
|
|
82
|
+
};
|
|
83
|
+
declare class Device implements IDevice {
|
|
84
|
+
id: string;
|
|
85
|
+
token: string;
|
|
86
|
+
name: string;
|
|
87
|
+
patchId: string | null;
|
|
88
|
+
userId: string;
|
|
89
|
+
static find(id: string): Promise<Device>;
|
|
90
|
+
static findByUserId(userId: string): Promise<Device[]>;
|
|
91
|
+
static findBy(opts: {
|
|
92
|
+
userId?: string;
|
|
93
|
+
token?: string;
|
|
94
|
+
}): Promise<Device[]>;
|
|
95
|
+
static all(): Promise<Device[]>;
|
|
96
|
+
constructor(props: Optional<IDevice, "id">);
|
|
97
|
+
save(): Promise<void>;
|
|
98
|
+
delete(): Promise<void>;
|
|
99
|
+
serialize(): IDevice;
|
|
100
|
+
private get docRef();
|
|
101
|
+
private get props();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export { Device, type FirebaseConfig, type IConfig, type IDevice, type IPatch, Patch, getApp, getDb, initializeFirebase };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{initializeApp as w}from"firebase/app";import{getFirestore as D}from"firebase/firestore";var m,d,g,P=o=>{m=o,d=w(m),g=D(d)},x=()=>{if(!d)throw Error("You have to initialize db first");return d},s=()=>{if(!g)throw Error("You have to initialize db first");return g};import{pick as S}from"@blibliki/utils";import{collection as l,addDoc as z,getDoc as F,doc as f,updateDoc as C,getDocs as R,deleteDoc as q}from"firebase/firestore";var c=class o{id;name;userId;config;static async find(e){let i=s(),r=f(i,"patches",e),t=await F(r);if(t.exists()){let n={id:t.id,...t.data()};return new o(n)}else throw Error(`Patch ${e} not found`)}static async all(){let e=s();return(await R(l(e,"patches"))).docs.map(r=>new o({id:r.id,...r.data()}))}constructor(e){Object.assign(this,S(e,["id","name","userId","config"]))}async save(){let e=s();if(this.id)await C(this.docRef,this.props);else{let i=await z(l(e,"patches"),this.props);this.id=i.id}}async delete(){if(!this.id)throw Error("Cannot delete a patch without id");await q(this.docRef)}serialize(){return{id:this.id,...this.props}}engineSerialize(){let{modules:e,gridNodes:i}=this.props.config,r=i.edges.map(t=>({id:t.id,source:{moduleId:t.source,ioName:t.sourceHandle},destination:{moduleId:t.target,ioName:t.targetHandle}}));return{bpm:120,timeSignature:[4,4],modules:e,routes:r}}get docRef(){let e=s();return f(e,"patches",this.id)}get props(){return{name:this.name,userId:this.userId,config:this.config}}};import{pick as O}from"@blibliki/utils";import{collection as p,addDoc as A,getDoc as E,doc as I,updateDoc as k,getDocs as h,deleteDoc as N,query as b,where as y}from"firebase/firestore";var u=class o{id;token;name;patchId;userId;static async find(e){let i=s(),r=I(i,"devices",e),t=await E(r);if(t.exists()){let n={id:t.id,...t.data()};return new o(n)}else throw Error(`Device ${e} not found`)}static async findByUserId(e){let i=s(),r=b(p(i,"devices"),y("userId","==",e));return(await h(r)).docs.map(n=>new o({id:n.id,...n.data()}))}static async findBy(e){let i=s(),r=Object.entries(e).map(([a,v])=>y(a,"==",v)),t=b(p(i,"devices"),...r);return(await h(t)).docs.map(a=>new o({id:a.id,...a.data()}))}static async all(){let e=s();return(await h(p(e,"devices"))).docs.map(r=>new o({id:r.id,...r.data()}))}constructor(e){Object.assign(this,O(e,["id","token","name","patchId","userId"]))}async save(){let e=s();if(this.id)await k(this.docRef,this.props);else{let i=await A(p(e,"devices"),this.props);this.id=i.id}}async delete(){if(!this.id)throw Error("Cannot delete a device without id");await N(this.docRef)}serialize(){return{id:this.id,...this.props}}get docRef(){let e=s();return I(e,"devices",this.id)}get props(){return{token:this.token,name:this.name,patchId:this.patchId,userId:this.userId}}};export{u as Device,c as Patch,x as getApp,s as getDb,P as initializeFirebase};
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/db.ts","../src/Patch.ts","../src/Device.ts"],"sourcesContent":["import { type FirebaseApp, initializeApp } from \"firebase/app\";\nimport { type Firestore, getFirestore } from \"firebase/firestore\";\n\nexport type FirebaseConfig = {\n apiKey: string;\n authDomain: string;\n projectId: string;\n storageBucket: string;\n messagingSenderId: string;\n appId: string;\n};\n\nlet firebaseConfig: FirebaseConfig | undefined;\nlet app: FirebaseApp | undefined;\nlet db: Firestore | undefined;\n\nexport const initializeFirebase = (config: FirebaseConfig) => {\n firebaseConfig = config;\n app = initializeApp(firebaseConfig);\n db = getFirestore(app);\n};\n\nexport const getApp = (): FirebaseApp => {\n if (!app) throw Error(\"You have to initialize db first\");\n\n return app;\n};\n\nexport const getDb = (): Firestore => {\n if (!db) throw Error(\"You have to initialize db first\");\n\n return db;\n};\n","import {\n IAnyModuleSerialize,\n IEngineSerialize,\n IRoute,\n} from \"@blibliki/engine\";\nimport { AnyObject, Optional, pick } from \"@blibliki/utils\";\nimport {\n collection,\n addDoc,\n getDoc,\n doc,\n updateDoc,\n getDocs,\n deleteDoc,\n} from \"firebase/firestore\";\nimport { getDb } from \"./db\";\n\nexport type IPatch = {\n id: string;\n name: string;\n userId: string;\n config: IConfig;\n};\n\ntype Viewport = {\n x: number;\n y: number;\n zoom: number;\n};\n\ntype Node = {\n data: AnyObject;\n dragging?: boolean;\n id: string;\n measured?: {\n height?: number;\n width?: number;\n };\n position: {\n x: number;\n y: number;\n };\n selected?: boolean;\n type?: string;\n};\n\ntype Edge = {\n id: string;\n source: string;\n sourceHandle?: string | null;\n target: string;\n targetHandle?: string | null;\n};\n\ntype IGridNodes = {\n nodes: Node[];\n edges: Edge[];\n viewport: Viewport;\n};\n\nexport type IConfig = {\n modules: IAnyModuleSerialize[];\n gridNodes: IGridNodes;\n};\n\nexport default class Patch implements IPatch {\n id!: string;\n name!: string;\n userId!: string;\n config!: IConfig;\n\n static async find(id: string): Promise<Patch> {\n const db = getDb();\n\n const docRef = doc(db, \"patches\", id);\n const docSnap = await getDoc(docRef);\n\n if (docSnap.exists()) {\n const data = { id: docSnap.id, ...docSnap.data() } as IPatch;\n return new Patch(data);\n } else {\n throw Error(`Patch ${id} not found`);\n }\n }\n\n static async all(): Promise<Patch[]> {\n const db = getDb();\n\n const querySnapshot = await getDocs(collection(db, \"patches\"));\n\n return querySnapshot.docs.map((doc) => {\n return new Patch({\n id: doc.id,\n ...doc.data(),\n } as IPatch);\n });\n }\n\n constructor(props: Optional<IPatch, \"id\">) {\n Object.assign(this, pick(props, [\"id\", \"name\", \"userId\", \"config\"]));\n }\n\n async save(): Promise<void> {\n const db = getDb();\n\n if (this.id) {\n await updateDoc(this.docRef, this.props);\n } else {\n const docRef = await addDoc(collection(db, \"patches\"), this.props);\n this.id = docRef.id;\n }\n }\n\n async delete(): Promise<void> {\n if (!this.id) throw Error(\"Cannot delete a patch without id\");\n\n await deleteDoc(this.docRef);\n }\n\n serialize(): IPatch {\n return {\n id: this.id,\n ...this.props,\n };\n }\n\n engineSerialize(): IEngineSerialize {\n const { modules, gridNodes } = this.props.config;\n\n const routes: IRoute[] = gridNodes.edges.map((edge) => {\n return {\n id: edge.id,\n source: { moduleId: edge.source, ioName: edge.sourceHandle! },\n destination: { moduleId: edge.target, ioName: edge.targetHandle! },\n };\n });\n\n return {\n bpm: 120, // Temp: static until I set it from grid\n timeSignature: [4, 4], // Temp: static until I set it from grid\n modules,\n routes,\n };\n }\n\n private get docRef() {\n const db = getDb();\n\n return doc(db, \"patches\", this.id);\n }\n\n private get props(): Omit<IPatch, \"id\"> {\n return { name: this.name, userId: this.userId, config: this.config };\n }\n}\n","import { Optional, pick } from \"@blibliki/utils\";\nimport {\n collection,\n addDoc,\n getDoc,\n doc,\n updateDoc,\n getDocs,\n deleteDoc,\n query,\n where,\n} from \"firebase/firestore\";\nimport { getDb } from \"./db\";\n\nexport type IDevice = {\n id: string;\n token: string;\n name: string;\n patchId: string | null;\n userId: string;\n};\n\nexport default class Device implements IDevice {\n id!: string;\n token!: string;\n name!: string;\n patchId!: string | null;\n userId!: string;\n\n static async find(id: string): Promise<Device> {\n const db = getDb();\n\n const docRef = doc(db, \"devices\", id);\n const docSnap = await getDoc(docRef);\n\n if (docSnap.exists()) {\n const data = { id: docSnap.id, ...docSnap.data() } as IDevice;\n return new Device(data);\n } else {\n throw Error(`Device ${id} not found`);\n }\n }\n\n static async findByUserId(userId: string): Promise<Device[]> {\n const db = getDb();\n\n const q = query(collection(db, \"devices\"), where(\"userId\", \"==\", userId));\n const querySnapshot = await getDocs(q);\n\n return querySnapshot.docs.map((doc) => {\n return new Device({\n id: doc.id,\n ...doc.data(),\n } as IDevice);\n });\n }\n\n static async findBy(opts: {\n userId?: string;\n token?: string;\n }): Promise<Device[]> {\n const db = getDb();\n\n const whereClauses = Object.entries(opts).map(([key, value]) =>\n where(key, \"==\", value),\n );\n const q = query(collection(db, \"devices\"), ...whereClauses);\n const querySnapshot = await getDocs(q);\n\n return querySnapshot.docs.map((doc) => {\n return new Device({\n id: doc.id,\n ...doc.data(),\n } as IDevice);\n });\n }\n\n static async all(): Promise<Device[]> {\n const db = getDb();\n\n const querySnapshot = await getDocs(collection(db, \"devices\"));\n\n return querySnapshot.docs.map((doc) => {\n return new Device({\n id: doc.id,\n ...doc.data(),\n } as IDevice);\n });\n }\n\n constructor(props: Optional<IDevice, \"id\">) {\n Object.assign(\n this,\n pick(props, [\"id\", \"token\", \"name\", \"patchId\", \"userId\"]),\n );\n }\n\n async save(): Promise<void> {\n const db = getDb();\n\n if (this.id) {\n await updateDoc(this.docRef, this.props);\n } else {\n const docRef = await addDoc(collection(db, \"devices\"), this.props);\n this.id = docRef.id;\n }\n }\n\n async delete(): Promise<void> {\n if (!this.id) throw Error(\"Cannot delete a device without id\");\n\n await deleteDoc(this.docRef);\n }\n\n serialize(): IDevice {\n return {\n id: this.id,\n ...this.props,\n };\n }\n\n private get docRef() {\n const db = getDb();\n return doc(db, \"devices\", this.id);\n }\n\n private get props(): Omit<IDevice, \"id\"> {\n return {\n token: this.token,\n name: this.name,\n patchId: this.patchId,\n userId: this.userId,\n };\n }\n}\n"],"mappings":"AAAA,OAA2B,iBAAAA,MAAqB,eAChD,OAAyB,gBAAAC,MAAoB,qBAW7C,IAAIC,EACAC,EACAC,EAESC,EAAsBC,GAA2B,CAC5DJ,EAAiBI,EACjBH,EAAMH,EAAcE,CAAc,EAClCE,EAAKH,EAAaE,CAAG,CACvB,EAEaI,EAAS,IAAmB,CACvC,GAAI,CAACJ,EAAK,MAAM,MAAM,iCAAiC,EAEvD,OAAOA,CACT,EAEaK,EAAQ,IAAiB,CACpC,GAAI,CAACJ,EAAI,MAAM,MAAM,iCAAiC,EAEtD,OAAOA,CACT,EC3BA,OAA8B,QAAAK,MAAY,kBAC1C,OACE,cAAAC,EACA,UAAAC,EACA,UAAAC,EACA,OAAAC,EACA,a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,MAAO,KAAK,MACZ,KAAM,KAAK,KACX,QAAS,KAAK,QACd,OAAQ,KAAK,MACf,CACF,CACF","names":["initializeApp","getFirestore","firebaseConfig","app","db","initializeFirebase","config","getApp","getDb","pick","collection","addDoc","getDoc","doc","updateDoc","getDocs","deleteDoc","Patch","_Patch","id","db","getDb","docRef","doc","docSnap","getDoc","data","getDocs","collection","props","pick","updateDoc","addDoc","deleteDoc","modules","gridNodes","routes","edge","pick","collection","addDoc","getDoc","doc","updateDoc","getDocs","deleteDoc","query","where","Device","_Device","id","db","getDb","docRef","doc","docSnap","getDoc","data","userId","q","query","collection","where","getDocs","opts","whereClauses","key","value","props","pick","updateDoc","addDoc","deleteDoc"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@blibliki/models",
|
|
3
|
+
"version": "0.5.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": {
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"import": "./dist/index.js"
|
|
9
|
+
},
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"README.md",
|
|
14
|
+
"src",
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/audioworklet": "0.0.92",
|
|
19
|
+
"@types/node": "^22.10.2"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"firebase": "12.6.0",
|
|
23
|
+
"@blibliki/utils": "^0.5.2",
|
|
24
|
+
"@blibliki/engine": "^0.5.2"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsup",
|
|
28
|
+
"dev": "tsup --watch",
|
|
29
|
+
"start": "NODE_ENV=development tsx src/index.ts",
|
|
30
|
+
"lint": "eslint src",
|
|
31
|
+
"tsc": "tsc --noEmit",
|
|
32
|
+
"format": "prettier . --write",
|
|
33
|
+
"format:check": "prettier . --check",
|
|
34
|
+
"bump": "npm version patch",
|
|
35
|
+
"release": "pnpm run build && pnpm publish --access public"
|
|
36
|
+
}
|
|
37
|
+
}
|
package/src/Device.ts
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { Optional, pick } from "@blibliki/utils";
|
|
2
|
+
import {
|
|
3
|
+
collection,
|
|
4
|
+
addDoc,
|
|
5
|
+
getDoc,
|
|
6
|
+
doc,
|
|
7
|
+
updateDoc,
|
|
8
|
+
getDocs,
|
|
9
|
+
deleteDoc,
|
|
10
|
+
query,
|
|
11
|
+
where,
|
|
12
|
+
} from "firebase/firestore";
|
|
13
|
+
import { getDb } from "./db";
|
|
14
|
+
|
|
15
|
+
export type IDevice = {
|
|
16
|
+
id: string;
|
|
17
|
+
token: string;
|
|
18
|
+
name: string;
|
|
19
|
+
patchId: string | null;
|
|
20
|
+
userId: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export default class Device implements IDevice {
|
|
24
|
+
id!: string;
|
|
25
|
+
token!: string;
|
|
26
|
+
name!: string;
|
|
27
|
+
patchId!: string | null;
|
|
28
|
+
userId!: string;
|
|
29
|
+
|
|
30
|
+
static async find(id: string): Promise<Device> {
|
|
31
|
+
const db = getDb();
|
|
32
|
+
|
|
33
|
+
const docRef = doc(db, "devices", id);
|
|
34
|
+
const docSnap = await getDoc(docRef);
|
|
35
|
+
|
|
36
|
+
if (docSnap.exists()) {
|
|
37
|
+
const data = { id: docSnap.id, ...docSnap.data() } as IDevice;
|
|
38
|
+
return new Device(data);
|
|
39
|
+
} else {
|
|
40
|
+
throw Error(`Device ${id} not found`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static async findByUserId(userId: string): Promise<Device[]> {
|
|
45
|
+
const db = getDb();
|
|
46
|
+
|
|
47
|
+
const q = query(collection(db, "devices"), where("userId", "==", userId));
|
|
48
|
+
const querySnapshot = await getDocs(q);
|
|
49
|
+
|
|
50
|
+
return querySnapshot.docs.map((doc) => {
|
|
51
|
+
return new Device({
|
|
52
|
+
id: doc.id,
|
|
53
|
+
...doc.data(),
|
|
54
|
+
} as IDevice);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
static async findBy(opts: {
|
|
59
|
+
userId?: string;
|
|
60
|
+
token?: string;
|
|
61
|
+
}): Promise<Device[]> {
|
|
62
|
+
const db = getDb();
|
|
63
|
+
|
|
64
|
+
const whereClauses = Object.entries(opts).map(([key, value]) =>
|
|
65
|
+
where(key, "==", value),
|
|
66
|
+
);
|
|
67
|
+
const q = query(collection(db, "devices"), ...whereClauses);
|
|
68
|
+
const querySnapshot = await getDocs(q);
|
|
69
|
+
|
|
70
|
+
return querySnapshot.docs.map((doc) => {
|
|
71
|
+
return new Device({
|
|
72
|
+
id: doc.id,
|
|
73
|
+
...doc.data(),
|
|
74
|
+
} as IDevice);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
static async all(): Promise<Device[]> {
|
|
79
|
+
const db = getDb();
|
|
80
|
+
|
|
81
|
+
const querySnapshot = await getDocs(collection(db, "devices"));
|
|
82
|
+
|
|
83
|
+
return querySnapshot.docs.map((doc) => {
|
|
84
|
+
return new Device({
|
|
85
|
+
id: doc.id,
|
|
86
|
+
...doc.data(),
|
|
87
|
+
} as IDevice);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
constructor(props: Optional<IDevice, "id">) {
|
|
92
|
+
Object.assign(
|
|
93
|
+
this,
|
|
94
|
+
pick(props, ["id", "token", "name", "patchId", "userId"]),
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async save(): Promise<void> {
|
|
99
|
+
const db = getDb();
|
|
100
|
+
|
|
101
|
+
if (this.id) {
|
|
102
|
+
await updateDoc(this.docRef, this.props);
|
|
103
|
+
} else {
|
|
104
|
+
const docRef = await addDoc(collection(db, "devices"), this.props);
|
|
105
|
+
this.id = docRef.id;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async delete(): Promise<void> {
|
|
110
|
+
if (!this.id) throw Error("Cannot delete a device without id");
|
|
111
|
+
|
|
112
|
+
await deleteDoc(this.docRef);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
serialize(): IDevice {
|
|
116
|
+
return {
|
|
117
|
+
id: this.id,
|
|
118
|
+
...this.props,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private get docRef() {
|
|
123
|
+
const db = getDb();
|
|
124
|
+
return doc(db, "devices", this.id);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
private get props(): Omit<IDevice, "id"> {
|
|
128
|
+
return {
|
|
129
|
+
token: this.token,
|
|
130
|
+
name: this.name,
|
|
131
|
+
patchId: this.patchId,
|
|
132
|
+
userId: this.userId,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
}
|
package/src/Patch.ts
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import {
|
|
2
|
+
IAnyModuleSerialize,
|
|
3
|
+
IEngineSerialize,
|
|
4
|
+
IRoute,
|
|
5
|
+
} from "@blibliki/engine";
|
|
6
|
+
import { AnyObject, Optional, pick } from "@blibliki/utils";
|
|
7
|
+
import {
|
|
8
|
+
collection,
|
|
9
|
+
addDoc,
|
|
10
|
+
getDoc,
|
|
11
|
+
doc,
|
|
12
|
+
updateDoc,
|
|
13
|
+
getDocs,
|
|
14
|
+
deleteDoc,
|
|
15
|
+
} from "firebase/firestore";
|
|
16
|
+
import { getDb } from "./db";
|
|
17
|
+
|
|
18
|
+
export type IPatch = {
|
|
19
|
+
id: string;
|
|
20
|
+
name: string;
|
|
21
|
+
userId: string;
|
|
22
|
+
config: IConfig;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
type Viewport = {
|
|
26
|
+
x: number;
|
|
27
|
+
y: number;
|
|
28
|
+
zoom: number;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
type Node = {
|
|
32
|
+
data: AnyObject;
|
|
33
|
+
dragging?: boolean;
|
|
34
|
+
id: string;
|
|
35
|
+
measured?: {
|
|
36
|
+
height?: number;
|
|
37
|
+
width?: number;
|
|
38
|
+
};
|
|
39
|
+
position: {
|
|
40
|
+
x: number;
|
|
41
|
+
y: number;
|
|
42
|
+
};
|
|
43
|
+
selected?: boolean;
|
|
44
|
+
type?: string;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
type Edge = {
|
|
48
|
+
id: string;
|
|
49
|
+
source: string;
|
|
50
|
+
sourceHandle?: string | null;
|
|
51
|
+
target: string;
|
|
52
|
+
targetHandle?: string | null;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
type IGridNodes = {
|
|
56
|
+
nodes: Node[];
|
|
57
|
+
edges: Edge[];
|
|
58
|
+
viewport: Viewport;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export type IConfig = {
|
|
62
|
+
modules: IAnyModuleSerialize[];
|
|
63
|
+
gridNodes: IGridNodes;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export default class Patch implements IPatch {
|
|
67
|
+
id!: string;
|
|
68
|
+
name!: string;
|
|
69
|
+
userId!: string;
|
|
70
|
+
config!: IConfig;
|
|
71
|
+
|
|
72
|
+
static async find(id: string): Promise<Patch> {
|
|
73
|
+
const db = getDb();
|
|
74
|
+
|
|
75
|
+
const docRef = doc(db, "patches", id);
|
|
76
|
+
const docSnap = await getDoc(docRef);
|
|
77
|
+
|
|
78
|
+
if (docSnap.exists()) {
|
|
79
|
+
const data = { id: docSnap.id, ...docSnap.data() } as IPatch;
|
|
80
|
+
return new Patch(data);
|
|
81
|
+
} else {
|
|
82
|
+
throw Error(`Patch ${id} not found`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
static async all(): Promise<Patch[]> {
|
|
87
|
+
const db = getDb();
|
|
88
|
+
|
|
89
|
+
const querySnapshot = await getDocs(collection(db, "patches"));
|
|
90
|
+
|
|
91
|
+
return querySnapshot.docs.map((doc) => {
|
|
92
|
+
return new Patch({
|
|
93
|
+
id: doc.id,
|
|
94
|
+
...doc.data(),
|
|
95
|
+
} as IPatch);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
constructor(props: Optional<IPatch, "id">) {
|
|
100
|
+
Object.assign(this, pick(props, ["id", "name", "userId", "config"]));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async save(): Promise<void> {
|
|
104
|
+
const db = getDb();
|
|
105
|
+
|
|
106
|
+
if (this.id) {
|
|
107
|
+
await updateDoc(this.docRef, this.props);
|
|
108
|
+
} else {
|
|
109
|
+
const docRef = await addDoc(collection(db, "patches"), this.props);
|
|
110
|
+
this.id = docRef.id;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async delete(): Promise<void> {
|
|
115
|
+
if (!this.id) throw Error("Cannot delete a patch without id");
|
|
116
|
+
|
|
117
|
+
await deleteDoc(this.docRef);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
serialize(): IPatch {
|
|
121
|
+
return {
|
|
122
|
+
id: this.id,
|
|
123
|
+
...this.props,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
engineSerialize(): IEngineSerialize {
|
|
128
|
+
const { modules, gridNodes } = this.props.config;
|
|
129
|
+
|
|
130
|
+
const routes: IRoute[] = gridNodes.edges.map((edge) => {
|
|
131
|
+
return {
|
|
132
|
+
id: edge.id,
|
|
133
|
+
source: { moduleId: edge.source, ioName: edge.sourceHandle! },
|
|
134
|
+
destination: { moduleId: edge.target, ioName: edge.targetHandle! },
|
|
135
|
+
};
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
bpm: 120, // Temp: static until I set it from grid
|
|
140
|
+
timeSignature: [4, 4], // Temp: static until I set it from grid
|
|
141
|
+
modules,
|
|
142
|
+
routes,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
private get docRef() {
|
|
147
|
+
const db = getDb();
|
|
148
|
+
|
|
149
|
+
return doc(db, "patches", this.id);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private get props(): Omit<IPatch, "id"> {
|
|
153
|
+
return { name: this.name, userId: this.userId, config: this.config };
|
|
154
|
+
}
|
|
155
|
+
}
|
package/src/db.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type FirebaseApp, initializeApp } from "firebase/app";
|
|
2
|
+
import { type Firestore, getFirestore } from "firebase/firestore";
|
|
3
|
+
|
|
4
|
+
export type FirebaseConfig = {
|
|
5
|
+
apiKey: string;
|
|
6
|
+
authDomain: string;
|
|
7
|
+
projectId: string;
|
|
8
|
+
storageBucket: string;
|
|
9
|
+
messagingSenderId: string;
|
|
10
|
+
appId: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
let firebaseConfig: FirebaseConfig | undefined;
|
|
14
|
+
let app: FirebaseApp | undefined;
|
|
15
|
+
let db: Firestore | undefined;
|
|
16
|
+
|
|
17
|
+
export const initializeFirebase = (config: FirebaseConfig) => {
|
|
18
|
+
firebaseConfig = config;
|
|
19
|
+
app = initializeApp(firebaseConfig);
|
|
20
|
+
db = getFirestore(app);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const getApp = (): FirebaseApp => {
|
|
24
|
+
if (!app) throw Error("You have to initialize db first");
|
|
25
|
+
|
|
26
|
+
return app;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const getDb = (): Firestore => {
|
|
30
|
+
if (!db) throw Error("You have to initialize db first");
|
|
31
|
+
|
|
32
|
+
return db;
|
|
33
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { initializeFirebase, getApp, getDb } from "./db";
|
|
2
|
+
export type { FirebaseConfig } from "./db";
|
|
3
|
+
|
|
4
|
+
export type { IPatch, IConfig } from "./Patch";
|
|
5
|
+
export { default as Patch } from "./Patch";
|
|
6
|
+
|
|
7
|
+
export type { IDevice } from "./Device";
|
|
8
|
+
export { default as Device } from "./Device";
|