create-protobase 0.0.0-stage → 0.14.2
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/LICENSE +202 -0
- package/bin/create-protobase.mjs +4 -0
- package/dist/create-project.js +20 -0
- package/dist/index.js +27 -0
- package/dist/next-steps.js +12 -0
- package/dist/template/.env.example +8 -0
- package/dist/template/AGENTS.md +22 -0
- package/dist/template/_.gitignore +3 -0
- package/dist/template/_bunfig.toml +4 -0
- package/dist/template/auth/auth.ts +26 -0
- package/dist/template/config/index.ts +4 -0
- package/dist/template/config/user-menu.ts +4 -0
- package/dist/template/db/connection.ts +8 -0
- package/dist/template/db/migrate.ts +30 -0
- package/dist/template/db/migrations/.gitkeep +0 -0
- package/dist/template/db/migrations/001_auth.sql +36 -0
- package/dist/template/db/migrations/002_auth_snake_case.sql +109 -0
- package/dist/template/db/migrations/003_auth.sql +6 -0
- package/dist/template/db/migrations/004_auth.sql +4 -0
- package/dist/template/node-types.d.ts +1 -0
- package/dist/template/package.json +35 -0
- package/dist/template/protobase.config.ts +4 -0
- package/dist/template/tsconfig.json +16 -0
- package/dist/template/vitest.config.ts +4 -0
- package/package.json +28 -4
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { cpSync, existsSync, readFileSync, readdirSync, renameSync, writeFileSync } from "node:fs";
|
|
3
|
+
//#region src/create-project.ts
|
|
4
|
+
/**
|
|
5
|
+
* Writes a new project to `targetDir` from `templateDir`, which the build writes from the scratch preset, with its
|
|
6
|
+
* package.json named after the folder. The folder may exist only when it is empty.
|
|
7
|
+
*/
|
|
8
|
+
const createProject = (templateDir, targetDir) => {
|
|
9
|
+
if (existsSync(targetDir) && readdirSync(targetDir).length > 0) throw new Error(`${targetDir} is not empty`);
|
|
10
|
+
cpSync(templateDir, targetDir, { recursive: true });
|
|
11
|
+
for (const file of readdirSync(targetDir).filter((file) => file.startsWith("_"))) renameSync(path.join(targetDir, file), path.join(targetDir, file.slice(1)));
|
|
12
|
+
const packageFile = path.join(targetDir, "package.json");
|
|
13
|
+
const manifest = JSON.parse(readFileSync(packageFile, "utf8"));
|
|
14
|
+
writeFileSync(packageFile, `${JSON.stringify({
|
|
15
|
+
...manifest,
|
|
16
|
+
name: path.basename(targetDir)
|
|
17
|
+
}, null, 2)}\n`);
|
|
18
|
+
};
|
|
19
|
+
//#endregion
|
|
20
|
+
export { createProject };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { createProject } from "./create-project.js";
|
|
2
|
+
import { nextSteps } from "./next-steps.js";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { parseArgs } from "node:util";
|
|
5
|
+
//#region src/index.ts
|
|
6
|
+
const templateDir = path.join(import.meta.dirname, "template");
|
|
7
|
+
const usage = "usage: npm create protobase <dir>";
|
|
8
|
+
const create = (argv) => {
|
|
9
|
+
const { positionals } = parseArgs({
|
|
10
|
+
args: argv.slice(2),
|
|
11
|
+
allowPositionals: true
|
|
12
|
+
});
|
|
13
|
+
const [dir, ...extra] = positionals;
|
|
14
|
+
if (!dir || extra.length > 0) throw new Error(usage);
|
|
15
|
+
createProject(templateDir, path.resolve(dir));
|
|
16
|
+
process.stdout.write(`Created a Protobase project in ${dir}. Next:\n\n${nextSteps(dir)}`);
|
|
17
|
+
};
|
|
18
|
+
const run = (argv) => {
|
|
19
|
+
try {
|
|
20
|
+
create(argv);
|
|
21
|
+
} catch (error) {
|
|
22
|
+
process.stderr.write(`error: ${error.message}\n`);
|
|
23
|
+
process.exitCode = 1;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
//#endregion
|
|
27
|
+
export { run };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region src/next-steps.ts
|
|
2
|
+
/** What to run in a new project, from `cwd`'s point of view, until its admin app serves. */
|
|
3
|
+
const nextSteps = (dir) => [
|
|
4
|
+
`cd ${dir}`,
|
|
5
|
+
"bun install",
|
|
6
|
+
"cp .env.example .env # then set DATABASE_URL and BETTER_AUTH_SECRET",
|
|
7
|
+
"bun run db:migrate",
|
|
8
|
+
"bun run protobase users create you@example.com --generate-password",
|
|
9
|
+
"bun run dev # http://localhost:5173"
|
|
10
|
+
].map((line) => ` ${line}\n`).join("");
|
|
11
|
+
//#endregion
|
|
12
|
+
export { nextSteps };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# The app's settings for `protobase dev`, `protobase serve` and the db:* scripts; a deployed app gets them from its host.
|
|
2
|
+
DATABASE_URL=postgres://protobase:protobase@localhost:55432/scratch
|
|
3
|
+
|
|
4
|
+
# Admin store (Better Auth users, sessions, signing keys): the `auth` schema of DATABASE_URL, created by `db:migrate`.
|
|
5
|
+
BETTER_AUTH_URL=http://localhost:5173
|
|
6
|
+
# Signs cookies and encrypts the token keys. Generate your own: openssl rand -base64 32
|
|
7
|
+
BETTER_AUTH_SECRET=change-me-generate-with-openssl-rand-base64-32
|
|
8
|
+
# TRUSTED_ORIGINS=
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Working on this app
|
|
2
|
+
|
|
3
|
+
A Protobase app: each resource's data config in `config/<name>/data.ts` and its screens in `config/<name>/ui.ts`, all
|
|
4
|
+
exported from `config/index.ts`, over a Postgres database. Reference: https://docs.protobase.net. It starts empty: the
|
|
5
|
+
first change adds its first tables and resources.
|
|
6
|
+
|
|
7
|
+
## Database changes
|
|
8
|
+
|
|
9
|
+
- Every schema change is a new migration file in `db/migrations/`, numbered after the last one (`001_customers.sql`
|
|
10
|
+
first). Never edit or remove a migration that exists: it has already run on the live database.
|
|
11
|
+
- Expand only, where possible: add tables, columns and indexes, and keep what the running app still reads. Rename or
|
|
12
|
+
drop only when asked to, once nothing uses it.
|
|
13
|
+
|
|
14
|
+
## Never touch
|
|
15
|
+
|
|
16
|
+
- `auth/` and the `auth` schema: sign-in and accounts.
|
|
17
|
+
- `.github/workflows/`.
|
|
18
|
+
|
|
19
|
+
## Scripts
|
|
20
|
+
|
|
21
|
+
The platform runs `db:migrate` (the auth schema's migrations included), `typecheck` and `test`, with their settings in
|
|
22
|
+
the environment: keep them working. Run `typecheck` and `test` before you finish.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { PostgresDialect } from 'kysely'
|
|
2
|
+
import pg from 'pg'
|
|
3
|
+
import { betterAuthAuthenticator, createAuth, handleDroppedConnections } from '@protobase/server'
|
|
4
|
+
import { databaseUrl } from '../db/connection'
|
|
5
|
+
|
|
6
|
+
const secret = process.env.BETTER_AUTH_SECRET
|
|
7
|
+
if (!secret) throw new Error('BETTER_AUTH_SECRET is not set; generate one with `openssl rand -base64 32` and put it in .env')
|
|
8
|
+
|
|
9
|
+
// One pool across hot reloads of this module
|
|
10
|
+
const store = globalThis as { __adminPool?: pg.Pool }
|
|
11
|
+
store.__adminPool ??= handleDroppedConnections(new pg.Pool({ connectionString: databaseUrl, max: 5 }))
|
|
12
|
+
|
|
13
|
+
/** The admin store (Better Auth's users, sessions and signing keys) lives in this schema, next to the app's own schemas. */
|
|
14
|
+
export const authSchema = 'auth'
|
|
15
|
+
|
|
16
|
+
export const auth = createAuth({
|
|
17
|
+
database: { dialect: new PostgresDialect({ pool: store.__adminPool }), type: 'postgres', schemaName: authSchema, transaction: true },
|
|
18
|
+
baseURL: process.env.BETTER_AUTH_URL ?? 'http://localhost:5173',
|
|
19
|
+
secret,
|
|
20
|
+
// No defaultRole on purpose: every account is created with an explicit role (`protobase users create --role`).
|
|
21
|
+
roles: ['admin'],
|
|
22
|
+
trustedOrigins: process.env.TRUSTED_ORIGINS?.split(',').filter(Boolean),
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
/** Every user works in tenant 1, which access rules can compare with an organization_id column. */
|
|
26
|
+
export const authenticate = betterAuthAuthenticator({ auth, tenant: 1 })
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
// Every resource, view and page of the app, exported from here: `export * from './orders/data'`. A new app has no
|
|
2
|
+
// resources yet, only the Users page, for admins; it is deployed with its first change.
|
|
3
|
+
export { usersPage } from '@protobase/layout'
|
|
4
|
+
export { accountMenu } from './user-menu'
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import postgres from 'postgres'
|
|
2
|
+
|
|
3
|
+
const url = process.env.DATABASE_URL
|
|
4
|
+
if (!url) throw new Error('DATABASE_URL is not set; put it in .env')
|
|
5
|
+
|
|
6
|
+
export const databaseUrl = url
|
|
7
|
+
|
|
8
|
+
export const connect = (options: postgres.Options<{}> = {}) => postgres(databaseUrl, { onnotice: () => {}, ...options })
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { readdir, readFile } from 'node:fs/promises'
|
|
2
|
+
import { fileURLToPath } from 'node:url'
|
|
3
|
+
import { connect } from './connection'
|
|
4
|
+
|
|
5
|
+
// Applies the migrations not applied yet, the app's and the auth schema's alike, in file name order and in one
|
|
6
|
+
// transaction: when one fails, none is applied and the database stays as the running version expects it. Run it before
|
|
7
|
+
// a new version serves, while nothing else writes. A statement Postgres runs only outside a transaction, such as
|
|
8
|
+
// `create index concurrently`, does not belong in these files.
|
|
9
|
+
|
|
10
|
+
const directory = fileURLToPath(new URL('./migrations/', import.meta.url))
|
|
11
|
+
// Any constant shared by every run of this script: two runs at once take turns.
|
|
12
|
+
const lockKey = 7_311_946_202
|
|
13
|
+
|
|
14
|
+
const sql = connect({ max: 1 })
|
|
15
|
+
const files = (await readdir(directory)).filter((file) => file.endsWith('.sql')).sort()
|
|
16
|
+
|
|
17
|
+
const applied = await sql.begin(async (tx) => {
|
|
18
|
+
await tx`select pg_advisory_xact_lock(${lockKey})`
|
|
19
|
+
await tx`create table if not exists public.schema_migrations (name text primary key, applied_at timestamptz not null default now())`
|
|
20
|
+
const done = new Set((await tx`select name from public.schema_migrations`).map((row) => row.name))
|
|
21
|
+
const pending = files.filter((file) => !done.has(file))
|
|
22
|
+
for (const file of pending) {
|
|
23
|
+
await tx.unsafe(await readFile(directory + file, 'utf8'))
|
|
24
|
+
await tx`insert into public.schema_migrations (name) values (${file})`
|
|
25
|
+
}
|
|
26
|
+
return pending
|
|
27
|
+
})
|
|
28
|
+
for (const file of applied) console.log(`applied ${file}`)
|
|
29
|
+
|
|
30
|
+
await sql.end()
|
|
File without changes
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
-- The auth schema changes this version of Protobase needs, written by `protobase auth migration`.
|
|
2
|
+
-- Applied with the project's other migrations; edit or remove statements the project handles differently.
|
|
3
|
+
|
|
4
|
+
create schema if not exists "auth";
|
|
5
|
+
|
|
6
|
+
create table if not exists "auth"."user" ("id" text not null primary key, "name" text not null, "email" text not null unique, "email_verified" boolean not null, "image" text, "created_at" timestamptz default CURRENT_TIMESTAMP not null, "updated_at" timestamptz default CURRENT_TIMESTAMP not null, "role" text, "banned" boolean, "ban_reason" text, "ban_expires" timestamptz, "two_factor_enabled" boolean);
|
|
7
|
+
|
|
8
|
+
create table if not exists "auth"."session" ("id" text not null primary key, "expires_at" timestamptz not null, "token" text not null unique, "created_at" timestamptz default CURRENT_TIMESTAMP not null, "updated_at" timestamptz not null, "ip_address" text, "user_agent" text, "user_id" text not null references "auth"."user" ("id") on delete cascade, "impersonated_by" text);
|
|
9
|
+
|
|
10
|
+
create table if not exists "auth"."account" ("id" text not null primary key, "account_id" text not null, "provider_id" text not null, "user_id" text not null references "auth"."user" ("id") on delete cascade, "access_token" text, "refresh_token" text, "id_token" text, "access_token_expires_at" timestamptz, "refresh_token_expires_at" timestamptz, "scope" text, "password" text, "created_at" timestamptz default CURRENT_TIMESTAMP not null, "updated_at" timestamptz not null);
|
|
11
|
+
|
|
12
|
+
create table if not exists "auth"."verification" ("id" text not null primary key, "identifier" text not null, "value" text not null, "expires_at" timestamptz not null, "created_at" timestamptz default CURRENT_TIMESTAMP not null, "updated_at" timestamptz default CURRENT_TIMESTAMP not null);
|
|
13
|
+
|
|
14
|
+
create table if not exists "auth"."jwks" ("id" text not null primary key, "public_key" text not null, "private_key" text not null, "created_at" timestamptz not null, "expires_at" timestamptz, "alg" text, "crv" text);
|
|
15
|
+
|
|
16
|
+
create table if not exists "auth"."two_factor" ("id" text not null primary key, "secret" text not null, "backup_codes" text not null, "user_id" text not null references "auth"."user" ("id") on delete cascade, "verified" boolean, "failed_verification_count" integer, "locked_until" timestamptz);
|
|
17
|
+
|
|
18
|
+
create table if not exists "auth"."passkey" ("id" text not null primary key, "name" text, "public_key" text not null, "user_id" text not null references "auth"."user" ("id") on delete cascade, "credential_id" text not null, "counter" integer not null, "device_type" text not null, "backed_up" boolean not null, "transports" text, "created_at" timestamptz, "aaguid" text);
|
|
19
|
+
|
|
20
|
+
create table if not exists "auth"."sign_in_policy" ("id" text not null primary key, "password" text not null, "email_code" text not null, "passkey" text not null, "two_factor" text not null, "staff_access" text not null, "platform_sign_in" text not null, "created_at" timestamptz not null, "created_by" text);
|
|
21
|
+
|
|
22
|
+
create table if not exists "auth"."staff_sign_in" ("id" text not null primary key, "user_id" text not null, "user_email" text not null, "staff" text not null, "staff_name" text, "staff_subject" text not null, "issuer" text not null, "reason" text not null, "session_id" text not null, "started_at" timestamptz not null, "expires_at" timestamptz not null, "ended_at" timestamptz);
|
|
23
|
+
|
|
24
|
+
create index if not exists "session_user_id_idx" on "auth"."session" ("user_id");
|
|
25
|
+
|
|
26
|
+
create index if not exists "account_user_id_idx" on "auth"."account" ("user_id");
|
|
27
|
+
|
|
28
|
+
create index if not exists "verification_identifier_idx" on "auth"."verification" ("identifier");
|
|
29
|
+
|
|
30
|
+
create index if not exists "two_factor_secret_idx" on "auth"."two_factor" ("secret");
|
|
31
|
+
|
|
32
|
+
create index if not exists "two_factor_user_id_idx" on "auth"."two_factor" ("user_id");
|
|
33
|
+
|
|
34
|
+
create index if not exists "passkey_user_id_idx" on "auth"."passkey" ("user_id");
|
|
35
|
+
|
|
36
|
+
create index if not exists "passkey_credential_id_idx" on "auth"."passkey" ("credential_id");
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
-- The auth schema changes this version of Protobase needs, written by `protobase auth migration`.
|
|
2
|
+
-- Applied with the project's other migrations; edit or remove statements the project handles differently.
|
|
3
|
+
|
|
4
|
+
-- Renames the auth store of Protobase 0.6 to snake_case. A store set up with the snake_case names has no "emailVerified"
|
|
5
|
+
-- and is left as it is.
|
|
6
|
+
do $$
|
|
7
|
+
declare
|
|
8
|
+
constraint_row record;
|
|
9
|
+
begin
|
|
10
|
+
if not exists (select from information_schema.columns where table_schema = 'auth' and table_name = 'user' and column_name = 'emailVerified') then
|
|
11
|
+
return;
|
|
12
|
+
end if;
|
|
13
|
+
|
|
14
|
+
alter table "auth"."twoFactor" rename to two_factor;
|
|
15
|
+
alter table "auth"."signInPolicy" rename to sign_in_policy;
|
|
16
|
+
alter table "auth"."staffSignIn" rename to staff_sign_in;
|
|
17
|
+
|
|
18
|
+
alter table "auth"."user" rename column "emailVerified" to email_verified;
|
|
19
|
+
alter table "auth"."user" rename column "createdAt" to created_at;
|
|
20
|
+
alter table "auth"."user" rename column "updatedAt" to updated_at;
|
|
21
|
+
alter table "auth"."user" rename column "banReason" to ban_reason;
|
|
22
|
+
alter table "auth"."user" rename column "banExpires" to ban_expires;
|
|
23
|
+
alter table "auth"."user" rename column "twoFactorEnabled" to two_factor_enabled;
|
|
24
|
+
|
|
25
|
+
alter table "auth".session rename column "expiresAt" to expires_at;
|
|
26
|
+
alter table "auth".session rename column "createdAt" to created_at;
|
|
27
|
+
alter table "auth".session rename column "updatedAt" to updated_at;
|
|
28
|
+
alter table "auth".session rename column "ipAddress" to ip_address;
|
|
29
|
+
alter table "auth".session rename column "userAgent" to user_agent;
|
|
30
|
+
alter table "auth".session rename column "userId" to user_id;
|
|
31
|
+
alter table "auth".session rename column "impersonatedBy" to impersonated_by;
|
|
32
|
+
|
|
33
|
+
alter table "auth".account rename column "accountId" to account_id;
|
|
34
|
+
alter table "auth".account rename column "providerId" to provider_id;
|
|
35
|
+
alter table "auth".account rename column "userId" to user_id;
|
|
36
|
+
alter table "auth".account rename column "accessToken" to access_token;
|
|
37
|
+
alter table "auth".account rename column "refreshToken" to refresh_token;
|
|
38
|
+
alter table "auth".account rename column "idToken" to id_token;
|
|
39
|
+
alter table "auth".account rename column "accessTokenExpiresAt" to access_token_expires_at;
|
|
40
|
+
alter table "auth".account rename column "refreshTokenExpiresAt" to refresh_token_expires_at;
|
|
41
|
+
alter table "auth".account rename column "createdAt" to created_at;
|
|
42
|
+
alter table "auth".account rename column "updatedAt" to updated_at;
|
|
43
|
+
|
|
44
|
+
alter table "auth".verification rename column "expiresAt" to expires_at;
|
|
45
|
+
alter table "auth".verification rename column "createdAt" to created_at;
|
|
46
|
+
alter table "auth".verification rename column "updatedAt" to updated_at;
|
|
47
|
+
|
|
48
|
+
alter table "auth".jwks rename column "publicKey" to public_key;
|
|
49
|
+
alter table "auth".jwks rename column "privateKey" to private_key;
|
|
50
|
+
alter table "auth".jwks rename column "createdAt" to created_at;
|
|
51
|
+
alter table "auth".jwks rename column "expiresAt" to expires_at;
|
|
52
|
+
|
|
53
|
+
alter table "auth".two_factor rename column "backupCodes" to backup_codes;
|
|
54
|
+
alter table "auth".two_factor rename column "userId" to user_id;
|
|
55
|
+
alter table "auth".two_factor rename column "failedVerificationCount" to failed_verification_count;
|
|
56
|
+
alter table "auth".two_factor rename column "lockedUntil" to locked_until;
|
|
57
|
+
|
|
58
|
+
alter table "auth".passkey rename column "publicKey" to public_key;
|
|
59
|
+
alter table "auth".passkey rename column "userId" to user_id;
|
|
60
|
+
alter table "auth".passkey rename column "credentialID" to credential_id;
|
|
61
|
+
alter table "auth".passkey rename column "deviceType" to device_type;
|
|
62
|
+
alter table "auth".passkey rename column "backedUp" to backed_up;
|
|
63
|
+
alter table "auth".passkey rename column "createdAt" to created_at;
|
|
64
|
+
|
|
65
|
+
alter table "auth".sign_in_policy rename column "emailCode" to email_code;
|
|
66
|
+
alter table "auth".sign_in_policy rename column "twoFactor" to two_factor;
|
|
67
|
+
alter table "auth".sign_in_policy rename column "staffAccess" to staff_access;
|
|
68
|
+
alter table "auth".sign_in_policy rename column "platformSignIn" to platform_sign_in;
|
|
69
|
+
alter table "auth".sign_in_policy rename column "createdAt" to created_at;
|
|
70
|
+
alter table "auth".sign_in_policy rename column "createdBy" to created_by;
|
|
71
|
+
|
|
72
|
+
alter table "auth".staff_sign_in rename column "userId" to user_id;
|
|
73
|
+
alter table "auth".staff_sign_in rename column "userEmail" to user_email;
|
|
74
|
+
alter table "auth".staff_sign_in rename column "staffName" to staff_name;
|
|
75
|
+
alter table "auth".staff_sign_in rename column "staffSubject" to staff_subject;
|
|
76
|
+
alter table "auth".staff_sign_in rename column "sessionId" to session_id;
|
|
77
|
+
alter table "auth".staff_sign_in rename column "startedAt" to started_at;
|
|
78
|
+
alter table "auth".staff_sign_in rename column "expiresAt" to expires_at;
|
|
79
|
+
alter table "auth".staff_sign_in rename column "endedAt" to ended_at;
|
|
80
|
+
|
|
81
|
+
-- A primary key's index takes the new name with it.
|
|
82
|
+
alter table "auth".two_factor rename constraint "twoFactor_pkey" to two_factor_pkey;
|
|
83
|
+
alter table "auth".sign_in_policy rename constraint "signInPolicy_pkey" to sign_in_policy_pkey;
|
|
84
|
+
alter table "auth".staff_sign_in rename constraint "staffSignIn_pkey" to staff_sign_in_pkey;
|
|
85
|
+
alter table "auth".session rename constraint "session_userId_fkey" to session_user_id_fkey;
|
|
86
|
+
alter table "auth".account rename constraint "account_userId_fkey" to account_user_id_fkey;
|
|
87
|
+
alter table "auth".two_factor rename constraint "twoFactor_userId_fkey" to two_factor_user_id_fkey;
|
|
88
|
+
alter table "auth".passkey rename constraint "passkey_userId_fkey" to passkey_user_id_fkey;
|
|
89
|
+
|
|
90
|
+
alter index "auth"."session_userId_idx" rename to session_user_id_idx;
|
|
91
|
+
alter index "auth"."account_userId_idx" rename to account_user_id_idx;
|
|
92
|
+
alter index "auth"."twoFactor_secret_idx" rename to two_factor_secret_idx;
|
|
93
|
+
alter index "auth"."twoFactor_userId_idx" rename to two_factor_user_id_idx;
|
|
94
|
+
alter index "auth"."passkey_userId_idx" rename to passkey_user_id_idx;
|
|
95
|
+
alter index "auth"."passkey_credentialID_idx" rename to passkey_credential_id_idx;
|
|
96
|
+
|
|
97
|
+
-- Postgres 18 names each not-null constraint after its table and column, as "user_emailVerified_not_null"; earlier
|
|
98
|
+
-- versions keep no such constraints, so there is nothing to rename there.
|
|
99
|
+
for constraint_row in
|
|
100
|
+
select c.conrelid::regclass as table_name, c.conname as old_name, t.relname || '_' || a.attname || '_not_null' as new_name
|
|
101
|
+
from pg_constraint c
|
|
102
|
+
join pg_class t on t.oid = c.conrelid
|
|
103
|
+
join pg_attribute a on a.attrelid = c.conrelid and a.attnum = c.conkey[1]
|
|
104
|
+
where c.contype = 'n' and t.relnamespace = (select oid from pg_namespace where nspname = 'auth') and c.conname ~ '[A-Z]'
|
|
105
|
+
loop
|
|
106
|
+
execute format('alter table %s rename constraint %I to %I', constraint_row.table_name, constraint_row.old_name, constraint_row.new_name);
|
|
107
|
+
end loop;
|
|
108
|
+
end
|
|
109
|
+
$$;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
-- The auth schema changes this version of Protobase needs, written by `protobase auth migration`.
|
|
2
|
+
-- Applied with the project's other migrations; edit or remove statements the project handles differently.
|
|
3
|
+
|
|
4
|
+
alter table "auth"."user" add column if not exists "last_role" text;
|
|
5
|
+
|
|
6
|
+
alter table "auth"."session" add column if not exists "active_role" text;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
-- The auth schema changes this version of Protobase needs, written by `protobase migration`.
|
|
2
|
+
-- Applied with the project's other migrations; edit or remove statements the project handles differently.
|
|
3
|
+
|
|
4
|
+
alter table "auth"."passkey" add column if not exists "rp_id" text;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "scratch",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"db:migrate": "bun db/migrate.ts",
|
|
8
|
+
"typecheck": "tsc --noEmit",
|
|
9
|
+
"test": "vitest run",
|
|
10
|
+
"protobase": "protobase",
|
|
11
|
+
"build": "protobase build",
|
|
12
|
+
"serve": "protobase build && protobase serve dist",
|
|
13
|
+
"dev": "protobase dev"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@protobase/cli": ">=0.14.2 <1",
|
|
17
|
+
"@protobase/layout": ">=0.14.2 <1",
|
|
18
|
+
"@protobase/schema": ">=0.14.2 <1",
|
|
19
|
+
"@protobase/server": ">=0.14.2 <1",
|
|
20
|
+
"@protobase/ui": ">=0.14.2 <1",
|
|
21
|
+
"better-auth": "^1.7.7",
|
|
22
|
+
"jose": "^6.2.12",
|
|
23
|
+
"kysely": "^0.29.6",
|
|
24
|
+
"pg": "^8.23.1",
|
|
25
|
+
"postgres": "^3.4.9",
|
|
26
|
+
"react": "^19.3.0"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/node": "^22.20.5",
|
|
30
|
+
"@types/pg": "^8.23.1",
|
|
31
|
+
"@types/react": "^19.3.0",
|
|
32
|
+
"typescript": "^6.0.3",
|
|
33
|
+
"vitest": "^5.0.3"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2023",
|
|
4
|
+
"lib": ["ES2023", "DOM", "DOM.Iterable"],
|
|
5
|
+
"module": "ESNext",
|
|
6
|
+
"moduleResolution": "Bundler",
|
|
7
|
+
"jsx": "react-jsx",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"noUncheckedIndexedAccess": true,
|
|
10
|
+
"verbatimModuleSyntax": true,
|
|
11
|
+
"isolatedModules": true,
|
|
12
|
+
"skipLibCheck": true,
|
|
13
|
+
"noEmit": true
|
|
14
|
+
},
|
|
15
|
+
"exclude": ["node_modules", "dist"]
|
|
16
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-protobase",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.14.2",
|
|
4
|
+
"description": "npm create protobase: starts a new Protobase project from scratch.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/PrototeamHQ/protobase.git",
|
|
9
|
+
"directory": "packages/create-protobase"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://docs.protobase.net",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=22.12.0"
|
|
15
|
+
},
|
|
16
|
+
"bin": {
|
|
17
|
+
"create-protobase": "./bin/create-protobase.mjs"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"bin",
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsdown && bun ../presets/src/write-template.ts",
|
|
25
|
+
"prepack": "tsdown && bun ../presets/src/write-template.ts"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
}
|
|
30
|
+
}
|
package/README.md
DELETED