11import { afterEach , beforeEach , describe , expect , test , vi } from "vitest" ;
2- import { TableAggregate } from "./index.js" ;
2+ import { DirectAggregate , TableAggregate } from "./index.js" ;
33import {
44 components ,
55 componentSchema ,
@@ -13,6 +13,7 @@ import {
1313} from "convex/server" ;
1414import { v } from "convex/values" ;
1515import { convexTest } from "convex-test" ;
16+ import batchWorker from "@convex-dev/batch-worker/test" ;
1617import type { DataModelFromSchemaDefinition } from "convex/server" ;
1718
1819const schema = defineSchema ( {
@@ -1236,3 +1237,202 @@ describe("stale reads", () => {
12361237 expect ( count ) . toBe ( 1 ) ;
12371238 } ) ;
12381239} ) ;
1240+
1241+ describe ( "namespacePrefix" , ( ) => {
1242+ function createPrefixedAggregates ( ) {
1243+ const unprefixed = new TableAggregate < {
1244+ Key : number ;
1245+ DataModel : DataModel ;
1246+ TableName : "testItems" ;
1247+ } > ( components . aggregate , {
1248+ sortKey : ( doc ) => doc . value ,
1249+ } ) ;
1250+ const byValue = new TableAggregate < {
1251+ Key : number ;
1252+ DataModel : DataModel ;
1253+ TableName : "testItems" ;
1254+ } > ( components . aggregate , {
1255+ namespacePrefix : "byValue" ,
1256+ sortKey : ( doc ) => doc . value ,
1257+ sumValue : ( doc ) => doc . value ,
1258+ } ) ;
1259+ const byName = new TableAggregate < {
1260+ Key : string ;
1261+ DataModel : DataModel ;
1262+ TableName : "testItems" ;
1263+ } > ( components . aggregate , {
1264+ namespacePrefix : "byName" ,
1265+ sortKey : ( doc ) => doc . name ,
1266+ } ) ;
1267+ const byAlbum = new TableAggregate < {
1268+ Namespace : string ;
1269+ Key : number ;
1270+ DataModel : DataModel ;
1271+ TableName : "photos" ;
1272+ } > ( components . aggregate , {
1273+ namespacePrefix : "byAlbum" ,
1274+ namespace : ( doc ) => doc . album ,
1275+ sortKey : ( doc ) => doc . score ,
1276+ } ) ;
1277+ return { unprefixed, byValue, byName, byAlbum } ;
1278+ }
1279+
1280+ async function insertPhoto (
1281+ ctx : GenericMutationCtx < DataModel > ,
1282+ album : string ,
1283+ score : number ,
1284+ ) {
1285+ const id = await ctx . db . insert ( "photos" , { album, url : "u" , score } ) ;
1286+ return ( await ctx . db . get ( "photos" , id ) ) ! ;
1287+ }
1288+
1289+ test ( "prefixed aggregates are independent of each other" , async ( ) => {
1290+ const t = setupTest ( ) ;
1291+ const { unprefixed, byValue, byName } = createPrefixedAggregates ( ) ;
1292+
1293+ await t . run ( async ( ctx ) => {
1294+ const a = await testItem ( ctx , { name : "a" , value : 1 } ) ;
1295+ const b = await testItem ( ctx , { name : "b" , value : 2 } ) ;
1296+ await byValue . insert ( ctx , a ) ;
1297+ await byValue . insert ( ctx , b ) ;
1298+ await byName . insert ( ctx , a ) ;
1299+ await unprefixed . insert ( ctx , a ) ;
1300+ await unprefixed . insert ( ctx , b ) ;
1301+ await unprefixed . insert (
1302+ ctx ,
1303+ await testItem ( ctx , { name : "c" , value : 3 } ) ,
1304+ ) ;
1305+ } ) ;
1306+
1307+ await t . run ( async ( ctx ) => {
1308+ expect ( await byValue . count ( ctx ) ) . toBe ( 2 ) ;
1309+ expect ( await byValue . sum ( ctx ) ) . toBe ( 3 ) ;
1310+ expect ( await byName . count ( ctx ) ) . toBe ( 1 ) ;
1311+ expect ( ( await byName . max ( ctx ) ) ?. key ) . toBe ( "a" ) ;
1312+ expect ( await unprefixed . count ( ctx ) ) . toBe ( 3 ) ;
1313+ } ) ;
1314+ } ) ;
1315+
1316+ test ( "namespaces are scoped to the prefix" , async ( ) => {
1317+ const t = setupTest ( ) ;
1318+ const { byAlbum } = createPrefixedAggregates ( ) ;
1319+ const otherAlbums = new TableAggregate < {
1320+ Namespace : string ;
1321+ Key : number ;
1322+ DataModel : DataModel ;
1323+ TableName : "photos" ;
1324+ } > ( components . aggregate , {
1325+ namespacePrefix : "byAlbum2" ,
1326+ namespace : ( doc ) => doc . album ,
1327+ sortKey : ( doc ) => doc . score ,
1328+ } ) ;
1329+
1330+ await t . run ( async ( ctx ) => {
1331+ await byAlbum . insert ( ctx , await insertPhoto ( ctx , "x" , 1 ) ) ;
1332+ await byAlbum . insert ( ctx , await insertPhoto ( ctx , "x" , 2 ) ) ;
1333+ await byAlbum . insert ( ctx , await insertPhoto ( ctx , "y" , 3 ) ) ;
1334+ await byAlbum . insert ( ctx , await insertPhoto ( ctx , "z" , 4 ) ) ;
1335+ await otherAlbums . insert ( ctx , await insertPhoto ( ctx , "x" , 5 ) ) ;
1336+ } ) ;
1337+
1338+ await t . run ( async ( ctx ) => {
1339+ expect ( await byAlbum . count ( ctx , { namespace : "x" } ) ) . toBe ( 2 ) ;
1340+ expect ( await otherAlbums . count ( ctx , { namespace : "x" } ) ) . toBe ( 1 ) ;
1341+ const namespaces = [ ] ;
1342+ for await ( const ns of byAlbum . iterNamespaces ( ctx , 1 ) ) {
1343+ namespaces . push ( ns ) ;
1344+ }
1345+ expect ( namespaces ) . toEqual ( [ "x" , "y" , "z" ] ) ;
1346+ const other = [ ] ;
1347+ for await ( const ns of otherAlbums . iterNamespaces ( ctx ) ) {
1348+ other . push ( ns ) ;
1349+ }
1350+ expect ( other ) . toEqual ( [ "x" ] ) ;
1351+ } ) ;
1352+ } ) ;
1353+
1354+ test ( "clearAll only clears the prefixed aggregate" , async ( ) => {
1355+ const t = setupTest ( ) ;
1356+ const { unprefixed, byValue, byAlbum } = createPrefixedAggregates ( ) ;
1357+
1358+ await t . run ( async ( ctx ) => {
1359+ const a = await testItem ( ctx , { name : "a" , value : 1 } ) ;
1360+ await byValue . insert ( ctx , a ) ;
1361+ await unprefixed . insert ( ctx , a ) ;
1362+ await byAlbum . insert ( ctx , await insertPhoto ( ctx , "x" , 1 ) ) ;
1363+ } ) ;
1364+
1365+ await t . run ( async ( ctx ) => {
1366+ await byAlbum . clearAll ( ctx ) ;
1367+ expect ( await byAlbum . count ( ctx , { namespace : "x" } ) ) . toBe ( 0 ) ;
1368+ expect ( await byValue . count ( ctx ) ) . toBe ( 1 ) ;
1369+ expect ( await unprefixed . count ( ctx ) ) . toBe ( 1 ) ;
1370+ } ) ;
1371+ } ) ;
1372+
1373+ test ( "DirectAggregate supports a namespacePrefix" , async ( ) => {
1374+ const t = setupTest ( ) ;
1375+ const direct = new DirectAggregate < { Key : number ; Id : string } > (
1376+ components . aggregate ,
1377+ { namespacePrefix : "direct" } ,
1378+ ) ;
1379+ const other = new DirectAggregate < { Key : number ; Id : string } > (
1380+ components . aggregate ,
1381+ ) ;
1382+
1383+ await t . run ( async ( ctx ) => {
1384+ await direct . insert ( ctx , { key : 1 , id : "a" , sumValue : 10 } ) ;
1385+ await direct . insert ( ctx , { key : 2 , id : "b" , sumValue : 20 } ) ;
1386+ await direct . replace ( ctx , { key : 2 , id : "b" } , { key : 3 , sumValue : 30 } ) ;
1387+ await other . insert ( ctx , { key : 1 , id : "a" } ) ;
1388+ } ) ;
1389+
1390+ await t . run ( async ( ctx ) => {
1391+ expect ( await direct . count ( ctx ) ) . toBe ( 2 ) ;
1392+ expect ( await direct . sum ( ctx ) ) . toBe ( 40 ) ;
1393+ expect (
1394+ await direct . countBatch ( ctx , [
1395+ { } ,
1396+ { bounds : { lower : { key : 2 , inclusive : true } } } ,
1397+ ] ) ,
1398+ ) . toEqual ( [ 2 , 1 ] ) ;
1399+ expect ( await other . count ( ctx ) ) . toBe ( 1 ) ;
1400+ } ) ;
1401+ } ) ;
1402+
1403+ test ( "queued writes use the prefix" , async ( ) => {
1404+ vi . useFakeTimers ( ) ;
1405+ try {
1406+ const t = setupTest ( ) ;
1407+ batchWorker . register ( t , "aggregate/batchWorker" ) ;
1408+ const direct = new DirectAggregate < { Key : number ; Id : string } > (
1409+ components . aggregate ,
1410+ { namespacePrefix : "direct" } ,
1411+ ) ;
1412+ const other = new DirectAggregate < { Key : number ; Id : string } > (
1413+ components . aggregate ,
1414+ ) ;
1415+
1416+ await t . run ( async ( ctx ) => {
1417+ await direct . insert ( ctx , { key : 1 , id : "a" } , { async : true } ) ;
1418+ await direct . enqueueBatch ( ctx , [
1419+ { type : "insert" , key : 2 , id : "b" } ,
1420+ {
1421+ type : "replace" ,
1422+ currentItem : { key : 1 , id : "a" } ,
1423+ newItem : { key : 4 } ,
1424+ } ,
1425+ ] ) ;
1426+ } ) ;
1427+ await t . finishAllScheduledFunctions ( vi . runAllTimers ) ;
1428+
1429+ await t . run ( async ( ctx ) => {
1430+ expect ( await direct . count ( ctx ) ) . toBe ( 2 ) ;
1431+ expect ( ( await direct . max ( ctx ) ) ?. key ) . toBe ( 4 ) ;
1432+ expect ( await other . count ( ctx ) ) . toBe ( 0 ) ;
1433+ } ) ;
1434+ } finally {
1435+ vi . useRealTimers ( ) ;
1436+ }
1437+ } ) ;
1438+ } ) ;
0 commit comments