Skip to content

Commit bc5eaba

Browse files
committed
Copied from @mondoe/orm
0 parents  commit bc5eaba

40 files changed

+12314
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

OldLocalCache.ts

Whitespace-only changes.

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Mufasa
2+
3+
A prototype local-first, reactive, highly-extensible, cross-backend, cross-frontend, cross-platform ORM. Very much a work in progress.

dist/ClientStorage/Capacitor.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { GetClientStorage } from "./ClientStorage";
2+
export declare function capacitorStorage(fileSystem: {
3+
readFile: (path: string) => Promise<string | undefined>;
4+
writeFile: (path: string, data: string) => Promise<void>;
5+
deleteFile: (path: string) => Promise<void>;
6+
}): GetClientStorage;

dist/ClientStorage/Capacitor.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.capacitorStorage = void 0;
4+
const utils_1 = require("../utils");
5+
function capacitorStorage(fileSystem) {
6+
return async function (fileName, init) {
7+
fileName = `${fileName}.json`;
8+
// Load data
9+
let data = await (async () => {
10+
const savedData = await fileSystem.readFile(fileName);
11+
if ((0, utils_1.exists)(savedData)) {
12+
return JSON.parse(savedData);
13+
}
14+
else {
15+
return init;
16+
}
17+
})();
18+
// Start save Loop
19+
let saveIndex = 0;
20+
let lastSaveIndex = saveIndex;
21+
function requestSave() {
22+
saveIndex += 1;
23+
}
24+
(async () => {
25+
while (true) {
26+
if (lastSaveIndex !== saveIndex) {
27+
lastSaveIndex = saveIndex;
28+
await fileSystem.writeFile(fileName, JSON.stringify(data));
29+
}
30+
await new Promise((resolve) => setTimeout(resolve, 250));
31+
}
32+
})();
33+
// Return interface
34+
return {
35+
// Adds and updates the given data
36+
updateData: ((updates) => {
37+
updateRec(data, updates);
38+
requestSave();
39+
function updateRec(original, updates) {
40+
for (const propName in updates) {
41+
const value = updates[propName];
42+
if (value === undefined) {
43+
// Undefined means delete
44+
delete original[propName];
45+
}
46+
else if (typeof value === "object" &&
47+
value !== null &&
48+
!Array.isArray(value)) {
49+
// Objects
50+
if (!(0, utils_1.exists)(original[propName]) ||
51+
typeof original[propName] !== "object" ||
52+
Array.isArray(original[propName]) ||
53+
original[propName] === null) {
54+
original[propName] = {};
55+
}
56+
updateRec(original[propName], value);
57+
}
58+
else {
59+
// Primitives
60+
original[propName] = value;
61+
}
62+
}
63+
}
64+
}),
65+
/** Readonly access to the data. */
66+
data: data,
67+
// File operations
68+
readFile: fileSystem.readFile,
69+
writeFile: fileSystem.writeFile,
70+
deleteFile: fileSystem.deleteFile,
71+
};
72+
};
73+
}
74+
exports.capacitorStorage = capacitorStorage;

dist/ClientStorage/ClientStorage.d.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export type Json = number | string | boolean | null | undefined | JsonObject;
2+
export type JsonObject = {
3+
[propName: string]: Json;
4+
};
5+
export type GetClientStorage = <T extends JsonObject>(fileName: string, init: T) => Promise<ClientStorage<T>>;
6+
type DeepReadonlyJsonObject<T> = T extends object ? {
7+
readonly [K in keyof T]: DeepReadonlyJsonObject<T[K]>;
8+
} : T;
9+
type SpecificJsonObject<T extends JsonObject> = DeepReadonlyJsonObject<Partial<T>>;
10+
export type ClientStorage<T extends JsonObject> = {
11+
/** Adds and updates the given data */
12+
updateData(updates: SpecificJsonObject<T>): void;
13+
/** Readonly access to the data. */
14+
data: SpecificJsonObject<T>;
15+
readFile: (path: string) => Promise<string | undefined>;
16+
writeFile: (path: string, data: string) => Promise<void>;
17+
deleteFile: (path: string) => Promise<void>;
18+
};
19+
export {};

dist/ClientStorage/ClientStorage.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });

dist/Define.d.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { _Doc, _List, _defineAppDataStructure } from "./Implement";
2+
import { capacitorStorage as _capacitorStorage } from "./ClientStorage/Capacitor";
3+
/** We should look into how drizzle orm handles definitions to get some inspiration for how to clean ours up. */
4+
export type Doc<T extends {} = {}> = _Doc<T>;
5+
export type List<T extends _Doc<{}> = _Doc<{}>> = _List<T>;
6+
export declare const defineAppDataStructure: typeof _defineAppDataStructure;
7+
export declare const capacitorStorage: typeof _capacitorStorage;
8+
/** Defines a prop of the specified primitive type. */
9+
export declare function prim<T extends number | string | boolean>(
10+
/** TODO: We might consider using Number, String, or Boolean as the first parameter
11+
* instead. */
12+
defaultValue: T | null | (() => T | null)): {
13+
format: "prim";
14+
refTypeName: undefined;
15+
primType: T;
16+
isList: false;
17+
isDefining: false;
18+
defaultValue: T | (() => T | null) | null;
19+
};
20+
/** Defines a prop that references a doc. */
21+
export declare function refTo<T extends string>(table: T, defaultValue?: string | null): {
22+
format: "one";
23+
refTypeName: T;
24+
primType: undefined;
25+
isList: false;
26+
isDefining: false;
27+
defaultValue: string | null;
28+
};
29+
/** Defines a list of docs that point back to this doc. */
30+
export declare function listOf<T extends string>(table: T): {
31+
format: "many";
32+
refTypeName: T;
33+
primType: undefined;
34+
isList: true;
35+
isDefining: true;
36+
defaultValue: undefined;
37+
};
38+
/** Defines a prop that references a file. */
39+
export declare function file(defaultValue?: string | null): {
40+
format: "file";
41+
refTypeName: undefined;
42+
primType: undefined;
43+
isList: false;
44+
isDefining: false;
45+
defaultValue: string | null;
46+
};

dist/Define.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.file = exports.listOf = exports.refTo = exports.prim = exports.capacitorStorage = exports.defineAppDataStructure = void 0;
4+
const Implement_1 = require("./Implement");
5+
const Capacitor_1 = require("./ClientStorage/Capacitor");
6+
exports.defineAppDataStructure = Implement_1._defineAppDataStructure;
7+
exports.capacitorStorage = Capacitor_1.capacitorStorage;
8+
/** Defines a prop of the specified primitive type. */
9+
function prim(
10+
/** TODO: We might consider using Number, String, or Boolean as the first parameter
11+
* instead. */
12+
defaultValue) {
13+
return {
14+
format: `prim`,
15+
refTypeName: undefined,
16+
primType: {},
17+
isList: false,
18+
isDefining: false,
19+
defaultValue: defaultValue,
20+
};
21+
}
22+
exports.prim = prim;
23+
/** Defines a prop that references a doc. */
24+
function refTo(table, defaultValue = null) {
25+
return {
26+
format: `one`,
27+
refTypeName: table,
28+
primType: undefined,
29+
isList: false,
30+
isDefining: false,
31+
defaultValue: defaultValue,
32+
};
33+
}
34+
exports.refTo = refTo;
35+
/** Defines a list of docs that point back to this doc. */
36+
function listOf(table) {
37+
return {
38+
format: `many`,
39+
refTypeName: table,
40+
primType: undefined,
41+
isList: true,
42+
isDefining: true,
43+
defaultValue: undefined,
44+
};
45+
}
46+
exports.listOf = listOf;
47+
/** Defines a prop that references a file. */
48+
function file(defaultValue = null) {
49+
return {
50+
format: `file`,
51+
refTypeName: undefined,
52+
primType: undefined,
53+
isList: false,
54+
isDefining: false,
55+
defaultValue: defaultValue,
56+
};
57+
}
58+
exports.file = file;

dist/Implement.d.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { FirebaseOptions } from "firebase/app";
2+
import { SchemaDictToTsType, SchemaToTsType, TypeSchemaDict, RootSchema } from "./Parse";
3+
import { LocalCache } from "./LocalCache";
4+
import { GetClientStorage } from "./ClientStorage/ClientStorage";
5+
export type Computed<T> = {
6+
get value(): T;
7+
};
8+
export type Signal<T> = {
9+
set value(newValue: T);
10+
} & Computed<T>;
11+
declare let _computed: <T>(compute: () => T) => Computed<T>;
12+
declare let _signal: <T>(initialValue: T) => Signal<T>;
13+
declare let _isSignal: (obj: any) => obj is Signal<any>;
14+
declare let _watchEffect: (effect: () => void) => void;
15+
export type _Doc<T extends {} = {}> = {
16+
[K in keyof T]: T[K] | undefined;
17+
} & _DocSpecificProps;
18+
export type _DocSpecificProps = {
19+
readonly _id: string | null | undefined;
20+
readonly isLoaded: boolean;
21+
readonly isDeleted: boolean;
22+
deleteDoc(): Promise<void>;
23+
};
24+
export declare function docProx<TypeName extends string, F extends TypeSchemaDict, T extends _Doc<{}> = SchemaToTsType<TypeName, F>>(docId: string | null | undefined, typeName: TypeName, objFormats: F, localCache: LocalCache): T;
25+
export type _List<T extends _Doc> = {
26+
[Symbol.iterator]: () => IterableIterator<T>;
27+
readonly length: number;
28+
filter(filterFn: (doc: T) => boolean): _List<T>;
29+
map<R>(mapFn: (doc: T) => R): Array<R>;
30+
add(params: Partial<T>): T;
31+
};
32+
export declare function _defineAppDataStructure<RS extends RootSchema, TSD extends TypeSchemaDict>(modelName: string, firebaseOptions: FirebaseOptions, reactivity: {
33+
computed: typeof _computed;
34+
signal: typeof _signal;
35+
isSignal: typeof _isSignal;
36+
watchEffect: typeof _watchEffect;
37+
}, options: {
38+
isProduction: boolean;
39+
getClientStorage: GetClientStorage;
40+
rootSchema: RS;
41+
typeSchemas: TSD;
42+
}): {
43+
getAppData: () => { [K in keyof RS]: _List<SchemaToTsType<NonNullable<RS[K]["refTypeName"]>, TSD>>; };
44+
types: SchemaDictToTsType<TSD>;
45+
};
46+
export {};

0 commit comments

Comments
 (0)