-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55512f1
commit b0fd340
Showing
8 changed files
with
489 additions
and
344 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,17 +6,7 @@ | |
"node": ">=18" | ||
}, | ||
"description": "A small tool which generates a typescript client for SurrealDB based on the schema of a given database", | ||
"keywords": [ | ||
"typescript", | ||
"surrealdb", | ||
"client", | ||
"javascript", | ||
"zod", | ||
"orm", | ||
"database", | ||
"generator", | ||
"tool" | ||
], | ||
"keywords": ["typescript", "surrealdb", "client", "javascript", "zod", "orm", "database", "generator", "tool"], | ||
"author": { | ||
"name": "Sebastian Wessel", | ||
"url": "https://github.com/sebastianwessel" | ||
|
@@ -31,9 +21,7 @@ | |
"email": "[email protected]" | ||
}, | ||
"license": "MIT", | ||
"files": [ | ||
"dist" | ||
], | ||
"files": ["dist"], | ||
"bin": { | ||
"surql-gen": "./dist/commonjs/index.js" | ||
}, | ||
|
@@ -47,16 +35,16 @@ | |
}, | ||
"devDependencies": { | ||
"@biomejs/biome": "^1.8.3", | ||
"@types/node": "^20.14.9", | ||
"tshy": "^1.15.1", | ||
"@types/node": "^20.14.10", | ||
"tshy": "^3.0.2", | ||
"tsx": "^4.15.7", | ||
"typescript": "^5.5.2", | ||
"vitest": "^1.6.0" | ||
"vitest": "^2.0.1" | ||
}, | ||
"dependencies": { | ||
"commander": "^12.1.0", | ||
"mkdirp": "^3.0.1", | ||
"rimraf": "^5.0.7", | ||
"rimraf": "^6.0.0", | ||
"surrealdb.js": "^1.0.0-beta.9", | ||
"zod": "^3.23.8" | ||
}, | ||
|
@@ -70,12 +58,10 @@ | |
"./package.json": "./package.json", | ||
".": { | ||
"import": { | ||
"source": "./src/index.ts", | ||
"types": "./dist/esm/index.d.ts", | ||
"default": "./dist/esm/index.js" | ||
}, | ||
"require": { | ||
"source": "./src/index.ts", | ||
"types": "./dist/commonjs/index.d.ts", | ||
"default": "./dist/commonjs/index.js" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { existsSync, writeFileSync } from 'node:fs' | ||
import { join } from 'node:path' | ||
import { mkdirp } from 'mkdirp' | ||
|
||
export const ensureRecordSchema = async (rootPath: string) => { | ||
const content = `import z from 'zod'; | ||
import { RecordId } from 'surrealdb.js' | ||
export const ZRecordIdInstanceOf = z.instanceof(RecordId); | ||
export function recordId<Table extends string = string>(table?: Table) { | ||
return z.custom<RecordId<\`$Table\`>>( | ||
val => { | ||
const instanceOfCheck = ZRecordIdInstanceOf.safeParse(val); | ||
const tableCheck = table ? val?.tb === table : true; | ||
return instanceOfCheck.success && tableCheck; | ||
}, | ||
val => { | ||
let msgArray: string[] = []; | ||
const instanceOfCheck = ZRecordIdInstanceOf.safeParse(val); | ||
if (!instanceOfCheck.success) msgArray.push('Must be a RecordId class'); | ||
const tableCheck = table ? val?.tb === table : true; | ||
if (!tableCheck) msgArray.push(\`RecordId must be of type '\${table}', not '\${val?.tb}'\`); | ||
return { message: msgArray.join("; ") }; | ||
} | ||
); | ||
}` | ||
|
||
await mkdirp(rootPath) | ||
|
||
const fileName = join(rootPath, 'recordSchema.ts') | ||
|
||
console.log(fileName) | ||
if (!existsSync(fileName)) { | ||
writeFileSync(fileName, content, { flag: 'wx' }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters