rads-db 3.0.28 → 3.0.29

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 CHANGED
@@ -312,9 +312,12 @@ interface RestFileUploadDriverOptions {
312
312
  * Otherwise, "application/json" body (with base64-encoded binary content) will be used */
313
313
  useFormData?: boolean;
314
314
  }
315
- type GenerateClientOptions = Partial<GenerateClientNormalizedOptions>;
315
+ type GenerateClientOptions = GenerateClientNormalizedOptions;
316
316
  interface GenerateClientNormalizedOptions {
317
- entitiesDir: string;
317
+ /** For servers - path to your entities directory, e.g. "./entities" */
318
+ entitiesDir?: string;
319
+ /** For client-only integrations - base url of your API, e.g. "https://my-website/api" */
320
+ apiUrl?: string;
318
321
  }
319
322
  interface RadsVitePluginOptions extends GenerateClientOptions {
320
323
  /** To simplify debugging, makes modules available on window.
@@ -1,7 +1,20 @@
1
1
  // #!/usr/bin/env node
2
2
  "use strict";
3
3
 
4
+ var _nodeUtil = require("node:util");
4
5
  var _node = require("./node.cjs");
5
- (0, _node.generateClient)({
6
- entitiesDir: "./test/entities"
7
- });
6
+ const args = (0, _nodeUtil.parseArgs)({
7
+ options: {
8
+ entitiesDir: {
9
+ type: "string",
10
+ short: "d"
11
+ },
12
+ apiUrl: {
13
+ type: "string",
14
+ short: "u"
15
+ }
16
+ },
17
+ allowPositionals: false,
18
+ strict: true
19
+ });
20
+ (0, _node.generateClient)(args.values);
@@ -1,3 +1,18 @@
1
1
  #!/usr/bin/env node
2
+ import { parseArgs } from "node:util";
2
3
  import { generateClient } from "./node.mjs";
3
- generateClient({ entitiesDir: "./test/entities" });
4
+ const args = parseArgs({
5
+ options: {
6
+ entitiesDir: {
7
+ type: "string",
8
+ short: "d"
9
+ },
10
+ apiUrl: {
11
+ type: "string",
12
+ short: "u"
13
+ }
14
+ },
15
+ allowPositionals: false,
16
+ strict: true
17
+ });
18
+ generateClient(args.values);
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.generateClient = generateClient;
7
7
  exports.getIndexDts = getIndexDts;
8
+ exports.getSchema = getSchema;
8
9
  var _promises = _interopRequireDefault(require("node:fs/promises"));
9
10
  var _nodeFs = _interopRequireDefault(require("node:fs"));
10
11
  var _nodePath = _interopRequireDefault(require("node:path"));
@@ -12,26 +13,9 @@ var _lodash = _interopRequireDefault(require("lodash"));
12
13
  var _lib = require("./lib.cjs");
13
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
15
  async function generateClient(options) {
15
- const normalizedOptions = {
16
- entitiesDir: "./entities",
17
- ...options
18
- };
19
- const {
20
- entitiesDir
21
- } = normalizedOptions;
16
+ const normalizedOptions = normalizeOptions(options);
22
17
  console.time("[rads-db] Schema generated in");
23
- if (!_nodeFs.default.existsSync(entitiesDir)) await _promises.default.mkdir(entitiesDir);
24
- const response = await _promises.default.readdir(entitiesDir, {
25
- withFileTypes: true
26
- });
27
- const entities = {};
28
- for (const file of response) {
29
- if (!file.isFile()) continue;
30
- if (!file.name.endsWith(".ts")) continue;
31
- const text = await _promises.default.readFile(_nodePath.default.resolve(entitiesDir, file.name), "utf-8");
32
- entities[file.name.slice(0, -3)] = text;
33
- }
34
- const schema = (0, _lib.parseSchema)(entities);
18
+ const schema = await getSchema(normalizedOptions);
35
19
  const nodeModulesPath = _nodePath.default.resolve("./node_modules");
36
20
  const radsDbPath = _nodePath.default.join(nodeModulesPath, "./_rads-db");
37
21
  if (!_nodeFs.default.existsSync(nodeModulesPath)) {
@@ -59,6 +43,53 @@ export const schema = ${JSON.stringify(schema)}
59
43
  }, null, 2));
60
44
  console.timeEnd("[rads-db] Schema generated in");
61
45
  }
46
+ async function getSchema(normalizedOptions) {
47
+ const {
48
+ entitiesDir,
49
+ apiUrl
50
+ } = normalizedOptions;
51
+ let schema;
52
+ if (entitiesDir) {
53
+ if (!_nodeFs.default.existsSync(entitiesDir)) await _promises.default.mkdir(entitiesDir);
54
+ const response = await _promises.default.readdir(entitiesDir, {
55
+ withFileTypes: true
56
+ });
57
+ const entities = {};
58
+ for (const file of response) {
59
+ if (!file.isFile()) continue;
60
+ if (!file.name.endsWith(".ts")) continue;
61
+ const text = await _promises.default.readFile(_nodePath.default.resolve(entitiesDir, file.name), "utf-8");
62
+ entities[file.name.slice(0, -3)] = text;
63
+ }
64
+ schema = (0, _lib.parseSchema)(entities);
65
+ } else if (apiUrl) {
66
+ const fetchResponse = await fetch(_nodePath.default.join(apiUrl, "radsTunnel"), {
67
+ method: "POST",
68
+ headers: {
69
+ "content-type": "application/json"
70
+ },
71
+ body: JSON.stringify({
72
+ method: "_schema"
73
+ })
74
+ });
75
+ schema = await fetchResponse.json();
76
+ if (!schema) {
77
+ throw new Error("Could not download schema from the server. Please check the server URL.");
78
+ }
79
+ } else {
80
+ throw new Error("Please specify entitiesDir or apiUrl");
81
+ }
82
+ return schema;
83
+ }
84
+ function normalizeOptions(options) {
85
+ options = options || {};
86
+ if (options.entitiesDir && options.apiUrl) throw new Error('You cannot specify both "apiUrl" and "entitiesDir". Choose one.');
87
+ if (options.apiUrl) return options;
88
+ return {
89
+ entitiesDir: "./entities",
90
+ ...options
91
+ };
92
+ }
62
93
  function getIndexDts(schema, options) {
63
94
  const imports = [];
64
95
  const rootFields = [];
@@ -66,7 +97,7 @@ function getIndexDts(schema, options) {
66
97
  const entityMeta = [];
67
98
  for (const key in schema) {
68
99
  const type = schema[key];
69
- if (!type.decorators.entity) continue;
100
+ if (!type.fields) continue;
70
101
  imports.push(`import type { ${type.name} } from '../../${options.entitiesDir}/${key}'`);
71
102
  rootFields.push(`${type.handle}: EntityMethods<${type.name}, '${type.name}', ${type.name}_Where>`);
72
103
  const fieldsArray = Object.values(type.fields);
@@ -1,3 +1,4 @@
1
1
  import type { GenerateClientNormalizedOptions, GenerateClientOptions, Schema } from '@/types';
2
2
  export declare function generateClient(options?: GenerateClientOptions): Promise<void>;
3
+ export declare function getSchema(normalizedOptions: GenerateClientNormalizedOptions): Promise<any>;
3
4
  export declare function getIndexDts(schema: Schema, options: GenerateClientNormalizedOptions): string;
@@ -4,22 +4,9 @@ import path from "node:path";
4
4
  import _ from "lodash";
5
5
  import { parseSchema } from "./lib.mjs";
6
6
  export async function generateClient(options) {
7
- const normalizedOptions = { entitiesDir: "./entities", ...options };
8
- const { entitiesDir } = normalizedOptions;
7
+ const normalizedOptions = normalizeOptions(options);
9
8
  console.time("[rads-db] Schema generated in");
10
- if (!fs2.existsSync(entitiesDir))
11
- await fs.mkdir(entitiesDir);
12
- const response = await fs.readdir(entitiesDir, { withFileTypes: true });
13
- const entities = {};
14
- for (const file of response) {
15
- if (!file.isFile())
16
- continue;
17
- if (!file.name.endsWith(".ts"))
18
- continue;
19
- const text = await fs.readFile(path.resolve(entitiesDir, file.name), "utf-8");
20
- entities[file.name.slice(0, -3)] = text;
21
- }
22
- const schema = parseSchema(entities);
9
+ const schema = await getSchema(normalizedOptions);
23
10
  const nodeModulesPath = path.resolve("./node_modules");
24
11
  const radsDbPath = path.join(nodeModulesPath, "./_rads-db");
25
12
  if (!fs2.existsSync(nodeModulesPath)) {
@@ -56,6 +43,46 @@ export const schema = ${JSON.stringify(schema)}
56
43
  );
57
44
  console.timeEnd("[rads-db] Schema generated in");
58
45
  }
46
+ export async function getSchema(normalizedOptions) {
47
+ const { entitiesDir, apiUrl } = normalizedOptions;
48
+ let schema;
49
+ if (entitiesDir) {
50
+ if (!fs2.existsSync(entitiesDir))
51
+ await fs.mkdir(entitiesDir);
52
+ const response = await fs.readdir(entitiesDir, { withFileTypes: true });
53
+ const entities = {};
54
+ for (const file of response) {
55
+ if (!file.isFile())
56
+ continue;
57
+ if (!file.name.endsWith(".ts"))
58
+ continue;
59
+ const text = await fs.readFile(path.resolve(entitiesDir, file.name), "utf-8");
60
+ entities[file.name.slice(0, -3)] = text;
61
+ }
62
+ schema = parseSchema(entities);
63
+ } else if (apiUrl) {
64
+ const fetchResponse = await fetch(path.join(apiUrl, "radsTunnel"), {
65
+ method: "POST",
66
+ headers: { "content-type": "application/json" },
67
+ body: JSON.stringify({ method: "_schema" })
68
+ });
69
+ schema = await fetchResponse.json();
70
+ if (!schema) {
71
+ throw new Error("Could not download schema from the server. Please check the server URL.");
72
+ }
73
+ } else {
74
+ throw new Error("Please specify entitiesDir or apiUrl");
75
+ }
76
+ return schema;
77
+ }
78
+ function normalizeOptions(options) {
79
+ options = options || {};
80
+ if (options.entitiesDir && options.apiUrl)
81
+ throw new Error('You cannot specify both "apiUrl" and "entitiesDir". Choose one.');
82
+ if (options.apiUrl)
83
+ return options;
84
+ return { entitiesDir: "./entities", ...options };
85
+ }
59
86
  export function getIndexDts(schema, options) {
60
87
  const imports = [];
61
88
  const rootFields = [];
@@ -63,7 +90,7 @@ export function getIndexDts(schema, options) {
63
90
  const entityMeta = [];
64
91
  for (const key in schema) {
65
92
  const type = schema[key];
66
- if (!type.decorators.entity)
93
+ if (!type.fields)
67
94
  continue;
68
95
  imports.push(`import type { ${type.name} } from '../../${options.entitiesDir}/${key}'`);
69
96
  rootFields.push(`${type.handle}: EntityMethods<${type.name}, '${type.name}', ${type.name}_Where>`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rads-db",
3
- "version": "3.0.28",
3
+ "version": "3.0.29",
4
4
  "files": [
5
5
  "dist",
6
6
  "drivers",