-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
428 lines (368 loc) · 12.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import * as fs from 'fs/promises'
import * as path from 'path'
import dedent from 'string-dedent'
import Hyperschema from 'hyperschema'
import HyperDB from 'hyperdb/builder/index.js'
import * as templates from './templates/basic.js'
import * as examples from './templates/examples.js'
export class HyperdbHelper {
static defaultConfig = {
databaseConfigDirectory: './database',
generatedCodeDirectory: './generated',
functionsFilepath: './functions.js',
schemaFilepath: './schema.js',
configFilepath: './config.js',
projectPackageJsonFilepath: './package.json',
package: {}
}
/**
* Hyperdb Helper - A utility class for managing Hyperdb database schemas
* @class
* @classdesc Provides methods for initializing and building Hyperdb databases from schema files
*
* @property {Object} options - Configuration options merged with defaults
* @property {Object} config - Current active configuration
* @property {string[]} requiredDependencies - Required npm dependencies
*
* @example
* const helper = new HyperdbHelper();
* await helper.init();
* await helper.build();
*/
constructor(options = {}) {
this.config = { ...HyperdbHelper.defaultConfig, ...options }
this.requiredDependencies = ['hyperschema', 'hyperdb', 'corestore']
}
/**
* Initializes a new database schema directory with required configuration files
* @async
* @param {string} filepath - Path where the database schema directory should be created
* @param {Object} options - Configuration options
* @param {boolean} [options.examples] - Whether to include example code in generated files
* @returns {Promise<Object>} Result object
* @returns {string[]} result.dependenciesNeeded - Array of npm package names that need to be installed
* @throws {Error} If package.json is missing from project directory
* @throws {Error} If schema directory already exists
* @throws {Error} If example index.js file already exists when using examples flag
* @example
* const helper = new HyperdbHelper();
* const { dependenciesNeeded } = await helper.init('./mydb', { examples: true });
* if (dependenciesNeeded.length) {
* console.log('Please install:', dependenciesNeeded.join(' '));
* }
*/
async init(filepath, options) {
this.config = await this.mergeConfig(filepath, options)
this.validateConfig(this.config) // Add validation here
if (!(await exists('./package.json'))) {
console.log(dedent`
Please first create a package.json file in your project directory
and choose either "module" or "commonjs" as the "type" field
You can create a package.json file by running:
npm init
`)
throw new Error('Error: package.json not found in project directory')
}
await this.createDefaultFiles()
const dependenciesNeeded = this.checkPackageDependencies()
return { dependenciesNeeded }
}
/**
* Builds a database from schema files in the configured directory
* @async
* @param {string} filepath - Path to the database directory
* @param {Object} options - Configuration options
* @param {boolean} [options.examples] - Whether to include example code
* @throws {Error} If schema directory does not exist
* @throws {Error} If schema files cannot be loaded or processed
* @returns {Promise<void>}
* @example
* const helper = new HyperdbHelper();
* await helper.build('./mydb', { examples: true });
* // Generated database code will be in ./mydb/generated/
*/
async build(filepath, options) {
this.config = await this.mergeConfig(filepath, options)
if (!(await exists(this.config.databaseConfigDirectory))) {
throw new Error(dedent`
Error: Database directory not found at ${this.config.databaseConfigDirectory}
Run 'hyperdb-helper init' to create a new schema directory
`)
}
const schemaDefinitions = await this.getSchemaDefinitions(
this.config.schemaFilepath
)
await this.buildSchema(schemaDefinitions)
await this.createDatabaseConfigPackageJsonFile()
await this.createGeneratedPackageJsonFile()
}
/**
* Cleans up generated files and resources
* @async
* @method
* @description Removes the generated code directory and any temporary files created during the build process
* @throws {Error} If cleanup operation fails
* @returns {Promise<void>}
* @example
* const helper = new HyperdbHelper();
* await helper.init();
* await helper.build();
* // When done with the helper
* await helper.cleanup();
*/
async cleanup() {
try {
// Cleanup temporary files/resources
await fs.rm(this.config.generatedCodeDirectory, {
recursive: true,
force: true
})
} catch (error) {
this.logger.warn('Cleanup failed:', error)
}
}
validateConfig(config) {
const required = [
// Core directories
'databaseConfigDirectory', // Where the schema configuration lives
'generatedCodeDirectory', // Where generated code will be output
// Essential file paths for schema definition
'functionsFilepath', // Custom functions for the schema
'schemaFilepath', // The main schema definition file
'configFilepath', // Configuration file path
// Package.json related paths
'projectPackageJsonFilepath', // Project's package.json
'databaseConfigJsonFilepath', // Database config package.json
'generatedPackageJsonFilepath', // Generated code package.json
// Generated code directories
'hyperschemaDirectory', // Where schema definitions are generated
'hyperdbDirectory', // Where database code is generated
// Module configuration
'moduleType' // 'commonjs' or 'module'
]
const missing = required.filter((field) => !config[field])
if (missing.length > 0) {
throw new Error(dedent`
Missing required config fields:
${missing.map((field) => ` - ${field}`).join('\n')}
`)
}
}
async mergeConfig(filepath, options) {
const databaseConfigDirectory = getDatabaseConfigDirectory(filepath)
const configFilepath = path.join(databaseConfigDirectory, 'config.js')
let userConfig = {}
try {
userConfig = await getConfig(configFilepath)
} catch (error) {}
const config = {
...HyperdbHelper.defaultConfig,
...userConfig,
databaseConfigDirectory
}
config.examples = options.examples
config.functionsFilepath = getFilepathFromConfig(
config,
'functionsFilepath',
'./functions.js'
)
config.schemaFilepath = getFilepathFromConfig(
config,
'schemaFilepath',
'./schema.js'
)
config.configFilepath = getFilepathFromConfig(
config,
'configFilepath',
'./config.js'
)
config.projectPackageJsonFilepath = getFilepathFromConfig(
config,
'projectPackageJsonFilepath',
'./package.json'
)
config.databaseConfigJsonFilepath = path.join(
config.databaseConfigDirectory,
'package.json'
)
config.generatedCodeDirectory = getFilepathFromConfig(
config,
'generatedCodeDirectory',
'./generated'
)
config.generatedPackageJsonFilepath = path.join(
config.generatedCodeDirectory,
'package.json'
)
config.hyperschemaDirectory = path.join(
config.generatedCodeDirectory,
'schemas'
)
config.hyperdbDirectory = path.join(
config.generatedCodeDirectory,
'database'
)
try {
config.package = await getPackageJson(process.cwd())
} catch (error) {}
if (config.package) {
config.moduleType = config.package.type || 'commonjs'
}
return config
}
async getConfig(configFilepath) {
const module = await import(configFilepath)
return { ...module.default }
}
async createDefaultFiles() {
if (await exists(this.config.databaseConfigDirectory)) {
throw new Error(
`Error: Schema directory already exists at ${this.config.databaseConfigDirectory}`
)
}
const exampleIndexFilepath = path.join(
path.dirname(this.config.databaseConfigDirectory),
'index.js'
)
if (this.config.examples) {
if (await exists(exampleIndexFilepath)) {
throw new Error(
`Error: index.js file already exists at ${exampleIndexFilepath}`
)
}
}
await fs.mkdir(this.config.databaseConfigDirectory, {
recursive: true
})
await this.createConfigFile()
await this.createFunctionsFile()
await this.createSchemaFile()
await fs.mkdir(this.config.generatedCodeDirectory)
await this.createDatabaseConfigPackageJsonFile()
await this.createGeneratedPackageJsonFile()
if (this.config.examples) {
await this.createExampleIndexFile(exampleIndexFilepath)
}
}
async createConfigFile() {
await fs.writeFile(
this.config.configFilepath,
templates.configFileTemplate(this.config.moduleType)
)
}
async createFunctionsFile() {
const template = this.config.examples
? examples.functionFileTemplate()
: templates.functionFileTemplate()
await fs.writeFile(this.config.functionsFilepath, template)
}
async createSchemaFile() {
const content = this.config.examples
? examples.schemaFileTemplate()
: templates.schemaFileTemplate()
await fs.writeFile(this.config.schemaFilepath, content)
}
async createDatabaseConfigPackageJsonFile() {
const content = JSON.stringify({
main: './schema.js',
exports: {
'.': './schema.js',
'./config': './config.js'
},
type: 'module'
})
await fs.writeFile(this.config.databaseConfigJsonFilepath, content)
}
async createGeneratedPackageJsonFile() {
const content = JSON.stringify({
main: './database/index.js',
exports: {
'.': './database/index.js',
'./db.json': './database/db.json',
'./messages': './database/messages.js',
'./schemas': './schemas/index.js',
'./schema.json': './schemas/schema.json'
},
type: 'commonjs'
})
await fs.writeFile(this.config.generatedPackageJsonFilepath, content)
}
async createExampleIndexFile(indexFilepath) {
const definitionsFilepath = path.join(
this.config.hyperdbDirectory,
'index.js'
)
const projectDirectory = path.dirname(this.config.databaseConfigDirectory)
const relativePath = path.relative(projectDirectory, definitionsFilepath)
console.log('this.config.moduleType', this.config.moduleType)
const content = examples.exampleIndexFileTemplate({
relativePath,
moduleType: this.config.moduleType
})
await fs.writeFile(indexFilepath, content)
}
checkPackageDependencies() {
if (!this.config.package) {
return this.requiredDependencies
}
const dependencies = this.config.package.dependencies || {}
const toInstall = []
for (const dependency of this.requiredDependencies) {
if (!dependencies[dependency]) {
toInstall.push(dependency)
}
}
return toInstall
}
async getSchemaDefinitions(schemaFilepath) {
const module = await import(schemaFilepath)
return { ...module }
}
async buildSchema(schemaModule) {
const hyperschema = Hyperschema.from(this.config.hyperschemaDirectory)
schemaModule.createSchema(hyperschema)
Hyperschema.toDisk(hyperschema)
const hyperdb = HyperDB.from(
this.config.hyperschemaDirectory,
this.config.hyperdbDirectory
)
schemaModule.createDatabase(hyperdb)
HyperDB.toDisk(hyperdb)
}
}
async function exists(filepath) {
try {
await fs.access(filepath)
return true
} catch (error) {
if (error.code === 'ENOENT') {
return false
}
throw error
}
}
function getDatabaseConfigDirectory(databaseFilepathArgument = './database') {
if (path.isAbsolute(databaseFilepathArgument)) {
return databaseFilepathArgument
}
return path.join(process.cwd(), databaseFilepathArgument)
}
function getFilepathFromConfig(config, configProperty, defaultFilepath) {
const filepath = config[configProperty] || defaultFilepath
if (path.isAbsolute(filepath)) {
return filepath
}
return path.join(config.databaseConfigDirectory, filepath)
}
async function getPackageJson(dir = process.cwd()) {
const packageJsonFilepath = path.join(dir, 'package.json')
if (!(await exists(packageJsonFilepath))) {
return
}
const file = await fs.readFile(packageJsonFilepath, 'utf8')
return JSON.parse(file)
}
async function getConfig(configFilepath) {
const module = await import(configFilepath)
return { ...module.default }
}