@rolder/kit 3.0.0-alpha.79 → 3.0.0-alpha.80
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/tanstackFunctions/index.d.ts +1 -0
- package/dist/tanstackFunctions/index.js +1 -0
- package/dist/tanstackFunctions/surreal/connection.d.ts +9 -0
- package/dist/tanstackFunctions/surreal/connection.js +49 -0
- package/dist/tanstackFunctions/surreal/deafaultCrud.d.ts +2 -0
- package/dist/tanstackFunctions/surreal/deafaultCrud.js +18 -0
- package/dist/tanstackFunctions/surreal/deserialize.d.ts +17 -0
- package/dist/tanstackFunctions/surreal/deserialize.js +46 -0
- package/dist/tanstackFunctions/surreal/encryption.d.ts +6 -0
- package/dist/tanstackFunctions/surreal/encryption.js +30 -0
- package/dist/tanstackFunctions/surreal/index.d.ts +4 -0
- package/dist/tanstackFunctions/surreal/index.js +4 -0
- package/package.json +1 -1
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type CodecOptions, Surreal } from 'surrealdb';
|
|
2
|
+
export declare const getDB: (params?: {
|
|
3
|
+
url?: string;
|
|
4
|
+
namespace?: string;
|
|
5
|
+
database?: string;
|
|
6
|
+
username?: string;
|
|
7
|
+
password?: string;
|
|
8
|
+
codecOptions?: CodecOptions;
|
|
9
|
+
} | undefined) => Promise<Surreal>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { createServerOnlyFn } from "@tanstack/react-start";
|
|
2
|
+
import { getCookie } from "@tanstack/react-start/server";
|
|
3
|
+
import { DateTime, Surreal } from "surrealdb";
|
|
4
|
+
let db = null;
|
|
5
|
+
const getDB = createServerOnlyFn(async (params = {})=>{
|
|
6
|
+
if (db?.isConnected) return db;
|
|
7
|
+
const locale = getCookie('locale') || 'ru-RU';
|
|
8
|
+
const timeZone = getCookie('tz') || 'UTC';
|
|
9
|
+
const instance = new Surreal({
|
|
10
|
+
codecOptions: params.codecOptions || {
|
|
11
|
+
valueDecodeVisitor (value) {
|
|
12
|
+
if (value instanceof DateTime) return new Date(value.toDate()).toLocaleDateString(locale, {
|
|
13
|
+
hour: 'numeric',
|
|
14
|
+
minute: 'numeric',
|
|
15
|
+
timeZone
|
|
16
|
+
});
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
try {
|
|
22
|
+
const url = params.url || process.env.SURREALDB_URL;
|
|
23
|
+
if (!url) throw new Error('Missing required SurrealDB URL');
|
|
24
|
+
const namespace = params.namespace || process.env.SURREALDB_NAMESPACE;
|
|
25
|
+
if (!namespace) throw new Error('Missing required SurrealDB namespace');
|
|
26
|
+
const database = params.database || process.env.SURREALDB_DATABASE;
|
|
27
|
+
if (!database) throw new Error('Missing required SurrealDB database');
|
|
28
|
+
const username = params.username || process.env.SURREALDB_USERNAME;
|
|
29
|
+
const password = params.password || process.env.SURREALDB_PASSWORD;
|
|
30
|
+
if (username && password) await instance.connect(url, {
|
|
31
|
+
authentication: {
|
|
32
|
+
username,
|
|
33
|
+
password
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
else await instance.connect(url);
|
|
37
|
+
await instance.use({
|
|
38
|
+
namespace,
|
|
39
|
+
database
|
|
40
|
+
});
|
|
41
|
+
db = instance;
|
|
42
|
+
return instance;
|
|
43
|
+
} catch (error) {
|
|
44
|
+
console.error('Failed to connect to SurrealDB:', error);
|
|
45
|
+
db = null;
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
export { getDB };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export declare const surrealDeleteFn: import("@tanstack/start-client-core").RequiredFetcher<undefined, (data: string) => string, Promise<void>>;
|
|
2
|
+
export declare const surrealUnsubscribeFn: import("@tanstack/start-client-core").RequiredFetcher<undefined, (data: string) => string, Promise<void>>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { createServerFn } from "@tanstack/react-start";
|
|
2
|
+
import { getDB } from "./connection.js";
|
|
3
|
+
import { deserialize } from "./deserialize.js";
|
|
4
|
+
const surrealDeleteFn = createServerFn({
|
|
5
|
+
method: 'POST'
|
|
6
|
+
}).inputValidator((data)=>data).handler(async ({ data })=>{
|
|
7
|
+
const db = await getDB();
|
|
8
|
+
const id = deserialize(data);
|
|
9
|
+
await db.delete(id);
|
|
10
|
+
});
|
|
11
|
+
const surrealUnsubscribeFn = createServerFn({
|
|
12
|
+
method: 'POST'
|
|
13
|
+
}).inputValidator((data)=>data).handler(async ({ data })=>{
|
|
14
|
+
const db = await getDB();
|
|
15
|
+
const live = await db.liveOf(data);
|
|
16
|
+
await live.kill();
|
|
17
|
+
});
|
|
18
|
+
export { surrealDeleteFn, surrealUnsubscribeFn };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { RecordId } from 'surrealdb';
|
|
2
|
+
/**
|
|
3
|
+
* Type that converts specified string paths to RecordId, others stay as their original types
|
|
4
|
+
*/
|
|
5
|
+
type DeserializeResult<T, IdPaths extends string = never> = T extends string ? RecordId<string> : T extends (infer U)[] ? U extends object ? {
|
|
6
|
+
[K in keyof U]: K extends IdPaths ? RecordId<string> : U[K];
|
|
7
|
+
}[] : DeserializeResult<U, IdPaths>[] : T extends object ? T extends Date ? T : {
|
|
8
|
+
[K in keyof T]: K extends IdPaths ? RecordId<string> : T[K];
|
|
9
|
+
} : T;
|
|
10
|
+
/**
|
|
11
|
+
* Deserializes DTO back to SurrealDB Record recursively based on specified ID paths
|
|
12
|
+
*/
|
|
13
|
+
export declare function deserialize<T, K extends keyof T & string>(dto: T[], idPaths: K[]): DeserializeResult<T, K>[];
|
|
14
|
+
export declare function deserialize<T, K extends keyof T & string>(dto: T, idPaths: K[]): DeserializeResult<T, K>;
|
|
15
|
+
export declare function deserialize<T>(dto: T[]): DeserializeResult<T>[];
|
|
16
|
+
export declare function deserialize<T>(dto: T): DeserializeResult<T>;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { RecordId } from "surrealdb";
|
|
2
|
+
const extractTableName = (str)=>{
|
|
3
|
+
const match = str.match(/^([^:]+):/);
|
|
4
|
+
return match ? match[1] : null;
|
|
5
|
+
};
|
|
6
|
+
const isRecordIdFormat = (str)=>/^[^:]+:.+$/.test(str);
|
|
7
|
+
function deserialize(dto, idPaths) {
|
|
8
|
+
if (Array.isArray(dto)) return dto.map((item)=>convertStringsToRecordIds(item, idPaths || []));
|
|
9
|
+
return convertStringsToRecordIds(dto, idPaths || []);
|
|
10
|
+
}
|
|
11
|
+
const convertStringsToRecordIds = (obj, idPaths, currentPath = '')=>{
|
|
12
|
+
if (null == obj) return obj;
|
|
13
|
+
if (Array.isArray(obj)) {
|
|
14
|
+
if (idPaths.includes(currentPath)) return obj.map((item)=>{
|
|
15
|
+
if ('string' == typeof item && isRecordIdFormat(item)) {
|
|
16
|
+
const tableName = extractTableName(item);
|
|
17
|
+
if (tableName) return new RecordId(tableName, item.split(':')[1]);
|
|
18
|
+
}
|
|
19
|
+
return item;
|
|
20
|
+
});
|
|
21
|
+
return obj.map((item, index)=>convertStringsToRecordIds(item, idPaths, `${currentPath}[${index}]`));
|
|
22
|
+
}
|
|
23
|
+
if ('object' == typeof obj && !(obj instanceof Date)) {
|
|
24
|
+
const result = {};
|
|
25
|
+
for (const [key, value] of Object.entries(obj)){
|
|
26
|
+
const fieldPath = currentPath ? `${currentPath}.${key}` : key;
|
|
27
|
+
if ('string' == typeof value) {
|
|
28
|
+
const shouldConvert = idPaths.includes(fieldPath) && isRecordIdFormat(value);
|
|
29
|
+
if (shouldConvert) {
|
|
30
|
+
const tableName = extractTableName(value);
|
|
31
|
+
if (tableName) result[key] = new RecordId(tableName, value.split(':')[1]);
|
|
32
|
+
else result[key] = value;
|
|
33
|
+
} else result[key] = value;
|
|
34
|
+
} else result[key] = convertStringsToRecordIds(value, idPaths, fieldPath);
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
if ('string' == typeof obj) {
|
|
39
|
+
if (0 === idPaths.length && isRecordIdFormat(obj)) {
|
|
40
|
+
const tableName = extractTableName(obj);
|
|
41
|
+
if (tableName) return new RecordId(tableName, obj.split(':')[1]);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return obj;
|
|
45
|
+
};
|
|
46
|
+
export { deserialize };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
|
|
2
|
+
import { createServerOnlyFn } from "@tanstack/react-start";
|
|
3
|
+
const encryptionFn = createServerOnlyFn((secretHash)=>{
|
|
4
|
+
class Encryption {
|
|
5
|
+
key;
|
|
6
|
+
constructor(secretHash){
|
|
7
|
+
this.key = Buffer.from(secretHash, 'hex');
|
|
8
|
+
}
|
|
9
|
+
encrypt(text) {
|
|
10
|
+
const algorithm = 'aes-256-cbc';
|
|
11
|
+
const iv = randomBytes(16);
|
|
12
|
+
const cipher = createCipheriv(algorithm, this.key, iv);
|
|
13
|
+
let encrypted = cipher.update(text, 'utf8', 'hex');
|
|
14
|
+
encrypted += cipher.final('hex');
|
|
15
|
+
return `${iv.toString('hex')}:${encrypted}`;
|
|
16
|
+
}
|
|
17
|
+
decrypt(encryptedText) {
|
|
18
|
+
const algorithm = 'aes-256-cbc';
|
|
19
|
+
const parts = encryptedText.split(':');
|
|
20
|
+
const iv = Buffer.from(parts[0], 'hex');
|
|
21
|
+
const encrypted = parts[1];
|
|
22
|
+
const decipher = createDecipheriv(algorithm, this.key, iv);
|
|
23
|
+
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
|
|
24
|
+
decrypted += decipher.final('utf8');
|
|
25
|
+
return decrypted;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return new Encryption(secretHash);
|
|
29
|
+
});
|
|
30
|
+
export { encryptionFn };
|