jazz-tools 0.18.15 → 0.18.16

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.
Files changed (58) hide show
  1. package/.turbo/turbo-build.log +45 -35
  2. package/CHANGELOG.md +9 -0
  3. package/dist/better-auth/database-adapter/index.d.ts +50 -0
  4. package/dist/better-auth/database-adapter/index.d.ts.map +1 -0
  5. package/dist/better-auth/database-adapter/index.js +920 -0
  6. package/dist/better-auth/database-adapter/index.js.map +1 -0
  7. package/dist/better-auth/database-adapter/repository/account.d.ts +24 -0
  8. package/dist/better-auth/database-adapter/repository/account.d.ts.map +1 -0
  9. package/dist/better-auth/database-adapter/repository/generic.d.ts +45 -0
  10. package/dist/better-auth/database-adapter/repository/generic.d.ts.map +1 -0
  11. package/dist/better-auth/database-adapter/repository/index.d.ts +6 -0
  12. package/dist/better-auth/database-adapter/repository/index.d.ts.map +1 -0
  13. package/dist/better-auth/database-adapter/repository/session.d.ts +29 -0
  14. package/dist/better-auth/database-adapter/repository/session.d.ts.map +1 -0
  15. package/dist/better-auth/database-adapter/repository/user.d.ts +30 -0
  16. package/dist/better-auth/database-adapter/repository/user.d.ts.map +1 -0
  17. package/dist/better-auth/database-adapter/repository/verification.d.ts +18 -0
  18. package/dist/better-auth/database-adapter/repository/verification.d.ts.map +1 -0
  19. package/dist/better-auth/database-adapter/schema.d.ts +27 -0
  20. package/dist/better-auth/database-adapter/schema.d.ts.map +1 -0
  21. package/dist/better-auth/database-adapter/tests/index.test.d.ts +2 -0
  22. package/dist/better-auth/database-adapter/tests/index.test.d.ts.map +1 -0
  23. package/dist/better-auth/database-adapter/tests/repository/account.test.d.ts +2 -0
  24. package/dist/better-auth/database-adapter/tests/repository/account.test.d.ts.map +1 -0
  25. package/dist/better-auth/database-adapter/tests/repository/generic.test.d.ts +2 -0
  26. package/dist/better-auth/database-adapter/tests/repository/generic.test.d.ts.map +1 -0
  27. package/dist/better-auth/database-adapter/tests/repository/session.test.d.ts +2 -0
  28. package/dist/better-auth/database-adapter/tests/repository/session.test.d.ts.map +1 -0
  29. package/dist/better-auth/database-adapter/tests/repository/user.test.d.ts +2 -0
  30. package/dist/better-auth/database-adapter/tests/repository/user.test.d.ts.map +1 -0
  31. package/dist/better-auth/database-adapter/tests/repository/verification.test.d.ts +2 -0
  32. package/dist/better-auth/database-adapter/tests/repository/verification.test.d.ts.map +1 -0
  33. package/dist/better-auth/database-adapter/tests/sync-utils.d.ts +16 -0
  34. package/dist/better-auth/database-adapter/tests/sync-utils.d.ts.map +1 -0
  35. package/dist/better-auth/database-adapter/tests/utils.test.d.ts +2 -0
  36. package/dist/better-auth/database-adapter/tests/utils.test.d.ts.map +1 -0
  37. package/dist/better-auth/database-adapter/utils.d.ts +16 -0
  38. package/dist/better-auth/database-adapter/utils.d.ts.map +1 -0
  39. package/jazz-tools-0.18.6.tgz +0 -0
  40. package/package.json +10 -5
  41. package/src/better-auth/database-adapter/index.ts +228 -0
  42. package/src/better-auth/database-adapter/repository/account.ts +131 -0
  43. package/src/better-auth/database-adapter/repository/generic.ts +297 -0
  44. package/src/better-auth/database-adapter/repository/index.ts +5 -0
  45. package/src/better-auth/database-adapter/repository/session.ts +190 -0
  46. package/src/better-auth/database-adapter/repository/user.ts +158 -0
  47. package/src/better-auth/database-adapter/repository/verification.ts +37 -0
  48. package/src/better-auth/database-adapter/schema.ts +222 -0
  49. package/src/better-auth/database-adapter/tests/index.test.ts +690 -0
  50. package/src/better-auth/database-adapter/tests/repository/account.test.ts +149 -0
  51. package/src/better-auth/database-adapter/tests/repository/generic.test.ts +183 -0
  52. package/src/better-auth/database-adapter/tests/repository/session.test.ts +419 -0
  53. package/src/better-auth/database-adapter/tests/repository/user.test.ts +673 -0
  54. package/src/better-auth/database-adapter/tests/repository/verification.test.ts +101 -0
  55. package/src/better-auth/database-adapter/tests/sync-utils.ts +127 -0
  56. package/src/better-auth/database-adapter/tests/utils.test.ts +787 -0
  57. package/src/better-auth/database-adapter/utils.ts +178 -0
  58. package/tsup.config.ts +7 -0
@@ -0,0 +1,149 @@
1
+ import { beforeEach, describe, expect, it } from "vitest";
2
+ import { Account, co } from "jazz-tools";
3
+ import { startWorker } from "jazz-tools/worker";
4
+ import { AccountRepository } from "../../repository/account";
5
+ import { createJazzSchema, Database } from "../../schema";
6
+ import { createWorkerAccount, startSyncServer } from "../sync-utils.js";
7
+
8
+ describe("AccountRepository", () => {
9
+ let syncServer: any;
10
+
11
+ let databaseSchema: Database;
12
+ let databaseRoot: co.loaded<Database, { group: true }>;
13
+ let worker: Account;
14
+
15
+ beforeEach(async () => {
16
+ syncServer = await startSyncServer();
17
+
18
+ const workerAccount = await createWorkerAccount({
19
+ name: "test",
20
+ peer: `ws://localhost:${syncServer.port}`,
21
+ });
22
+
23
+ const JazzSchema = createJazzSchema({
24
+ account: {
25
+ modelName: "account",
26
+ fields: {
27
+ accountId: {
28
+ type: "string",
29
+ required: true,
30
+ },
31
+ providerId: {
32
+ type: "string",
33
+ required: true,
34
+ },
35
+ },
36
+ },
37
+ user: {
38
+ modelName: "user",
39
+ fields: {
40
+ email: {
41
+ type: "string",
42
+ required: true,
43
+ },
44
+ },
45
+ },
46
+ session: {
47
+ modelName: "session",
48
+ fields: {
49
+ userId: {
50
+ type: "string",
51
+ required: true,
52
+ },
53
+ token: {
54
+ type: "string",
55
+ required: true,
56
+ },
57
+ },
58
+ },
59
+ });
60
+
61
+ const result = await startWorker({
62
+ AccountSchema: JazzSchema.WorkerAccount,
63
+ syncServer: `ws://localhost:${syncServer.port}`,
64
+ accountID: workerAccount.accountID,
65
+ accountSecret: workerAccount.agentSecret,
66
+ });
67
+
68
+ databaseSchema = JazzSchema.DatabaseRoot;
69
+ databaseRoot = await JazzSchema.loadDatabase(result.worker);
70
+ worker = result.worker;
71
+ });
72
+
73
+ it("should create a session repository", async () => {
74
+ const repository = new AccountRepository(
75
+ databaseSchema,
76
+ databaseRoot,
77
+ worker,
78
+ );
79
+ expect(repository).toBeDefined();
80
+ });
81
+
82
+ describe("create", () => {
83
+ it("should create an account", async () => {
84
+ const accountRepository = new AccountRepository(
85
+ databaseSchema,
86
+ databaseRoot,
87
+ worker,
88
+ );
89
+
90
+ const account = await accountRepository.create("account", {
91
+ accountId: "test",
92
+ providerId: "test",
93
+ });
94
+
95
+ expect(account.$jazz.id).toBeDefined();
96
+ expect(account.accountId).toBe("test");
97
+ expect(account.providerId).toBe("test");
98
+ });
99
+ });
100
+
101
+ describe("findMany", () => {
102
+ it("should find many accounts", async () => {
103
+ const accountRepository = new AccountRepository(
104
+ databaseSchema,
105
+ databaseRoot,
106
+ worker,
107
+ );
108
+
109
+ const account = await accountRepository.create("account", {
110
+ accountId: "test",
111
+ providerId: "test",
112
+ });
113
+
114
+ const accounts = await accountRepository.findMany("account", []);
115
+
116
+ expect(accounts.length).toBe(1);
117
+ expect(accounts[0]?.$jazz.id).toBe(account.$jazz.id);
118
+ });
119
+
120
+ it("should find many accounts with where clause", async () => {
121
+ const accountRepository = new AccountRepository(
122
+ databaseSchema,
123
+ databaseRoot,
124
+ worker,
125
+ );
126
+
127
+ await accountRepository.create("account", {
128
+ accountId: "test",
129
+ providerId: "test",
130
+ });
131
+
132
+ await accountRepository.create("account", {
133
+ accountId: "test2",
134
+ providerId: "test",
135
+ });
136
+
137
+ const accounts = await accountRepository.findMany("account", [
138
+ {
139
+ field: "accountId",
140
+ operator: "eq",
141
+ value: "test",
142
+ connector: "AND",
143
+ },
144
+ ]);
145
+
146
+ expect(accounts.length).toBe(1);
147
+ });
148
+ });
149
+ });
@@ -0,0 +1,183 @@
1
+ import { beforeEach, describe, expect, it } from "vitest";
2
+ import { Account, co } from "jazz-tools";
3
+ import { startWorker } from "jazz-tools/worker";
4
+ import { JazzRepository } from "../../repository/generic";
5
+ import { createJazzSchema, Database } from "../../schema";
6
+ import { createWorkerAccount, startSyncServer } from "../sync-utils.js";
7
+
8
+ describe("Generic Repository", () => {
9
+ let syncServer: any;
10
+
11
+ let databaseSchema: Database;
12
+ let databaseRoot: co.loaded<Database, { group: true }>;
13
+ let worker: Account;
14
+
15
+ beforeEach(async () => {
16
+ syncServer = await startSyncServer();
17
+
18
+ const workerAccount = await createWorkerAccount({
19
+ name: "test",
20
+ peer: `ws://localhost:${syncServer.port}`,
21
+ });
22
+
23
+ const JazzSchema = createJazzSchema({
24
+ user: {
25
+ modelName: "user",
26
+ fields: {
27
+ email: {
28
+ type: "string",
29
+ required: true,
30
+ },
31
+ },
32
+ },
33
+ session: {
34
+ modelName: "session",
35
+ fields: {
36
+ userId: {
37
+ type: "string",
38
+ required: true,
39
+ },
40
+ token: {
41
+ type: "string",
42
+ required: true,
43
+ },
44
+ },
45
+ },
46
+ });
47
+
48
+ const result = await startWorker({
49
+ AccountSchema: JazzSchema.WorkerAccount,
50
+ syncServer: `ws://localhost:${syncServer.port}`,
51
+ accountID: workerAccount.accountID,
52
+ accountSecret: workerAccount.agentSecret,
53
+ });
54
+
55
+ databaseSchema = JazzSchema.DatabaseRoot;
56
+ databaseRoot = await JazzSchema.loadDatabase(result.worker);
57
+ worker = result.worker;
58
+ });
59
+
60
+ it("should throw an error on create if the model is not found", async () => {
61
+ const repository = new JazzRepository(databaseSchema, databaseRoot, worker);
62
+
63
+ await expect(
64
+ repository.create("not-found", {
65
+ name: "test",
66
+ }),
67
+ ).rejects.toThrow('Schema for model "not-found" not found');
68
+ });
69
+
70
+ it("should throw an error on find if the model is not found", async () => {
71
+ const repository = new JazzRepository(databaseSchema, databaseRoot, worker);
72
+
73
+ await expect(
74
+ repository.findMany("not-found", [
75
+ {
76
+ field: "id",
77
+ operator: "eq",
78
+ value: "test",
79
+ connector: "AND",
80
+ },
81
+ ]),
82
+ ).rejects.toThrow('Schema for model "not-found" not found');
83
+ });
84
+
85
+ it("should create a new entity and append to the list", async () => {
86
+ const repository = new JazzRepository(databaseSchema, databaseRoot, worker);
87
+
88
+ const entity = await repository.create("session", {
89
+ userId: "test",
90
+ token: "test",
91
+ });
92
+
93
+ const { tables } = await databaseRoot.$jazz.ensureLoaded({
94
+ resolve: {
95
+ tables: {
96
+ session: {
97
+ $each: true,
98
+ },
99
+ },
100
+ },
101
+ });
102
+
103
+ expect(tables.session.length).toBe(1);
104
+ expect(tables.session[0]?.$jazz.id).toBe(entity.$jazz.id);
105
+ });
106
+
107
+ it("should delete an entity and remove it from the list", async () => {
108
+ const repository = new JazzRepository(databaseSchema, databaseRoot, worker);
109
+
110
+ const entity = await repository.create("session", {
111
+ userId: "test",
112
+ token: "test",
113
+ });
114
+
115
+ await repository.deleteValue("session", [
116
+ {
117
+ field: "id",
118
+ operator: "eq",
119
+ value: entity.$jazz.id,
120
+ connector: "AND",
121
+ },
122
+ ]);
123
+
124
+ const { tables } = await databaseRoot.$jazz.ensureLoaded({
125
+ resolve: {
126
+ tables: {
127
+ session: {
128
+ $each: true,
129
+ },
130
+ },
131
+ },
132
+ });
133
+
134
+ expect(tables.session.length).toBe(0);
135
+
136
+ const found = await repository.findOne("session", [
137
+ {
138
+ field: "id",
139
+ operator: "eq",
140
+ value: entity.$jazz.id,
141
+ connector: "AND",
142
+ },
143
+ ]);
144
+
145
+ expect(found).toBeNull();
146
+ });
147
+
148
+ it("should allow recreating an entity after deletion by identifier", async () => {
149
+ const repository = new JazzRepository(databaseSchema, databaseRoot, worker);
150
+
151
+ const identifier = "test";
152
+
153
+ const entity = await repository.create(
154
+ "session",
155
+ {
156
+ userId: "test",
157
+ token: "test",
158
+ },
159
+ identifier,
160
+ );
161
+
162
+ await repository.deleteValue("session", [
163
+ {
164
+ field: "id",
165
+ operator: "eq",
166
+ value: entity.$jazz.id,
167
+ connector: "AND",
168
+ },
169
+ ]);
170
+
171
+ const entity2 = await repository.create(
172
+ "session",
173
+ {
174
+ userId: "test",
175
+ token: "test",
176
+ },
177
+ identifier,
178
+ );
179
+
180
+ expect(entity2.$jazz.id).toBe(entity.$jazz.id);
181
+ expect(entity2.$jazz.raw.get("_deleted")).toBe(false);
182
+ });
183
+ });