@rolder-kit/surreal-db 0.1.0-alpha.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/dist/deserialize.d.ts +17 -0
- package/dist/deserialize.js +46 -0
- package/dist/getAuthedDBSession.d.ts +5 -0
- package/dist/getAuthedDBSession.js +13 -0
- package/dist/getDBInstance.d.ts +3 -0
- package/dist/getDBInstance.js +30 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/types.d.ts +10 -0
- package/dist/types.js +0 -0
- package/package.json +24 -0
|
@@ -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,13 @@
|
|
|
1
|
+
import { getDBInstance } from "./getDBInstance.js";
|
|
2
|
+
const sessions = new Map();
|
|
3
|
+
const getAuthedDBSession = async (user, token, surrealDBProps = {})=>{
|
|
4
|
+
const db = await getDBInstance(surrealDBProps);
|
|
5
|
+
if (sessions.has(user.id)) return sessions.get(user.id);
|
|
6
|
+
{
|
|
7
|
+
const dbSession = await db.newSession();
|
|
8
|
+
sessions.set(user.id, dbSession);
|
|
9
|
+
await dbSession.authenticate(token);
|
|
10
|
+
return dbSession;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
export { getAuthedDBSession };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Surreal, applyDiagnostics, createRemoteEngines } from "surrealdb";
|
|
2
|
+
let db = null;
|
|
3
|
+
const getDBInstance = async (params = {})=>{
|
|
4
|
+
if (db?.isConnected) return db;
|
|
5
|
+
const instance = new Surreal(params.debug ? {
|
|
6
|
+
engines: applyDiagnostics(createRemoteEngines(), (event)=>{
|
|
7
|
+
console.log('Surreal debug:', event);
|
|
8
|
+
})
|
|
9
|
+
} : void 0);
|
|
10
|
+
try {
|
|
11
|
+
const url = params.url || process.env.SURREALDB_URL;
|
|
12
|
+
if (!url) throw new Error('Missing required SurrealDB URL');
|
|
13
|
+
const namespace = params.namespace || process.env.SURREALDB_NAMESPACE;
|
|
14
|
+
if (!namespace) throw new Error('Missing required SurrealDB namespace');
|
|
15
|
+
const database = params.database || process.env.SURREALDB_DATABASE;
|
|
16
|
+
if (!database) throw new Error('Missing required SurrealDB database');
|
|
17
|
+
await instance.connect(url);
|
|
18
|
+
await instance.use({
|
|
19
|
+
namespace,
|
|
20
|
+
database
|
|
21
|
+
});
|
|
22
|
+
db = instance;
|
|
23
|
+
return instance;
|
|
24
|
+
} catch (error) {
|
|
25
|
+
console.error('Failed to connect to SurrealDB:', error);
|
|
26
|
+
db = null;
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
export { getDBInstance };
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/types.d.ts
ADDED
package/dist/types.js
ADDED
|
File without changes
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rolder-kit/surreal-db",
|
|
3
|
+
"version": "0.1.0-alpha.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": {
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"import": "./dist/index.js"
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
"sideEffects": false,
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "rslib build",
|
|
17
|
+
"dev": "rslib build --watch",
|
|
18
|
+
"prepublishOnly": "bun run build",
|
|
19
|
+
"check": "biome check --write && tsc --noEmit"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"surrealdb": "^2.0.0-alpha.18"
|
|
23
|
+
}
|
|
24
|
+
}
|