|
1 | 1 | import fetch from 'node-fetch'; |
| 2 | +import _ from 'lodash'; |
2 | 3 | import fs from 'fs'; |
3 | 4 | import path from 'path'; |
4 | | - |
5 | | -import headers from '../utils/headers.js'; |
6 | | -import constants from '../utils/constants.js'; |
| 5 | +import crypto from 'crypto'; |
7 | 6 |
|
8 | 7 | function hash(value, salt) { |
9 | 8 | const hashItem = crypto.createHmac('sha512', salt); |
@@ -31,36 +30,106 @@ if (fs.existsSync(path.join(process.cwd(), '.replapirc.json'))) { |
31 | 30 |
|
32 | 31 | if (isExperimentalFeaturesEnabled) { |
33 | 32 | exportable = class Database { |
34 | | - constructor(replitdbtoken, salt, options) { |
35 | | - this.replitdbtoken = replitdbtoken || process.env.REPLIT_DB_URL.split('/')[4]; |
| 33 | + constructor(dbToken, salt = '', options = {}) { |
| 34 | + if (!process.env.REPLIT_DB_URL) throw new Error('Please run the Database Class on a Replit Project only.'); |
| 35 | + |
| 36 | + this.dbToken = dbToken || process.env.REPLIT_DB_URL; |
36 | 37 | this.salt = salt; |
37 | | - this.options = options; |
| 38 | + this.options = { |
| 39 | + id: String(dbToken).split('/')[4] || process.env.REPLIT_DB_URL.split('/')[4], |
| 40 | + owner: process.env.REPL_OWNER, |
| 41 | + collaborators: { ...options.collaborators }, |
| 42 | + password: { ...hash(String(options.password), String(salt)) }, |
| 43 | + type: options.type, |
| 44 | + encrypted: options.encrypted || [false], |
| 45 | + 'max-items': options['max-items'] || 10, |
| 46 | + }; |
| 47 | + } |
| 48 | + |
| 49 | + async init() { |
| 50 | + const isDatabaseSetup = await fetch(`${this.dbToken}/${encodeURIComponent('replapi_database_config')}`, { |
| 51 | + method: 'GET', |
| 52 | + }).then((res) => res.text()); |
| 53 | + |
| 54 | + if (!isDatabaseSetup) { |
| 55 | + if (this.options.type === 'plus') { |
| 56 | + let createDatabaseFlag; |
| 57 | + if (fs.existsSync(path.join(process.cwd(), '.replapirc.json'))) { |
| 58 | + createDatabaseFlag = JSON.parse(fs.readFileSync(path.join(process.cwd(), '.replapirc.json'))).createDatabaseFlag; |
| 59 | + } |
| 60 | + |
| 61 | + if (createDatabaseFlag) { |
| 62 | + const info = await fetch(`${this.dbToken}`, { |
| 63 | + method: 'POST', |
| 64 | + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, |
| 65 | + body: `${encodeURIComponent('replapi_database_config')}=${encodeURIComponent(JSON.stringify({ ...this.options }))}`, |
| 66 | + }); |
| 67 | + } else { |
| 68 | + throw new Error('Are you sure you want to use these options to configure a Database? You will not be able to change these options in the future. If you are, then in your .replapirc.json file set a "createDatabaseFlag" key to "true". For more information, read the documentation.'); |
| 69 | + } |
| 70 | + } else if (this.options.type === 'repldb') { |
| 71 | + const info = await fetch(`${this.dbToken}`, { |
| 72 | + method: 'POST', |
| 73 | + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, |
| 74 | + body: `${encodeURIComponent('replapi_database_config')}=${encodeURIComponent(JSON.stringify({ ...this.options }))}`, |
| 75 | + }); |
| 76 | + } else { |
| 77 | + throw new Error('Invalid Database Type. For a normal database, use the "repldb" option.'); |
| 78 | + } |
| 79 | + } |
38 | 80 | } |
39 | 81 |
|
40 | | - async set(key, value) { |
41 | | - const info = await fetch(`https://kv.replit.com/v0/${this.replitdbtoken}`, { |
| 82 | + async createCollection(collectionName) { |
| 83 | + const info = await fetch(`${this.dbToken}`, { |
42 | 84 | method: 'POST', |
43 | | - headers, |
44 | | - body: `${encodeURIComponent(key)}=${encodeURIComponent(JSON.stringify(value))}`, |
| 85 | + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, |
| 86 | + body: `${encodeURIComponent(collectionName)}=${encodeURIComponent(JSON.stringify({}))}`, |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + async createDoc(collectionName, docName, docItems) { |
| 91 | + const collection = await this.getCollection(collectionName); |
| 92 | + collection[docName] = { ...docItems }; |
| 93 | + |
| 94 | + const info = await fetch(`${this.dbToken}`, { |
| 95 | + method: 'POST', |
| 96 | + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, |
| 97 | + body: `${encodeURIComponent(collectionName)}=${encodeURIComponent(JSON.stringify(collection))}`, |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + async getCollection(collectionName) { |
| 102 | + const info = await fetch(`${this.dbToken}/${collectionName}`, { |
| 103 | + method: 'GET', |
45 | 104 | }).then((res) => res.json()); |
| 105 | + |
| 106 | + return info; |
46 | 107 | } |
47 | 108 |
|
48 | | - async get(key) { |
49 | | - const info = await fetch(`https://kv.replit.com/v0/${this.replitdbtoken}/${key}`, { |
| 109 | + async getDoc(collectionName, docName) { |
| 110 | + const info = await fetch(`${this.dbToken}/${collectionName}`, { |
50 | 111 | method: 'GET', |
51 | | - headers, |
52 | | - }).then((res) => { |
53 | | - res.json(); |
| 112 | + }).then((res) => res.json()); |
| 113 | + |
| 114 | + return info[docName]; |
| 115 | + } |
| 116 | + |
| 117 | + async updateDoc(collectionName, docName, docItems) { |
| 118 | + const collection = await this.getCollection(collectionName); |
| 119 | + _.assignIn(collection[docName], docItems); |
| 120 | + |
| 121 | + const info = await fetch(`${this.dbToken}`, { |
| 122 | + method: 'POST', |
| 123 | + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, |
| 124 | + body: `${encodeURIComponent(collectionName)}=${encodeURIComponent(JSON.stringify(collection))}`, |
54 | 125 | }); |
55 | 126 | } |
56 | 127 |
|
57 | 128 | async delete(key) { |
58 | 129 | const info = await fetch(`https://kv.replit.com/v0/${this.replitdbtoken}/${key}`, { |
59 | 130 | method: 'GET', |
60 | 131 | headers, |
61 | | - }).then((res) => { |
62 | | - res.json(); |
63 | | - }); |
| 132 | + }).then((res) => res.json() ); |
64 | 133 | } |
65 | 134 | }; |
66 | 135 | } else { |
|
0 commit comments